michael@0: michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsIPlatformCharset.h" michael@0: #include "nsUConvPropertySearch.h" michael@0: #include michael@0: #include "nsWin32Locale.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsPlatformCharset.h" michael@0: #include "nsEncoderDecoderUtils.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: static const char* kWinCharsets[][3] = { michael@0: #include "wincharset.properties.h" michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsPlatformCharset, nsIPlatformCharset) michael@0: michael@0: nsPlatformCharset::nsPlatformCharset() michael@0: { michael@0: nsAutoString acpKey(NS_LITERAL_STRING("acp.")); michael@0: acpKey.AppendInt(int32_t(::GetACP() & 0x00FFFF), 10); michael@0: MapToCharset(acpKey, mCharset); michael@0: } michael@0: michael@0: nsPlatformCharset::~nsPlatformCharset() michael@0: { michael@0: } michael@0: michael@0: nsresult michael@0: nsPlatformCharset::MapToCharset(nsAString& inANSICodePage, nsACString& outCharset) michael@0: { michael@0: nsAutoCString key; michael@0: LossyCopyUTF16toASCII(inANSICodePage, key); michael@0: michael@0: nsresult rv = nsUConvPropertySearch::SearchPropertyValue(kWinCharsets, michael@0: ArrayLength(kWinCharsets), key, outCharset); michael@0: if (NS_FAILED(rv)) { michael@0: outCharset.AssignLiteral("windows-1252"); michael@0: return NS_SUCCESS_USING_FALLBACK_LOCALE; michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPlatformCharset::GetCharset(nsPlatformCharsetSel selector, michael@0: nsACString& oResult) michael@0: { michael@0: oResult = mCharset; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPlatformCharset::GetDefaultCharsetForLocale(const nsAString& localeName, nsACString& oResult) michael@0: { michael@0: LCID localeAsLCID; michael@0: michael@0: // michael@0: // convert locale name to a code page (through the LCID) michael@0: // michael@0: nsresult rv; michael@0: oResult.Truncate(); michael@0: michael@0: rv = nsWin32Locale::GetPlatformLocale(localeName, &localeAsLCID); michael@0: if (NS_FAILED(rv)) { return rv; } michael@0: michael@0: wchar_t acp_name[6]; michael@0: if (GetLocaleInfoW(localeAsLCID, LOCALE_IDEFAULTANSICODEPAGE, acp_name, michael@0: ArrayLength(acp_name))==0) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: nsAutoString acp_key(NS_LITERAL_STRING("acp.")); michael@0: acp_key.Append(acp_name); michael@0: michael@0: return MapToCharset(acp_key, oResult); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsPlatformCharset::Init() michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsPlatformCharset::InitGetCharset(nsACString &oString) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsPlatformCharset::VerifyCharset(nsCString &aCharset) michael@0: { michael@0: return NS_OK; michael@0: }