]> git.sesse.net Git - vlc/blob - src/text/wincp.c
Remove always true tests
[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
27 #ifndef WIN32
28 # include <locale.h>
29 #else
30 # include <windows.h>
31 #endif
32
33 #ifdef __APPLE__
34 #   include <errno.h>
35 #   include <string.h>
36 #endif
37
38 #include <vlc_charset.h>
39
40
41 #ifndef WIN32 /* should work on Win32, but useless */
42 static inline int locale_match (const char *tab, const char *locale)
43 {
44     for (;*tab; tab += 2)
45         if (memcmp (tab, locale, 2) == 0)
46             return 0;
47     return 1;
48 }
49
50
51 /**
52  * @return a fallback characters encoding to be used, given a locale.
53  */
54 static const char *FindFallbackEncoding (const char *locale)
55 {
56     if ((locale == NULL) || (strlen (locale) < 2)
57      || !strcasecmp (locale, "POSIX"))
58         return "ASCII";
59
60
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"
67         "eo" "mt" "cy";
68     if (!locale_match (western, locale))
69         return "CP1252"; // Compatible Microsoft superset
70
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
75
76     // Latin-3 Southern European languages (ISO-8859-3)
77     // "eo" and "mt" -> Latin-1 instead, I presume(?).
78     // "tr" -> ISO-8859-9 instead
79
80     // Latin-4 North-European languages (ISO-8859-4)
81     // -> Latin-1 instead
82
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(?)
87
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(?)
92
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
97
98     /* Hebrew (ISO-8859-8) */
99     if (!locale_match ("he" "iw" "yi", locale))
100         return "CP1255"; // Compatible Microsoft superset
101
102     /* Latin-5 Turkish (ISO-8859-9) */
103     if (!locale_match ("tr" "ku", locale))
104         return "CP1254"; // Compatible Microsoft superset
105
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. */
110
111     // ISO-8859-11 does arguably not exist. Thai is handled below.
112
113     // ISO-8859-12 really doesn't exist.
114
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
119
120     // Latin-8 Celtic languages (ISO-8859-14)
121     // "cy" -> use Latin-1 instead (most likely English or French)
122
123     // Latin-9 (ISO-8859-15) -> see Latin-1
124
125     // Latin-10 (ISO-8859-16) does not seem to be used
126
127     /*** KOI series ***/
128     // For Russian, we use CP1251
129     if (!locale_match ("uk", locale))
130         return "KOI8-U";
131
132     if (!locale_match ("tg", locale))
133         return "KOI8-T";
134
135     /*** Asia ***/
136     // Japanese
137     if (!locale_match ("jp", locale))
138         return "SHIFT-JIS"; // Shift-JIS is way more common than EUC-JP
139
140     // Korean
141     if (!locale_match ("ko", locale))
142         return "EUC-KR";
143
144     // Thai
145     if (!locale_match ("th", locale))
146         return "TIS-620";
147
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 */
152         return "CP1258";
153
154     /* Kazakh (FIXME: more infos needed) */
155     if (!locale_match ("kk", locale))
156         return "PT154";
157
158     // Chinese. The politically incompatible character sets.
159     if (!locale_match ("zh", locale))
160     {
161         if ((strlen (locale) >= 5) && (locale[2] != '_'))
162             locale += 3;
163
164         // Hong Kong
165         if (!locale_match ("HK", locale))
166             return "BIG5-HKSCS"; /* FIXME: use something else? */
167
168         // Taiwan island
169         if (!locale_match ("TW", locale))
170             return "BIG5";
171
172         // People's Republic of China and Singapore
173         /*
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?
178          */
179         return "GB18030";
180     }
181
182     return "ASCII";
183 }
184 #endif
185
186 /**
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
189  * guess.
190  */
191 const char *GetFallbackEncoding( void )
192 {
193 #ifndef WIN32
194     const char *psz_lang = NULL;
195
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);
200 #  endif
201     if (psz_lang == NULL)
202     {
203         psz_lang = getenv ("LC_ALL");
204         if ((psz_lang == NULL) || !*psz_lang)
205         {
206             psz_lang = getenv ("LC_CTYPE");
207             if ((psz_lang == NULL))
208                 psz_lang = getenv ("LANG");
209         }
210     }
211
212     return FindFallbackEncoding (psz_lang);
213 #else
214     static char buf[16] = "";
215     if (buf[0] == 0)
216         snprintf (buf, sizeof (buf), "CP%u", GetACP ());
217     return buf;
218 #endif
219 }