]> git.sesse.net Git - vlc/blob - src/text/charset.c
Remove always true tests
[vlc] / src / text / charset.c
1 /*****************************************************************************
2  * charset.c: Locale's character encoding stuff.
3  *****************************************************************************
4  * See also unicode.c for Unicode to locale conversion helpers.
5  *
6  * Copyright (C) 2003-2006 the VideoLAN team
7  * $Id$
8  *
9  * Authors: Derk-Jan Hartman <thedj at users.sf.net>
10  *          Christophe Massiot
11  *          RĂ©mi Denis-Courmont
12  *
13  * vlc_current_charset() an adaption of mp_locale_charset():
14  *
15  *  Copyright (C) 2001-2003 The Mape Project
16  *  Written by Karel Zak  <zakkr@zf.jcu.cz>.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
31  *****************************************************************************/
32
33 #include <vlc/vlc.h>
34
35 #if !defined WIN32
36 # if HAVE_LANGINFO_CODESET
37 #  include <langinfo.h>
38 # endif
39 # include <locale.h>
40 #else
41 # include <windows.h>
42 #endif
43
44 #ifdef __APPLE__
45 #   include <errno.h>
46 #   include <string.h>
47 #endif
48
49 #include <vlc_charset.h>
50
51 typedef struct VLCCharsetAlias
52 {
53     char *psz_alias, *psz_name;
54 } VLCCharsetAlias;
55
56 /*
57  * The libcharset load all from external text file, but it's strange and
58  * slow solution, we rather use array(s) compiled into source. In the
59  * "good" libc this is not needful -- for example in linux.
60  *
61  * Please, put to this funtion exotic aliases only. The libc 'iconv' knows
62  * a lot of basic aliases (check it first by iconv -l).
63  *
64  */
65 #if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
66 static const char* vlc_encoding_from_language( const char *l )
67 {
68     /* check for language (and perhaps country) codes */
69     if (strstr(l, "zh_TW")) return "Big5";
70     if (strstr(l, "zh_HK")) return "Big5HKSCS";   /* no MIME charset */
71     if (strstr(l, "zh")) return "GB2312";
72     if (strstr(l, "th")) return "TIS-620";
73     if (strstr(l, "ja")) return "EUC-JP";
74     if (strstr(l, "ko")) return "EUC-KR";
75     if (strstr(l, "ru")) return "KOI8-R";
76     if (strstr(l, "uk")) return "KOI8-U";
77     if (strstr(l, "pl") || strstr(l, "hr") ||
78         strstr(l, "hu") || strstr(l, "cs") ||
79         strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2";
80     if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3";
81     if (strstr(l, "lt") || strstr(l, "la")) return "ISO-8859-4";
82     if (strstr(l, "bg") || strstr(l, "be") ||
83         strstr(l, "mk") || strstr(l, "uk")) return "ISO-8859-5";
84     if (strstr(l, "ar")) return "ISO-8859-6";
85     if (strstr(l, "el")) return "ISO-8859-7";
86     if (strstr(l, "he") || strstr(l, "iw")) return "ISO-8859-8";
87     if (strstr(l, "tr")) return "ISO-8859-9";
88     if (strstr(l, "th")) return "ISO-8859-11";
89     if (strstr(l, "lv")) return "ISO-8859-13";
90     if (strstr(l, "cy")) return "ISO-8859-14";
91     if (strstr(l, "et")) return "ISO-8859-15"; /* all latin1 could be iso15 as well */
92     if (strstr(l, "ro")) return "ISO-8859-2";   /* or ISO-8859-16 */
93     if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8";
94     /* We don't know. This ain't working go to default. */
95     return "ISO-8859-1";
96 }
97 #endif
98
99 static const char* vlc_charset_aliases( const char *psz_name )
100 {
101     VLCCharsetAlias     *a;
102
103 #if defined WIN32
104     VLCCharsetAlias aliases[] =
105     {
106         { "CP936",      "GBK" },
107         { "CP1361",     "JOHAB" },
108         { "CP20127",    "ASCII" },
109         { "CP20866",    "KOI8-R" },
110         { "CP21866",    "KOI8-RU" },
111         { "CP28591",    "ISO-8859-1" },
112         { "CP28592",    "ISO-8859-2" },
113         { "CP28593",    "ISO-8859-3" },
114         { "CP28594",    "ISO-8859-4" },
115         { "CP28595",    "ISO-8859-5" },
116         { "CP28596",    "ISO-8859-6" },
117         { "CP28597",    "ISO-8859-7" },
118         { "CP28598",    "ISO-8859-8" },
119         { "CP28599",    "ISO-8859-9" },
120         { "CP28605",    "ISO-8859-15" },
121         { NULL,         NULL }
122     };
123 #elif defined (SYS_AIX)
124     VLCCharsetAlias aliases[] =
125     {
126         { "IBM-850",    "CP850" },
127         { "IBM-856",    "CP856" },
128         { "IBM-921",    "ISO-8859-13" },
129         { "IBM-922",    "CP922" },
130         { "IBM-932",    "CP932" },
131         { "IBM-943",    "CP943" },
132         { "IBM-1046",   "CP1046" },
133         { "IBM-1124",   "CP1124" },
134         { "IBM-1129",   "CP1129" },
135         { "IBM-1252",   "CP1252" },
136         { "IBM-EUCCN",  "GB2312" },
137         { "IBM-EUCJP",  "EUC-JP" },
138         { "IBM-EUCKR",  "EUC-KR" },
139         { "IBM-EUCTW",  "EUC-TW" },
140         { NULL, NULL }
141     };
142 #elif defined (SYS_HPUX)
143     VLCCharsetAlias aliases[] =
144     {
145         { "ROMAN8",     "HP-ROMAN8" },
146         { "ARABIC8",    "HP-ARABIC8" },
147         { "GREEK8",     "HP-GREEK8" },
148         { "HEBREW8",    "HP-HEBREW8" },
149         { "TURKISH8",   "HP-TURKISH8" },
150         { "KANA8",      "HP-KANA8" },
151         { "HP15CN",     "GB2312" },
152         { NULL, NULL }
153     };
154 #elif defined (SYS_IRIX)
155     VLCCharsetAlias aliases[] =
156     {
157         { "EUCCN",      "GB2312" },
158         { NULL, NULL }
159     };
160 #elif defined (SYS_OSF)
161     VLCCharsetAlias aliases[] =
162     {
163         { "KSC5601",    "CP949" },
164         { "SDECKANJI",  "EUC-JP" },
165         { "TACTIS",     "TIS-620" },
166         { NULL, NULL }
167     };
168 #elif defined (SYS_SOLARIS)
169     VLCCharsetAlias aliases[] =
170     {
171         { "646", "ASCII" },
172         { "CNS11643",   "EUC-TW" },
173         { "5601",       "EUC-KR" },
174         { "JOHAP92",    "JOHAB" },
175         { "PCK", "SHIFT_JIS" },
176         { "2533",       "TIS-620" },
177         { NULL, NULL }
178     };
179 #elif defined (SYS_BSD)
180     VLCCharsetAlias aliases[] =
181     {
182         { "646", " ASCII" },
183         { "EUCCN", "GB2312" },
184         { NULL, NULL }
185     };
186 #else
187     VLCCharsetAlias aliases[] = {{NULL, NULL}};
188 #endif
189
190     for (a = aliases; a->psz_alias; a++)
191         if (strcasecmp (a->psz_alias, psz_name) == 0)
192             return a->psz_name;
193
194     /* we return original name beacuse iconv() probably will know
195      * something better about name if we don't know it :-) */
196     return psz_name;
197 }
198
199 /* Returns charset from "language_COUNTRY.charset@modifier" string */
200 #if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
201 static void vlc_encoding_from_locale( char *psz_locale, char *psz_charset )
202 {
203     char *psz_dot = strchr( psz_locale, '.' );
204
205     if( psz_dot != NULL )
206     {
207         const char *psz_modifier;
208
209         psz_dot++;
210
211         /* Look for the possible @... trailer and remove it, if any.  */
212         psz_modifier = strchr( psz_dot, '@' );
213
214         if( psz_modifier == NULL )
215         {
216             strcpy( psz_charset, psz_dot );
217             return;
218         }
219         if( 0 < ( psz_modifier - psz_dot )
220              && ( psz_modifier - psz_dot ) < 2 + 10 + 1 )
221         {
222             memcpy( psz_charset, psz_dot, psz_modifier - psz_dot );
223             psz_charset[ psz_modifier - psz_dot ] = '\0';
224             return;
225         }
226     }
227     /* try language mapping */
228     strcpy( psz_charset, vlc_encoding_from_language( psz_locale ) );
229 }
230 #endif
231
232 vlc_bool_t vlc_current_charset( char **psz_charset )
233 {
234     const char *psz_codeset;
235
236 #if !(defined WIN32 || defined OS2 || defined __APPLE__)
237
238 # if HAVE_LANGINFO_CODESET
239     /* Most systems support nl_langinfo( CODESET ) nowadays.  */
240     psz_codeset = nl_langinfo( CODESET );
241     if( !strcmp( psz_codeset, "ANSI_X3.4-1968" ) )
242         psz_codeset = "ASCII";
243 # else
244     /* On old systems which lack it, use setlocale or getenv.  */
245     const char *psz_locale = NULL;
246     char buf[2 + 10 + 1];
247
248     /* But most old systems don't have a complete set of locales.  Some
249      * (like SunOS 4 or DJGPP) have only the C locale.  Therefore we don't
250      * use setlocale here; it would return "C" when it doesn't support the
251      * locale name the user has set. Darwin's setlocale is broken. */
252 #  if HAVE_SETLOCALE && !__APPLE__
253     psz_locale = setlocale( LC_ALL, NULL );
254 #  endif
255     if( psz_locale == NULL || psz_locale[0] == '\0' )
256     {
257         psz_locale = getenv( "LC_ALL" );
258         if( psz_locale == NULL || psz_locale[0] == '\0' )
259         {
260             psz_locale = getenv( "LC_CTYPE" );
261             if( psz_locale == NULL || psz_locale[0] == '\0')
262                 psz_locale = getenv( "LANG" );
263         }
264     }
265
266     /* On some old systems, one used to set locale = "iso8859_1". On others,
267      * you set it to "language_COUNTRY.charset". Darwin only has LANG :( */
268     vlc_encoding_from_locale( (char *)psz_locale, buf );
269     psz_codeset =  buf;
270 # endif /* HAVE_LANGINFO_CODESET */
271
272 #elif defined __APPLE__
273
274     /* Darwin is always using UTF-8 internally. */
275     psz_codeset = "UTF-8";
276
277 #elif defined WIN32
278
279     char buf[2 + 10 + 1];
280
281     /* Woe32 has a function returning the locale's codepage as a number.  */
282     snprintf( buf, sizeof( buf ), "CP%u", GetACP() );
283     psz_codeset = buf;
284
285 #elif defined OS2
286
287     const char *psz_locale;
288     char buf[2 + 10 + 1];
289     ULONG cp[3];
290     ULONG cplen;
291
292     /* Allow user to override the codeset, as set in the operating system,
293      * with standard language environment variables. */
294     psz_locale = getenv( "LC_ALL" );
295     if( psz_locale == NULL || psz_locale[0] == '\0' )
296     {
297         psz+locale = getenv( "LC_CTYPE" );
298         if( psz_locale == NULL || locale[0] == '\0' )
299             locale = getenv( "LANG" );
300     }
301     if( psz_locale != NULL && psz_locale[0] != '\0' )
302         vlc_encoding_from_locale( psz_locale, buf );
303         psz_codeset = buf;
304     else
305     {
306         /* OS/2 has a function returning the locale's codepage as a number. */
307         if( DosQueryCp( sizeof( cp ), cp, &cplen ) )
308             psz_codeset = "";
309         else
310         {
311             snprintf( buf, sizeof( buf ), "CP%u", cp[0] );
312             psz_codeset = buf;
313         }
314     }
315 #endif
316     if( psz_codeset == NULL )
317         /* The canonical name cannot be determined. */
318         psz_codeset = "";
319     else
320         psz_codeset = vlc_charset_aliases( psz_codeset );
321
322     /* Don't return an empty string.  GNU libc and GNU libiconv interpret
323      * the empty string as denoting "the locale's character encoding",
324      * thus GNU libiconv would call this function a second time. */
325     if( psz_codeset[0] == '\0' )
326     {
327         /* Last possibility is 'CHARSET' enviroment variable */
328         if( !( psz_codeset = getenv( "CHARSET" ) ) )
329             psz_codeset = "ISO-8859-1";
330     }
331
332     if( psz_charset )
333         *psz_charset = strdup(psz_codeset);
334
335     if( !strcasecmp(psz_codeset, "UTF8") || !strcasecmp(psz_codeset, "UTF-8") )
336         return VLC_TRUE;
337
338     return VLC_FALSE;
339 }
340
341
342 char *vlc_fix_readdir( const char *psz_string )
343 {
344 #ifdef __APPLE__
345     vlc_iconv_t hd = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
346
347     if (hd != (vlc_iconv_t)(-1))
348     {
349         const char *psz_in = psz_string;
350         size_t i_in = strlen(psz_in);
351         size_t i_out = i_in * 2;
352         char *psz_utf8 = malloc(i_out + 1);
353         char *psz_out = psz_utf8;
354
355         size_t i_ret = vlc_iconv (hd, &psz_in, &i_in, &psz_out, &i_out);
356         vlc_iconv_close (hd);
357         if( i_ret == (size_t)(-1) || i_in )
358         {
359             free( psz_utf8 );
360             return strdup( psz_string );
361         }
362
363         *psz_out = '\0';
364         return psz_utf8;
365     }
366 #endif
367     return strdup( psz_string );
368 }
369
370
371 /**
372  * There are two decimal separators in the computer world-wide locales:
373  * dot (which is the american default), and comma (which is used in France,
374  * the country with the most VLC developers, among others).
375  *
376  * i18n_strtod() has the same prototype as ANSI C strtod() but it accepts
377  * either decimal separator when deserializing the string to a float number,
378  * independant of the local computer setting.
379  */
380 double i18n_strtod( const char *str, char **end )
381 {
382     char *end_buf, e;
383     double d;
384
385     if( end == NULL )
386         end = &end_buf;
387     d = strtod( str, end );
388
389     e = **end;
390     if(( e == ',' ) || ( e == '.' ))
391     {
392         char dup[strlen( str ) + 1];
393         strcpy( dup, str );
394
395         if( dup == NULL )
396             return d;
397
398         dup[*end - str] = ( e == ',' ) ? '.' : ',';
399         d = strtod( dup, end );
400     }
401     return d;
402 }
403
404 /**
405  * i18n_atof() has the same prototype as ANSI C atof() but it accepts
406  * either decimal separator when deserializing the string to a float number,
407  * independant of the local computer setting.
408  */
409 double i18n_atof( const char *str )
410 {
411     return i18n_strtod( str, NULL );
412 }
413
414
415 /**
416  * us_strtod() has the same prototype as ANSI C strtod() but it expects
417  * a dot as decimal separator regardless of the system locale.
418  */
419 double us_strtod( const char *str, char **end )
420 {
421     char dup[strlen( str ) + 1], *ptr;
422     double d;
423     strcpy( dup, str );
424
425     ptr = strchr( dup, ',' );
426     if( ptr != NULL )
427         *ptr = '\0';
428
429     d = strtod( dup, &ptr );
430     if( end != NULL )
431         *end = (char *)&str[ptr - dup];
432
433     return d;
434 }
435
436 /**
437  * us_atof() has the same prototype as ANSI C atof() but it expects a dot
438  * as decimal separator, regardless of the system locale.
439  */
440 double us_atof( const char *str )
441 {
442     return us_strtod( str, NULL );
443 }
444