1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
28 * Alternatively, the contents of this file may be used under the terms of
29 * either of the GNU General Public License Version 2 or later (the "GPL"),
30 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
41 #include "nsIPlatformCharset.h"
42 #include "nsGREResProperties.h"
46 #include "nsUConvDll.h"
47 #include "nsIOS2Locale.h"
49 #include "nsReadableUtils.h"
50 #include "nsLocaleCID.h"
51 #include "nsIComponentManager.h"
52 #include "nsITimelineService.h"
53 #include "nsPlatformCharset.h"
55 static nsGREResProperties
*gInfo
= nsnull
;
56 static PRInt32 gCnt
= 0;
58 NS_IMPL_ISUPPORTS1(nsPlatformCharset
, nsIPlatformCharset
)
60 nsPlatformCharset::nsPlatformCharset()
62 NS_TIMELINE_START_TIMER("nsPlatformCharset()");
64 UINT acp
= ::WinQueryCp(HMQ_CURRENT
);
65 PRInt32 acpint
= (PRInt32
)(acp
& 0x00FFFF);
66 nsAutoString
acpKey(NS_LITERAL_STRING("os2."));
67 acpKey
.AppendInt(acpint
, 10);
68 nsresult res
= MapToCharset(acpKey
, mCharset
);
70 NS_TIMELINE_STOP_TIMER("nsPlatformCharset()");
71 NS_TIMELINE_MARK_TIMER("nsPlatformCharset()");
73 nsPlatformCharset::~nsPlatformCharset()
75 PR_AtomicDecrement(&gCnt
);
76 if ((0 == gCnt
) && (nsnull
!= gInfo
)) {
83 nsPlatformCharset::InitInfo()
85 PR_AtomicIncrement(&gCnt
); // count for gInfo
87 if (gInfo
== nsnull
) {
88 nsGREResProperties
*info
=
89 new nsGREResProperties(NS_LITERAL_CSTRING("os2charset.properties"));
91 NS_ASSERTION(info
, "cannot open properties file");
92 NS_ENSURE_TRUE(info
, NS_ERROR_FAILURE
);
99 nsPlatformCharset::MapToCharset(nsAString
& inANSICodePage
, nsACString
& outCharset
)
101 //delay loading os2charset.properties bundle if possible
102 if (inANSICodePage
.EqualsLiteral("os2.850")) {
103 outCharset
.AssignLiteral("IBM850");
107 if (inANSICodePage
.EqualsLiteral("os2.932")) {
108 outCharset
.AssignLiteral("Shift_JIS");
112 // ensure the .property file is loaded
113 nsresult rv
= InitInfo();
115 outCharset
.AssignLiteral("IBM850");
119 nsAutoString charset
;
120 rv
= gInfo
->Get(inANSICodePage
, charset
);
122 outCharset
.AssignLiteral("IBM850");
126 LossyCopyUTF16toASCII(charset
, outCharset
);
131 nsPlatformCharset::GetCharset(nsPlatformCharsetSel selector
,
134 if ((selector
== kPlatformCharsetSel_4xBookmarkFile
) || (selector
== kPlatformCharsetSel_4xPrefsJS
)) {
135 if ((mCharset
.Find("IBM850", IGNORE_CASE
) != -1) || (mCharset
.Find("IBM437", IGNORE_CASE
) != -1))
136 oResult
.AssignLiteral("ISO-8859-1");
137 else if (mCharset
.Find("IBM852", IGNORE_CASE
) != -1)
138 oResult
.AssignLiteral("windows-1250");
139 else if ((mCharset
.Find("IBM855", IGNORE_CASE
) != -1) || (mCharset
.Find("IBM866", IGNORE_CASE
) != -1))
140 oResult
.AssignLiteral("windows-1251");
141 else if ((mCharset
.Find("IBM869", IGNORE_CASE
) != -1) || (mCharset
.Find("IBM813", IGNORE_CASE
) != -1))
142 oResult
.AssignLiteral("windows-1253");
143 else if (mCharset
.Find("IBM857", IGNORE_CASE
) != -1)
144 oResult
.AssignLiteral("windows-1254");
154 nsPlatformCharset::GetDefaultCharsetForLocale(const nsAString
& localeName
, nsACString
&oResult
)
156 nsCOMPtr
<nsIOS2Locale
> os2Locale
;
161 // convert locale name to a code page
166 os2Locale
= do_CreateInstance(NS_OS2LOCALE_CONTRACTID
, &rv
);
167 if (NS_FAILED(rv
)) { return rv
; }
169 rv
= os2Locale
->GetPlatformLocale(localeName
, &codepage
);
170 if (NS_FAILED(rv
)) { return rv
; }
172 nsAutoString
os2_key(NS_LITERAL_STRING("os2."));
173 os2_key
.AppendInt((PRUint32
)codepage
);
175 return MapToCharset(os2_key
, oResult
);
180 nsPlatformCharset::Init()
186 nsPlatformCharset::MapToCharset(short script
, short region
, nsACString
& outCharset
)
192 nsPlatformCharset::InitGetCharset(nsACString
&oString
)
198 nsPlatformCharset::ConvertLocaleToCharsetUsingDeprecatedConfig(nsAString
& locale
, nsACString
& oResult
)
204 nsPlatformCharset::VerifyCharset(nsCString
&aCharset
)