Bug 454376 add -lCrun -lCstd for Solaris OS_LIBS, r=bsmedberg
[wine-gecko.git] / intl / uconv / src / nsCharsetConverterManager.cpp
blob5b2dc368abd9aed6ba860ad9f5936ae0a07e722c
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 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMPtr.h"
40 #include "nsString.h"
41 #include "nsReadableUtils.h"
42 #include "nsUnicharUtils.h"
43 #include "nsICharsetAlias.h"
44 #include "nsIServiceManager.h"
45 #include "nsICategoryManager.h"
46 #include "nsICharsetConverterManager.h"
47 #include "nsEncoderDecoderUtils.h"
48 #include "nsIStringBundle.h"
49 #include "nsILocaleService.h"
50 #include "nsUConvDll.h"
51 #include "prmem.h"
52 #include "nsCRT.h"
53 #include "nsVoidArray.h"
54 #include "nsStringEnumerator.h"
56 #include "nsXPCOM.h"
57 #include "nsISupportsPrimitives.h"
59 // just for CONTRACTIDs
60 #include "nsCharsetConverterManager.h"
62 #ifdef MOZ_USE_NATIVE_UCONV
63 #include "nsNativeUConvService.h"
64 #endif
66 // Pattern of cached, commonly used, single byte decoder
67 #define NS_1BYTE_CODER_PATTERN "ISO-8859"
68 #define NS_1BYTE_CODER_PATTERN_LEN 8
70 // Class nsCharsetConverterManager [implementation]
72 NS_IMPL_THREADSAFE_ISUPPORTS1(nsCharsetConverterManager,
73 nsICharsetConverterManager)
75 nsCharsetConverterManager::nsCharsetConverterManager()
76 :mDataBundle(NULL), mTitleBundle(NULL)
78 #ifdef MOZ_USE_NATIVE_UCONV
79 mNativeUC = do_GetService(NS_NATIVE_UCONV_SERVICE_CONTRACT_ID);
80 #endif
83 nsCharsetConverterManager::~nsCharsetConverterManager()
85 NS_IF_RELEASE(mDataBundle);
86 NS_IF_RELEASE(mTitleBundle);
89 nsresult nsCharsetConverterManager::Init()
91 if (!mDecoderHash.Init())
92 return NS_ERROR_OUT_OF_MEMORY;
93 return NS_OK;
96 nsresult nsCharsetConverterManager::RegisterConverterManagerData()
98 nsresult rv;
99 nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
100 if (NS_FAILED(rv))
101 return rv;
103 RegisterConverterCategory(catman, NS_TITLE_BUNDLE_CATEGORY,
104 "chrome://global/locale/charsetTitles.properties");
105 RegisterConverterCategory(catman, NS_DATA_BUNDLE_CATEGORY,
106 "resource://gre/res/charsetData.properties");
108 return NS_OK;
111 nsresult
112 nsCharsetConverterManager::RegisterConverterCategory(nsICategoryManager* catman,
113 const char* aCategory,
114 const char* aURL)
116 return catman->AddCategoryEntry(aCategory, aURL, "",
117 PR_TRUE, PR_TRUE, nsnull);
120 nsresult nsCharsetConverterManager::LoadExtensibleBundle(
121 const char* aCategory,
122 nsIStringBundle ** aResult)
124 nsresult rv = NS_OK;
126 nsCOMPtr<nsIStringBundleService> sbServ =
127 do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
128 if (NS_FAILED(rv))
129 return rv;
131 return sbServ->CreateExtensibleBundle(aCategory, aResult);
134 nsresult nsCharsetConverterManager::GetBundleValue(nsIStringBundle * aBundle,
135 const char * aName,
136 const nsAFlatString& aProp,
137 PRUnichar ** aResult)
139 nsAutoString key;
141 key.AssignWithConversion(aName);
142 ToLowerCase(key); // we lowercase the main comparison key
143 key.Append(aProp);
145 return aBundle->GetStringFromName(key.get(), aResult);
148 nsresult nsCharsetConverterManager::GetBundleValue(nsIStringBundle * aBundle,
149 const char * aName,
150 const nsAFlatString& aProp,
151 nsAString& aResult)
153 nsresult rv = NS_OK;
155 nsXPIDLString value;
156 rv = GetBundleValue(aBundle, aName, aProp, getter_Copies(value));
157 if (NS_FAILED(rv))
158 return rv;
160 aResult = value;
162 return NS_OK;
166 //----------------------------------------------------------------------------//----------------------------------------------------------------------------
167 // Interface nsICharsetConverterManager [implementation]
169 NS_IMETHODIMP
170 nsCharsetConverterManager::GetUnicodeEncoder(const char * aDest,
171 nsIUnicodeEncoder ** aResult)
173 // resolve the charset first
174 nsCAutoString charset;
176 // fully qualify to possibly avoid vtable call
177 nsCharsetConverterManager::GetCharsetAlias(aDest, charset);
179 return nsCharsetConverterManager::GetUnicodeEncoderRaw(charset.get(),
180 aResult);
184 NS_IMETHODIMP
185 nsCharsetConverterManager::GetUnicodeEncoderRaw(const char * aDest,
186 nsIUnicodeEncoder ** aResult)
188 *aResult= nsnull;
189 nsCOMPtr<nsIUnicodeEncoder> encoder;
191 #ifdef MOZ_USE_NATIVE_UCONV
192 if (mNativeUC) {
193 nsCOMPtr<nsISupports> supports;
194 mNativeUC->GetNativeConverter("UCS-2",
195 aDest,
196 getter_AddRefs(supports));
198 encoder = do_QueryInterface(supports);
200 if (encoder) {
201 NS_ADDREF(*aResult = encoder);
202 return NS_OK;
205 #endif
206 nsresult rv = NS_OK;
208 nsCAutoString
209 contractid(NS_LITERAL_CSTRING(NS_UNICODEENCODER_CONTRACTID_BASE) +
210 nsDependentCString(aDest));
212 // Always create an instance since encoders hold state.
213 encoder = do_CreateInstance(contractid.get(), &rv);
215 if (NS_FAILED(rv))
216 rv = NS_ERROR_UCONV_NOCONV;
217 else
219 *aResult = encoder.get();
220 NS_ADDREF(*aResult);
222 return rv;
225 NS_IMETHODIMP
226 nsCharsetConverterManager::GetUnicodeDecoder(const char * aSrc,
227 nsIUnicodeDecoder ** aResult)
229 // resolve the charset first
230 nsCAutoString charset;
232 // fully qualify to possibly avoid vtable call
233 nsCharsetConverterManager::GetCharsetAlias(aSrc, charset);
235 return nsCharsetConverterManager::GetUnicodeDecoderRaw(charset.get(),
236 aResult);
239 NS_IMETHODIMP
240 nsCharsetConverterManager::GetUnicodeDecoderRaw(const char * aSrc,
241 nsIUnicodeDecoder ** aResult)
243 *aResult= nsnull;
244 nsCOMPtr<nsIUnicodeDecoder> decoder;
246 #ifdef MOZ_USE_NATIVE_UCONV
247 if (mNativeUC) {
248 nsCOMPtr<nsISupports> supports;
249 mNativeUC->GetNativeConverter(aSrc,
250 "UCS-2",
251 getter_AddRefs(supports));
253 decoder = do_QueryInterface(supports);
255 if (decoder) {
256 NS_ADDREF(*aResult = decoder);
257 return NS_OK;
260 #endif
261 nsresult rv = NS_OK;
263 NS_NAMED_LITERAL_CSTRING(contractbase, NS_UNICODEDECODER_CONTRACTID_BASE);
264 nsDependentCString src(aSrc);
266 if (!strncmp(aSrc, NS_1BYTE_CODER_PATTERN, NS_1BYTE_CODER_PATTERN_LEN))
268 // Single byte decoders don't hold state. Optimize by using a service, and
269 // cache it in our hash to avoid repeated trips through the service manager.
270 if (!mDecoderHash.Get(aSrc, getter_AddRefs(decoder))) {
271 decoder = do_GetService(PromiseFlatCString(contractbase + src).get(),
272 &rv);
273 if (NS_SUCCEEDED(rv))
274 mDecoderHash.Put(aSrc, decoder);
277 else
279 decoder = do_CreateInstance(PromiseFlatCString(contractbase + src).get(),
280 &rv);
282 NS_ENSURE_SUCCESS(rv, NS_ERROR_UCONV_NOCONV);
284 decoder.forget(aResult);
285 return rv;
288 nsresult
289 nsCharsetConverterManager::GetList(const nsACString& aCategory,
290 const nsACString& aPrefix,
291 nsIUTF8StringEnumerator** aResult)
293 if (aResult == NULL)
294 return NS_ERROR_NULL_POINTER;
295 *aResult = NULL;
297 nsresult rv;
298 nsCAutoString alias;
300 nsCOMPtr<nsICategoryManager> catman = do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
301 if (NS_FAILED(rv))
302 return rv;
304 nsCStringArray* array = new nsCStringArray;
305 if (!array)
306 return NS_ERROR_OUT_OF_MEMORY;
308 nsCOMPtr<nsISimpleEnumerator> enumerator;
309 catman->EnumerateCategory(PromiseFlatCString(aCategory).get(),
310 getter_AddRefs(enumerator));
312 PRBool hasMore;
313 while (NS_SUCCEEDED(enumerator->HasMoreElements(&hasMore)) && hasMore) {
314 nsCOMPtr<nsISupports> supports;
315 if (NS_FAILED(enumerator->GetNext(getter_AddRefs(supports))))
316 continue;
318 nsCOMPtr<nsISupportsCString> supStr = do_QueryInterface(supports);
319 if (!supStr)
320 continue;
322 nsCAutoString fullName(aPrefix);
324 nsCAutoString name;
325 if (NS_FAILED(supStr->GetData(name)))
326 continue;
328 fullName += name;
329 rv = GetCharsetAlias(fullName.get(), alias);
330 if (NS_FAILED(rv))
331 continue;
333 rv = array->AppendCString(alias);
336 return NS_NewAdoptingUTF8StringEnumerator(aResult, array);
339 // we should change the interface so that we can just pass back a enumerator!
340 NS_IMETHODIMP
341 nsCharsetConverterManager::GetDecoderList(nsIUTF8StringEnumerator ** aResult)
343 return GetList(NS_LITERAL_CSTRING(NS_UNICODEDECODER_NAME),
344 EmptyCString(), aResult);
347 NS_IMETHODIMP
348 nsCharsetConverterManager::GetEncoderList(nsIUTF8StringEnumerator ** aResult)
350 return GetList(NS_LITERAL_CSTRING(NS_UNICODEENCODER_NAME),
351 EmptyCString(), aResult);
354 NS_IMETHODIMP
355 nsCharsetConverterManager::GetCharsetDetectorList(nsIUTF8StringEnumerator** aResult)
357 return GetList(NS_LITERAL_CSTRING("charset-detectors"),
358 NS_LITERAL_CSTRING("chardet."), aResult);
361 // XXX Improve the implementation of this method. Right now, it is build on
362 // top of the nsCharsetAlias service. We can make the nsCharsetAlias
363 // better, with its own hash table (not the StringBundle anymore) and
364 // a nicer file format.
365 NS_IMETHODIMP
366 nsCharsetConverterManager::GetCharsetAlias(const char * aCharset,
367 nsACString& aResult)
369 NS_PRECONDITION(aCharset, "null param");
370 if (!aCharset)
371 return NS_ERROR_NULL_POINTER;
373 // We try to obtain the preferred name for this charset from the charset
374 // aliases. If we don't get it from there, we just use the original string
375 nsDependentCString charset(aCharset);
376 nsCOMPtr<nsICharsetAlias> csAlias(do_GetService(NS_CHARSETALIAS_CONTRACTID));
377 NS_ASSERTION(csAlias, "failed to get the CharsetAlias service");
378 if (csAlias) {
379 nsAutoString pref;
380 nsresult rv = csAlias->GetPreferred(charset, aResult);
381 if (NS_SUCCEEDED(rv)) {
382 return (!aResult.IsEmpty()) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
386 aResult = charset;
387 return NS_OK;
391 NS_IMETHODIMP
392 nsCharsetConverterManager::GetCharsetTitle(const char * aCharset,
393 nsAString& aResult)
395 if (aCharset == NULL) return NS_ERROR_NULL_POINTER;
397 if (mTitleBundle == NULL) {
398 nsresult rv = LoadExtensibleBundle(NS_TITLE_BUNDLE_CATEGORY, &mTitleBundle);
399 if (NS_FAILED(rv))
400 return rv;
403 return GetBundleValue(mTitleBundle, aCharset, NS_LITERAL_STRING(".title"), aResult);
406 NS_IMETHODIMP
407 nsCharsetConverterManager::GetCharsetData(const char * aCharset,
408 const PRUnichar * aProp,
409 nsAString& aResult)
411 if (aCharset == NULL)
412 return NS_ERROR_NULL_POINTER;
413 // aProp can be NULL
415 if (mDataBundle == NULL) {
416 nsresult rv = LoadExtensibleBundle(NS_DATA_BUNDLE_CATEGORY, &mDataBundle);
417 if (NS_FAILED(rv))
418 return rv;
421 return GetBundleValue(mDataBundle, aCharset, nsDependentString(aProp), aResult);
424 NS_IMETHODIMP
425 nsCharsetConverterManager::GetCharsetLangGroup(const char * aCharset,
426 nsIAtom** aResult)
428 // resolve the charset first
429 nsCAutoString charset;
431 nsresult rv = GetCharsetAlias(aCharset, charset);
432 if (NS_FAILED(rv))
433 return rv;
435 // fully qualify to possibly avoid vtable call
436 return nsCharsetConverterManager::GetCharsetLangGroupRaw(charset.get(),
437 aResult);
440 NS_IMETHODIMP
441 nsCharsetConverterManager::GetCharsetLangGroupRaw(const char * aCharset,
442 nsIAtom** aResult)
445 *aResult = nsnull;
446 if (aCharset == NULL)
447 return NS_ERROR_NULL_POINTER;
449 nsresult rv = NS_OK;
451 if (mDataBundle == NULL) {
452 rv = LoadExtensibleBundle(NS_DATA_BUNDLE_CATEGORY, &mDataBundle);
453 if (NS_FAILED(rv))
454 return rv;
457 nsAutoString langGroup;
458 rv = GetBundleValue(mDataBundle, aCharset, NS_LITERAL_STRING(".LangGroup"), langGroup);
460 if (NS_SUCCEEDED(rv))
461 *aResult = NS_NewAtom(langGroup);
463 return rv;