Bug 454376 add -lCrun -lCstd for Solaris OS_LIBS, r=bsmedberg
[wine-gecko.git] / intl / uconv / src / nsCharsetAliasImp.cpp
blob2c6300c62d0891ea5acdba022ddfdbd6ee832486
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):
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 #include "nsICharsetAlias.h"
40 #include "pratom.h"
42 // for NS_IMPL_IDS only
43 #include "nsIPlatformCharset.h"
45 #include "nsUConvDll.h"
46 #include "nsReadableUtils.h"
47 #include "nsUnicharUtils.h"
48 #include "nsGREResProperties.h"
49 #include "nsITimelineService.h"
50 #include "nsCharsetAlias.h"
52 //--------------------------------------------------------------
53 NS_IMPL_ISUPPORTS1(nsCharsetAlias2, nsICharsetAlias)
55 //--------------------------------------------------------------
56 nsCharsetAlias2::nsCharsetAlias2()
58 mDelegate = nsnull; // delay the load of mDelegate untill we need it.
60 //--------------------------------------------------------------
61 nsCharsetAlias2::~nsCharsetAlias2()
63 if(mDelegate)
64 delete mDelegate;
67 //
68 static const char* kAliases[][3] = {
69 // Triple with { lower-case test string, out string, length of out string }
70 { "iso-8859-1", "ISO-8859-1", (const char*)NS_INT32_TO_PTR(10) },
71 { "utf-8", "UTF-8", (const char*)NS_INT32_TO_PTR(5) },
72 { "x-sjis", "Shift_JIS", (const char*)NS_INT32_TO_PTR(9) },
73 { "shift_jis", "Shift_JIS", (const char*)NS_INT32_TO_PTR(9) }
76 //--------------------------------------------------------------
77 NS_IMETHODIMP nsCharsetAlias2::GetPreferred(const nsACString& aAlias,
78 nsACString& oResult)
80 if (aAlias.IsEmpty()) return NS_ERROR_NULL_POINTER;
81 NS_TIMELINE_START_TIMER("nsCharsetAlias2:GetPreferred");
84 // Delay loading charsetalias.properties by hardcoding the most
85 // frequent aliases. Note that it's possible to recur in to this
86 // function *while loading* charsetalias.properties (see bug 190951),
87 // so we might have an |mDelegate| already that isn't valid yet, but
88 // the load is guaranteed to be "UTF-8" so things will be OK.
89 for (PRUint32 index = 0; index < NS_ARRAY_LENGTH(kAliases); index++) {
90 if (aAlias.LowerCaseEqualsASCII(kAliases[index][0])) {
91 oResult.Assign(nsDependentCString(kAliases[index][1],
92 NS_PTR_TO_UINT32(kAliases[index][2])));
93 NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
94 return NS_OK;
98 oResult.Truncate();
100 if(!mDelegate) {
101 //load charsetalias.properties string bundle with all remaining aliases
102 // we may need to protect the following section with a lock so we won't call the
103 // 'new nsGREResProperties' from two different threads
104 mDelegate = new nsGREResProperties( NS_LITERAL_CSTRING("charsetalias.properties") );
105 NS_ASSERTION(mDelegate, "cannot create nsGREResProperties");
106 if(nsnull == mDelegate)
107 return NS_ERROR_OUT_OF_MEMORY;
110 NS_TIMELINE_STOP_TIMER("nsCharsetAlias2:GetPreferred");
111 NS_TIMELINE_MARK_TIMER("nsCharsetAlias2:GetPreferred");
113 nsCAutoString key(aAlias);
114 ToLowerCase(key);
116 // hack for now, have to fix nsGREResProperties, but we can't until
117 // string bundles use UTF8 keys
118 nsAutoString result;
119 nsresult rv = mDelegate->Get(NS_ConvertASCIItoUTF16(key), result);
120 LossyAppendUTF16toASCII(result, oResult);
121 return rv;
124 //--------------------------------------------------------------
125 NS_IMETHODIMP
126 nsCharsetAlias2::Equals(const nsACString& aCharset1,
127 const nsACString& aCharset2, PRBool* oResult)
129 nsresult res = NS_OK;
131 if(aCharset1.Equals(aCharset2, nsCaseInsensitiveCStringComparator())) {
132 *oResult = PR_TRUE;
133 return res;
136 if(aCharset1.IsEmpty() || aCharset2.IsEmpty()) {
137 *oResult = PR_FALSE;
138 return res;
141 *oResult = PR_FALSE;
142 nsCAutoString name1;
143 nsCAutoString name2;
144 res = this->GetPreferred(aCharset1, name1);
145 if(NS_SUCCEEDED(res)) {
146 res = this->GetPreferred(aCharset2, name2);
147 if(NS_SUCCEEDED(res)) {
148 *oResult = name1.Equals(name2, nsCaseInsensitiveCStringComparator());
152 return res;