No bug, remove left over printf debug statement from PluginInstanceChild. r=me, a...
[mozilla-central.git] / intl / uconv / src / nsScriptableUConv.cpp
blob622a362b51c7aad166c18b5a22586235b0574ab9
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
40 #include "pratom.h"
41 #include "nsString.h"
42 #include "nsReadableUtils.h"
43 #include "nsIServiceManager.h"
44 #include "nsICharsetConverterManager.h"
45 #include "nsIScriptableUConv.h"
46 #include "nsScriptableUConv.h"
47 #include "nsIStringStream.h"
48 #include "nsCRT.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);
68 nsresult
69 nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const nsAString& aSrc,
70 PRInt32* aOutLen,
71 char **_retval)
73 if (!mEncoder)
74 return NS_ERROR_FAILURE;
76 nsresult rv = NS_OK;
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);
82 if (!*_retval)
83 return NS_ERROR_OUT_OF_MEMORY;
85 rv = mEncoder->Convert(flatSrc.get(), &inLength, *_retval, aOutLen);
86 if (NS_SUCCEEDED(rv))
88 (*_retval)[*aOutLen] = '\0';
89 return NS_OK;
91 nsMemory::Free(*_retval);
93 *_retval = nsnull;
94 return NS_ERROR_FAILURE;
97 /* ACString ConvertFromUnicode (in AString src); */
98 NS_IMETHODIMP
99 nsScriptableUnicodeConverter::ConvertFromUnicode(const nsAString& aSrc,
100 nsACString& _retval)
102 PRInt32 len;
103 char* str;
104 nsresult rv = ConvertFromUnicodeWithLength(aSrc, &len, &str);
105 if (NS_SUCCEEDED(rv)) {
106 // No Adopt on nsACString :(
107 _retval.Assign(str, len);
108 nsMemory::Free(str);
110 return rv;
113 nsresult
114 nsScriptableUnicodeConverter::FinishWithLength(char **_retval, PRInt32* aLength)
116 if (!mEncoder)
117 return NS_ERROR_FAILURE;
119 PRInt32 finLength = 32;
121 *_retval = (char *) nsMemory::Alloc(finLength);
122 if (!*_retval)
123 return NS_ERROR_OUT_OF_MEMORY;
125 nsresult rv = mEncoder->Finish(*_retval, &finLength);
126 if (NS_SUCCEEDED(rv))
127 *aLength = finLength;
128 else
129 nsMemory::Free(*_retval);
131 return rv;
135 /* ACString Finish(); */
136 NS_IMETHODIMP
137 nsScriptableUnicodeConverter::Finish(nsACString& _retval)
139 PRInt32 len;
140 char* str;
141 nsresult rv = FinishWithLength(&str, &len);
142 if (NS_SUCCEEDED(rv)) {
143 // No Adopt on nsACString :(
144 _retval.Assign(str, len);
145 nsMemory::Free(str);
147 return rv;
150 /* AString ConvertToUnicode (in ACString src); */
151 NS_IMETHODIMP
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()),
157 aSrc.Length(),
158 _retval);
161 /* AString convertFromByteArray([const,array,size_is(aCount)] in octet aData,
162 in unsigned long aCount);
164 NS_IMETHODIMP
165 nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8* aData,
166 PRUint32 aCount,
167 nsAString& _retval)
169 if (!mDecoder)
170 return NS_ERROR_FAILURE;
172 nsresult rv = NS_OK;
173 PRInt32 inLength = aCount;
174 PRInt32 outLength;
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));
180 if (!buf)
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))
187 buf[outLength] = 0;
188 _retval.Assign(buf, outLength);
190 nsMemory::Free(buf);
191 return rv;
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);
201 NS_IMETHODIMP
202 nsScriptableUnicodeConverter::ConvertToByteArray(const nsAString& aString,
203 PRUint32* aLen,
204 PRUint8** _aData)
206 char* data;
207 PRInt32 len;
208 nsresult rv = ConvertFromUnicodeWithLength(aString, &len, &data);
209 if (NS_FAILED(rv))
210 return rv;
211 nsXPIDLCString str;
212 str.Adopt(data, len); // NOTE: This uses the XPIDLCString as a byte array
214 rv = FinishWithLength(&data, &len);
215 if (NS_FAILED(rv))
216 return rv;
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()));
223 if (!*_aData)
224 return NS_ERROR_OUT_OF_MEMORY;
225 *aLen = str.Length();
226 return NS_OK;
229 /* nsIInputStream convertToInputStream(in AString aString); */
230 NS_IMETHODIMP
231 nsScriptableUnicodeConverter::ConvertToInputStream(const nsAString& aString,
232 nsIInputStream** _retval)
234 nsresult rv;
235 nsCOMPtr<nsIStringInputStream> inputStream =
236 do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv);
237 if (NS_FAILED(rv))
238 return rv;
240 PRUint8* data;
241 PRUint32 dataLen;
242 rv = ConvertToByteArray(aString, &dataLen, &data);
243 if (NS_FAILED(rv))
244 return rv;
246 rv = inputStream->AdoptData(reinterpret_cast<char*>(data), dataLen);
247 if (NS_FAILED(rv)) {
248 nsMemory::Free(data);
249 return rv;
252 NS_ADDREF(*_retval = inputStream);
253 return rv;
256 /* attribute string charset; */
257 NS_IMETHODIMP
258 nsScriptableUnicodeConverter::GetCharset(char * *aCharset)
260 *aCharset = ToNewCString(mCharset);
261 if (!*aCharset)
262 return NS_ERROR_OUT_OF_MEMORY;
264 return NS_OK;
267 NS_IMETHODIMP
268 nsScriptableUnicodeConverter::SetCharset(const char * aCharset)
270 mCharset.Assign(aCharset);
271 return InitConverter();
274 nsresult
275 nsScriptableUnicodeConverter::InitConverter()
277 nsresult rv = NS_OK;
278 mEncoder = NULL ;
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));
295 return rv ;