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