1999-01-16 03:20:44 +03:00
|
|
|
/* -*- 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 "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS"
|
|
|
|
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
* the License for the specific language governing rights and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
|
|
|
* Netscape Communications Corporation. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
1999-02-12 22:46:02 +03:00
|
|
|
#define NS_IMPL_IDS
|
1999-01-16 03:20:44 +03:00
|
|
|
|
1999-02-25 16:15:07 +03:00
|
|
|
#include "pratom.h"
|
1999-03-09 12:44:27 +03:00
|
|
|
#include "nsIComponentManager.h"
|
1999-03-11 01:46:38 +03:00
|
|
|
#include "nsIServiceManager.h"
|
1999-02-25 16:15:07 +03:00
|
|
|
#include "nsIFactory.h"
|
1999-07-03 01:15:16 +04:00
|
|
|
#include "nsIRegistry.h"
|
1999-03-11 01:46:38 +03:00
|
|
|
#include "nsCOMPtr.h"
|
1999-02-25 16:15:07 +03:00
|
|
|
#include "nsICharsetConverterInfo.h"
|
|
|
|
#include "nsUCVJA2CID.h"
|
1999-03-11 01:46:38 +03:00
|
|
|
#include "nsUCVJA2Dll.h"
|
1999-01-16 03:20:44 +03:00
|
|
|
#include "nsEUCJPToUnicode.h"
|
1999-02-12 22:46:02 +03:00
|
|
|
#include "nsISO2022JPToUnicode.h"
|
1999-03-11 01:46:38 +03:00
|
|
|
#include "nsUnicodeToEUCJP.h"
|
|
|
|
#include "nsUnicodeToISO2022JP.h"
|
1999-06-09 07:17:50 +04:00
|
|
|
#include "nsUnicodeToJISx0201.h"
|
|
|
|
#include "nsUnicodeToJISx0208.h"
|
|
|
|
#include "nsUnicodeToJISx0212.h"
|
1999-01-16 03:20:44 +03:00
|
|
|
|
1999-02-12 22:46:02 +03:00
|
|
|
// just for NS_IMPL_IDS; this is a good, central place to implement GUIDs
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
|
|
|
#include "nsIUnicodeDecodeUtil.h"
|
1999-03-11 01:46:38 +03:00
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIUnicodeEncodeHelper.h"
|
1999-02-12 22:46:02 +03:00
|
|
|
#include "nsICharsetConverterManager.h"
|
1999-07-15 01:33:51 +04:00
|
|
|
#define DECODER_NAME_BASE "Unicode Decoder-"
|
|
|
|
#define ENCODER_NAME_BASE "Unicode Encoder-"
|
1999-02-12 22:46:02 +03:00
|
|
|
|
1999-01-16 03:20:44 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Global functions and data [declaration]
|
|
|
|
|
1999-03-11 01:46:38 +03:00
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
static NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
|
|
|
|
static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID);
|
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRInt32 g_InstanceCount = 0;
|
|
|
|
PRInt32 g_LockCount = 0;
|
1999-03-11 01:46:38 +03:00
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRUint16 g_ut0201Mapping[] = {
|
1999-02-12 22:46:02 +03:00
|
|
|
#include "jis0201.ut"
|
|
|
|
};
|
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRUint16 g_ut0208Mapping[] = {
|
1999-02-12 22:46:02 +03:00
|
|
|
#include "jis0208.ut"
|
|
|
|
};
|
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRUint16 g_ut0212Mapping[] = {
|
1999-02-12 22:46:02 +03:00
|
|
|
#include "jis0212.ut"
|
|
|
|
};
|
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRUint16 g_uf0201Mapping[] = {
|
1999-04-01 05:47:05 +04:00
|
|
|
#include "jis0201.uf"
|
|
|
|
};
|
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRUint16 g_uf0208Mapping[] = {
|
1999-04-01 05:47:05 +04:00
|
|
|
#include "jis0208.uf"
|
|
|
|
};
|
|
|
|
|
1999-06-09 01:06:47 +04:00
|
|
|
PRUint16 g_uf0212Mapping[] = {
|
1999-04-01 05:47:05 +04:00
|
|
|
#include "jis0212.uf"
|
|
|
|
};
|
|
|
|
|
1999-02-25 16:15:07 +03:00
|
|
|
typedef nsresult (* fpCreateInstance) (nsISupports **);
|
|
|
|
|
|
|
|
struct FactoryData
|
|
|
|
{
|
|
|
|
const nsCID * mCID;
|
|
|
|
fpCreateInstance CreateInstance;
|
|
|
|
char * mCharsetSrc;
|
|
|
|
char * mCharsetDest;
|
|
|
|
};
|
|
|
|
|
|
|
|
FactoryData g_FactoryData[] =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
&kISO2022JPToUnicodeCID,
|
|
|
|
nsISO2022JPToUnicode::CreateInstance,
|
|
|
|
"ISO-2022-JP",
|
|
|
|
"Unicode"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&kEUCJPToUnicodeCID,
|
|
|
|
nsEUCJPToUnicode::CreateInstance,
|
1999-03-11 01:46:38 +03:00
|
|
|
"EUC-JP",
|
1999-02-25 16:15:07 +03:00
|
|
|
"Unicode"
|
1999-03-11 01:46:38 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
&kUnicodeToEUCJPCID,
|
|
|
|
nsUnicodeToEUCJP::CreateInstance,
|
|
|
|
"Unicode",
|
|
|
|
"EUC-JP"
|
|
|
|
},
|
1999-06-09 07:17:50 +04:00
|
|
|
{
|
|
|
|
&kUnicodeToJISx0201CID,
|
|
|
|
nsUnicodeToJISx0201::CreateInstance,
|
|
|
|
"Unicode",
|
|
|
|
"jis_0201"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&kUnicodeToJISx0208CID,
|
|
|
|
nsUnicodeToJISx0208::CreateInstance,
|
|
|
|
"Unicode",
|
|
|
|
"jis_0208-1983"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&kUnicodeToJISx0212CID,
|
|
|
|
nsUnicodeToJISx0212::CreateInstance,
|
|
|
|
"Unicode",
|
|
|
|
"jis_0212-1990"
|
|
|
|
},
|
1999-03-11 01:46:38 +03:00
|
|
|
{
|
|
|
|
&kUnicodeToISO2022JPCID,
|
|
|
|
nsUnicodeToISO2022JP::CreateInstance,
|
|
|
|
"Unicode",
|
|
|
|
"ISO-2022-JP"
|
1999-02-25 16:15:07 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(_array) \
|
|
|
|
(sizeof(_array) / sizeof(_array[0]))
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Class nsConverterFactory [declaration]
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General factory class for converter objects.
|
|
|
|
*
|
|
|
|
* @created 24/Feb/1998
|
|
|
|
* @author Catalin Rotaru [CATA]
|
|
|
|
*/
|
|
|
|
class nsConverterFactory : public nsIFactory,
|
|
|
|
public nsICharsetConverterInfo
|
|
|
|
{
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
FactoryData * mData;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class constructor.
|
|
|
|
*/
|
|
|
|
nsConverterFactory(FactoryData * aData);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class destructor.
|
|
|
|
*/
|
|
|
|
virtual ~nsConverterFactory();
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// Interface nsIFactory [declaration]
|
|
|
|
|
|
|
|
NS_IMETHOD CreateInstance(nsISupports *aDelegate, const nsIID &aIID,
|
|
|
|
void **aResult);
|
|
|
|
|
|
|
|
NS_IMETHOD LockFactory(PRBool aLock);
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// Interface nsICharsetConverterInfo [declaration]
|
|
|
|
|
|
|
|
NS_IMETHOD GetCharsetSrc(char ** aCharset);
|
|
|
|
NS_IMETHOD GetCharsetDest(char ** aCharset);
|
|
|
|
};
|
|
|
|
|
1999-01-16 03:20:44 +03:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Global functions and data [implementation]
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
extern "C" NS_EXPORT PRBool NSCanUnload(nsISupports* aServMgr)
|
1999-01-16 03:20:44 +03:00
|
|
|
{
|
|
|
|
return PRBool(g_InstanceCount == 0 && g_LockCount == 0);
|
|
|
|
}
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
extern "C" NS_EXPORT nsresult NSGetFactory(nsISupports* aServMgr,
|
1999-02-26 13:17:14 +03:00
|
|
|
const nsCID &aClass,
|
|
|
|
const char *aClassName,
|
|
|
|
const char *aProgID,
|
1999-01-16 03:20:44 +03:00
|
|
|
nsIFactory **aFactory)
|
|
|
|
{
|
|
|
|
if (aFactory == NULL) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-02-25 16:15:07 +03:00
|
|
|
nsresult res;
|
|
|
|
nsConverterFactory * fac;
|
|
|
|
FactoryData * data;
|
1999-01-16 03:20:44 +03:00
|
|
|
|
1999-02-26 19:58:33 +03:00
|
|
|
for (PRUint32 i=0; i<ARRAY_SIZE(g_FactoryData); i++) {
|
1999-02-25 16:15:07 +03:00
|
|
|
data = &(g_FactoryData[i]);
|
1999-02-26 13:17:14 +03:00
|
|
|
if (aClass.Equals(*(data->mCID))) {
|
1999-02-25 16:15:07 +03:00
|
|
|
fac = new nsConverterFactory(data);
|
|
|
|
res = fac->QueryInterface(kIFactoryIID, (void **) aFactory);
|
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
*aFactory = NULL;
|
|
|
|
delete fac;
|
|
|
|
}
|
1999-02-12 22:46:02 +03:00
|
|
|
|
1999-02-25 16:15:07 +03:00
|
|
|
return res;
|
1999-02-12 22:46:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-16 03:20:44 +03:00
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
1999-07-03 01:15:16 +04:00
|
|
|
extern "C" NS_EXPORT nsresult NSRegisterSelf(nsISupports * aServMgr,
|
|
|
|
const char * path)
|
1999-01-16 03:20:44 +03:00
|
|
|
{
|
1999-07-03 01:15:16 +04:00
|
|
|
nsresult res;
|
|
|
|
PRUint32 i;
|
|
|
|
nsIComponentManager * compMgr = NULL;
|
|
|
|
nsIRegistry * registry = NULL;
|
|
|
|
nsIRegistry::Key key;
|
|
|
|
char buff[1024];
|
|
|
|
|
|
|
|
// get the service manager
|
|
|
|
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &res));
|
|
|
|
|
|
|
|
// get the component manager
|
|
|
|
res = servMgr->GetService(kComponentManagerCID,
|
|
|
|
nsIComponentManager::GetIID(),
|
|
|
|
(nsISupports**)&compMgr);
|
|
|
|
if (NS_FAILED(res)) goto done;
|
|
|
|
|
|
|
|
// get the registry
|
|
|
|
res = servMgr->GetService(NS_REGISTRY_PROGID,
|
|
|
|
nsIRegistry::GetIID(),
|
|
|
|
(nsISupports**)®istry);
|
|
|
|
if (NS_FAILED(res)) goto done;
|
|
|
|
|
|
|
|
// open the registry
|
|
|
|
res = registry->OpenWellKnownRegistry(
|
|
|
|
nsIRegistry::ApplicationComponentRegistry);
|
|
|
|
if (NS_FAILED(res)) goto done;
|
|
|
|
|
1999-07-15 01:33:51 +04:00
|
|
|
char name[128];
|
|
|
|
char progid[128];
|
1999-07-03 01:15:16 +04:00
|
|
|
for (i=0; i<ARRAY_SIZE(g_FactoryData); i++) {
|
1999-07-15 01:33:51 +04:00
|
|
|
if(0==PL_strcmp(g_FactoryData[i].mCharsetSrc,"Unicode"))
|
|
|
|
{
|
|
|
|
PL_strcpy(name, DECODER_NAME_BASE);
|
|
|
|
PL_strcat(name, g_FactoryData[i].mCharsetDest);
|
|
|
|
PL_strcpy(progid, NS_UNICODEDECODER_PROGID_BASE);
|
|
|
|
PL_strcat(progid, g_FactoryData[i].mCharsetDest);
|
|
|
|
} else {
|
|
|
|
PL_strcpy(name, ENCODER_NAME_BASE);
|
|
|
|
PL_strcat(name, g_FactoryData[i].mCharsetSrc);
|
|
|
|
PL_strcpy(progid, NS_UNICODEENCODER_PROGID_BASE);
|
|
|
|
PL_strcat(progid, g_FactoryData[i].mCharsetSrc);
|
|
|
|
}
|
1999-07-03 01:15:16 +04:00
|
|
|
// register component
|
1999-07-15 01:33:51 +04:00
|
|
|
res = compMgr->RegisterComponent(*(g_FactoryData[i].mCID), name, progid,
|
1999-02-26 19:18:23 +03:00
|
|
|
path, PR_TRUE, PR_TRUE);
|
1999-07-03 01:15:16 +04:00
|
|
|
if(NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) goto done;
|
|
|
|
|
|
|
|
// register component info
|
|
|
|
// XXX take these KONSTANTS out of here
|
|
|
|
// XXX free the string from "ToString()"
|
|
|
|
sprintf(buff, "%s/%s", "software/netscape/intl/uconv", (g_FactoryData[i].mCID -> ToString()));
|
|
|
|
res = registry -> AddSubtree(nsIRegistry::Common, buff, &key);
|
|
|
|
if (NS_FAILED(res)) goto done;
|
|
|
|
res = registry -> SetString(key, "source", g_FactoryData[i].mCharsetSrc);
|
|
|
|
if (NS_FAILED(res)) goto done;
|
|
|
|
res = registry -> SetString(key, "destination", g_FactoryData[i].mCharsetDest);
|
|
|
|
if (NS_FAILED(res)) goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (compMgr != NULL)
|
|
|
|
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
|
|
|
|
if (registry != NULL) {
|
|
|
|
registry -> Close();
|
|
|
|
(void)servMgr->ReleaseService(NS_REGISTRY_PROGID, registry);
|
1999-02-25 16:15:07 +03:00
|
|
|
}
|
1999-01-16 03:20:44 +03:00
|
|
|
|
1999-07-03 01:15:16 +04:00
|
|
|
return res;
|
1999-01-16 03:20:44 +03:00
|
|
|
}
|
|
|
|
|
1999-03-09 12:44:27 +03:00
|
|
|
extern "C" NS_EXPORT nsresult NSUnregisterSelf(nsISupports* aServMgr, const char * path)
|
1999-01-16 03:20:44 +03:00
|
|
|
{
|
1999-03-09 12:44:27 +03:00
|
|
|
nsresult rv;
|
1999-03-09 14:49:41 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIServiceManager> servMgr(do_QueryInterface(aServMgr, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsIComponentManager* compMgr;
|
|
|
|
rv = servMgr->GetService(kComponentManagerCID,
|
|
|
|
nsIComponentManager::GetIID(),
|
|
|
|
(nsISupports**)&compMgr);
|
1999-03-09 12:44:27 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-01-16 03:20:44 +03:00
|
|
|
|
1999-02-26 19:58:33 +03:00
|
|
|
for (PRUint32 i=0; i<ARRAY_SIZE(g_FactoryData); i++) {
|
1999-05-06 05:37:04 +04:00
|
|
|
rv = compMgr->UnregisterComponent(*(g_FactoryData[i].mCID), path);
|
1999-03-09 15:36:13 +03:00
|
|
|
if(NS_FAILED(rv)) goto done;
|
1999-02-25 16:15:07 +03:00
|
|
|
}
|
|
|
|
|
1999-03-09 14:49:41 +03:00
|
|
|
done:
|
|
|
|
(void)servMgr->ReleaseService(kComponentManagerCID, compMgr);
|
1999-03-09 12:44:27 +03:00
|
|
|
return rv;
|
1999-02-25 16:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Class nsConverterFactory [implementation]
|
|
|
|
|
|
|
|
nsConverterFactory::nsConverterFactory(FactoryData * aData)
|
|
|
|
{
|
|
|
|
mData = aData;
|
|
|
|
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
PR_AtomicIncrement(&g_InstanceCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsConverterFactory::~nsConverterFactory()
|
|
|
|
{
|
|
|
|
PR_AtomicDecrement(&g_InstanceCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Interface nsISupports [implementation]
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsConverterFactory);
|
|
|
|
NS_IMPL_RELEASE(nsConverterFactory);
|
|
|
|
|
|
|
|
nsresult nsConverterFactory::QueryInterface(REFNSIID aIID,
|
|
|
|
void** aInstancePtr)
|
|
|
|
{
|
|
|
|
if (NULL == aInstancePtr) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aInstancePtr = NULL;
|
1999-03-11 01:46:38 +03:00
|
|
|
|
|
|
|
if (aIID.Equals(kICharsetConverterInfoIID)) {
|
|
|
|
*aInstancePtr = (void*) ((nsICharsetConverterInfo*)this);
|
|
|
|
} else if (aIID.Equals(kIFactoryIID)) {
|
|
|
|
*aInstancePtr = (void*) ((nsIFactory*)this);
|
|
|
|
} else if (aIID.Equals(kISupportsIID)) {
|
1999-02-25 16:15:07 +03:00
|
|
|
*aInstancePtr = (void*) ((nsISupports*)(nsIFactory*)this);
|
1999-03-11 01:46:38 +03:00
|
|
|
} else {
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
1999-02-25 16:15:07 +03:00
|
|
|
|
1999-03-11 01:46:38 +03:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
1999-02-25 16:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Interface nsIFactory [implementation]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsConverterFactory::CreateInstance(nsISupports *aDelegate,
|
|
|
|
const nsIID &aIID,
|
|
|
|
void **aResult)
|
|
|
|
{
|
|
|
|
if (aResult == NULL) return NS_ERROR_NULL_POINTER;
|
|
|
|
if (aDelegate != NULL) return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsISupports * t;
|
|
|
|
mData->CreateInstance(&t);
|
|
|
|
if (t == NULL) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult res = t->QueryInterface(aIID, aResult);
|
|
|
|
if (NS_FAILED(res)) delete t;
|
1999-02-12 22:46:02 +03:00
|
|
|
|
1999-01-16 03:20:44 +03:00
|
|
|
return res;
|
|
|
|
}
|
1999-02-25 16:15:07 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsConverterFactory::LockFactory(PRBool aLock)
|
|
|
|
{
|
|
|
|
if (aLock) PR_AtomicIncrement(&g_LockCount);
|
|
|
|
else PR_AtomicDecrement(&g_LockCount);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Interface nsICharsetConverterInfo [implementation]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsConverterFactory::GetCharsetSrc(char ** aCharset)
|
|
|
|
{
|
|
|
|
(*aCharset) = mData->mCharsetSrc;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsConverterFactory::GetCharsetDest(char ** aCharset)
|
|
|
|
{
|
|
|
|
(*aCharset) = mData->mCharsetDest;
|
|
|
|
return NS_OK;
|
|
|
|
}
|