1 /*****************************************************************************
2 * charset.c: Determine a canonical name for the current locale's character
4 *****************************************************************************
5 * Copyright (C) 2003-2004 VideoLAN
6 * $Id: charset.c,v 1.6 2004/01/06 12:02:06 zorglub Exp $
8 * Author: Derk-Jan Hartman <thedj at users.sf.net>
10 * vlc_current_charset() an adaption of mp_locale_charset():
12 * Copyright (C) 2001-2003 The Mape Project
13 * Written by Karel Zak <zakkr@zf.jcu.cz>.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
28 *****************************************************************************/
35 # if HAVE_LANGINFO_CODESET
36 # include <langinfo.h>
48 typedef struct VLCCharsetAlias
50 char *psz_alias, *psz_name;
54 * The libcharset load all from external text file, but it's strange and
55 * slow solution, we rather use array(s) compiled into source. In the
56 * "good" libc this is not needful -- for example in linux.
58 * Please, put to this funtion exotic aliases only. The libc 'iconv' knows
59 * a lot of basic aliases (check it first by iconv -l).
63 static const char* vlc_encoding_from_language( const char *l )
65 /* check for language (and perhaps country) codes */
66 if (strstr(l, "zh_TW")) return "Big5";
67 if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */
68 if (strstr(l, "zh")) return "GB2312";
69 if (strstr(l, "th")) return "TIS-620";
70 if (strstr(l, "ja")) return "EUC-JP";
71 if (strstr(l, "ko")) return "EUC-KR";
72 if (strstr(l, "ru")) return "KOI8-R";
73 if (strstr(l, "uk")) return "KOI8-U";
74 if (strstr(l, "pl") || strstr(l, "hr") ||
75 strstr(l, "hu") || strstr(l, "cs") ||
76 strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2";
77 if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3";
78 if (strstr(l, "lt") || strstr(l, "la")) return "ISO-8859-4";
79 if (strstr(l, "bg") || strstr(l, "be") ||
80 strstr(l, "mk") || strstr(l, "uk")) return "ISO-8859-5";
81 if (strstr(l, "ar")) return "ISO-8859-6";
82 if (strstr(l, "el")) return "ISO-8859-7";
83 if (strstr(l, "he") || strstr(l, "iw")) return "ISO-8859-8";
84 if (strstr(l, "tr")) return "ISO-8859-9";
85 if (strstr(l, "th")) return "ISO-8859-11";
86 if (strstr(l, "lv")) return "ISO-8859-13";
87 if (strstr(l, "cy")) return "ISO-8859-14";
88 if (strstr(l, "et")) return "ISO-8859-15"; /* all latin1 could be iso15 as well */
89 if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */
90 if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8";
91 /* We don't know. This ain't working go to default. */
96 static const char* vlc_charset_aliases( const char *psz_name )
101 VLCCharsetAlias aliases[] =
104 { "CP1361", "JOHAB" },
105 { "CP20127", "ASCII" },
106 { "CP20866", "KOI8-R" },
107 { "CP21866", "KOI8-RU" },
108 { "CP28591", "ISO-8859-1" },
109 { "CP28592", "ISO-8859-2" },
110 { "CP28593", "ISO-8859-3" },
111 { "CP28594", "ISO-8859-4" },
112 { "CP28595", "ISO-8859-5" },
113 { "CP28596", "ISO-8859-6" },
114 { "CP28597", "ISO-8859-7" },
115 { "CP28598", "ISO-8859-8" },
116 { "CP28599", "ISO-8859-9" },
117 { "CP28605", "ISO-8859-15" },
121 VLCCharsetAlias aliases[] =
123 { "IBM-850", "CP850" },
124 { "IBM-856", "CP856" },
125 { "IBM-921", "ISO-8859-13" },
126 { "IBM-922", "CP922" },
127 { "IBM-932", "CP932" },
128 { "IBM-943", "CP943" },
129 { "IBM-1046", "CP1046" },
130 { "IBM-1124", "CP1124" },
131 { "IBM-1129", "CP1129" },
132 { "IBM-1252", "CP1252" },
133 { "IBM-EUCCN", "GB2312" },
134 { "IBM-EUCJP", "EUC-JP" },
135 { "IBM-EUCKR", "EUC-KR" },
136 { "IBM-EUCTW", "EUC-TW" },
140 VLCCharsetAlias aliases[] =
142 { "ROMAN8", "HP-ROMAN8" },
143 { "ARABIC8", "HP-ARABIC8" },
144 { "GREEK8", "HP-GREEK8" },
145 { "HEBREW8", "HP-HEBREW8" },
146 { "TURKISH8", "HP-TURKISH8" },
147 { "KANA8", "HP-KANA8" },
148 { "HP15CN", "GB2312" },
152 VLCCharsetAlias aliases[] =
154 { "EUCCN", "GB2312" },
158 VLCCharsetAlias aliases[] =
160 { "KSC5601", "CP949" },
161 { "SDECKANJI", "EUC-JP" },
162 { "TACTIS", "TIS-620" },
166 VLCCharsetAlias aliases[] =
169 { "CNS11643", "EUC-TW" },
170 { "5601", "EUC-KR" },
171 { "JOHAP92", "JOHAB" },
172 { "PCK", "SHIFT_JIS" },
173 { "2533", "TIS-620" },
177 VLCCharsetAlias aliases[] =
180 { "EUCCN", "GB2312" },
184 VLCCharsetAlias aliases[] = {{NULL, NULL}};
189 for (a = aliases; a->psz_alias; a++)
190 if (strcasecmp (a->psz_alias, psz_name) == 0)
194 /* we return original name beacuse iconv() probably will know
195 * something better about name if we don't know it :-) */
199 /* Returns charset from "language_COUNTRY.charset@modifier" string */
200 static char *vlc_encoding_from_locale( char *psz_locale )
202 char *psz_dot = strchr( psz_locale, '.' );
204 if( psz_dot != NULL )
206 const char *psz_modifier;
207 static char buf[2 + 10 + 1];
211 /* Look for the possible @... trailer and remove it, if any. */
212 psz_modifier = strchr( psz_dot, '@' );
214 if( psz_modifier == NULL )
216 if( 0 < ( psz_modifier - psz_dot ) < sizeof( buf ))
218 memcpy( buf, psz_dot, psz_modifier - psz_dot );
219 buf[ psz_modifier - psz_dot ] = '\0';
223 /* try language mapping */
224 return (char *)vlc_encoding_from_language( psz_locale );
227 vlc_bool_t vlc_current_charset( char **psz_charset )
229 const char *psz_codeset;
231 #if !(defined WIN32 || defined OS2)
233 # if HAVE_LANGINFO_CODESET
234 /* Most systems support nl_langinfo( CODESET ) nowadays. */
235 psz_codeset = nl_langinfo( CODESET );
237 /* On old systems which lack it, use setlocale or getenv. */
238 const char *psz_locale = NULL;
240 /* But most old systems don't have a complete set of locales. Some
241 * (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
242 * use setlocale here; it would return "C" when it doesn't support the
243 * locale name the user has set. Darwin's setlocale is broken. */
244 # if HAVE_SETLOCALE && !SYS_DARWIN
245 psz_locale = setlocale( LC_ALL, NULL );
247 if( psz_locale == NULL || psz_locale[0] == '\0' )
249 psz_locale = getenv( "LC_ALL" );
250 if( psz_locale == NULL || psz_locale[0] == '\0' )
252 psz_locale = getenv( "LC_CTYPE" );
253 if( psz_locale == NULL || psz_locale[0] == '\0')
254 psz_locale = getenv( "LANG" );
258 /* On some old systems, one used to set locale = "iso8859_1". On others,
259 * you set it to "language_COUNTRY.charset". Darwin only has LANG :( */
260 psz_codeset = vlc_encoding_from_locale( (char *)psz_locale );
261 # endif /* HAVE_LANGINFO_CODESET */
265 static char buf[2 + 10 + 1];
267 /* Woe32 has a function returning the locale's codepage as a number. */
268 sprintf( buf, "CP%u", GetACP() );
273 const char *psz_locale;
274 static char buf[2 + 10 + 1];
278 /* Allow user to override the codeset, as set in the operating system,
279 * with standard language environment variables. */
280 psz_locale = getenv( "LC_ALL" );
281 if( psz_locale == NULL || psz_locale[0] == '\0' )
283 psz+locale = getenv( "LC_CTYPE" );
284 if( psz_locale == NULL || locale[0] == '\0' )
285 locale = getenv( "LANG" );
287 if( psz_locale != NULL && psz_locale[0] != '\0' )
288 psz_codeset = vlc_encoding_from_locale( psz_locale );
291 /* OS/2 has a function returning the locale's codepage as a number. */
292 if( DosQueryCp( sizeof( cp ), cp, &cplen ) )
296 sprintf( buf, "CP%u", cp[0] );
301 if( psz_codeset == NULL )
302 /* The canonical name cannot be determined. */
305 psz_codeset = vlc_charset_aliases( psz_codeset );
307 /* Don't return an empty string. GNU libc and GNU libiconv interpret
308 * the empty string as denoting "the locale's character encoding",
309 * thus GNU libiconv would call this function a second time. */
310 if( psz_codeset[0] == '\0' )
312 /* Last possibility is 'CHARSET' enviroment variable */
313 if( !( psz_codeset = getenv( "CHARSET" ) ) )
314 psz_codeset = "ISO-8859-1";
318 *psz_charset = strdup((char *)psz_codeset);
320 if( !strcasecmp(psz_codeset, "UTF8") || !strcasecmp(psz_codeset, "UTF-8") )