diff --git a/intl/uconv/idl/nsIScriptableUConv.idl b/intl/uconv/idl/nsIScriptableUConv.idl index 3c26fa3bff7..ce706a38309 100644 --- a/intl/uconv/idl/nsIScriptableUConv.idl +++ b/intl/uconv/idl/nsIScriptableUConv.idl @@ -37,6 +37,8 @@ #include "nsISupports.idl" +interface nsIInputStream; + %{C++ // {0A698C44-3BFF-11d4-9649-00C0CA135B4E} #define NS_ISCRIPTABLEUNICODECONVERTER_CID { 0x0A698C44, 0x3BFF, 0x11d4, { 0x96, 0x49, 0x00, 0xC0, 0xCA, 0x13, 0x5B, 0x4E } } @@ -49,7 +51,7 @@ * @created 8/Jun/2000 * @author Makoto Kato [m_kato@ga2.so-net.ne.jp] */ -[scriptable, uuid(6a4bd9ef-abbc-47bb-a634-8c944205515a)] +[scriptable, uuid(b5a06c58-1e32-4386-85ba-f54c4e8a9b48)] interface nsIScriptableUnicodeConverter : nsISupports { /** @@ -57,7 +59,7 @@ interface nsIScriptableUnicodeConverter : nsISupports * Returns the converted string. After converting, Finish should be called * and its return value appended to this return value. */ - string ConvertFromUnicode(in wstring aSrc); + string ConvertFromUnicode(in AString aSrc); /** * Returns the terminator string. @@ -69,26 +71,34 @@ interface nsIScriptableUnicodeConverter : nsISupports /** * Converts the data from one Charset to Unicode. */ - wstring ConvertToUnicode(in string aSrc); + AString ConvertToUnicode(in string aSrc); /** * Converts an array of bytes to a unicode string. */ - wstring convertFromByteArray([const,array,size_is(aCount)] in octet aData, + AString convertFromByteArray([const,array,size_is(aCount)] in octet aData, in unsigned long aCount); /** * Convert a unicode string to an array of bytes. Finish does not need to be * called. */ - void convertToByteArray(in wstring aString, + void convertToByteArray(in AString aString, out unsigned long aLen, [array, size_is(aLen),retval] out octet aData); + /** + * Converts a unicode string to an input stream. The bytes in the stream are + * encoded according to the charset attribute. + * The returned stream will be nonblocking. + */ + nsIInputStream convertToInputStream(in AString aString); + /** * Current character set. * - * @throw NS_ERROR_UCONV_NOCONV The requested charset is not supported. + * @throw NS_ERROR_UCONV_NOCONV + * The requested charset is not supported. */ attribute string charset; }; diff --git a/intl/uconv/src/nsScriptableUConv.cpp b/intl/uconv/src/nsScriptableUConv.cpp index 4b5fa0106cf..5a024353475 100644 --- a/intl/uconv/src/nsScriptableUConv.cpp +++ b/intl/uconv/src/nsScriptableUConv.cpp @@ -44,6 +44,7 @@ #include "nsICharsetConverterManager.h" #include "nsIScriptableUConv.h" #include "nsScriptableUConv.h" +#include "nsIStringStream.h" #include "nsCRT.h" #include "nsIPlatformCharset.h" @@ -64,7 +65,7 @@ nsScriptableUnicodeConverter::~nsScriptableUnicodeConverter() } nsresult -nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const PRUnichar *aSrc, +nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const nsAString& aSrc, PRInt32* aOutLen, char **_retval) { @@ -72,14 +73,15 @@ nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const PRUnichar *aSrc return NS_ERROR_FAILURE; nsresult rv = NS_OK; - PRInt32 inLength = nsCRT::strlen(aSrc); - rv = mEncoder->GetMaxLength(aSrc, inLength, aOutLen); + PRInt32 inLength = aSrc.Length(); + const nsAFlatString& flatSrc = PromiseFlatString(aSrc); + rv = mEncoder->GetMaxLength(flatSrc.get(), inLength, aOutLen); if (NS_SUCCEEDED(rv)) { *_retval = (char*) nsMemory::Alloc(*aOutLen+1); if (!*_retval) return NS_ERROR_OUT_OF_MEMORY; - rv = mEncoder->Convert(aSrc, &inLength, *_retval, aOutLen); + rv = mEncoder->Convert(flatSrc.get(), &inLength, *_retval, aOutLen); if (NS_SUCCEEDED(rv)) { (*_retval)[*aOutLen] = '\0'; @@ -91,9 +93,9 @@ nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const PRUnichar *aSrc return NS_ERROR_FAILURE; } -/* string ConvertFromUnicode ([const] in wstring src); */ +/* string ConvertFromUnicode ([const] in AString src); */ NS_IMETHODIMP -nsScriptableUnicodeConverter::ConvertFromUnicode(const PRUnichar *aSrc, char **_retval) +nsScriptableUnicodeConverter::ConvertFromUnicode(const nsAString& aSrc, char **_retval) { PRInt32 len; return ConvertFromUnicodeWithLength(aSrc, &len, _retval); @@ -130,22 +132,22 @@ nsScriptableUnicodeConverter::Finish(char **_retval) return FinishWithLength(_retval, &len); } -/* wstring ConvertToUnicode ([const] in string src); */ +/* AString ConvertToUnicode ([const] in string src); */ NS_IMETHODIMP -nsScriptableUnicodeConverter::ConvertToUnicode(const char *aSrc, PRUnichar **_retval) +nsScriptableUnicodeConverter::ConvertToUnicode(const char *aSrc, nsAString& _retval) { return ConvertFromByteArray(NS_REINTERPRET_CAST(const PRUint8*, aSrc), strlen(aSrc), _retval); } -/* wstring convertFromByteArray([const,array,size_is(aCount)] in octet aData, +/* AString convertFromByteArray([const,array,size_is(aCount)] in octet aData, in unsigned long aCount); */ NS_IMETHODIMP nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8* aData, PRUint32 aCount, - PRUnichar** _retval) + nsAString& _retval) { if (!mDecoder) return NS_ERROR_FAILURE; @@ -157,30 +159,30 @@ nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8* aData, inLength, &outLength); if (NS_SUCCEEDED(rv)) { - *_retval = (PRUnichar*) nsMemory::Alloc((outLength+1)*sizeof(PRUnichar)); - if (!*_retval) + PRUnichar* buf = (PRUnichar*) nsMemory::Alloc((outLength+1)*sizeof(PRUnichar)); + if (!buf) return NS_ERROR_OUT_OF_MEMORY; rv = mDecoder->Convert(NS_REINTERPRET_CAST(const char*, aData), - &inLength, *_retval, &outLength); + &inLength, buf, &outLength); if (NS_SUCCEEDED(rv)) { - (*_retval)[outLength] = 0; - return NS_OK; + buf[outLength] = 0; + _retval.Assign(buf, outLength); } - nsMemory::Free(*_retval); + nsMemory::Free(buf); + return rv; } - *_retval = nsnull; return NS_ERROR_FAILURE; } -/* void convertToByteArray(in wstring aString, +/* void convertToByteArray(in AString aString, out unsigned long aLen, [array, size_is(aLen),retval] out octet aData); */ NS_IMETHODIMP -nsScriptableUnicodeConverter::ConvertToByteArray(const PRUnichar* aString, +nsScriptableUnicodeConverter::ConvertToByteArray(const nsAString& aString, PRUint32* aLen, PRUint8** _aData) { @@ -206,7 +208,34 @@ nsScriptableUnicodeConverter::ConvertToByteArray(const PRUnichar* aString, return NS_OK; } -/* attribute wstring charset; */ +/* nsIInputStream convertToInputStream(in AString aString); */ +NS_IMETHODIMP +nsScriptableUnicodeConverter::ConvertToInputStream(const nsAString& aString, + nsIInputStream** _retval) +{ + nsresult rv; + nsCOMPtr inputStream = + do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); + if (NS_FAILED(rv)) + return rv; + + PRUint8* data; + PRUint32 dataLen; + rv = ConvertToByteArray(aString, &dataLen, &data); + if (NS_FAILED(rv)) + return rv; + + rv = inputStream->AdoptData(NS_REINTERPRET_CAST(char*, data), dataLen); + if (NS_FAILED(rv)) { + nsMemory::Free(data); + return rv; + } + + NS_ADDREF(*_retval = inputStream); + return rv; +} + +/* attribute string charset; */ NS_IMETHODIMP nsScriptableUnicodeConverter::GetCharset(char * *aCharset) { diff --git a/intl/uconv/src/nsScriptableUConv.h b/intl/uconv/src/nsScriptableUConv.h index 870ff8f0160..401d7d09e86 100644 --- a/intl/uconv/src/nsScriptableUConv.h +++ b/intl/uconv/src/nsScriptableUConv.h @@ -61,7 +61,7 @@ protected: nsCOMPtr mDecoder; nsresult FinishWithLength(char **_retval, PRInt32* aLength); - nsresult ConvertFromUnicodeWithLength(const PRUnichar *aSrc, + nsresult ConvertFromUnicodeWithLength(const nsAString& aSrc, PRInt32* aOutLen, char **_retval);