Prototype of xpc-ed str res.

This commit is contained in:
tao%netscape.com 1999-07-22 04:38:02 +00:00
parent 1df4c2d244
commit dc84474c50
3 changed files with 202 additions and 177 deletions

View File

@ -46,9 +46,14 @@ class nsIStringBundle : public nsISupports
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ISTRINGBUNDLE_IID)
NS_IMETHOD GetStringFromID(PRInt32 aID, nsString& aResult) = 0;
NS_IMETHOD GetStringFromName(const nsString& aName, nsString& aResult) = 0;
NS_IMETHOD GetEnumeration(nsIBidirectionalEnumerator** elements) = 0;
/* void GetStringFromID (in long aID, out wstring aResult); */
NS_IMETHOD GetStringFromID(PRInt32 aID, PRUnichar **aResult) = 0;
/* void GetStringFromName ([const] in wstring aName, out wstring aResult); */
NS_IMETHOD GetStringFromName(const PRUnichar *aName, PRUnichar **aResult) = 0;
/* void GetEnumeration (out nsIBidirectionalEnumerator elements); */
NS_IMETHOD GetEnumeration(nsIBidirectionalEnumerator **elements) = 0;
};
class nsIStringBundleService : public nsISupports
@ -56,10 +61,11 @@ class nsIStringBundleService : public nsISupports
public:
NS_DEFINE_STATIC_IID_ACCESSOR(NS_ISTRINGBUNDLESERVICE_IID)
NS_IMETHOD CreateBundle(nsIURI* aURL, nsILocale* aLocale,
nsIStringBundle** aResult) = 0; /* deprecated */
NS_IMETHOD CreateBundle(const char* aURLSpec, nsILocale* aLocale,
nsIStringBundle** aResult) = 0;
/* void CreateBundle ([const] in string aURLSpec, in nsILocale aLocale, [retval] out nsIStringBundle aResult); */
NS_IMETHOD CreateBundle(const char *aURLSpec, nsILocale *aLocale, nsIStringBundle **aResult) = 0;
/* void CreateXPCBundle ([const] in string aURLSpec, [const] in wstring aLocaleName, [retval] out nsIStringBundle aResult); */
NS_IMETHOD CreateXPCBundle(const char *aURLSpec, const PRUnichar *aLocaleName, nsIStringBundle **aResult) = 0;
};
#endif /* nsIStringBundle_h___ */

View File

@ -24,6 +24,8 @@
#include "nsIStringBundle.h"
#include "nscore.h"
#include "nsILocale.h"
#include "nsIAllocator.h"
#include "plstr.h"
#ifndef NECKO
#include "nsINetService.h"
@ -37,6 +39,7 @@
#include "nsCOMPtr.h"
#include "nsString.h"
#include "pratom.h"
#include "prmem.h"
#include "nsIServiceManager.h"
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
@ -58,18 +61,19 @@ static NS_DEFINE_IID(kIPersistentPropertiesIID, NS_IPERSISTENTPROPERTIES_IID);
class nsStringBundle : public nsIStringBundle
{
public:
nsStringBundle(const char* aURLSpec, nsILocale* aLocale, nsresult* aResult);
// deprecated
nsStringBundle(nsIURI* aURL, nsILocale* aLocale, nsresult* aResult);
virtual ~nsStringBundle();
NS_DECL_ISUPPORTS
NS_IMETHOD GetStringFromID(PRInt32 aID, nsString& aResult);
NS_IMETHOD GetStringFromName(const nsString& aName, nsString& aResult);
NS_IMETHOD GetEnumeration(nsIBidirectionalEnumerator** elements);
/* void GetStringFromID (in long aID, out wstring aResult); */
NS_IMETHOD GetStringFromID(PRInt32 aID, PRUnichar **aResult);
/* void GetStringFromName ([const] in wstring aName, out wstring aResult); */
NS_IMETHOD GetStringFromName(const PRUnichar *aName, PRUnichar **aResult);
/* void GetEnumeration (out nsIBidirectionalEnumerator elements); */
NS_IMETHOD GetEnumeration(nsIBidirectionalEnumerator **elements);
nsIPersistentProperties* mProps;
@ -77,11 +81,14 @@ protected:
//
// functional decomposition of the funitions repeatively called
//
nsresult GetStringFromID(PRInt32 aID, nsString& aResult);
nsresult GetStringFromName(const nsString& aName, nsString& aResult);
nsresult GetInputStream(const char* aURLSpec, nsILocale* aLocale, nsIInputStream*& in);
nsresult OpenInputStream(nsString2& aURLStr, nsIInputStream*& in);
nsresult GetLangCountry(nsILocale* aLocale, nsString2& lang, nsString2& country);
};
nsStringBundle::nsStringBundle(const char* aURLSpec, nsILocale* aLocale, nsresult* aResult)
{ NS_INIT_REFCNT();
@ -111,87 +118,75 @@ nsStringBundle::nsStringBundle(const char* aURLSpec, nsILocale* aLocale, nsresul
NS_RELEASE(in);
}
// deprecated
nsStringBundle::nsStringBundle(nsIURI* aURL, nsILocale* aLocale,
nsresult* aResult)
{
NS_INIT_REFCNT();
mProps = nsnull;
nsIInputStream *in = nsnull;
#ifndef NECKO
nsINetService* pNetService = nsnull;
*aResult = nsServiceManager::GetService(kNetServiceCID,
kINetServiceIID, (nsISupports**) &pNetService);
if (NS_FAILED(*aResult)) {
#ifdef NS_DEBUG
printf("cannot get net service\n");
#endif
return;
}
*aResult = pNetService->OpenBlockingStream(aURL, nsnull, &in);
if (NS_FAILED(*aResult)) {
#ifdef NS_DEBUG
printf("cannot open stream\n");
#endif
return;
}
#else // NECKO
nsresult rv;
rv = NS_OpenURI(&in, aURL);
if (NS_FAILED(rv)) {
#ifdef NS_DEBUG
printf("cannot open uri\n");
#endif
*aResult = rv;
return;
}
#endif // NECKO
if (!in) {
#ifdef NS_DEBUG
printf("OpenBlockingStream returned success value, but pointer is NULL\n");
#endif
*aResult = NS_ERROR_UNEXPECTED;
return;
}
*aResult = nsComponentManager::CreateInstance(kPersistentPropertiesCID, NULL,
kIPersistentPropertiesIID, (void**) &mProps);
if (NS_FAILED(*aResult)) {
#ifdef NS_DEBUG
printf("create nsIPersistentProperties failed\n");
#endif
return;
}
*aResult = mProps->Load(in);
NS_RELEASE(in);
}
nsStringBundle::~nsStringBundle()
{
NS_IF_RELEASE(mProps);
}
NS_IMPL_ISUPPORTS(nsStringBundle, kIStringBundleIID)
NS_IMETHODIMP
nsresult
nsStringBundle::GetStringFromID(PRInt32 aID, nsString& aResult)
{
nsAutoString name("");
name.Append(aID, 10);
nsresult ret = mProps->GetProperty(name, aResult);
char *s = aResult.ToNewCString();
printf("\n** GetStringFromID: %s\n", s?s:"null");
delete s;
return ret;
}
NS_IMETHODIMP
nsresult
nsStringBundle::GetStringFromName(const nsString& aName, nsString& aResult)
{
nsresult ret = mProps->GetProperty(aName, aResult);
char *s = aResult.ToNewCString(),
*ss = aName.ToNewCString();
printf("\n** GetStringFromName: %s, %s\n", ss?ss:"null", s?s:"null");
delete s;
return ret;
}
NS_IMPL_ISUPPORTS(nsStringBundle, nsIStringBundle::GetIID())
/* void GetStringFromID (in long aID, out wstring aResult); */
NS_IMETHODIMP
nsStringBundle::GetStringFromID(PRInt32 aID, PRUnichar **aResult)
{
*aResult = nsnull;
nsString tmpstr("");
nsresult ret = GetStringFromID(aID, tmpstr);
PRInt32 len = tmpstr.Length();
if (NS_FAILED(ret) || !len) {
return ret;
}
*aResult = (PRUnichar *) PR_Malloc(len);
*aResult = (PRUnichar *) memcpy(*aResult, tmpstr.GetUnicode(), len);
return ret;
}
/* void GetStringFromName ([const] in wstring aName, out wstring aResult); */
NS_IMETHODIMP
nsStringBundle::GetStringFromName(const PRUnichar *aName, PRUnichar **aResult)
{
*aResult = nsnull;
nsString tmpstr("");
nsString nameStr(aName);
nsresult ret = GetStringFromName(nameStr, tmpstr);
PRInt32 len = tmpstr.Length();
if (NS_FAILED(ret) || !len) {
return ret;
}
*aResult = (PRUnichar *) PR_Malloc(len);
*aResult = (PRUnichar *) memcpy(*aResult, tmpstr.GetUnicode(), len);
return ret;
}
@ -241,7 +236,7 @@ nsStringBundle::GetInputStream(const char* aURLSpec, nsILocale* aLocale, nsIInpu
strFile2 += "_";
strFile2 += lc_country;;
}
/* insert it
*/
nsString2 fileRight;
@ -273,6 +268,13 @@ nsStringBundle::GetInputStream(const char* aURLSpec, nsILocale* aLocale, nsIInpu
nsresult
nsStringBundle::OpenInputStream(nsString2& aURLStr, nsIInputStream*& in)
{
#ifdef DEBUG
{
char *s = aURLStr.ToNewCString();
printf("\n** nsStringBundle::OpenInputStream: %s\n", s?s:"null");
delete s;
}
#endif
nsresult ret;
#ifndef NECKO
nsINetService* pNetService = nsnull;
@ -355,16 +357,18 @@ public:
NS_DECL_ISUPPORTS
NS_IMETHOD CreateBundle(const char* aURLSpec, nsILocale* aLocale,
nsIStringBundle** aResult);
// deprecate
NS_IMETHOD CreateBundle(nsIURI* aURL, nsILocale* aLocale,
nsIStringBundle** aResult);
/* void CreateBundle ([const] in string aURLSpec, in nsILocale aLocale, out nsIStringBundle aResult); */
NS_IMETHOD CreateBundle(const char *aURLSpec, nsILocale * aLocale,
nsIStringBundle **aResult);
/* void CreateXPCBundle ([const] in string aURLSpec, [const] in wstring aLocaleName, out nsIStringBundle aResult); */
NS_IMETHOD CreateXPCBundle(const char *aURLSpec, const PRUnichar *aLocaleName, nsIStringBundle **aResult);
};
nsStringBundleService::nsStringBundleService()
{
#if DEBUG
printf("\n++ nsStringBundleService::nsStringBundleService ++\n");
#endif
NS_INIT_REFCNT();
}
@ -372,13 +376,21 @@ nsStringBundleService::~nsStringBundleService()
{
}
NS_IMPL_ISUPPORTS(nsStringBundleService, kIStringBundleServiceIID)
NS_IMPL_ISUPPORTS(nsStringBundleService, nsIStringBundleService::GetIID())
NS_IMETHODIMP
nsStringBundleService::CreateBundle(const char* aURLSpec, nsILocale* aLocale,
nsIStringBundle** aResult)
nsIStringBundle** aResult)
{
#ifdef DEBUG
printf("\n++ nsStringBundleService::CreateBundle ++\n");
{
nsString2 aURLStr(aURLSpec);
char *s = aURLStr.ToNewCString();
printf("\n** nsStringBundleService::CreateBundle: %s\n", s?s:"null");
delete s;
}
#endif
nsresult ret = NS_OK;
nsStringBundle* bundle = new nsStringBundle(aURLSpec, aLocale, &ret);
if (!bundle) {
@ -396,13 +408,22 @@ nsStringBundleService::CreateBundle(const char* aURLSpec, nsILocale* aLocale,
return ret;
}
/* deprecated */
NS_IMETHODIMP
nsStringBundleService::CreateBundle(nsIURI* aURL, nsILocale* aLocale,
nsIStringBundle** aResult)
/* void CreateXPCBundle ([const] in string aURLSpec, [const] in wstring aLocaleName, out nsIStringBundle aResult); */
NS_IMETHODIMP
nsStringBundleService::CreateXPCBundle(const char *aURLSpec, const PRUnichar *aLocaleName, nsIStringBundle **aResult)
{
#ifdef DEBUG
printf("\n++ nsStringBundleService::CreateXPCBundle ++\n");
{
nsString2 aURLStr(aURLSpec);
char *s = aURLStr.ToNewCString();
printf("\n** nsStringBundleService::XPCCreateBundle: %s\n", s?s:"null");
delete s;
}
#endif
nsresult ret = NS_OK;
nsStringBundle* bundle = new nsStringBundle(aURL, aLocale, &ret);
nsStringBundle* bundle = new nsStringBundle(aURLSpec, nsnull, &ret);
if (!bundle) {
return NS_ERROR_OUT_OF_MEMORY;
}
@ -432,6 +453,9 @@ public:
nsStringBundleServiceFactory::nsStringBundleServiceFactory()
{
#ifdef DEBUG
printf("\n++ nsStringBundleServiceFactory::nsStringBundleServiceFactory ++\n");
#endif
NS_INIT_REFCNT();
}
@ -445,6 +469,9 @@ NS_IMETHODIMP
nsStringBundleServiceFactory::CreateInstance(nsISupports* aOuter,
REFNSIID aIID, void** aResult)
{
#ifdef DEBUG
printf("\n++ nsStringBundleServiceFactory::CreateInstance ++\n");
#endif
nsStringBundleService* service = new nsStringBundleService();
if (!service) {
return NS_ERROR_OUT_OF_MEMORY;
@ -461,6 +488,9 @@ nsStringBundleServiceFactory::CreateInstance(nsISupports* aOuter,
NS_IMETHODIMP
nsStringBundleServiceFactory::LockFactory(PRBool aLock)
{
#ifdef DEBUG
printf("\n++ nsStringBundleServiceFactory::LockFactory ++\n");
#endif
if (aLock) {
PR_AtomicIncrement(&gLockCount);
}
@ -474,6 +504,9 @@ nsStringBundleServiceFactory::LockFactory(PRBool aLock)
extern "C" NS_EXPORT nsresult
NSRegisterSelf(nsISupports* aServMgr, const char* path)
{
#ifdef DEBUG
printf("\n++ str bunlde NSRegisterSelf ++\n");
#endif
nsresult rv;
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
@ -489,7 +522,7 @@ NSRegisterSelf(nsISupports* aServMgr, const char* path)
"String Bundle",
NS_STRINGBUNDLE_PROGID,
path,
PR_TRUE, PR_TRUE);
PR_TRUE, PR_TRUE);
if (NS_FAILED(rv)) goto done;
done:
@ -500,6 +533,9 @@ NSRegisterSelf(nsISupports* aServMgr, const char* path)
extern "C" NS_EXPORT nsresult
NSUnregisterSelf(nsISupports* aServMgr, const char* path)
{
#ifdef DEBUG
printf("\n++ str bunlde NSUnregisterSelf ++\n");
#endif
nsresult rv;
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
@ -526,6 +562,9 @@ NSGetFactory(nsISupports* aServMgr,
const char *aProgID,
nsIFactory **aFactory)
{
#ifdef DEBUG
printf("\n++ str bunlde NSGetFactory ++\n");
#endif
nsresult res;
if (!aFactory) {

View File

@ -43,13 +43,19 @@
#ifdef XP_PC
#define NETLIB_DLL "netlib.dll"
#else
#define RAPTORBASE_DLL "raptorbase.dll"
#define XPCOM_DLL "xpcom32.dll"
#else /* else XP_PC */
#ifdef XP_MAC
#include "nsMacRepository.h"
#else
#define NETLIB_DLL "NETLIB_DLL"
#define RAPTORBASE_DLL "base.shlb"
#define XPCOM_DLL "XPCOM_DLL"
#else /* else XP_MAC */
#define NETLIB_DLL "libnetlib"MOZ_DLL_SUFFIX
#endif
#endif
#define RAPTORBASE_DLL "libraptorbase"MOZ_DLL_SUFFIX
#define XPCOM_DLL "libxpcom"MOZ_DLL_SUFFIX
#endif /* XP_MAC */
#endif /* XP_PC */
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
@ -131,7 +137,38 @@ get_applocale(void)
////////////////////////////////////////////////////////////////////////////////////////////////////
nsresult NS_AutoregisterComponents()
{
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
NULL /* default */);
// startup netlib:
nsComponentManager::RegisterComponent(kEventQueueServiceCID,
NULL, NULL,
XPCOM_DLL,
PR_FALSE, PR_FALSE);
#ifndef NECKO
nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE);
#else
nsComponentManager::RegisterComponent(kIOServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE);
#endif // NECKO
// Create the Event Queue for this thread...
nsIEventQueueService* pEventQService;
pEventQService = nsnull;
nsresult result = nsServiceManager::GetService(kEventQueueServiceCID,
kIEventQueueServiceIID,
(nsISupports **)&pEventQService);
if (NS_SUCCEEDED(result)) {
// XXX: What if this fails?
result = pEventQService->CreateThreadEventQueue();
}
nsComponentManager::RegisterComponent(kPersistentPropertiesCID,
NULL,
NULL,
RAPTORBASE_DLL,
PR_FALSE,
PR_FALSE);
return rv;
}
@ -140,88 +177,22 @@ main(int argc, char *argv[])
{
nsresult ret;
#if 1
NS_AutoregisterComponents();
#else
ret = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
NULL /* default */);
if (NS_FAILED(ret)) {
printf("auto-registration failed\n");
return 1;
}
#endif
#ifndef NECKO
nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL,
PR_FALSE, PR_FALSE);
#else
nsComponentManager::RegisterComponent(kIOServiceCID, NULL, NULL, NETLIB_DLL,
PR_FALSE, PR_FALSE);
#endif // NECKO
nsIStringBundleService* service = nsnull;
ret = nsServiceManager::GetService(kStringBundleServiceCID,
kIStringBundleServiceIID, (nsISupports**) &service);
kIStringBundleServiceIID, (nsISupports**) &service);
if (NS_FAILED(ret)) {
printf("cannot create service\n");
return 1;
}
nsIEventQueueService* pEventQueueService = nsnull;
ret = nsServiceManager::GetService(kEventQueueServiceCID,
kIEventQueueServiceIID, (nsISupports**) &pEventQueueService);
if (NS_FAILED(ret)) {
printf("cannot get event queue service\n");
return 1;
}
ret = pEventQueueService->CreateThreadEventQueue();
if (NS_FAILED(ret)) {
printf("CreateThreadEventQueue failed\n");
return 1;
}
nsILocale* locale = get_applocale();
nsIURI *url = nsnull;
#ifndef NECKO
nsINetService* pNetService = nsnull;
ret = nsServiceManager::GetService(kNetServiceCID, kINetServiceIID,
(nsISupports**) &pNetService);
if (NS_FAILED(ret)) {
printf("cannot get net service\n");
return 1;
}
ret = pNetService->CreateURL(&url, nsString(TEST_URL), nsnull, nsnull,
nsnull);
#else
NS_WITH_SERVICE(nsIIOService, pNetService, kIOServiceCID, &ret);
if (NS_FAILED(ret)) {
printf("cannot get io service\n");
return 1;
}
nsIURI *uri = nsnull;
ret = pNetService->NewURI(TEST_URL, nsnull, &uri);
if (NS_FAILED(ret)) {
printf("cannot get uri\n");
return 1;
}
ret = uri->QueryInterface(nsIURI::GetIID(), (void**)&url);
NS_RELEASE(uri);
#endif // NECKO
if (NS_FAILED(ret)) {
printf("cannot create URL\n");
return 1;
}
nsIStringBundle* bundle = nsnull;
#if 0
ret = service->CreateBundle(url, locale, &bundle);
#else
ret = service->CreateBundle(TEST_URL, locale, &bundle);
#endif
/* free it
*/
locale->Release();
@ -232,23 +203,32 @@ main(int argc, char *argv[])
}
nsAutoString v("");
ret = bundle->GetStringFromName(nsString("file"), v);
if (NS_FAILED(ret)) {
printf("cannot get string from name\n");
return 1;
}
char *value = v.ToNewCString();
cout << "file=\"" << value << "\"" << endl;
PRUnichar *ptrv = nsnull;
char *value = nsnull;
ret = bundle->GetStringFromID(123, v);
// 123
ret = bundle->GetStringFromID(123, &ptrv);
if (NS_FAILED(ret)) {
printf("cannot get string from ID\n");
return 1;
}
v = ptrv;
value = v.ToNewCString();
cout << "123=\"" << value << "\"" << endl;
nsIBidirectionalEnumerator* propEnum = nsnull;
// file
nsString strfile("file");
const PRUnichar *ptrFile = strfile.GetUnicode();
ret = bundle->GetStringFromName(ptrFile, &ptrv);
if (NS_FAILED(ret)) {
printf("cannot get string from name\n");
return 1;
}
v = ptrv;
value = v.ToNewCString();
cout << "file=\"" << value << "\"" << endl;
nsIBidirectionalEnumerator* propEnum = nsnull;
ret = bundle->GetEnumeration(&propEnum);
if (NS_FAILED(ret)) {
printf("cannot get enumeration\n");