Hacky Unicode converters implementation replaced with the real xpcom thing.

This commit is contained in:
cata%netscape.com 1999-02-03 18:55:10 +00:00
Родитель 3966fece84
Коммит c277c4a8e9
5 изменённых файлов: 79 добавлений и 201 удалений

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

@ -25,11 +25,6 @@ class nsString;
{ 0x2d97fbf0, 0x93b5, 0x11d1, \
{0x89, 0x5b, 0x00, 0x60, 0x08, 0x91, 0x1b, 0x81} }
#define NS_IB2UCONVERTER_IID \
{ 0x35e40290, 0x93b5, 0x11d1, \
{0x89, 0x5b, 0x00, 0x60, 0x08, 0x91, 0x1b, 0x81} }
/** Abstract unicode character input stream
* @see nsIInputStream
*/
@ -52,21 +47,6 @@ extern NS_BASE nsresult
NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
nsString* aString);
/// Abstract interface for converting from bytes to unicode characters
class nsIB2UConverter : public nsISupports {
public:
/** aDstLen is updated to indicate how much data was translated into
* aDst; aSrcLen is updated to indicate how much data was used in
* the source buffer.
*/
NS_IMETHOD Convert(PRUnichar* aDst,
PRUint32 aDstOffset,
PRUint32& aDstLen,
const char* aSrc,
PRUint32 aSrcOffset,
PRUint32& aSrcLen) = 0;
};
/** Create a new nsUnicharInputStream that provides a converter for the
* byte input stream aStreamToWrap. If no converter can be found then
* nsnull is returned and the error code is set to
@ -79,13 +59,4 @@ extern NS_BASE nsresult
PRInt32 aBufferSize = 0,
nsString* aCharSet = nsnull);
/** Create a new nsB2UConverter for the given character set. When given
* nsnull, the converter for iso-latin1 to unicode is provided. If no
* converter can be found, nsnull is returned.
*/
extern NS_BASE nsresult
NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
nsISupports* aOuter,
nsString* aCharSet = nsnull);
#endif /* nsUnicharInputStream_h___ */

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

@ -15,9 +15,14 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#define NS_IMPL_IDS
#include "nsIUnicharInputStream.h"
#include "nsIByteBuffer.h"
#include "nsIUnicharBuffer.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsIUnicodeDecoder.h"
#include "nsString.h"
#include "nsCRT.h"
#include <fcntl.h>
@ -115,66 +120,34 @@ NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
//----------------------------------------------------------------------
class IsoLatin1Converter : public nsIB2UConverter {
public:
IsoLatin1Converter();
NS_DECL_ISUPPORTS
NS_IMETHOD Convert(PRUnichar* aDst,
PRUint32 aDstOffset,
PRUint32& aDstLen,
const char* aSrc,
PRUint32 aSrcOffset,
PRUint32& aSrcLen);
};
IsoLatin1Converter::IsoLatin1Converter()
{
NS_INIT_REFCNT();
}
NS_DEFINE_IID(kIB2UConverterIID, NS_IB2UCONVERTER_IID);
NS_IMPL_ISUPPORTS(IsoLatin1Converter,kIB2UConverterIID);
nsresult IsoLatin1Converter::Convert(PRUnichar* aDst,
PRUint32 aDstOffset,
PRUint32& aDstLen,
const char* aSrc,
PRUint32 aSrcOffset,
PRUint32& aSrcLen)
{
PRUint32 amount = aSrcLen;
if (aSrcLen > aDstLen) {
amount = aDstLen;
}
const char* end = aSrc + amount;
while (aSrc < end) {
PRUint8 isoLatin1 = PRUint8(*aSrc++);
/* XXX insert table based lookup converter here */
*aDst++ = isoLatin1;
}
aDstLen = amount;
aSrcLen = amount;
return NS_OK;
}
NS_BASE nsresult
NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
/**
* This function used to be public, with the NS_BASE declaration. I am
* changing it right now into a module private visibility because there are
* better and more xpcom-like ways to get a Converter.
*/
nsresult
NS_NewB2UConverter(nsIUnicodeDecoder** aInstancePtrResult,
nsISupports* aOuter,
nsString* aCharSet)
{
if (nsnull != aOuter) {
return NS_ERROR_NO_AGGREGATION;
}
// We cannot use enum to pass charset id
if ((nsnull != aCharSet) && (! aCharSet->EqualsIgnoreCase( "iso-8859-1" ))){
return NS_BASE_STREAM_NO_CONVERTER;
}
IsoLatin1Converter* it = new IsoLatin1Converter();
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(kIB2UConverterIID, (void**)aInstancePtrResult);
// Create converter
nsresult res;
nsICharsetConverterManager * ccm;
nsAutoString defaultCharset("ISO-8859-1");
if (aCharSet == nsnull) aCharSet = &defaultCharset;
res = nsServiceManager::GetService(kCharsetConverterManagerCID,
kICharsetConverterManagerIID, (nsISupports**)&ccm);
if (NS_FAILED(res)) return res;
res = ccm->GetUnicodeDecoder(aCharSet, aInstancePtrResult);
nsServiceManager::ReleaseService(kCharsetConverterManagerCID, ccm);
return res;
}
//----------------------------------------------------------------------
@ -182,7 +155,7 @@ NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
class ConverterInputStream : public nsIUnicharInputStream {
public:
ConverterInputStream(nsIInputStream* aStream,
nsIB2UConverter* aConverter,
nsIUnicodeDecoder* aConverter,
PRUint32 aBufSize);
~ConverterInputStream();
@ -197,7 +170,7 @@ protected:
PRInt32 Fill(nsresult * aErrorCode);
nsIInputStream* mInput;
nsIB2UConverter* mConverter;
nsIUnicodeDecoder* mConverter;
nsIByteBuffer* mByteData;
PRUint32 mByteDataOffset;
nsIUnicharBuffer* mUnicharData;
@ -206,7 +179,7 @@ protected:
};
ConverterInputStream::ConverterInputStream(nsIInputStream* aStream,
nsIB2UConverter* aConverter,
nsIUnicodeDecoder* aConverter,
PRUint32 aBufferSize)
{
NS_INIT_REFCNT();
@ -300,10 +273,10 @@ PRInt32 ConverterInputStream::Fill(nsresult * aErrorCode)
NS_ASSERTION(remainder + nb == mByteData->GetLength(), "bad nb");
// Now convert as much of the byte buffer to unicode as possible
PRUint32 dstLen = mUnicharData->GetBufferSize();
PRUint32 srcLen = remainder + nb;
*aErrorCode = mConverter->Convert(mUnicharData->GetBuffer(), 0, dstLen,
mByteData->GetBuffer(), 0, srcLen);
PRInt32 dstLen = mUnicharData->GetBufferSize();
PRInt32 srcLen = remainder + nb;
*aErrorCode = mConverter->Convert(mUnicharData->GetBuffer(), 0, &dstLen,
mByteData->GetBuffer(), 0, &srcLen);
mUnicharDataOffset = 0;
mUnicharDataLength = dstLen;
mByteDataOffset += srcLen;
@ -323,7 +296,7 @@ NS_NewConverterStream(nsIUnicharInputStream** aInstancePtrResult,
}
// Create converter
nsIB2UConverter* converter;
nsIUnicodeDecoder* converter;
nsresult rv = NS_NewB2UConverter(&converter, nsnull, aCharSet);
if (NS_OK != rv) {
return rv;
@ -332,7 +305,7 @@ NS_NewConverterStream(nsIUnicharInputStream** aInstancePtrResult,
// Create converter input stream
ConverterInputStream* it =
new ConverterInputStream(aStreamToWrap, converter, aBufferSize);
converter->Release();
NS_RELEASE(converter);
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}

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

@ -23,7 +23,6 @@
#include "nsIHTMLContent.h"
#include "nsIHTMLAttributes.h"
#include "nsITextContent.h"
#include "nsIUnicharInputStream.h"
#include "nsString.h"
#include "nsIDocument.h"
#include "nsISupportsArray.h"
@ -192,30 +191,21 @@ MyDocument::~MyDocument()
int main(int argc, char** argv)
{
// Create Byte2Unicode converter
nsresult rv;
nsIB2UConverter *converter;
rv = NS_NewB2UConverter(&converter,NULL);
if (NS_OK != rv) {
printf("Could not create converter.\n");
return -1;
}
/* Create Byte2Unicode converter? Not anymore. The converters are not tested
here, they have their own test code. And if you just want to use them, you
need a properly intialized xpcom system. This simple test program doesn't do
that. */
// Create a unicode string
static const char* srcStr = "This is some meaningless text about nothing at all";
PRInt32 rv2;
nsresult rv;
PRUint32 origSrcLen = nsCRT::strlen((char *)srcStr);
const int BUFFER_LENGTH = 100;
PRUnichar destStr[BUFFER_LENGTH];
PRUint32 srcLen = origSrcLen;
PRUint32 destLen = BUFFER_LENGTH;
rv2 = converter->Convert(destStr,0,destLen,srcStr,0,srcLen);
if ((NS_OK != rv2) || (srcLen < origSrcLen)) {
printf("Failed to convert all characters to unicode.\n");
return -1;
}
delete converter;
// hacky Ascii conversion to unicode, because we don't have a real Converter.
for (PRUint32 i=0; i<srcLen; i++) destStr[i] = ((PRUint8)srcStr[i]);
// Create test document.
MyDocument *myDoc = new MyDocument();

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

@ -25,11 +25,6 @@ class nsString;
{ 0x2d97fbf0, 0x93b5, 0x11d1, \
{0x89, 0x5b, 0x00, 0x60, 0x08, 0x91, 0x1b, 0x81} }
#define NS_IB2UCONVERTER_IID \
{ 0x35e40290, 0x93b5, 0x11d1, \
{0x89, 0x5b, 0x00, 0x60, 0x08, 0x91, 0x1b, 0x81} }
/** Abstract unicode character input stream
* @see nsIInputStream
*/
@ -52,21 +47,6 @@ extern NS_BASE nsresult
NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
nsString* aString);
/// Abstract interface for converting from bytes to unicode characters
class nsIB2UConverter : public nsISupports {
public:
/** aDstLen is updated to indicate how much data was translated into
* aDst; aSrcLen is updated to indicate how much data was used in
* the source buffer.
*/
NS_IMETHOD Convert(PRUnichar* aDst,
PRUint32 aDstOffset,
PRUint32& aDstLen,
const char* aSrc,
PRUint32 aSrcOffset,
PRUint32& aSrcLen) = 0;
};
/** Create a new nsUnicharInputStream that provides a converter for the
* byte input stream aStreamToWrap. If no converter can be found then
* nsnull is returned and the error code is set to
@ -79,13 +59,4 @@ extern NS_BASE nsresult
PRInt32 aBufferSize = 0,
nsString* aCharSet = nsnull);
/** Create a new nsB2UConverter for the given character set. When given
* nsnull, the converter for iso-latin1 to unicode is provided. If no
* converter can be found, nsnull is returned.
*/
extern NS_BASE nsresult
NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
nsISupports* aOuter,
nsString* aCharSet = nsnull);
#endif /* nsUnicharInputStream_h___ */

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

@ -15,9 +15,14 @@
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#define NS_IMPL_IDS
#include "nsIUnicharInputStream.h"
#include "nsIByteBuffer.h"
#include "nsIUnicharBuffer.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsIUnicodeDecoder.h"
#include "nsString.h"
#include "nsCRT.h"
#include <fcntl.h>
@ -115,66 +120,34 @@ NS_NewStringUnicharInputStream(nsIUnicharInputStream** aInstancePtrResult,
//----------------------------------------------------------------------
class IsoLatin1Converter : public nsIB2UConverter {
public:
IsoLatin1Converter();
NS_DECL_ISUPPORTS
NS_IMETHOD Convert(PRUnichar* aDst,
PRUint32 aDstOffset,
PRUint32& aDstLen,
const char* aSrc,
PRUint32 aSrcOffset,
PRUint32& aSrcLen);
};
IsoLatin1Converter::IsoLatin1Converter()
{
NS_INIT_REFCNT();
}
NS_DEFINE_IID(kIB2UConverterIID, NS_IB2UCONVERTER_IID);
NS_IMPL_ISUPPORTS(IsoLatin1Converter,kIB2UConverterIID);
nsresult IsoLatin1Converter::Convert(PRUnichar* aDst,
PRUint32 aDstOffset,
PRUint32& aDstLen,
const char* aSrc,
PRUint32 aSrcOffset,
PRUint32& aSrcLen)
{
PRUint32 amount = aSrcLen;
if (aSrcLen > aDstLen) {
amount = aDstLen;
}
const char* end = aSrc + amount;
while (aSrc < end) {
PRUint8 isoLatin1 = PRUint8(*aSrc++);
/* XXX insert table based lookup converter here */
*aDst++ = isoLatin1;
}
aDstLen = amount;
aSrcLen = amount;
return NS_OK;
}
NS_BASE nsresult
NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
/**
* This function used to be public, with the NS_BASE declaration. I am
* changing it right now into a module private visibility because there are
* better and more xpcom-like ways to get a Converter.
*/
nsresult
NS_NewB2UConverter(nsIUnicodeDecoder** aInstancePtrResult,
nsISupports* aOuter,
nsString* aCharSet)
{
if (nsnull != aOuter) {
return NS_ERROR_NO_AGGREGATION;
}
// We cannot use enum to pass charset id
if ((nsnull != aCharSet) && (! aCharSet->EqualsIgnoreCase( "iso-8859-1" ))){
return NS_BASE_STREAM_NO_CONVERTER;
}
IsoLatin1Converter* it = new IsoLatin1Converter();
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}
return it->QueryInterface(kIB2UConverterIID, (void**)aInstancePtrResult);
// Create converter
nsresult res;
nsICharsetConverterManager * ccm;
nsAutoString defaultCharset("ISO-8859-1");
if (aCharSet == nsnull) aCharSet = &defaultCharset;
res = nsServiceManager::GetService(kCharsetConverterManagerCID,
kICharsetConverterManagerIID, (nsISupports**)&ccm);
if (NS_FAILED(res)) return res;
res = ccm->GetUnicodeDecoder(aCharSet, aInstancePtrResult);
nsServiceManager::ReleaseService(kCharsetConverterManagerCID, ccm);
return res;
}
//----------------------------------------------------------------------
@ -182,7 +155,7 @@ NS_NewB2UConverter(nsIB2UConverter** aInstancePtrResult,
class ConverterInputStream : public nsIUnicharInputStream {
public:
ConverterInputStream(nsIInputStream* aStream,
nsIB2UConverter* aConverter,
nsIUnicodeDecoder* aConverter,
PRUint32 aBufSize);
~ConverterInputStream();
@ -197,7 +170,7 @@ protected:
PRInt32 Fill(nsresult * aErrorCode);
nsIInputStream* mInput;
nsIB2UConverter* mConverter;
nsIUnicodeDecoder* mConverter;
nsIByteBuffer* mByteData;
PRUint32 mByteDataOffset;
nsIUnicharBuffer* mUnicharData;
@ -206,7 +179,7 @@ protected:
};
ConverterInputStream::ConverterInputStream(nsIInputStream* aStream,
nsIB2UConverter* aConverter,
nsIUnicodeDecoder* aConverter,
PRUint32 aBufferSize)
{
NS_INIT_REFCNT();
@ -300,10 +273,10 @@ PRInt32 ConverterInputStream::Fill(nsresult * aErrorCode)
NS_ASSERTION(remainder + nb == mByteData->GetLength(), "bad nb");
// Now convert as much of the byte buffer to unicode as possible
PRUint32 dstLen = mUnicharData->GetBufferSize();
PRUint32 srcLen = remainder + nb;
*aErrorCode = mConverter->Convert(mUnicharData->GetBuffer(), 0, dstLen,
mByteData->GetBuffer(), 0, srcLen);
PRInt32 dstLen = mUnicharData->GetBufferSize();
PRInt32 srcLen = remainder + nb;
*aErrorCode = mConverter->Convert(mUnicharData->GetBuffer(), 0, &dstLen,
mByteData->GetBuffer(), 0, &srcLen);
mUnicharDataOffset = 0;
mUnicharDataLength = dstLen;
mByteDataOffset += srcLen;
@ -323,7 +296,7 @@ NS_NewConverterStream(nsIUnicharInputStream** aInstancePtrResult,
}
// Create converter
nsIB2UConverter* converter;
nsIUnicodeDecoder* converter;
nsresult rv = NS_NewB2UConverter(&converter, nsnull, aCharSet);
if (NS_OK != rv) {
return rv;
@ -332,7 +305,7 @@ NS_NewConverterStream(nsIUnicharInputStream** aInstancePtrResult,
// Create converter input stream
ConverterInputStream* it =
new ConverterInputStream(aStreamToWrap, converter, aBufferSize);
converter->Release();
NS_RELEASE(converter);
if (nsnull == it) {
return NS_ERROR_OUT_OF_MEMORY;
}