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