1 /*****************************************************************************
2 * charset.c: Determine a canonical name for the current locale's character
4 *****************************************************************************
5 * Copyright (C) 2003-2005 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28 *****************************************************************************/
35 # if HAVE_LANGINFO_CODESET
36 # include <langinfo.h>
53 typedef struct VLCCharsetAlias
55 char *psz_alias, *psz_name;
59 * The libcharset load all from external text file, but it's strange and
60 * slow solution, we rather use array(s) compiled into source. In the
61 * "good" libc this is not needful -- for example in linux.
63 * Please, put to this funtion exotic aliases only. The libc 'iconv' knows
64 * a lot of basic aliases (check it first by iconv -l).
67 #if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
68 static const char* vlc_encoding_from_language( const char *l )
70 /* check for language (and perhaps country) codes */
71 if (strstr(l, "zh_TW")) return "Big5";
72 if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */
73 if (strstr(l, "zh")) return "GB2312";
74 if (strstr(l, "th")) return "TIS-620";
75 if (strstr(l, "ja")) return "EUC-JP";
76 if (strstr(l, "ko")) return "EUC-KR";
77 if (strstr(l, "ru")) return "KOI8-R";
78 if (strstr(l, "uk")) return "KOI8-U";
79 if (strstr(l, "pl") || strstr(l, "hr") ||
80 strstr(l, "hu") || strstr(l, "cs") ||
81 strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2";
82 if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3";
83 if (strstr(l, "lt") || strstr(l, "la")) return "ISO-8859-4";
84 if (strstr(l, "bg") || strstr(l, "be") ||
85 strstr(l, "mk") || strstr(l, "uk")) return "ISO-8859-5";
86 if (strstr(l, "ar")) return "ISO-8859-6";
87 if (strstr(l, "el")) return "ISO-8859-7";
88 if (strstr(l, "he") || strstr(l, "iw")) return "ISO-8859-8";
89 if (strstr(l, "tr")) return "ISO-8859-9";
90 if (strstr(l, "th")) return "ISO-8859-11";
91 if (strstr(l, "lv")) return "ISO-8859-13";
92 if (strstr(l, "cy")) return "ISO-8859-14";
93 if (strstr(l, "et")) return "ISO-8859-15"; /* all latin1 could be iso15 as well */
94 if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */
95 if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8";
96 /* We don't know. This ain't working go to default. */
101 static const char* vlc_charset_aliases( const char *psz_name )
106 VLCCharsetAlias aliases[] =
109 { "CP1361", "JOHAB" },
110 { "CP20127", "ASCII" },
111 { "CP20866", "KOI8-R" },
112 { "CP21866", "KOI8-RU" },
113 { "CP28591", "ISO-8859-1" },
114 { "CP28592", "ISO-8859-2" },
115 { "CP28593", "ISO-8859-3" },
116 { "CP28594", "ISO-8859-4" },
117 { "CP28595", "ISO-8859-5" },
118 { "CP28596", "ISO-8859-6" },
119 { "CP28597", "ISO-8859-7" },
120 { "CP28598", "ISO-8859-8" },
121 { "CP28599", "ISO-8859-9" },
122 { "CP28605", "ISO-8859-15" },
126 VLCCharsetAlias aliases[] =
128 { "IBM-850", "CP850" },
129 { "IBM-856", "CP856" },
130 { "IBM-921", "ISO-8859-13" },
131 { "IBM-922", "CP922" },
132 { "IBM-932", "CP932" },
133 { "IBM-943", "CP943" },
134 { "IBM-1046", "CP1046" },
135 { "IBM-1124", "CP1124" },
136 { "IBM-1129", "CP1129" },
137 { "IBM-1252", "CP1252" },
138 { "IBM-EUCCN", "GB2312" },
139 { "IBM-EUCJP", "EUC-JP" },
140 { "IBM-EUCKR", "EUC-KR" },
141 { "IBM-EUCTW", "EUC-TW" },
145 VLCCharsetAlias aliases[] =
147 { "ROMAN8", "HP-ROMAN8" },
148 { "ARABIC8", "HP-ARABIC8" },
149 { "GREEK8", "HP-GREEK8" },
150 { "HEBREW8", "HP-HEBREW8" },
151 { "TURKISH8", "HP-TURKISH8" },
152 { "KANA8", "HP-KANA8" },
153 { "HP15CN", "GB2312" },
157 VLCCharsetAlias aliases[] =
159 { "EUCCN", "GB2312" },
163 VLCCharsetAlias aliases[] =
165 { "KSC5601", "CP949" },
166 { "SDECKANJI", "EUC-JP" },
167 { "TACTIS", "TIS-620" },
171 VLCCharsetAlias aliases[] =
174 { "CNS11643", "EUC-TW" },
175 { "5601", "EUC-KR" },
176 { "JOHAP92", "JOHAB" },
177 { "PCK", "SHIFT_JIS" },
178 { "2533", "TIS-620" },
182 VLCCharsetAlias aliases[] =
185 { "EUCCN", "GB2312" },
189 VLCCharsetAlias aliases[] = {{NULL, NULL}};
192 for (a = aliases; a->psz_alias; a++)
193 if (strcasecmp (a->psz_alias, psz_name) == 0)
196 /* we return original name beacuse iconv() probably will know
197 * something better about name if we don't know it :-) */
201 /* Returns charset from "language_COUNTRY.charset@modifier" string */
202 #if (defined OS2 || !HAVE_LANGINFO_CODESET) && !defined WIN32
203 static void vlc_encoding_from_locale( char *psz_locale, char *psz_charset )
205 char *psz_dot = strchr( psz_locale, '.' );
207 if( psz_dot != NULL )
209 const char *psz_modifier;
213 /* Look for the possible @... trailer and remove it, if any. */
214 psz_modifier = strchr( psz_dot, '@' );
216 if( psz_modifier == NULL )
218 strcpy( psz_charset, psz_dot );
221 if( 0 < ( psz_modifier - psz_dot )
222 && ( psz_modifier - psz_dot ) < 2 + 10 + 1 )
224 memcpy( psz_charset, psz_dot, psz_modifier - psz_dot );
225 psz_charset[ psz_modifier - psz_dot ] = '\0';
229 /* try language mapping */
230 strcpy( psz_charset, vlc_encoding_from_language( psz_locale ) );
234 vlc_bool_t vlc_current_charset( char **psz_charset )
236 const char *psz_codeset;
238 #if !(defined WIN32 || defined OS2 || defined __APPLE__)
240 # if HAVE_LANGINFO_CODESET
241 /* Most systems support nl_langinfo( CODESET ) nowadays. */
242 psz_codeset = nl_langinfo( CODESET );
243 if( !strcmp( psz_codeset, "ANSI_X3.4-1968" ) )
244 psz_codeset = "ASCII";
246 /* On old systems which lack it, use setlocale or getenv. */
247 const char *psz_locale = NULL;
248 char buf[2 + 10 + 1];
250 /* But most old systems don't have a complete set of locales. Some
251 * (like SunOS 4 or DJGPP) have only the C locale. Therefore we don't
252 * use setlocale here; it would return "C" when it doesn't support the
253 * locale name the user has set. Darwin's setlocale is broken. */
254 # if HAVE_SETLOCALE && !__APPLE__
255 psz_locale = setlocale( LC_ALL, NULL );
257 if( psz_locale == NULL || psz_locale[0] == '\0' )
259 psz_locale = getenv( "LC_ALL" );
260 if( psz_locale == NULL || psz_locale[0] == '\0' )
262 psz_locale = getenv( "LC_CTYPE" );
263 if( psz_locale == NULL || psz_locale[0] == '\0')
264 psz_locale = getenv( "LANG" );
268 /* On some old systems, one used to set locale = "iso8859_1". On others,
269 * you set it to "language_COUNTRY.charset". Darwin only has LANG :( */
270 vlc_encoding_from_locale( (char *)psz_locale, buf );
272 # endif /* HAVE_LANGINFO_CODESET */
274 #elif defined __APPLE__
276 /* Darwin is always using UTF-8 internally. */
277 psz_codeset = "UTF-8";
281 char buf[2 + 10 + 1];
283 /* Woe32 has a function returning the locale's codepage as a number. */
284 sprintf( buf, "CP%u", GetACP() );
289 const char *psz_locale;
290 char buf[2 + 10 + 1];
294 /* Allow user to override the codeset, as set in the operating system,
295 * with standard language environment variables. */
296 psz_locale = getenv( "LC_ALL" );
297 if( psz_locale == NULL || psz_locale[0] == '\0' )
299 psz+locale = getenv( "LC_CTYPE" );
300 if( psz_locale == NULL || locale[0] == '\0' )
301 locale = getenv( "LANG" );
303 if( psz_locale != NULL && psz_locale[0] != '\0' )
304 vlc_encoding_from_locale( psz_locale, buf );
308 /* OS/2 has a function returning the locale's codepage as a number. */
309 if( DosQueryCp( sizeof( cp ), cp, &cplen ) )
313 sprintf( buf, "CP%u", cp[0] );
318 if( psz_codeset == NULL )
319 /* The canonical name cannot be determined. */
322 psz_codeset = vlc_charset_aliases( psz_codeset );
324 /* Don't return an empty string. GNU libc and GNU libiconv interpret
325 * the empty string as denoting "the locale's character encoding",
326 * thus GNU libiconv would call this function a second time. */
327 if( psz_codeset[0] == '\0' )
329 /* Last possibility is 'CHARSET' enviroment variable */
330 if( !( psz_codeset = getenv( "CHARSET" ) ) )
331 psz_codeset = "ISO-8859-1";
335 *psz_charset = strdup(psz_codeset);
337 if( !strcasecmp(psz_codeset, "UTF8") || !strcasecmp(psz_codeset, "UTF-8") )
343 char *__vlc_fix_readdir_charset( vlc_object_t *p_this, const char *psz_string )
346 if ( p_this->p_libvlc->iconv_macosx != (vlc_iconv_t)-1 )
348 const char *psz_in = psz_string;
349 size_t i_in = strlen(psz_in);
350 size_t i_out = i_in * 2;
351 char *psz_utf8 = malloc(i_out + 1);
352 char *psz_out = psz_utf8;
354 vlc_mutex_lock( &p_this->p_libvlc->iconv_lock );
355 size_t i_ret = vlc_iconv( p_this->p_libvlc->iconv_macosx,
356 &psz_in, &i_in, &psz_out, &i_out );
357 vlc_mutex_unlock( &p_this->p_libvlc->iconv_lock );
358 if( i_ret == (size_t)-1 || i_in )
361 "failed to convert \"%s\" from HFS+ charset (%s)",
362 psz_string, strerror(errno) );
364 return strdup( psz_string );
372 return strdup( psz_string );