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 ***/
38 #include <vlc_charset.h>
41 #ifndef WIN32 /* should work on Win32, but useless */
42 static inline int locale_match (const char *tab, const char *locale)
45 if (memcmp (tab, locale, 2) == 0)
52 * @return a fallback characters encoding to be used, given a locale.
54 static const char *FindFallbackEncoding (const char *locale)
56 if ((locale == NULL) || (strlen (locale) < 2)
57 || !strcasecmp (locale, "POSIX"))
61 /*** The ISO-8859 series (anything but Asia) ***/
62 // Latin-1 Western-European languages (ISO-8859-1)
63 static const char western[] =
64 "aa" "af" "an" "br" "ca" "da" "de" "en" "es" "et" "eu" "fi" "fo" "fr"
65 "ga" "gd" "gl" "gv" "id" "is" "it" "kl" "kw" "mg" "ms" "nb" "nl" "nn"
66 "no" "oc" "om" "pt" "so" "sq" "st" "sv" "tl" "uz" "wa" "xh" "zu"
68 if (!locale_match (western, locale))
69 return "CP1252"; // Compatible Microsoft superset
71 // Latin-2 Slavic languages (ISO-8859-2)
72 static const char slavic[] = "bs" "cs" "hr" "hu" "pl" "ro" "sk" "sl";
73 if (!locale_match (slavic, locale))
74 return "CP1250"; // CP1250 is more common, but incompatible
76 // Latin-3 Southern European languages (ISO-8859-3)
77 // "eo" and "mt" -> Latin-1 instead, I presume(?).
78 // "tr" -> ISO-8859-9 instead
80 // Latin-4 North-European languages (ISO-8859-4)
83 /* Cyrillic alphabet languages (ISO-8859-5) */
84 static const char cyrillic[] = "be" "bg" "mk" "ru" "sr";
85 if (!locale_match (cyrillic, locale))
86 return "CP1251"; // KOI8, ISO-8859-5 and CP1251 are incompatible(?)
88 /* Arabic (ISO-8859-6) */
89 if (!locale_match ("ar", locale))
90 // FIXME: someone check if we should return CP1256 or ISO-8859-6
91 return "CP1256"; // CP1256 is(?) more common, but incompatible(?)
93 /* Greek (ISO-8859-7) */
94 if (!locale_match ("el", locale))
95 // FIXME: someone check if we should return CP1253 or ISO-8859-7
96 return "CP1253"; // CP1253 is(?) more common and less incompatible
98 /* Hebrew (ISO-8859-8) */
99 if (!locale_match ("he" "iw" "yi", locale))
100 return "CP1255"; // Compatible Microsoft superset
102 /* Latin-5 Turkish (ISO-8859-9) */
103 if (!locale_match ("tr" "ku", locale))
104 return "CP1254"; // Compatible Microsoft superset
106 /* Latin-6 “North-European” languages (ISO-8859-10) */
107 /* It is so much north European that glibc only uses that for Luganda
108 * which is spoken in Uganda... unless someone complains, I'm not
109 * using this one; let's fallback to CP1252 here. */
111 // ISO-8859-11 does arguably not exist. Thai is handled below.
113 // ISO-8859-12 really doesn't exist.
115 // Latin-7 Baltic languages (ISO-8859-13)
116 if (!locale_match ("lt" "lv" "mi", locale))
117 // FIXME: mi = New Zealand, doesn't sound baltic!
118 return "CP1257"; // Compatible Microsoft superset
120 // Latin-8 Celtic languages (ISO-8859-14)
121 // "cy" -> use Latin-1 instead (most likely English or French)
123 // Latin-9 (ISO-8859-15) -> see Latin-1
125 // Latin-10 (ISO-8859-16) does not seem to be used
128 // For Russian, we use CP1251
129 if (!locale_match ("uk", locale))
132 if (!locale_match ("tg", locale))
137 if (!locale_match ("jp", locale))
138 return "SHIFT-JIS"; // Shift-JIS is way more common than EUC-JP
141 if (!locale_match ("ko", locale))
145 if (!locale_match ("th", locale))
148 // Vietnamese (FIXME: more infos needed)
149 if (!locale_match ("vt", locale))
150 /* VISCII is probably a bad idea as it is not extended ASCII */
151 /* glibc has TCVN5712-1 */
154 /* Kazakh (FIXME: more infos needed) */
155 if (!locale_match ("kk", locale))
158 // Chinese. The politically incompatible character sets.
159 if (!locale_match ("zh", locale))
161 if ((strlen (locale) >= 5) && (locale[2] != '_'))
165 if (!locale_match ("HK", locale))
166 return "BIG5-HKSCS"; /* FIXME: use something else? */
169 if (!locale_match ("TW", locale))
172 // People's Republic of China and Singapore
174 * GB18030 can represent any Unicode code point
175 * (like UTF-8), while remaining compatible with GBK
176 * FIXME: is it compatible with GB2312? if not, should we
177 * use GB2312 instead?
187 * GetFallbackEncoding() suggests an encoding to be used for non UTF-8
188 * text files accord to the system's local settings. It is only a best
191 const char *GetFallbackEncoding( void )
194 const char *psz_lang = NULL;
196 /* Some systems (like Darwin, SunOS 4 or DJGPP) have only the C locale.
197 * Therefore we don't use setlocale here; it would return "C". */
198 # if defined (HAVE_SETLOCALE) && !defined ( __APPLE__)
199 psz_lang = setlocale (LC_ALL, NULL);
201 if (psz_lang == NULL)
203 psz_lang = getenv ("LC_ALL");
204 if ((psz_lang == NULL) || !*psz_lang)
206 psz_lang = getenv ("LC_CTYPE");
207 if ((psz_lang == NULL))
208 psz_lang = getenv ("LANG");
212 return FindFallbackEncoding (psz_lang);
214 static char buf[16] = "";
216 snprintf (buf, sizeof (buf), "CP%u", GetACP ());