Bug:54857

a new ScriptableUnicodeConverter IDL
/r=nhotta,/sr=blizzard
This commit is contained in:
yokoyama%netscape.com 2001-08-01 22:36:54 +00:00
parent e7bc717e12
commit d9d429cc55
8 changed files with 272 additions and 0 deletions

View File

@ -32,6 +32,7 @@ XPIDLSRCS = \
nsITextToSubURI.idl \
nsICurrentCharsetListener.idl \
nsICharsetConverterManager2.idl \
nsIScriptableUConv.idl \
$(NULL)
include $(topsrcdir)/config/rules.mk

View File

@ -27,6 +27,7 @@ XPIDLSRCS = \
.\nsITextToSubURI.idl \
.\nsICurrentCharsetListener.idl \
.\nsICharsetConverterManager2.idl \
.\nsIScriptableUConv.idl \
$(NULL)
include <$(DEPTH)/config/rules.mak>

View File

@ -0,0 +1,54 @@
/* -*- 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.1 (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.
*
* Contributor(s):
*/
#include "nsISupports.idl"
%{C++
// {0A698C44-3BFF-11d4-9649-00C0CA135B4E}
#define NS_ISCRIPTABLEUNICODECONVERTER_CID { 0x0A698C44, 0x3BFF, 0x11d4, { 0x96, 0x49, 0x00, 0xC0, 0xCA, 0x13, 0x5B, 0x4E } }
#define NS_ISCRIPTABLEUNICODECONVERTER_CONTRACTID "@mozilla.org/intl/scriptableunicodeconverter"
%}
/**
* This interface is unicode encoder using from script
*
* @created 8/Jun/2000
* @author Makoto Kato [m_kato@ga2.so-net.ne.jp]
*/
[scriptable, uuid(0A698C44-3BFF-11d4-9649-00C0CA135B4E)]
interface nsIScriptableUnicodeConverter : nsISupports
{
/**
* Converts the data from Unicode to one Charset.
*/
string ConvertFromUnicode([const] in wstring aSrc);
/**
* Converts the data from one Charset to Unicode.
*/
wstring ConvertToUnicode([const] in string aSrc);
/**
* Current charactor set
*/
attribute wstring charset;
};

View File

@ -61,6 +61,7 @@ CPPSRCS = \
nsUnicodeToMacRoman.cpp \
nsUnicodeToUTF8.cpp \
nsUCvMinSupport.cpp \
nsScriptableUConv.cpp \
$(NULL)
EXPORT_RESOURCE = \

View File

@ -48,6 +48,7 @@ CPPSRCS = \
nsUnicodeToMacRoman.cpp \
nsUnicodeToUTF8.cpp \
nsUCvMinSupport.cpp \
nsScriptableUConv.cpp \
$(NULL)
CPP_OBJS= \
@ -70,6 +71,7 @@ CPP_OBJS= \
.\$(OBJDIR)\nsUnicodeToMacRoman.obj \
.\$(OBJDIR)\nsUnicodeToUTF8.obj \
.\$(OBJDIR)\nsUCvMinSupport.obj \
.\$(OBJDIR)\nsScriptableUConv.obj \
$(NULL)
CSRCS = \

View File

@ -0,0 +1,156 @@
/* -*- 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.1 (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.
*
* Contributor(s):
* Makoto Kato <m_kato@ga2.so-net.ne.jp >
* Ryoichi Furukawa <oliver@1000cp.com>
*/
#include "pratom.h"
#include "nsString.h"
#include "nsIServiceManager.h"
#include "nsICharsetConverterManager.h"
#include "nsICharsetConverterManager2.h"
#include "nsIScriptableUConv.h"
#include "nsScriptableUConv.h"
#define NS_IMPL_IDS
#include "nsIPlatformCharset.h"
#undef NS_IMPL_IDS
static NS_DEFINE_CID(kIScriptableUnicodeConverterCID, NS_ISCRIPTABLEUNICODECONVERTER_CID);
static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID);
static PRInt32 gInstanceCount = 0;
/* Implementation file */
NS_IMPL_ISUPPORTS1(nsScriptableUnicodeConverter, nsIScriptableUnicodeConverter)
nsScriptableUnicodeConverter::nsScriptableUnicodeConverter()
{
NS_INIT_REFCNT();
PR_AtomicIncrement(&gInstanceCount);
}
nsScriptableUnicodeConverter::~nsScriptableUnicodeConverter()
{
PR_AtomicDecrement(&gInstanceCount);
}
/* string ConvertFromUnicode ([const] in wstring src); */
NS_IMETHODIMP
nsScriptableUnicodeConverter::ConvertFromUnicode(const PRUnichar *aSrc, char **_retval)
{
if (!mEncoder)
return NS_ERROR_FAILURE;
nsresult rv = NS_OK;
PRInt32 inLength = nsCRT::strlen(aSrc);
PRInt32 outLength;
rv = mEncoder->GetMaxLength(aSrc, inLength, &outLength);
if (NS_SUCCEEDED(rv)) {
*_retval = (char*) nsMemory::Alloc(outLength+1);
if (!*_retval)
return NS_ERROR_OUT_OF_MEMORY;
rv = mEncoder->Convert(aSrc, &inLength, *_retval, &outLength);
if (NS_SUCCEEDED(rv))
{
(*_retval)[outLength] = '\0';
return NS_OK;
}
nsMemory::Free(*_retval);
}
*_retval = nsnull;
return NS_ERROR_FAILURE;
}
/* wstring ConvertToUnicode ([const] in string src); */
NS_IMETHODIMP
nsScriptableUnicodeConverter::ConvertToUnicode(const char *aSrc, PRUnichar **_retval)
{
if (!mDecoder)
return NS_ERROR_FAILURE;
nsresult rv = NS_OK;
PRInt32 inLength = nsCRT::strlen(aSrc);
PRInt32 outLength;
rv = mDecoder->GetMaxLength(aSrc, inLength, &outLength);
if (NS_SUCCEEDED(rv))
{
*_retval = (PRUnichar*) nsMemory::Alloc((outLength+1)*sizeof(PRUnichar));
if (!*_retval)
return NS_ERROR_OUT_OF_MEMORY;
rv = mDecoder->Convert(aSrc, &inLength, *_retval, &outLength);
if (NS_SUCCEEDED(rv))
{
(*_retval)[outLength] = 0;
return NS_OK;
}
nsMemory::Free(*_retval);
}
*_retval = nsnull;
return NS_ERROR_FAILURE;
}
/* attribute wstring charset; */
NS_IMETHODIMP
nsScriptableUnicodeConverter::GetCharset(PRUnichar * *aCharset)
{
*aCharset = mCharset.ToNewUnicode() ;
if (!*aCharset)
return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
NS_IMETHODIMP
nsScriptableUnicodeConverter::SetCharset(const PRUnichar * aCharset)
{
mCharset.Assign(aCharset);
return InitConverter();
}
nsresult
nsScriptableUnicodeConverter::InitConverter()
{
nsresult rv = NS_OK;
mEncoder = NULL ;
nsCOMPtr<nsICharsetConverterManager2> ccm2 = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
if (NS_SUCCEEDED( rv) && (nsnull != ccm2)) {
// get charset atom due to getting unicode converter
nsCOMPtr <nsIAtom> charsetAtom;
rv = ccm2->GetCharsetAtom(mCharset.get(), getter_AddRefs(charsetAtom));
if (NS_SUCCEEDED(rv)) {
// get an unicode converter
rv = ccm2->GetUnicodeEncoder(charsetAtom, getter_AddRefs(mEncoder));
if(NS_SUCCEEDED(rv)) {
rv = mEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, (PRUnichar)'?');
if(NS_SUCCEEDED(rv)) {
rv = ccm2->GetUnicodeDecoder(charsetAtom, getter_AddRefs(mDecoder));
}
}
}
}
return rv ;
}

View File

@ -0,0 +1,49 @@
/* -*- 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.1 (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.
*
* Contributor(s):
* Makoto Kato <m_kato@ga2.so-net.ne.jp >
* Ryoichi Furukawa <oliver@1000cp.com>
*/
#ifndef __nsScriptableUConv_h_
#define __nsScriptableUConv_h_
#include "nsICharsetConverterManager2.h"
#include "nsIScriptableUConv.h"
class nsScriptableUnicodeConverter : public nsIScriptableUnicodeConverter
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSISCRIPTABLEUNICODECONVERTER
nsScriptableUnicodeConverter();
virtual ~nsScriptableUnicodeConverter();
protected:
nsAutoString mCharset;
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
nsresult InitConverter();
};
#endif

View File

@ -37,6 +37,7 @@
#include "nsUConvDll.h"
#include "nsFileSpec.h"
#include "nsIFile.h"
#include "nsIScriptableUConv.h"
#include "nsUCvMinSupport.h"
#include "nsISO88591ToUnicode.h"
@ -47,6 +48,7 @@
#include "nsUnicodeToCP1252.h"
#include "nsUnicodeToMacRoman.h"
#include "nsUnicodeToUTF8.h"
#include "nsScriptableUConv.h"
//----------------------------------------------------------------------
// Global functions and data [declaration]
@ -56,6 +58,7 @@ static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CI
static NS_DEFINE_CID(kCharsetMenuCID, NS_CHARSETMENU_CID);
static NS_DEFINE_CID(kTextToSubURICID, NS_TEXTTOSUBURI_CID);
static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID);
static NS_DEFINE_CID(kIScriptableUnicodeConverterCID, NS_ISCRIPTABLEUNICODECONVERTER_CID);
// converters
NS_DEFINE_CID(kISO88591ToUnicodeCID, NS_ISO88591TOUNICODE_CID);
@ -66,6 +69,7 @@ NS_DEFINE_CID(kUnicodeToISO88591CID, NS_UNICODETOISO88591_CID);
NS_DEFINE_CID(kUnicodeToCP1252CID, NS_UNICODETOCP1252_CID);
NS_DEFINE_CID(kUnicodeToMacRomanCID, NS_UNICODETOMACROMAN_CID);
NS_DEFINE_CID(kUnicodeToUTF8CID, NS_UNICODETOUTF8_CID);
NS_GENERIC_FACTORY_CONSTRUCTOR(nsScriptableUnicodeConverter);
class nsUConvModule : public nsIModule {
public:
@ -184,6 +188,10 @@ static Components gComponents[] = {
{ { "Unicode To UTF-8 Converter", NS_UNICODETOUTF8_CID,
NS_UNICODETOUTF8_CONTRACTID, NS_NewUnicodeToUTF8 },
"Unicode", "UTF-8", },
{ { "Unicode Encoder / Decoder for Script", NS_ISCRIPTABLEUNICODECONVERTER_CID,
NS_ISCRIPTABLEUNICODECONVERTER_CONTRACTID, nsScriptableUnicodeConverterConstructor },
NULL, NULL, },
};
#define NUM_COMPONENTS (sizeof(gComponents) / sizeof(gComponents[0]))