1 /*****************************************************************************
2 * charset.c: Locale's character encoding stuff.
3 *****************************************************************************
4 * See also unicode.c for Unicode to locale conversion helpers.
6 * Copyright (C) 2003-2006 the VideoLAN team
9 * Authors: Derk-Jan Hartman <thedj at users.sf.net>
11 * RĂ©mi Denis-Courmont
13 * vlc_current_charset() an adaption of mp_locale_charset():
15 * Copyright (C) 2001-2003 The Mape Project
16 * Written by Karel Zak <zakkr@zf.jcu.cz>.
18 * which itself is an adaptation of locale_charset():
20 * Copyright (C) 2000-2002 Free Software Foundation, Inc.
21 * Written by Bruno Haible <bruno@clisp.org>.
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.
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.
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 *****************************************************************************/
45 # ifdef HAVE_LANGINFO_CODESET
46 # include <langinfo.h>
59 #include <vlc_charset.h>
61 typedef struct VLCCharsetAlias
63 char *psz_alias, *psz_name;
67 * The libcharset load all from external text file, but it's strange and
68 * slow solution, we rather use array(s) compiled into source. In the
69 * "good" libc this is not needful -- for example in linux.
71 * Please, put to this funtion exotic aliases only. The libc 'iconv' knows
72 * a lot of basic aliases (check it first by iconv -l).
75 #if (defined OS2 || !defined(HAVE_LANGINFO_CODESET)) && !defined WIN32
76 static const char* vlc_encoding_from_language( const char *l )
78 /* check for language (and perhaps country) codes */
79 if (strstr(l, "zh_TW")) return "Big5";
80 if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */
81 if (strstr(l, "zh")) return "GB2312";
82 if (strstr(l, "th")) return "TIS-620";
83 if (strstr(l, "ja")) return "EUC-JP";
84 if (strstr(l, "ko")) return "EUC-KR";
85 if (strstr(l, "ru")) return "KOI8-R";
86 if (strstr(l, "uk")) return "KOI8-U";
87 if (strstr(l, "pl") || strstr(l, "hr") ||
88 strstr(l, "hu") || strstr(l, "cs") ||
89 strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2";
90 if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3";
91 if (strstr(l, "lt") || strstr(l, "la")) return "ISO-8859-4";
92 if (strstr(l, "bg") || strstr(l, "be") ||
93 strstr(l, "mk") || strstr(l, "uk")) return "ISO-8859-5";
94 if (strstr(l, "ar")) return "ISO-8859-6";
95 if (strstr(l, "el")) return "ISO-8859-7";
96 if (strstr(l, "he") || strstr(l, "iw")) return "ISO-8859-8";
97 if (strstr(l, "tr")) return "ISO-8859-9";
98 if (strstr(l, "th")) return "ISO-8859-11";
99 if (strstr(l, "lv")) return "ISO-8859-13";
100 if (strstr(l, "cy")) return "ISO-8859-14";
101 if (strstr(l, "et")) return "ISO-8859-15"; /* all latin1 could be iso15 as well */
102 if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */
103 if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8";
104 /* We don't know. This ain't working go to default. */
109 static const char* vlc_charset_aliases( const char *psz_name )
114 VLCCharsetAlias aliases[] =
117 { "CP1361", "JOHAB" },
118 { "CP20127", "ASCII" },
119 { "CP20866", "KOI8-R" },
120 { "CP21866", "KOI8-RU" },
121 { "CP28591", "ISO-8859-1" },
122 { "CP28592", "ISO-8859-2" },
123 { "CP28593", "ISO-8859-3" },
124 { "CP28594", "ISO-8859-4" },
125 { "CP28595", "ISO-8859-5" },
126 { "CP28596", "ISO-8859-6" },
127 { "CP28597", "ISO-8859-7" },
128 { "CP28598", "ISO-8859-8" },
129 { "CP28599", "ISO-8859-9" },
130 { "CP28605", "ISO-8859-15" },
133 #elif defined (SYS_AIX)
134 VLCCharsetAlias aliases[] =
136 { "IBM-850", "CP850" },
137 { "IBM-856", "CP856" },
138 { "IBM-921", "ISO-8859-13" },
139 { "IBM-922", "CP922" },
140 { "IBM-932", "CP932" },
141 { "IBM-943", "CP943" },
142 { "IBM-1046", "CP1046" },
143 { "IBM-1124", "CP1124" },
144 { "IBM-1129", "CP1129" },
145 { "IBM-1252", "CP1252" },
146 { "IBM-EUCCN", "GB2312" },
147 { "IBM-EUCJP", "EUC-JP" },
148 { "IBM-EUCKR", "EUC-KR" },
149 { "IBM-EUCTW", "EUC-TW" },
152 #elif defined (SYS_HPUX)
153 VLCCharsetAlias aliases[] =
155 { "ROMAN8", "HP-ROMAN8" },
156 { "ARABIC8", "HP-ARABIC8" },
157 { "GREEK8", "HP-GREEK8" },
158 { "HEBREW8", "HP-HEBREW8" },
159 { "TURKISH8", "HP-TURKISH8" },
160 { "KANA8", "HP-KANA8" },
161 { "HP15CN", "GB2312" },
164 #elif defined (SYS_IRIX)
165 VLCCharsetAlias aliases[] =
167 { "EUCCN", "GB2312" },
170 #elif defined (SYS_OSF)
171 VLCCharsetAlias aliases[] =
173 { "KSC5601", "CP949" },
174 { "SDECKANJI", "EUC-JP" },
175 { "TACTIS", "TIS-620" },
178 #elif defined (SYS_SOLARIS)
179 VLCCharsetAlias aliases[] =
182 { "CNS11643", "EUC-TW" },
183 { "5601", "EUC-KR" },
184 { "JOHAP92", "JOHAB" },
185 { "PCK", "SHIFT_JIS" },
186 { "2533", "TIS-620" },
189 #elif defined (SYS_BSD)
190 VLCCharsetAlias aliases[] =
193 { "EUCCN", "GB2312" },
197 VLCCharsetAlias aliases[] = {{NULL, NULL}};
200 for (a = aliases; a->psz_alias; a++)
201 if (strcasecmp (a->psz_alias, psz_name) == 0)
204 /* we return original name beacuse iconv() probably will know
205 * something better about name if we don't know it :-) */
209 /* Returns charset from "language_COUNTRY.charset@modifier" string */
210 #if (defined OS2 || !defined(HAVE_LANGINFO_CODESET)) && !defined WIN32
211 static void vlc_encoding_from_locale( char *psz_locale, char *psz_charset )
213 char *psz_dot = strchr( psz_locale, '.' );
215 if( psz_dot != NULL )
217 const char *psz_modifier;
221 /* Look for the possible @... trailer and remove it, if any. */
222 psz_modifier = strchr( psz_dot, '@' );
224 if( psz_modifier == NULL )
226 strcpy( psz_charset, psz_dot );
229 if( 0 < ( psz_modifier - psz_dot )
230 && ( psz_modifier - psz_dot ) < 2 + 10 + 1 )
232 memcpy( psz_charset, psz_dot, psz_modifier - psz_dot );
233 psz_charset[ psz_modifier - psz_dot ] = '\0';
237 /* try language mapping */
238 strcpy( psz_charset, vlc_encoding_from_language( psz_locale ) );
242 bool vlc_current_charset( char **psz_charset )
244 const char *psz_codeset;
246 #if !(defined WIN32 || defined OS2 || defined __APPLE__)
248 # ifdef HAVE_LANGINFO_CODESET
249 /* Most systems support nl_langinfo( CODESET ) nowadays. */
250 psz_codeset = nl_langinfo( CODESET );
251 if( !strcmp( psz_codeset, "ANSI_X3.4-1968" ) )
252 psz_codeset = "ASCII";
254 /* On old systems which lack it, use setlocale or getenv. */
255 const char *psz_locale = NULL;
256 char buf[2 + 10 + 1];
258 /* But most old systems don't have a complete set of locales. Some
259 * (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
260 * use setlocale here; it would return "C" when it doesn't support the
261 * locale name the user has set. Darwin's setlocale is broken. */
262 # if defined (HAVE_SETLOCALE) && !defined (__APPLE__)
263 psz_locale = setlocale( LC_ALL, NULL );
265 if( psz_locale == NULL || psz_locale[0] == '\0' )
267 psz_locale = getenv( "LC_ALL" );
268 if( psz_locale == NULL || psz_locale[0] == '\0' )
270 psz_locale = getenv( "LC_CTYPE" );
271 if( psz_locale == NULL || psz_locale[0] == '\0')
272 psz_locale = getenv( "LANG" );
276 /* On some old systems, one used to set locale = "iso8859_1". On others,
277 * you set it to "language_COUNTRY.charset". Darwin only has LANG :( */
278 vlc_encoding_from_locale( (char *)psz_locale, buf );
280 # endif /* HAVE_LANGINFO_CODESET */
282 #elif defined __APPLE__
284 /* Darwin is always using UTF-8 internally. */
285 psz_codeset = "UTF-8";
289 char buf[2 + 10 + 1];
291 /* Woe32 has a function returning the locale's codepage as a number. */
292 snprintf( buf, sizeof( buf ), "CP%u", GetACP() );
297 const char *psz_locale;
298 char buf[2 + 10 + 1];
302 /* Allow user to override the codeset, as set in the operating system,
303 * with standard language environment variables. */
304 psz_locale = getenv( "LC_ALL" );
305 if( psz_locale == NULL || psz_locale[0] == '\0' )
307 psz+locale = getenv( "LC_CTYPE" );
308 if( psz_locale == NULL || locale[0] == '\0' )
309 locale = getenv( "LANG" );
311 if( psz_locale != NULL && psz_locale[0] != '\0' )
312 vlc_encoding_from_locale( psz_locale, buf );
316 /* OS/2 has a function returning the locale's codepage as a number. */
317 if( DosQueryCp( sizeof( cp ), cp, &cplen ) )
321 snprintf( buf, sizeof( buf ), "CP%u", cp[0] );
326 if( psz_codeset == NULL )
327 /* The canonical name cannot be determined. */
330 psz_codeset = vlc_charset_aliases( psz_codeset );
332 /* Don't return an empty string. GNU libc and GNU libiconv interpret
333 * the empty string as denoting "the locale's character encoding",
334 * thus GNU libiconv would call this function a second time. */
335 if( psz_codeset[0] == '\0' )
337 /* Last possibility is 'CHARSET' enviroment variable */
338 if( !( psz_codeset = getenv( "CHARSET" ) ) )
339 psz_codeset = "ISO-8859-1";
343 *psz_charset = strdup(psz_codeset);
345 if( !strcasecmp(psz_codeset, "UTF8") || !strcasecmp(psz_codeset, "UTF-8") )
352 char *vlc_fix_readdir( const char *psz_string )
355 vlc_iconv_t hd = vlc_iconv_open( "UTF-8", "UTF-8-MAC" );
357 if (hd != (vlc_iconv_t)(-1))
359 const char *psz_in = psz_string;
360 size_t i_in = strlen(psz_in);
361 size_t i_out = i_in * 2;
362 char *psz_utf8 = malloc(i_out + 1);
363 char *psz_out = psz_utf8;
365 size_t i_ret = vlc_iconv (hd, &psz_in, &i_in, &psz_out, &i_out);
366 vlc_iconv_close (hd);
367 if( i_ret == (size_t)(-1) || i_in )
370 return strdup( psz_string );
377 return strdup( psz_string );
382 * us_strtod() has the same prototype as ANSI C strtod() but it uses the
383 * POSIX/C decimal format, regardless of the current numeric locale.
385 double us_strtod( const char *str, char **end )
387 locale_t loc = newlocale (LC_NUMERIC_MASK, "C", NULL);
388 locale_t oldloc = uselocale (loc);
389 double res = strtod (str, end);
391 if (loc != (locale_t)0)
400 * us_atof() has the same prototype as ANSI C atof() but it expects a dot
401 * as decimal separator, regardless of the system locale.
403 double us_atof( const char *str )
405 return us_strtod( str, NULL );