Ejemplo n.º 1
0
	TextEncoding TextEncoding::WebCharset(const UnicodeChar * name)
	{
		// This method of converting encoding names to code pages
		// supported on all Windows machines with IE 5.0 or later installed,
		// and on some Windows CE devices.  However, we can't depend on it,
		// so fall back to hard-coded names if using IMultiLanguage2 fails.
		try {
			Component<IMultiLanguage2> multiLanguage(CLSID_CMultiLanguage);
			Component<IEnumCodePage> enumCodePage;

			ThrowIfXMLError(multiLanguage->EnumCodePages(0, LOCALE_INVARIANT, enumCodePage.Adopt()));

			MIMECPINFO cpInfo;
			ULONG cpCount;
			while (enumCodePage->Next(1, &cpInfo, &cpCount) == S_OK) {
				if (::_wcsicmp(cpInfo.wszWebCharset, name) == 0) {
					return TextEncoding(cpInfo.uiCodePage);
				}
			}
		}
		catch (...) {
		}

		if (_wcsicmp(name, L"UTF-8") == 0) {
			return UTF8();
		}
		else if (_wcsicmp(name, L"UTF-16") == 0) {
			return UTF16();
		}
		else if (_wcsicmp(name, L"ISO-10646-UCS-2") == 0) {
			return UCS2();
		}
		else if (_wcsicmp(name, L"ISO-8859-1") == 0) {
			return ISOLatin1();
		}
		else if (_wcsicmp(name, L"Shift_JIS") == 0) {
			return ShiftJIS();
		}
		else if (_wcsicmp(name, L"Windows-1252") == 0) {
			return WindowsLatin1();
		}
		else {
			throw XMLException(0, "Unknown encoding");
		}
	}