Bug 468575 - Scrape some gunk off the config/ grout, r=ted
[wine-gecko.git] / intl / uconv / native / nsNativeUConvService.cpp
blob81481c065c9ed2506056139b1a3059750d188aaa
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
39 #ifdef MOZ_USE_NATIVE_UCONV
40 #include "nsString.h"
41 #include "nsIGenericFactory.h"
43 #include "nsINativeUConvService.h"
45 #include "nsIUnicodeDecoder.h"
46 #include "nsIUnicodeEncoder.h"
47 #include "nsICharRepresentable.h"
49 #include "nsNativeUConvService.h"
50 #include "nsAutoPtr.h"
52 #include <nl_types.h> // CODESET
53 #include <langinfo.h> // nl_langinfo
54 #include <iconv.h> // iconv_open, iconv, iconv_close
55 #include <errno.h>
58 class IConvAdaptor : public nsIUnicodeDecoder,
59 public nsIUnicodeEncoder,
60 public nsICharRepresentable
62 public:
63 IConvAdaptor();
64 virtual ~IConvAdaptor();
66 nsresult Init(const char* from, const char* to);
68 NS_DECL_ISUPPORTS
70 // Decoder methods:
72 NS_IMETHOD Convert(const char * aSrc,
73 PRInt32 * aSrcLength,
74 PRUnichar * aDest,
75 PRInt32 * aDestLength);
77 NS_IMETHOD GetMaxLength(const char * aSrc,
78 PRInt32 aSrcLength,
79 PRInt32 * aDestLength);
80 NS_IMETHOD Reset();
82 // Encoder methods:
84 NS_IMETHOD Convert(const PRUnichar * aSrc,
85 PRInt32 * aSrcLength,
86 char * aDest,
87 PRInt32 * aDestLength);
90 NS_IMETHOD Finish(char * aDest, PRInt32 * aDestLength);
92 NS_IMETHOD GetMaxLength(const PRUnichar * aSrc,
93 PRInt32 aSrcLength,
94 PRInt32 * aDestLength);
96 // defined by the Decoder: NS_IMETHOD Reset();
98 NS_IMETHOD SetOutputErrorBehavior(PRInt32 aBehavior,
99 nsIUnicharEncoder * aEncoder,
100 PRUnichar aChar);
102 NS_IMETHOD FillInfo(PRUint32* aInfo);
105 private:
106 nsresult ConvertInternal(void * aSrc,
107 PRInt32 * aSrcLength,
108 PRInt32 aSrcCharSize,
109 void * aDest,
110 PRInt32 * aDestLength,
111 PRInt32 aDestCharSize);
114 iconv_t mConverter;
115 PRBool mReplaceOnError;
116 PRUnichar mReplaceChar;
118 #ifdef DEBUG
119 nsCString mFrom, mTo;
120 #endif
123 NS_IMPL_ISUPPORTS3(IConvAdaptor,
124 nsIUnicodeEncoder,
125 nsIUnicodeDecoder,
126 nsICharRepresentable)
128 IConvAdaptor::IConvAdaptor()
130 mConverter = 0;
131 mReplaceOnError = PR_FALSE;
134 IConvAdaptor::~IConvAdaptor()
136 if (mConverter)
137 iconv_close(mConverter);
140 nsresult
141 IConvAdaptor::Init(const char* from, const char* to)
143 #ifdef DEBUG
144 mFrom = from;
145 mTo = to;
146 #endif
148 mConverter = iconv_open(to, from);
149 if (mConverter == (iconv_t) -1 )
151 #ifdef DEBUG
152 printf(" * IConvAdaptor - FAILED Initing: %s ==> %s\n", from, to);
153 #endif
154 mConverter = nsnull;
155 return NS_ERROR_FAILURE;
157 return NS_OK;
160 // From some charset to ucs2
161 nsresult
162 IConvAdaptor::Convert(const char * aSrc,
163 PRInt32 * aSrcLength,
164 PRUnichar * aDest,
165 PRInt32 * aDestLength)
167 return ConvertInternal( (void*) aSrc,
168 aSrcLength,
170 (void*) aDest,
171 aDestLength,
175 nsresult
176 IConvAdaptor::GetMaxLength(const char * aSrc,
177 PRInt32 aSrcLength,
178 PRInt32 * aDestLength)
180 if (!mConverter)
181 return NS_ERROR_UENC_NOMAPPING;
183 *aDestLength = aSrcLength*4; // sick
184 #ifdef DEBUG
185 printf(" * IConvAdaptor - - GetMaxLength %d ( %s -> %s )\n", *aDestLength, mFrom.get(), mTo.get());
186 #endif
187 return NS_OK;
191 nsresult
192 IConvAdaptor::Reset()
194 const char *zero_char_in_ptr = NULL;
195 char *zero_char_out_ptr = NULL;
196 size_t zero_size_in = 0,
197 zero_size_out = 0;
199 iconv(mConverter,
200 (char **)&zero_char_in_ptr,
201 &zero_size_in,
202 &zero_char_out_ptr,
203 &zero_size_out);
205 #ifdef DEBUG
206 printf(" * IConvAdaptor - - Reset\n");
207 #endif
208 return NS_OK;
212 // convert unicode data into some charset.
213 nsresult
214 IConvAdaptor::Convert(const PRUnichar * aSrc,
215 PRInt32 * aSrcLength,
216 char * aDest,
217 PRInt32 * aDestLength)
219 return ConvertInternal( (void*) aSrc,
220 aSrcLength,
222 (void*) aDest,
223 aDestLength,
228 nsresult
229 IConvAdaptor::Finish(char * aDest, PRInt32 * aDestLength)
231 *aDestLength = 0;
232 return NS_OK;
235 nsresult
236 IConvAdaptor::GetMaxLength(const PRUnichar * aSrc,
237 PRInt32 aSrcLength,
238 PRInt32 * aDestLength)
240 if (!mConverter)
241 return NS_ERROR_UENC_NOMAPPING;
243 *aDestLength = aSrcLength*4; // sick
245 return NS_OK;
249 nsresult
250 IConvAdaptor::SetOutputErrorBehavior(PRInt32 aBehavior,
251 nsIUnicharEncoder * aEncoder,
252 PRUnichar aChar)
254 if (aBehavior == nsIUnicodeEncoder::kOnError_Signal) {
255 mReplaceOnError = PR_FALSE;
256 return NS_OK;
258 else if (aBehavior == nsIUnicodeEncoder::kOnError_Replace) {
259 mReplaceOnError = PR_TRUE;
260 mReplaceChar = aChar;
261 return NS_OK;
264 NS_WARNING("Uconv Error Behavior not support");
265 return NS_ERROR_FAILURE;
268 nsresult
269 IConvAdaptor::FillInfo(PRUint32* aInfo)
271 #ifdef DEBUG
272 printf(" * IConvAdaptor - FillInfo called\n");
273 #endif
274 *aInfo = 0;
275 return NS_OK;
279 nsresult
280 IConvAdaptor::ConvertInternal(void * aSrc,
281 PRInt32 * aSrcLength,
282 PRInt32 aSrcCharSize,
283 void * aDest,
284 PRInt32 * aDestLength,
285 PRInt32 aDestCharSize)
287 if (!mConverter) {
288 NS_WARNING("Converter Not Initialize");
289 return NS_ERROR_NOT_INITIALIZED;
291 size_t res = 0;
292 size_t inLeft = (size_t) *aSrcLength * aSrcCharSize;
293 size_t outLeft = (size_t) *aDestLength * aDestCharSize;
294 size_t outputAvail = outLeft;
296 while (true){
298 res = iconv(mConverter,
299 (char**)&aSrc,
300 &inLeft,
301 (char**)&aDest,
302 &outLeft);
304 if (res == (size_t) -1) {
305 // on some platforms (e.g., linux) iconv will fail with
306 // E2BIG if it cannot convert _all_ of its input. it'll
307 // still adjust all of the in/out params correctly, so we
308 // can ignore this error. the assumption is that we will
309 // be called again to complete the conversion.
310 if ((errno == E2BIG) && (outLeft < outputAvail)) {
311 res = 0;
312 break;
315 if (errno == EILSEQ) {
317 if (mReplaceOnError) {
318 if (aDestCharSize == 1) {
319 (*(char*)aDest) = (char)mReplaceChar;
320 aDest = (char*)aDest + sizeof(char);
322 else
324 (*(PRUnichar*)aDest) = (PRUnichar)mReplaceChar;
325 aDest = (PRUnichar*)aDest + sizeof(PRUnichar);
328 inLeft -= aSrcCharSize;
329 outLeft -= aDestCharSize;
331 #ifdef DEBUG
332 printf(" * IConvAdaptor - replacing char in output ( %s -> %s )\n",
333 mFrom.get(), mTo.get());
335 #endif
336 res = 0;
340 if (res == -1) {
341 #ifdef DEBUG
342 printf(" * IConvAdaptor - Bad input ( %s -> %s )\n", mFrom.get(), mTo.get());
343 #endif
344 return NS_ERROR_UENC_NOMAPPING;
348 if (inLeft <= 0 || outLeft <= 0 || res == -1)
349 break;
353 if (res != (size_t) -1) {
355 // xp_iconv deals with how much is remaining in a given buffer
356 // but what uconv wants how much we read/written already. So
357 // we fix it up here.
358 *aSrcLength -= (inLeft / aSrcCharSize);
359 *aDestLength -= (outLeft / aDestCharSize);
360 return NS_OK;
363 #ifdef DEBUG
364 printf(" * IConvAdaptor - - xp_iconv error( %s -> %s )\n", mFrom.get(), mTo.get());
365 #endif
366 Reset();
367 return NS_ERROR_UENC_NOMAPPING;
370 NS_IMPL_ISUPPORTS1(NativeUConvService, nsINativeUConvService)
372 NS_IMETHODIMP
373 NativeUConvService::GetNativeConverter(const char* from,
374 const char* to,
375 nsISupports** aResult)
377 *aResult = nsnull;
379 //nsRefPtr<IConvAdaptor> ucl = new IConvAdaptor();
380 IConvAdaptor *adaptor=new IConvAdaptor();
381 nsCOMPtr<nsISupports> ucl(static_cast<nsIUnicodeDecoder*>(adaptor));
382 if (!ucl)
383 return NS_ERROR_OUT_OF_MEMORY;
385 //nsresult rv = ucl->Init(from, to);
386 nsresult rv=adaptor->Init(from,to);
387 if (NS_SUCCEEDED(rv))
388 NS_ADDREF(*aResult = ucl);
390 return rv;
392 #endif