Bug 454376 add -lCrun -lCstd for Solaris OS_LIBS, r=bsmedberg
[wine-gecko.git] / intl / uconv / src / nsScriptableUConv.cpp
blob30a0ecb9f801d3618b0d485f4648deac0f43733c
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"
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);
67 nsresult
68 nsScriptableUnicodeConverter::ConvertFromUnicodeWithLength(const nsAString& aSrc,
69 PRInt32* aOutLen,
70 char **_retval)
72 if (!mEncoder)
73 return NS_ERROR_FAILURE;
75 nsresult rv = NS_OK;
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);
81 if (!*_retval)
82 return NS_ERROR_OUT_OF_MEMORY;
84 rv = mEncoder->Convert(flatSrc.get(), &inLength, *_retval, aOutLen);
85 if (NS_SUCCEEDED(rv))
87 (*_retval)[*aOutLen] = '\0';
88 return NS_OK;
90 nsMemory::Free(*_retval);
92 *_retval = nsnull;
93 return NS_ERROR_FAILURE;
96 /* ACString ConvertFromUnicode (in AString src); */
97 NS_IMETHODIMP
98 nsScriptableUnicodeConverter::ConvertFromUnicode(const nsAString& aSrc,
99 nsACString& _retval)
101 PRInt32 len;
102 char* str;
103 nsresult rv = ConvertFromUnicodeWithLength(aSrc, &len, &str);
104 if (NS_SUCCEEDED(rv)) {
105 // No Adopt on nsACString :(
106 _retval.Assign(str, len);
107 nsMemory::Free(str);
109 return rv;
112 nsresult
113 nsScriptableUnicodeConverter::FinishWithLength(char **_retval, PRInt32* aLength)
115 if (!mEncoder)
116 return NS_ERROR_FAILURE;
118 PRInt32 finLength = 32;
120 *_retval = (char *) nsMemory::Alloc(finLength);
121 if (!*_retval)
122 return NS_ERROR_OUT_OF_MEMORY;
124 nsresult rv = mEncoder->Finish(*_retval, &finLength);
125 if (NS_SUCCEEDED(rv))
126 *aLength = finLength;
127 else
128 nsMemory::Free(*_retval);
130 return rv;
134 /* ACString Finish(); */
135 NS_IMETHODIMP
136 nsScriptableUnicodeConverter::Finish(nsACString& _retval)
138 PRInt32 len;
139 char* str;
140 nsresult rv = FinishWithLength(&str, &len);
141 if (NS_SUCCEEDED(rv)) {
142 // No Adopt on nsACString :(
143 _retval.Assign(str, len);
144 nsMemory::Free(str);
146 return rv;
149 /* AString ConvertToUnicode (in ACString src); */
150 NS_IMETHODIMP
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()),
156 aSrc.Length(),
157 _retval);
160 /* AString convertFromByteArray([const,array,size_is(aCount)] in octet aData,
161 in unsigned long aCount);
163 NS_IMETHODIMP
164 nsScriptableUnicodeConverter::ConvertFromByteArray(const PRUint8* aData,
165 PRUint32 aCount,
166 nsAString& _retval)
168 if (!mDecoder)
169 return NS_ERROR_FAILURE;
171 nsresult rv = NS_OK;
172 PRInt32 inLength = aCount;
173 PRInt32 outLength;
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));
179 if (!buf)
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))
186 buf[outLength] = 0;
187 _retval.Assign(buf, outLength);
189 nsMemory::Free(buf);
190 return rv;
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);
200 NS_IMETHODIMP
201 nsScriptableUnicodeConverter::ConvertToByteArray(const nsAString& aString,
202 PRUint32* aLen,
203 PRUint8** _aData)
205 char* data;
206 PRInt32 len;
207 nsresult rv = ConvertFromUnicodeWithLength(aString, &len, &data);
208 if (NS_FAILED(rv))
209 return rv;
210 nsXPIDLCString str;
211 str.Adopt(data, len); // NOTE: This uses the XPIDLCString as a byte array
213 rv = FinishWithLength(&data, &len);
214 if (NS_FAILED(rv))
215 return rv;
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()));
222 if (!*_aData)
223 return NS_ERROR_OUT_OF_MEMORY;
224 *aLen = str.Length();
225 return NS_OK;
228 /* nsIInputStream convertToInputStream(in AString aString); */
229 NS_IMETHODIMP
230 nsScriptableUnicodeConverter::ConvertToInputStream(const nsAString& aString,
231 nsIInputStream** _retval)
233 nsresult rv;
234 nsCOMPtr<nsIStringInputStream> inputStream =
235 do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv);
236 if (NS_FAILED(rv))
237 return rv;
239 PRUint8* data;
240 PRUint32 dataLen;
241 rv = ConvertToByteArray(aString, &dataLen, &data);
242 if (NS_FAILED(rv))
243 return rv;
245 rv = inputStream->AdoptData(reinterpret_cast<char*>(data), dataLen);
246 if (NS_FAILED(rv)) {
247 nsMemory::Free(data);
248 return rv;
251 NS_ADDREF(*_retval = inputStream);
252 return rv;
255 /* attribute string charset; */
256 NS_IMETHODIMP
257 nsScriptableUnicodeConverter::GetCharset(char * *aCharset)
259 *aCharset = ToNewCString(mCharset);
260 if (!*aCharset)
261 return NS_ERROR_OUT_OF_MEMORY;
263 return NS_OK;
266 NS_IMETHODIMP
267 nsScriptableUnicodeConverter::SetCharset(const char * aCharset)
269 mCharset.Assign(aCharset);
270 return InitConverter();
273 nsresult
274 nsScriptableUnicodeConverter::InitConverter()
276 nsresult rv = NS_OK;
277 mEncoder = NULL ;
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));
294 return rv ;