]> git.sesse.net Git - vlc/blob - src/text/wincp.c
- unicode.c, wincp.c: compile fix
[vlc] / src / text / wincp.c
1 /*****************************************************************************
2  * wincp.c: Guessing "local" ANSI code page on Microsoft Windows®
3  *****************************************************************************
4  *
5  * Copyright © 2006-2007 Rémi Denis-Courmont
6  * $Id$
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*** We need your help to complete this file!! Look for FIXME ***/
24
25 #include <vlc/vlc.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #ifndef WIN32
31 # include <locale.h>
32 #else
33 # include <windows.h>
34 #endif
35
36 #ifdef __APPLE__
37 #   include <errno.h>
38 #   include <string.h>
39 #endif
40
41 #include <vlc_charset.h>
42
43
44 #ifndef WIN32 /* should work on Win32, but useless */
45 static inline int locale_match (const char *tab, const char *locale)
46 {
47     for (;*tab; tab += 2)
48         if (memcmp (tab, locale, 2) == 0)
49             return 0;
50     return 1;
51 }
52
53
54 /**
55  * @return a fallback characters encoding to be used, given a locale.
56  */
57 static const char *FindFallbackEncoding (const char *locale)
58 {
59     if ((locale == NULL) || (strlen (locale) < 2)
60      || !strcasecmp (locale, "POSIX"))
61         return "ASCII";
62
63
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"
70         "eo" "mt" "cy";
71     if (!locale_match (western, locale))
72         return "CP1252"; // Compatible Microsoft superset
73
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
78
79     // Latin-3 Southern European languages (ISO-8859-3)
80     // "eo" and "mt" -> Latin-1 instead, I presume(?).
81     // "tr" -> ISO-8859-9 instead
82
83     // Latin-4 North-European languages (ISO-8859-4)
84     // -> Latin-1 instead
85
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(?)
90
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(?)
95
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
100
101     /* Hebrew (ISO-8859-8) */
102     if (!locale_match ("he" "iw" "yi", locale))
103         return "CP1255"; // Compatible Microsoft superset
104
105     /* Latin-5 Turkish (ISO-8859-9) */
106     if (!locale_match ("tr" "ku", locale))
107         return "CP1254"; // Compatible Microsoft superset
108
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. */
113
114     // ISO-8859-11 does arguably not exist. Thai is handled below.
115
116     // ISO-8859-12 really doesn't exist.
117
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
122
123     // Latin-8 Celtic languages (ISO-8859-14)
124     // "cy" -> use Latin-1 instead (most likely English or French)
125
126     // Latin-9 (ISO-8859-15) -> see Latin-1
127
128     // Latin-10 (ISO-8859-16) does not seem to be used
129
130     /*** KOI series ***/
131     // For Russian, we use CP1251
132     if (!locale_match ("uk", locale))
133         return "KOI8-U";
134
135     if (!locale_match ("tg", locale))
136         return "KOI8-T";
137
138     /*** Asia ***/
139     // Japanese
140     if (!locale_match ("jp", locale))
141         return "SHIFT-JIS"; // Shift-JIS is way more common than EUC-JP
142
143     // Korean
144     if (!locale_match ("ko", locale))
145         return "EUC-KR";
146
147     // Thai
148     if (!locale_match ("th", locale))
149         return "TIS-620";
150
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 */
155         return "CP1258";
156
157     /* Kazakh (FIXME: more infos needed) */
158     if (!locale_match ("kk", locale))
159         return "PT154";
160
161     // Chinese. The politically incompatible character sets.
162     if (!locale_match ("zh", locale))
163     {
164         if ((strlen (locale) >= 5) && (locale[2] != '_'))
165             locale += 3;
166
167         // Hong Kong
168         if (!locale_match ("HK", locale))
169             return "BIG5-HKSCS"; /* FIXME: use something else? */
170
171         // Taiwan island
172         if (!locale_match ("TW", locale))
173             return "BIG5";
174
175         // People's Republic of China and Singapore
176         /*
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?
181          */
182         return "GB18030";
183     }
184
185     return "ASCII";
186 }
187 #endif
188
189 /**
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
192  * guess.
193  */
194 const char *GetFallbackEncoding( void )
195 {
196 #ifndef WIN32
197     const char *psz_lang = NULL;
198
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);
203 #  endif
204     if (psz_lang == NULL)
205     {
206         psz_lang = getenv ("LC_ALL");
207         if ((psz_lang == NULL) || !*psz_lang)
208         {
209             psz_lang = getenv ("LC_CTYPE");
210             if ((psz_lang == NULL))
211                 psz_lang = getenv ("LANG");
212         }
213     }
214
215     return FindFallbackEncoding (psz_lang);
216 #else
217     static char buf[16] = "";
218     if (buf[0] == 0)
219         snprintf (buf, sizeof (buf), "CP%u", GetACP ());
220     return buf;
221 #endif
222 }