fix 22921. reviewed by dp,cata,jbetak change module code to use macro

This commit is contained in:
ftang%netscape.com 2000-02-08 06:21:17 +00:00
parent 6de717d753
commit 873461dae8
9 changed files with 99 additions and 364 deletions

View File

@ -48,6 +48,67 @@
#define NS_ERROR_UCONV_NOCONV \
NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_UCONV, 0x01)
#define NS_IMPL_NSUCONVERTERREGSELF \
static NS_IMETHODIMP \
nsUConverterRegSelf( const char* aFromCharset, \
const char* aToCharset, \
nsCID aCID) \
{ \
nsresult res = NS_OK; \
nsRegistryKey key; \
char buff[1024]; \
PRBool isOpen = PR_FALSE; \
NS_WITH_SERVICE( nsIRegistry, registry, NS_REGISTRY_PROGID, &res); \
if (NS_FAILED(res)) \
goto done; \
res = registry->IsOpen(&isOpen); \
if (NS_FAILED(res)) \
goto done; \
if(! isOpen) { \
res = registry->OpenWellKnownRegistry( \
nsIRegistry::ApplicationComponentRegistry); \
if (NS_FAILED(res)) \
goto done; \
} \
char * cid_string; \
cid_string = aCID.ToString(); \
sprintf(buff, "%s/%s", "software/netscape/intl/uconv", cid_string); \
nsCRT::free(cid_string); \
res = registry -> AddSubtree(nsIRegistry::Common, buff, &key); \
if (NS_FAILED(res)) \
goto done; \
res = registry -> SetString(key, "source", aFromCharset); \
if (NS_FAILED(res)) \
goto done; \
res = registry -> SetString(key, "destination", aToCharset); \
if (NS_FAILED(res)) \
goto done; \
printf("RegSelf %s to %s converter complete\n", \
aFromCharset, aToCharset); \
done: \
return res; \
}
#define NS_UCONV_REG_UNREG(_InstanceClass, _From, _To, _CID ) \
static NS_IMETHODIMP \
_InstanceClass##RegSelf (nsIComponentManager *aCompMgr, \
nsIFile *aPath, \
const char* registryLocation, \
const char* componentType) \
{ \
nsCID cid = _CID; \
return nsUConverterRegSelf( _From, _To, cid); \
} \
static NS_IMETHODIMP \
_InstanceClass##UnRegSelf (nsIComponentManager *aCompMgr, \
nsIFile *aPath, \
const char* registryLocation) \
{ \
printf("UnRegSelf " _From " to " _To "converter not implement\n"); \
return NS_OK; \
}
/**
* Interface for a Manager of Charset Converters.
*

View File

@ -63,11 +63,6 @@ nsBIG5ToUnicode::nsBIG5ToUnicode()
{
}
nsresult nsBIG5ToUnicode::CreateInstance(nsISupports ** aResult)
{
*aResult = new nsBIG5ToUnicode();
return (*aResult == NULL)? NS_ERROR_OUT_OF_MEMORY : NS_OK;
}
//----------------------------------------------------------------------
// Subclassing of nsMultiTableDecoderSupport class [implementation]

View File

@ -43,11 +43,6 @@ public:
*/
nsBIG5ToUnicode();
/**
* Static class constructor.
*/
static nsresult CreateInstance(nsISupports **aResult);
protected:
//--------------------------------------------------------------------

View File

@ -29,28 +29,38 @@
// {EFC323E1-EC62-11d2-8AAC-00600811A836}
NS_DECLARE_ID(kBIG5ToUnicodeCID,
0xefc323e1, 0xec62, 0x11d2, 0x8a, 0xac, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36);
#define NS_BIG5TOUNICODE_CID \
{ 0xefc323e1, 0xec62, 0x11d2, {0x8a, 0xac, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36}}
// Class ID for our UnicodeToBIG5 charset converter
// {EFC323E2-EC62-11d2-8AAC-00600811A836}
NS_DECLARE_ID(kUnicodeToBIG5CID,
0xefc323e2, 0xec62, 0x11d2, 0x8a, 0xac, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36);
#define NS_UNICODETOBIG5_CID \
{ 0xefc323e2, 0xec62, 0x11d2, {0x8a, 0xac, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36}}
// Class ID for our UnicodeToBIG5NoAscii charset converter
// {BA615195-1DFA-11d3-B3BF-00805F8A6670}
NS_DECLARE_ID(kUnicodeToBIG5NoAsciiCID,
0xba615195, 0x1dfa, 0x11d3, 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70);
#define NS_UNICODETOBIG5NOASCII_CID \
{ 0xba615195, 0x1dfa, 0x11d3, {0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70}}
// Class ID for our CP950ToUnicode charset converter
NS_DECLARE_ID(kCP950ToUnicodeCID,
// {9416BFBA-1F93-11d3-B3BF-00805F8A6670}
0x9416bfba, 0x1f93, 0x11d3, 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70);
#define NS_CP950TOUNICODE_CID \
{ 0x9416bfba, 0x1f93, 0x11d3, {0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70}}
// Class ID for our UnicodeToCP950 charset converter
NS_DECLARE_ID(kUnicodeToCP950CID,
// {9416BFBB-1F93-11d3-B3BF-00805F8A6670}
0x9416bfbb, 0x1f93, 0x11d3, 0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70);
#define NS_UNICODETOCP950_CID \
{ 0x9416bfbb, 0x1f93, 0x11d3, {0xb3, 0xbf, 0x0, 0x80, 0x5f, 0x8a, 0x66, 0x70}}
#endif /* nsUCvTWCID_h___ */

View File

@ -18,9 +18,7 @@
* Rights Reserved.
*
* Contributor(s):
* Pierre Phaneuf <pp@ludusdesign.com>
*/
#define NS_IMPL_IDS
#include "nspr.h"
@ -29,7 +27,9 @@
#include "nsCOMPtr.h"
#include "nsIFactory.h"
#include "nsIRegistry.h"
#include "nsIGenericFactory.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsIModule.h"
#include "nsUCvTWCID.h"
#include "nsUCvTWDll.h"
@ -61,336 +61,38 @@ PRUint16 g_ASCIIMapping[] = {
0x0001, 0x0004, 0x0005, 0x0008, 0x0000, 0x0000, 0x007F, 0x0000
};
typedef nsresult (* fpCreateInstance) (nsISupports **);
struct FactoryData
{
const nsCID * mCID;
fpCreateInstance CreateInstance;
char * mCharsetSrc;
char * mCharsetDest;
};
NS_IMPL_NSUCONVERTERREGSELF
static FactoryData g_FactoryData[] =
NS_UCONV_REG_UNREG(nsBIG5ToUnicode, "Big5", "Unicode" , NS_BIG5TOUNICODE_CID);
NS_UCONV_REG_UNREG(nsUnicodeToBIG5NoAscii, "Unicode", "x-x-big5", NS_UNICODETOBIG5NOASCII_CID);
NS_UCONV_REG_UNREG(nsUnicodeToBIG5, "Unicode", "Big5" , NS_UNICODETOBIG5_CID);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsBIG5ToUnicode);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsUnicodeToBIG5);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsUnicodeToBIG5NoAscii);
static nsModuleComponentInfo components[] =
{
{
&kBIG5ToUnicodeCID,
nsBIG5ToUnicode::CreateInstance,
"Big5",
"Unicode"
{
ENCODER_NAME_BASE "Big5" , NS_UNICODETOBIG5_CID,
NS_UNICODEENCODER_PROGID_BASE "Big5",
nsUnicodeToBIG5Constructor,
nsUnicodeToBIG5RegSelf, nsUnicodeToBIG5UnRegSelf
},
{
&kUnicodeToBIG5CID,
nsUnicodeToBIG5::CreateInstance,
"Unicode",
"Big5"
{
ENCODER_NAME_BASE "x-x-big5" , NS_UNICODETOBIG5NOASCII_CID,
NS_UNICODEENCODER_PROGID_BASE "x-x-big5",
nsUnicodeToBIG5NoAsciiConstructor,
nsUnicodeToBIG5NoAsciiRegSelf, nsUnicodeToBIG5NoAsciiUnRegSelf
},
{
&kUnicodeToBIG5NoAsciiCID,
nsUnicodeToBIG5NoAscii::CreateInstance,
"Unicode",
"x-x-big5"
{
DECODER_NAME_BASE "Big5" , NS_BIG5TOUNICODE_CID,
NS_UNICODEDECODER_PROGID_BASE "Big5",
nsBIG5ToUnicodeConstructor ,
nsBIG5ToUnicodeRegSelf , nsBIG5ToUnicodeUnRegSelf
}
};
#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
{
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);
};
//----------------------------------------------------------------------------
// Class nsConverterModule [declaration]
class nsConverterModule : public nsIModule
{
NS_DECL_ISUPPORTS
NS_DECL_NSIMODULE
private:
PRBool mInitialized;
void Shutdown();
public:
nsConverterModule();
virtual ~nsConverterModule();
nsresult Initialize();
};
//----------------------------------------------------------------------------
// Global functions and data [implementation]
static nsConverterModule * gModule = NULL;
extern "C" NS_EXPORT nsresult NSGetModule(nsIComponentManager * compMgr,
nsIFile* location,
nsIModule** return_cobj)
{
nsresult rv = NS_OK;
NS_ENSURE_ARG_POINTER(return_cobj);
NS_ENSURE_FALSE(gModule, NS_ERROR_FAILURE);
// Create an initialize the module instance
nsConverterModule * m = new nsConverterModule();
if (!m) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Increase refcnt and store away nsIModule interface to m in return_cobj
rv = m->QueryInterface(NS_GET_IID(nsIModule), (void**)return_cobj);
if (NS_FAILED(rv)) {
delete m;
m = nsnull;
}
gModule = m; // WARNING: Weak Reference
return rv;
}
//----------------------------------------------------------------------------
// Class nsConverterFactory [implementation]
NS_IMPL_ISUPPORTS(nsConverterFactory, NS_GET_IID(nsIFactory));
nsConverterFactory::nsConverterFactory(FactoryData * aData)
{
mData = aData;
NS_INIT_REFCNT();
PR_AtomicIncrement(&g_InstanceCount);
}
nsConverterFactory::~nsConverterFactory()
{
PR_AtomicDecrement(&g_InstanceCount);
}
//----------------------------------------------------------------------------
// 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;
NS_ADDREF(t); // Stabilize
nsresult res = t->QueryInterface(aIID, aResult);
NS_RELEASE(t); // Destabilize and avoid leaks. Avoid calling delete <interface pointer>
return res;
}
NS_IMETHODIMP nsConverterFactory::LockFactory(PRBool aLock)
{
if (aLock) PR_AtomicIncrement(&g_LockCount);
else PR_AtomicDecrement(&g_LockCount);
return NS_OK;
}
//----------------------------------------------------------------------------
// Class nsConverterModule [implementation]
NS_IMPL_ISUPPORTS(nsConverterModule, NS_GET_IID(nsIModule))
nsConverterModule::nsConverterModule()
: mInitialized(PR_FALSE)
{
NS_INIT_ISUPPORTS();
}
nsConverterModule::~nsConverterModule()
{
Shutdown();
}
nsresult nsConverterModule::Initialize()
{
return NS_OK;
}
void nsConverterModule::Shutdown()
{
}
//----------------------------------------------------------------------------
// Interface nsIModule [implementation]
NS_IMETHODIMP nsConverterModule::GetClassObject(nsIComponentManager *aCompMgr,
const nsCID& aClass,
const nsIID& aIID,
void ** r_classObj)
{
nsresult rv;
// Defensive programming: Initialize *r_classObj in case of error below
if (!r_classObj) {
return NS_ERROR_INVALID_POINTER;
}
*r_classObj = NULL;
if (!mInitialized) {
rv = Initialize();
if (NS_FAILED(rv)) {
return rv;
}
mInitialized = PR_TRUE;
}
FactoryData * data;
nsConverterFactory * fact;
// XXX cache these factories
for (PRUint32 i=0; i<ARRAY_SIZE(g_FactoryData); i++) {
data = &(g_FactoryData[i]);
if (aClass.Equals(*(data->mCID))) {
fact = new nsConverterFactory(data);
if (fact == NULL) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = fact->QueryInterface(aIID, (void **) r_classObj);
if (NS_FAILED(rv)) delete fact;
return rv;
}
}
return NS_ERROR_FACTORY_NOT_REGISTERED;
}
NS_IMETHODIMP nsConverterModule::RegisterSelf(nsIComponentManager *aCompMgr,
nsIFile* aPath,
const char* registryLocation,
const char* componentType)
{
nsresult res;
PRUint32 i;
nsIRegistry * registry = NULL;
nsRegistryKey key;
char buff[1024];
// get the registry
res = nsServiceManager::GetService(NS_REGISTRY_PROGID,
NS_GET_IID(nsIRegistry), (nsISupports**)&registry);
if (NS_FAILED(res)) goto done;
// open the registry
res = registry->OpenWellKnownRegistry(
nsIRegistry::ApplicationComponentRegistry);
if (NS_FAILED(res)) goto done;
char name[128];
char progid[128];
char * cid_string;
for (i=0; i<ARRAY_SIZE(g_FactoryData); i++) {
if(0==PL_strcmp(g_FactoryData[i].mCharsetSrc,"Unicode"))
{
PL_strcpy(name, ENCODER_NAME_BASE);
PL_strcat(name, g_FactoryData[i].mCharsetDest);
PL_strcpy(progid, NS_UNICODEENCODER_PROGID_BASE);
PL_strcat(progid, g_FactoryData[i].mCharsetDest);
} else {
PL_strcpy(name, DECODER_NAME_BASE);
PL_strcat(name, g_FactoryData[i].mCharsetSrc);
PL_strcpy(progid, NS_UNICODEDECODER_PROGID_BASE);
PL_strcat(progid, g_FactoryData[i].mCharsetSrc);
}
// register component
res = aCompMgr->RegisterComponentSpec(*(g_FactoryData[i].mCID), name,
progid, aPath, PR_TRUE, PR_TRUE);
if(NS_FAILED(res) && (NS_ERROR_FACTORY_EXISTS != res)) goto done;
// register component info
// XXX take these KONSTANTS out of here; refine this code
cid_string = g_FactoryData[i].mCID->ToString();
sprintf(buff, "%s/%s", "software/netscape/intl/uconv", cid_string);
nsCRT::free(cid_string);
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 (registry != NULL) {
registry -> Close();
nsServiceManager::ReleaseService(NS_REGISTRY_PROGID, registry);
}
return res;
}
NS_IMETHODIMP nsConverterModule::UnregisterSelf(nsIComponentManager *aCompMgr,
nsIFile* aPath,
const char* registryLocation)
{
// XXX also delete the stuff I added to the registry
nsresult rv;
for (PRUint32 i=0; i<ARRAY_SIZE(g_FactoryData); i++) {
rv = aCompMgr->UnregisterComponentSpec(*(g_FactoryData[i].mCID), aPath);
}
return NS_OK;
}
NS_IMETHODIMP nsConverterModule::CanUnload(nsIComponentManager *aCompMgr,
PRBool *okToUnload)
{
if (!okToUnload) {
return NS_ERROR_INVALID_POINTER;
}
*okToUnload = (g_InstanceCount == 0 && g_LockCount == 0);
return NS_OK;
}
NS_IMPL_NSGETMODULE("nsUCvTWModule", components);

View File

@ -58,15 +58,6 @@ nsUnicodeToBIG5::nsUnicodeToBIG5()
{
}
nsresult nsUnicodeToBIG5::CreateInstance(nsISupports ** aResult)
{
nsIUnicodeEncoder *p = new nsUnicodeToBIG5();
if(p) {
*aResult = p;
return NS_OK;
}
return NS_ERROR_OUT_OF_MEMORY;
}
//----------------------------------------------------------------------
// Subclassing of nsTableEncoderSupport class [implementation]

View File

@ -43,11 +43,6 @@ public:
*/
nsUnicodeToBIG5();
/**
* Static class constructor.
*/
static nsresult CreateInstance(nsISupports **aResult);
protected:
//--------------------------------------------------------------------

View File

@ -41,15 +41,6 @@ nsUnicodeToBIG5NoAscii::nsUnicodeToBIG5NoAscii()
{
}
nsresult nsUnicodeToBIG5NoAscii::CreateInstance(nsISupports ** aResult)
{
nsIUnicodeEncoder *p = new nsUnicodeToBIG5NoAscii();
if(p) {
*aResult = p;
return NS_OK;
}
return NS_ERROR_OUT_OF_MEMORY;
}
//----------------------------------------------------------------------
// Subclassing of nsTableEncoderSupport class [implementation]

View File

@ -41,11 +41,6 @@ public:
*/
nsUnicodeToBIG5NoAscii();
/**
* Static class constructor.
*/
static nsresult CreateInstance(nsISupports **aResult);
protected:
//--------------------------------------------------------------------