1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Makoto Kato <m_kato@ga2.so-net.ne.jp >
24 * Ryoichi Furukawa <oliver@1000cp.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
42 #include "nsReadableUtils.h"
43 #include "nsIServiceManager.h"
44 #include "nsICharsetConverterManager.h"
45 #include "nsIScriptableUConv.h"
46 #include "nsScriptableUConv.h"
47 #include "nsIStringStream.h"
49 #include "nsComponentManagerUtils.h"
51 #include "nsIPlatformCharset.h"
53 static PRInt32 gInstanceCount
= 0;
55 /* Implementation file */
56 NS_IMPL_ISUPPORTS1(nsScriptableUnicodeConverter
, nsIScriptableUnicodeConverter
)
58 nsScriptableUnicodeConverter::nsScriptableUnicodeConverter()
60 PR_AtomicIncrement(&gInstanceCount
);
63 nsScriptableUnicodeConverter::~nsScriptableUnicodeConverter()
65 PR_AtomicDecrement(&gInstanceCount
);
69 nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const nsAString
& aSrc
,
74 return NS_ERROR_FAILURE
;
77 PRInt32 inLength
= aSrc
.Length();
78 const nsAFlatString
& flatSrc
= PromiseFlatString(aSrc
);
79 rv
= mEncoder
->GetMaxLength(flatSrc
.get(), inLength
, aOutLen
);
80 if (NS_SUCCEEDED(rv
)) {
81 *_retval
= (char*) nsMemory::Alloc(*aOutLen
+1);
83 return NS_ERROR_OUT_OF_MEMORY
;
85 rv
= mEncoder
->Convert(flatSrc
.get(), &inLength
, *_retval
, aOutLen
);
88 (*_retval
)[*aOutLen
] = '\0';
91 nsMemory::Free(*_retval
);
94 return NS_ERROR_FAILURE
;
97 /* ACString ConvertFromUnicode (in AString src); */
99 nsScriptableUnicodeConverter::ConvertFromUnicode(const nsAString
& aSrc
,
104 nsresult rv
= ConvertFromUnicodeWithLength(aSrc
, &len
, &str
);
105 if (NS_SUCCEEDED(rv
)) {
106 // No Adopt on nsACString :(
107 _retval
.Assign(str
, len
);
114 nsScriptableUnicodeConverter::FinishWithLength(char **_retval
, PRInt32
* aLength
)
117 return NS_ERROR_FAILURE
;
119 PRInt32 finLength
= 32;
121 *_retval
= (char *) nsMemory::Alloc(finLength
);
123 return NS_ERROR_OUT_OF_MEMORY
;
125 nsresult rv
= mEncoder
->Finish(*_retval
, &finLength
);
126 if (NS_SUCCEEDED(rv
))
127 *aLength
= finLength
;
129 nsMemory::Free(*_retval
);
135 /* ACString Finish(); */
137 nsScriptableUnicodeConverter::Finish(nsACString
& _retval
)
141 nsresult rv
= FinishWithLength(&str
, &len
);
142 if (NS_SUCCEEDED(rv
)) {
143 // No Adopt on nsACString :(
144 _retval
.Assign(str
, len
);
150 /* AString ConvertToUnicode (in ACString src); */
152 nsScriptableUnicodeConverter::ConvertToUnicode(const nsACString
& aSrc
, nsAString
& _retval
)
154 nsACString::const_iterator i
;
155 aSrc
.BeginReading(i
);
156 return ConvertFromByteArray(reinterpret_cast<const PRUint8
*>(i
.get()),
161 /* AString convertFromByteArray([const,array,size_is(aCount)] in octet aData,
162 in unsigned long aCount);
165 nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8
* aData
,
170 return NS_ERROR_FAILURE
;
173 PRInt32 inLength
= aCount
;
175 rv
= mDecoder
->GetMaxLength(reinterpret_cast<const char*>(aData
),
176 inLength
, &outLength
);
177 if (NS_SUCCEEDED(rv
))
179 PRUnichar
* buf
= (PRUnichar
*) nsMemory::Alloc((outLength
+1)*sizeof(PRUnichar
));
181 return NS_ERROR_OUT_OF_MEMORY
;
183 rv
= mDecoder
->Convert(reinterpret_cast<const char*>(aData
),
184 &inLength
, buf
, &outLength
);
185 if (NS_SUCCEEDED(rv
))
188 _retval
.Assign(buf
, outLength
);
193 return NS_ERROR_FAILURE
;
197 /* void convertToByteArray(in AString aString,
198 [optional] out unsigned long aLen,
199 [array, size_is(aLen),retval] out octet aData);
202 nsScriptableUnicodeConverter::ConvertToByteArray(const nsAString
& aString
,
208 nsresult rv
= ConvertFromUnicodeWithLength(aString
, &len
, &data
);
212 str
.Adopt(data
, len
); // NOTE: This uses the XPIDLCString as a byte array
214 rv
= FinishWithLength(&data
, &len
);
218 str
.Append(data
, len
);
219 nsMemory::Free(data
);
220 // NOTE: this being a byte array, it needs no null termination
221 *_aData
= reinterpret_cast<PRUint8
*>
222 (nsMemory::Clone(str
.get(), str
.Length()));
224 return NS_ERROR_OUT_OF_MEMORY
;
225 *aLen
= str
.Length();
229 /* nsIInputStream convertToInputStream(in AString aString); */
231 nsScriptableUnicodeConverter::ConvertToInputStream(const nsAString
& aString
,
232 nsIInputStream
** _retval
)
235 nsCOMPtr
<nsIStringInputStream
> inputStream
=
236 do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv
);
242 rv
= ConvertToByteArray(aString
, &dataLen
, &data
);
246 rv
= inputStream
->AdoptData(reinterpret_cast<char*>(data
), dataLen
);
248 nsMemory::Free(data
);
252 NS_ADDREF(*_retval
= inputStream
);
256 /* attribute string charset; */
258 nsScriptableUnicodeConverter::GetCharset(char * *aCharset
)
260 *aCharset
= ToNewCString(mCharset
);
262 return NS_ERROR_OUT_OF_MEMORY
;
268 nsScriptableUnicodeConverter::SetCharset(const char * aCharset
)
270 mCharset
.Assign(aCharset
);
271 return InitConverter();
275 nsScriptableUnicodeConverter::InitConverter()
280 nsCOMPtr
<nsICharsetConverterManager
> ccm
= do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID
, &rv
);
282 if (NS_SUCCEEDED( rv
) && (nsnull
!= ccm
)) {
283 // get charset atom due to getting unicode converter
285 // get an unicode converter
286 rv
= ccm
->GetUnicodeEncoder(mCharset
.get(), getter_AddRefs(mEncoder
));
287 if(NS_SUCCEEDED(rv
)) {
288 rv
= mEncoder
->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace
, nsnull
, (PRUnichar
)'?');
289 if(NS_SUCCEEDED(rv
)) {
290 rv
= ccm
->GetUnicodeDecoder(mCharset
.get(), getter_AddRefs(mDecoder
));