1 /*****************************************************************************
2 * wincp.c: Guessing "local" ANSI code page on Microsoft Windows®
3 *****************************************************************************
5 * Copyright © 2006-2007 Rémi Denis-Courmont
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*** We need your help to complete this file!! Look for FIXME ***/
41 #include <vlc_charset.h>
44 #ifndef WIN32 /* should work on Win32, but useless */
45 static inline int locale_match (const char *tab, const char *locale)
48 if (memcmp (tab, locale, 2) == 0)
55 * @return a fallback characters encoding to be used, given a locale.
57 static const char *FindFallbackEncoding (const char *locale)
59 if ((locale == NULL) || (strlen (locale) < 2)
60 || !strcasecmp (locale, "POSIX"))
64 /*** The ISO-8859 series (anything but Asia) ***/
65 // Latin-1 Western-European languages (ISO-8859-1)
66 static const char western[] =
67 "aa" "af" "an" "br" "ca" "da" "de" "en" "es" "et" "eu" "fi" "fo" "fr"
68 "ga" "gd" "gl" "gv" "id" "is" "it" "kl" "kw" "mg" "ms" "nb" "nl" "nn"
69 "no" "oc" "om" "pt" "so" "sq" "st" "sv" "tl" "uz" "wa" "xh" "zu"
71 if (!locale_match (western, locale))
72 return "CP1252"; // Compatible Microsoft superset
74 // Latin-2 Slavic languages (ISO-8859-2)
75 static const char slavic[] = "bs" "cs" "hr" "hu" "pl" "ro" "sk" "sl";
76 if (!locale_match (slavic, locale))
77 return "CP1250"; // CP1250 is more common, but incompatible
79 // Latin-3 Southern European languages (ISO-8859-3)
80 // "eo" and "mt" -> Latin-1 instead, I presume(?).
81 // "tr" -> ISO-8859-9 instead
83 // Latin-4 North-European languages (ISO-8859-4)
86 /* Cyrillic alphabet languages (ISO-8859-5) */
87 static const char cyrillic[] = "be" "bg" "mk" "ru" "sr";
88 if (!locale_match (cyrillic, locale))
89 return "CP1251"; // KOI8, ISO-8859-5 and CP1251 are incompatible(?)
91 /* Arabic (ISO-8859-6) */
92 if (!locale_match ("ar", locale))
93 // FIXME: someone check if we should return CP1256 or ISO-8859-6
94 return "CP1256"; // CP1256 is(?) more common, but incompatible(?)
96 /* Greek (ISO-8859-7) */
97 if (!locale_match ("el", locale))
98 // FIXME: someone check if we should return CP1253 or ISO-8859-7
99 return "CP1253"; // CP1253 is(?) more common and less incompatible
101 /* Hebrew (ISO-8859-8) */
102 if (!locale_match ("he" "iw" "yi", locale))
103 return "CP1255"; // Compatible Microsoft superset
105 /* Latin-5 Turkish (ISO-8859-9) */
106 if (!locale_match ("tr" "ku", locale))
107 return "CP1254"; // Compatible Microsoft superset
109 /* Latin-6 “North-European” languages (ISO-8859-10) */
110 /* It is so much north European that glibc only uses that for Luganda
111 * which is spoken in Uganda... unless someone complains, I'm not
112 * using this one; let's fallback to CP1252 here. */
114 // ISO-8859-11 does arguably not exist. Thai is handled below.
116 // ISO-8859-12 really doesn't exist.
118 // Latin-7 Baltic languages (ISO-8859-13)
119 if (!locale_match ("lt" "lv" "mi", locale))
120 // FIXME: mi = New Zealand, doesn't sound baltic!
121 return "CP1257"; // Compatible Microsoft superset
123 // Latin-8 Celtic languages (ISO-8859-14)
124 // "cy" -> use Latin-1 instead (most likely English or French)
126 // Latin-9 (ISO-8859-15) -> see Latin-1
128 // Latin-10 (ISO-8859-16) does not seem to be used
131 // For Russian, we use CP1251
132 if (!locale_match ("uk", locale))
135 if (!locale_match ("tg", locale))
140 if (!locale_match ("jp", locale))
141 return "SHIFT-JIS"; // Shift-JIS is way more common than EUC-JP
144 if (!locale_match ("ko", locale))
148 if (!locale_match ("th", locale))
151 // Vietnamese (FIXME: more infos needed)
152 if (!locale_match ("vt", locale))
153 /* VISCII is probably a bad idea as it is not extended ASCII */
154 /* glibc has TCVN5712-1 */
157 /* Kazakh (FIXME: more infos needed) */
158 if (!locale_match ("kk", locale))
161 // Chinese. The politically incompatible character sets.
162 if (!locale_match ("zh", locale))
164 if ((strlen (locale) >= 5) && (locale[2] != '_'))
168 if (!locale_match ("HK", locale))
169 return "BIG5-HKSCS"; /* FIXME: use something else? */
172 if (!locale_match ("TW", locale))
175 // People's Republic of China and Singapore
177 * GB18030 can represent any Unicode code point
178 * (like UTF-8), while remaining compatible with GBK
179 * FIXME: is it compatible with GB2312? if not, should we
180 * use GB2312 instead?
190 * GetFallbackEncoding() suggests an encoding to be used for non UTF-8
191 * text files accord to the system's local settings. It is only a best
194 const char *GetFallbackEncoding( void )
197 const char *psz_lang = NULL;
199 /* Some systems (like Darwin, SunOS 4 or DJGPP) have only the C locale.
200 * Therefore we don't use setlocale here; it would return "C". */
201 # if defined (HAVE_SETLOCALE) && !defined ( __APPLE__)
202 psz_lang = setlocale (LC_ALL, NULL);
204 if (psz_lang == NULL)
206 psz_lang = getenv ("LC_ALL");
207 if ((psz_lang == NULL) || !*psz_lang)
209 psz_lang = getenv ("LC_CTYPE");
210 if ((psz_lang == NULL))
211 psz_lang = getenv ("LANG");
215 return FindFallbackEncoding (psz_lang);
217 static char buf[16] = "";
219 snprintf (buf, sizeof (buf), "CP%u", GetACP ());