This commit is contained in:
tao%netscape.com 1999-07-22 04:38:02 +00:00
Родитель ede5a246de
Коммит df850e6b1f
3 изменённых файлов: 189 добавлений и 235 удалений

Просмотреть файл

@ -1,65 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef nsIStringBundle_h___
#define nsIStringBundle_h___
#include "nsILocale.h"
#include "nsIURL.h"
#include "nsString.h"
#include "nsIEnumerator.h"
// {D85A17C0-AA7C-11d2-9B8C-00805F8A16D9}
#define NS_ISTRINGBUNDLESERVICE_IID \
{ 0xd85a17c0, 0xaa7c, 0x11d2, \
{ 0x9b, 0x8c, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9 } }
// {D85A17C1-AA7C-11d2-9B8C-00805F8A16D9}
#define NS_STRINGBUNDLESERVICE_CID \
{ 0xd85a17c1, 0xaa7c, 0x11d2, \
{ 0x9b, 0x8c, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9 } }
// {D85A17C2-AA7C-11d2-9B8C-00805F8A16D9}
#define NS_ISTRINGBUNDLE_IID \
{ 0xd85a17c2, 0xaa7c, 0x11d2, \
{ 0x9b, 0x8c, 0x0, 0x80, 0x5f, 0x8a, 0x16, 0xd9 } }
#define NS_STRINGBUNDLE_PROGID "component://netscape/intl/stringbundle"
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;
};
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;
};
#endif /* nsIStringBundle_h___ */

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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");