bug 262385 add byte array methods to nsIScriptableUnicodeConverter

r=jshin sr=darin
This commit is contained in:
cbiesinger%web.de 2004-11-16 23:45:34 +00:00
Родитель 50857de7a0
Коммит 81f3573945
3 изменённых файлов: 106 добавлений и 17 удалений

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

@ -44,32 +44,51 @@
%}
/**
* This interface is unicode encoder using from script
* This interface is a unicode encoder for use by scripts
*
* @created 8/Jun/2000
* @author Makoto Kato [m_kato@ga2.so-net.ne.jp]
*/
[scriptable, uuid(d395ef67-a613-47b9-b5e3-dc7ce1bdb71a)]
[scriptable, uuid(6a4bd9ef-abbc-47bb-a634-8c944205515a)]
interface nsIScriptableUnicodeConverter : nsISupports
{
/**
* Converts the data from Unicode to one Charset.
* Returns the converted string. After converting, Finish should be called
* and its return value appended to this return value.
*/
string ConvertFromUnicode(in wstring aSrc);
/**
* Returns the terminator string.
* Should be called after ConvertFromUnicode().
* Should be called after ConvertFromUnicode() and appended to that
* function's return value.
*/
string Finish();
/**
* Converts the data from one Charset to Unicode.
*/
wstring ConvertToUnicode([const] in string aSrc);
wstring ConvertToUnicode(in string aSrc);
/**
* Current charactor set
* Converts an array of bytes to a unicode string.
*/
wstring 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,
out unsigned long aLen,
[array, size_is(aLen),retval] out octet aData);
/**
* Current character set.
*
* @throw NS_ERROR_UCONV_NOCONV The requested charset is not supported.
*/
attribute string charset;
};

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

@ -63,26 +63,26 @@ nsScriptableUnicodeConverter::~nsScriptableUnicodeConverter()
PR_AtomicDecrement(&gInstanceCount);
}
/* string ConvertFromUnicode ([const] in wstring src); */
NS_IMETHODIMP
nsScriptableUnicodeConverter::ConvertFromUnicode(const PRUnichar *aSrc, char **_retval)
nsresult
nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const PRUnichar *aSrc,
PRInt32* aOutLen,
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);
rv = mEncoder->GetMaxLength(aSrc, inLength, aOutLen);
if (NS_SUCCEEDED(rv)) {
*_retval = (char*) nsMemory::Alloc(outLength+1);
*_retval = (char*) nsMemory::Alloc(*aOutLen+1);
if (!*_retval)
return NS_ERROR_OUT_OF_MEMORY;
rv = mEncoder->Convert(aSrc, &inLength, *_retval, &outLength);
rv = mEncoder->Convert(aSrc, &inLength, *_retval, aOutLen);
if (NS_SUCCEEDED(rv))
{
(*_retval)[outLength] = '\0';
(*_retval)[*aOutLen] = '\0';
return NS_OK;
}
nsMemory::Free(*_retval);
@ -91,8 +91,16 @@ nsScriptableUnicodeConverter::ConvertFromUnicode(const PRUnichar *aSrc, char **_
return NS_ERROR_FAILURE;
}
/* string ConvertFromUnicode ([const] in wstring src); */
NS_IMETHODIMP
nsScriptableUnicodeConverter::Finish(char **_retval)
nsScriptableUnicodeConverter::ConvertFromUnicode(const PRUnichar *aSrc, char **_retval)
{
PRInt32 len;
return ConvertFromUnicodeWithLength(aSrc, &len, _retval);
}
nsresult
nsScriptableUnicodeConverter::FinishWithLength(char **_retval, PRInt32* aLength)
{
if (!mEncoder)
return NS_ERROR_FAILURE;
@ -109,28 +117,52 @@ nsScriptableUnicodeConverter::Finish(char **_retval)
else
nsMemory::Free(*_retval);
*aLength = finLength;
return rv;
}
NS_IMETHODIMP
nsScriptableUnicodeConverter::Finish(char **_retval)
{
PRInt32 len;
return FinishWithLength(_retval, &len);
}
/* wstring ConvertToUnicode ([const] in string src); */
NS_IMETHODIMP
nsScriptableUnicodeConverter::ConvertToUnicode(const char *aSrc, PRUnichar **_retval)
{
return ConvertFromByteArray(NS_REINTERPRET_CAST(const PRUint8*, aSrc),
strlen(aSrc),
_retval);
}
/* wstring convertFromByteArray([const,array,size_is(aCount)] in octet aData,
in unsigned long aCount);
*/
NS_IMETHODIMP
nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8* aData,
PRUint32 aCount,
PRUnichar** _retval)
{
if (!mDecoder)
return NS_ERROR_FAILURE;
nsresult rv = NS_OK;
PRInt32 inLength = strlen(aSrc);
PRInt32 inLength = aCount;
PRInt32 outLength;
rv = mDecoder->GetMaxLength(aSrc, inLength, &outLength);
rv = mDecoder->GetMaxLength(NS_REINTERPRET_CAST(const char*, aData),
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);
rv = mDecoder->Convert(NS_REINTERPRET_CAST(const char*, aData),
&inLength, *_retval, &outLength);
if (NS_SUCCEEDED(rv))
{
(*_retval)[outLength] = 0;
@ -140,6 +172,38 @@ nsScriptableUnicodeConverter::ConvertToUnicode(const char *aSrc, PRUnichar **_re
}
*_retval = nsnull;
return NS_ERROR_FAILURE;
}
/* void convertToByteArray(in wstring aString,
out unsigned long aLen,
[array, size_is(aLen),retval] out octet aData);
*/
NS_IMETHODIMP
nsScriptableUnicodeConverter::ConvertToByteArray(const PRUnichar* aString,
PRUint32* aLen,
PRUint8** _aData)
{
char* data;
PRInt32 len;
nsresult rv = ConvertFromUnicodeWithLength(aString, &len, &data);
if (NS_FAILED(rv))
return rv;
nsXPIDLCString str;
str.Adopt(data, len); // NOTE: This uses the XPIDLCString as a byte array
rv = FinishWithLength(&data, &len);
if (NS_FAILED(rv))
return rv;
str.Append(data, len);
// NOTE: this being a byte array, it needs no null termination
*_aData = NS_REINTERPRET_CAST(PRUint8*,
nsMemory::Clone(str.get(), str.Length()));
if (!*_aData)
return NS_ERROR_OUT_OF_MEMORY;
*aLen = str.Length();
return NS_OK;
}
/* attribute wstring charset; */

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

@ -60,6 +60,12 @@ protected:
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
nsresult FinishWithLength(char **_retval, PRInt32* aLength);
nsresult ConvertFromUnicodeWithLength(const PRUnichar *aSrc,
PRInt32* aOutLen,
char **_retval);
nsresult InitConverter();
};