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"
50 #include "nsIPlatformCharset.h"
52 static PRInt32 gInstanceCount
= 0;
54 /* Implementation file */
55 NS_IMPL_ISUPPORTS1(nsScriptableUnicodeConverter
, nsIScriptableUnicodeConverter
)
57 nsScriptableUnicodeConverter::nsScriptableUnicodeConverter()
59 PR_AtomicIncrement(&gInstanceCount
);
62 nsScriptableUnicodeConverter::~nsScriptableUnicodeConverter()
64 PR_AtomicDecrement(&gInstanceCount
);
68 nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const nsAString
& aSrc
,
73 return NS_ERROR_FAILURE
;
76 PRInt32 inLength
= aSrc
.Length();
77 const nsAFlatString
& flatSrc
= PromiseFlatString(aSrc
);
78 rv
= mEncoder
->GetMaxLength(flatSrc
.get(), inLength
, aOutLen
);
79 if (NS_SUCCEEDED(rv
)) {
80 *_retval
= (char*) nsMemory::Alloc(*aOutLen
+1);
82 return NS_ERROR_OUT_OF_MEMORY
;
84 rv
= mEncoder
->Convert(flatSrc
.get(), &inLength
, *_retval
, aOutLen
);
87 (*_retval
)[*aOutLen
] = '\0';
90 nsMemory::Free(*_retval
);
93 return NS_ERROR_FAILURE
;
96 /* ACString ConvertFromUnicode (in AString src); */
98 nsScriptableUnicodeConverter::ConvertFromUnicode(const nsAString
& aSrc
,
103 nsresult rv
= ConvertFromUnicodeWithLength(aSrc
, &len
, &str
);
104 if (NS_SUCCEEDED(rv
)) {
105 // No Adopt on nsACString :(
106 _retval
.Assign(str
, len
);
113 nsScriptableUnicodeConverter::FinishWithLength(char **_retval
, PRInt32
* aLength
)
116 return NS_ERROR_FAILURE
;
118 PRInt32 finLength
= 32;
120 *_retval
= (char *) nsMemory::Alloc(finLength
);
122 return NS_ERROR_OUT_OF_MEMORY
;
124 nsresult rv
= mEncoder
->Finish(*_retval
, &finLength
);
125 if (NS_SUCCEEDED(rv
))
126 *aLength
= finLength
;
128 nsMemory::Free(*_retval
);
134 /* ACString Finish(); */
136 nsScriptableUnicodeConverter::Finish(nsACString
& _retval
)
140 nsresult rv
= FinishWithLength(&str
, &len
);
141 if (NS_SUCCEEDED(rv
)) {
142 // No Adopt on nsACString :(
143 _retval
.Assign(str
, len
);
149 /* AString ConvertToUnicode (in ACString src); */
151 nsScriptableUnicodeConverter::ConvertToUnicode(const nsACString
& aSrc
, nsAString
& _retval
)
153 nsACString::const_iterator i
;
154 aSrc
.BeginReading(i
);
155 return ConvertFromByteArray(reinterpret_cast<const PRUint8
*>(i
.get()),
160 /* AString convertFromByteArray([const,array,size_is(aCount)] in octet aData,
161 in unsigned long aCount);
164 nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8
* aData
,
169 return NS_ERROR_FAILURE
;
172 PRInt32 inLength
= aCount
;
174 rv
= mDecoder
->GetMaxLength(reinterpret_cast<const char*>(aData
),
175 inLength
, &outLength
);
176 if (NS_SUCCEEDED(rv
))
178 PRUnichar
* buf
= (PRUnichar
*) nsMemory::Alloc((outLength
+1)*sizeof(PRUnichar
));
180 return NS_ERROR_OUT_OF_MEMORY
;
182 rv
= mDecoder
->Convert(reinterpret_cast<const char*>(aData
),
183 &inLength
, buf
, &outLength
);
184 if (NS_SUCCEEDED(rv
))
187 _retval
.Assign(buf
, outLength
);
192 return NS_ERROR_FAILURE
;
196 /* void convertToByteArray(in AString aString,
197 out unsigned long aLen,
198 [array, size_is(aLen),retval] out octet aData);
201 nsScriptableUnicodeConverter::ConvertToByteArray(const nsAString
& aString
,
207 nsresult rv
= ConvertFromUnicodeWithLength(aString
, &len
, &data
);
211 str
.Adopt(data
, len
); // NOTE: This uses the XPIDLCString as a byte array
213 rv
= FinishWithLength(&data
, &len
);
217 str
.Append(data
, len
);
218 nsMemory::Free(data
);
219 // NOTE: this being a byte array, it needs no null termination
220 *_aData
= reinterpret_cast<PRUint8
*>
221 (nsMemory::Clone(str
.get(), str
.Length()));
223 return NS_ERROR_OUT_OF_MEMORY
;
224 *aLen
= str
.Length();
228 /* nsIInputStream convertToInputStream(in AString aString); */
230 nsScriptableUnicodeConverter::ConvertToInputStream(const nsAString
& aString
,
231 nsIInputStream
** _retval
)
234 nsCOMPtr
<nsIStringInputStream
> inputStream
=
235 do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv
);
241 rv
= ConvertToByteArray(aString
, &dataLen
, &data
);
245 rv
= inputStream
->AdoptData(reinterpret_cast<char*>(data
), dataLen
);
247 nsMemory::Free(data
);
251 NS_ADDREF(*_retval
= inputStream
);
255 /* attribute string charset; */
257 nsScriptableUnicodeConverter::GetCharset(char * *aCharset
)
259 *aCharset
= ToNewCString(mCharset
);
261 return NS_ERROR_OUT_OF_MEMORY
;
267 nsScriptableUnicodeConverter::SetCharset(const char * aCharset
)
269 mCharset
.Assign(aCharset
);
270 return InitConverter();
274 nsScriptableUnicodeConverter::InitConverter()
279 nsCOMPtr
<nsICharsetConverterManager
> ccm
= do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID
, &rv
);
281 if (NS_SUCCEEDED( rv
) && (nsnull
!= ccm
)) {
282 // get charset atom due to getting unicode converter
284 // get an unicode converter
285 rv
= ccm
->GetUnicodeEncoder(mCharset
.get(), getter_AddRefs(mEncoder
));
286 if(NS_SUCCEEDED(rv
)) {
287 rv
= mEncoder
->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace
, nsnull
, (PRUnichar
)'?');
288 if(NS_SUCCEEDED(rv
)) {
289 rv
= ccm
->GetUnicodeDecoder(mCharset
.get(), getter_AddRefs(mDecoder
));