]> git.sesse.net Git - vlc/blob - src/misc/charset.c
Updated copyrights in libvlc
[vlc] / src / misc / charset.c
1 /*****************************************************************************
2  * charset.c: Determine a canonical name for the current locale's character
3  *            encoding.
4  *****************************************************************************
5  * Copyright (C) 2003-2004 VideoLAN
6  * $Id: charset.c,v 1.6 2004/01/06 12:02:06 zorglub Exp $
7  *
8  * Author: Derk-Jan Hartman <thedj at users.sf.net>
9  *
10  * vlc_current_charset() an adaption of mp_locale_charset():
11  *
12  *  Copyright (C) 2001-2003 The Mape Project
13  *  Written by Karel Zak  <zakkr@zf.jcu.cz>.
14  *
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.
19  *
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.
24  *
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  *****************************************************************************/
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <vlc/vlc.h>
33
34 #if !defined WIN32
35 # if HAVE_LANGINFO_CODESET
36 #  include <langinfo.h>
37 # else
38 #  if HAVE_SETLOCALE
39 #   include <locale.h>
40 #  endif
41 # endif
42 #elif defined WIN32
43 # include <windows.h>
44 #endif
45
46 #include "charset.h"
47
48 typedef struct VLCCharsetAlias
49 {
50     char *psz_alias, *psz_name;
51 } VLCCharsetAlias;
52
53 /*
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.
57  *
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).
60  *
61  */
62
63 static const char* vlc_encoding_from_language( const char *l )
64 {
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. */
92     return "ISO-8859-1";
93 }
94
95
96 static const char* vlc_charset_aliases( const char *psz_name )
97 {
98     VLCCharsetAlias     *a;
99
100 #if defined WIN32
101     VLCCharsetAlias aliases[] =
102     {
103         { "CP936",      "GBK" },
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" },
118         { NULL,         NULL }
119     };
120 #elif SYS_AIX
121     VLCCharsetAlias aliases[] =
122     {
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" },
137         { NULL, NULL }
138     };
139 #elif SYS_HPUX
140     VLCCharsetAlias aliases[] =
141     {
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" },
149         { NULL, NULL }
150     };
151 #elif SYS_IRIX
152     VLCCharsetAlias aliases[] =
153     {
154         { "EUCCN",      "GB2312" },
155         { NULL, NULL }
156     };
157 #elif SYS_OSF
158     VLCCharsetAlias aliases[] =
159     {
160         { "KSC5601",    "CP949" },
161         { "SDECKANJI",  "EUC-JP" },
162         { "TACTIS",     "TIS-620" },
163         { NULL, NULL }
164     };
165 #elif SYS_SOLARIS
166     VLCCharsetAlias aliases[] =
167     {
168         { "646",        "ASCII" },
169         { "CNS11643",   "EUC-TW" },
170         { "5601",       "EUC-KR" },
171         { "JOHAP92",    "JOHAB" },
172         { "PCK",        "SHIFT_JIS" },
173         { "2533",       "TIS-620" },
174         { NULL, NULL }
175     };
176 #elif SYS_BSD
177     VLCCharsetAlias aliases[] =
178     {
179         { "646", " ASCII" },
180         { "EUCCN", "GB2312" },
181         { NULL, NULL }
182     };
183 #else
184     VLCCharsetAlias aliases[] = {{NULL, NULL}};
185 #endif
186
187     if( aliases )
188     {
189         for (a = aliases; a->psz_alias; a++)
190             if (strcasecmp (a->psz_alias, psz_name) == 0)
191                 return a->psz_name;
192     }
193
194     /* we return original name beacuse iconv() probably will know
195      * something better about name if we don't know it :-) */
196     return psz_name;
197 }
198
199 /* Returns charset from "language_COUNTRY.charset@modifier" string */
200 static char *vlc_encoding_from_locale( char *psz_locale )
201 {
202     char *psz_dot = strchr( psz_locale, '.' );
203
204     if( psz_dot != NULL )
205     {
206         const char *psz_modifier;
207         static char buf[2 + 10 + 1];
208
209         psz_dot++;
210
211         /* Look for the possible @... trailer and remove it, if any.  */
212         psz_modifier = strchr( psz_dot, '@' );
213
214         if( psz_modifier == NULL )
215             return psz_dot;
216         if( 0 < ( psz_modifier - psz_dot ) < sizeof( buf ))
217         {
218             memcpy( buf, psz_dot, psz_modifier - psz_dot );
219             buf[ psz_modifier - psz_dot ] = '\0';
220             return buf;
221         }
222     }
223     /* try language mapping */
224     return (char *)vlc_encoding_from_language( psz_locale );
225 }
226
227 vlc_bool_t vlc_current_charset( char **psz_charset )
228 {
229     const char *psz_codeset;
230
231 #if !(defined WIN32 || defined OS2)
232
233 # if HAVE_LANGINFO_CODESET
234     /* Most systems support nl_langinfo( CODESET ) nowadays.  */
235     psz_codeset = nl_langinfo( CODESET );
236 # else
237     /* On old systems which lack it, use setlocale or getenv.  */
238     const char *psz_locale = NULL;
239
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 );
246 #  endif
247     if( psz_locale == NULL || psz_locale[0] == '\0' )
248     {
249         psz_locale = getenv( "LC_ALL" );
250         if( psz_locale == NULL || psz_locale[0] == '\0' )
251         {
252             psz_locale = getenv( "LC_CTYPE" );
253             if( psz_locale == NULL || psz_locale[0] == '\0')
254                 psz_locale = getenv( "LANG" );
255         }
256     }
257
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 */
262
263 #elif defined WIN32
264
265     static char buf[2 + 10 + 1];
266
267     /* Woe32 has a function returning the locale's codepage as a number.  */
268     sprintf( buf, "CP%u", GetACP() );
269     psz_codeset = buf;
270
271 #elif defined OS2
272
273     const char *psz_locale;
274     static char buf[2 + 10 + 1];
275     ULONG cp[3];
276     ULONG cplen;
277
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' )
282     {
283         psz+locale = getenv( "LC_CTYPE" );
284         if( psz_locale == NULL || locale[0] == '\0' )
285             locale = getenv( "LANG" );
286     }
287     if( psz_locale != NULL && psz_locale[0] != '\0' )
288         psz_codeset = vlc_encoding_from_locale( psz_locale );
289     else
290     {
291         /* OS/2 has a function returning the locale's codepage as a number. */
292         if( DosQueryCp( sizeof( cp ), cp, &cplen ) )
293             psz_codeset = "";
294         else
295         {
296             sprintf( buf, "CP%u", cp[0] );
297             psz_codeset = buf;
298         }
299     }
300 #endif
301     if( psz_codeset == NULL )
302         /* The canonical name cannot be determined. */
303         psz_codeset = "";
304     else
305         psz_codeset = vlc_charset_aliases( psz_codeset );
306
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' )
311     {
312         /* Last possibility is 'CHARSET' enviroment variable */
313         if( !( psz_codeset = getenv( "CHARSET" ) ) )
314             psz_codeset = "ISO-8859-1";
315     }
316
317     if( psz_charset )
318         *psz_charset = strdup((char *)psz_codeset);
319
320     if( !strcasecmp(psz_codeset, "UTF8") || !strcasecmp(psz_codeset, "UTF-8") )
321         return VLC_TRUE;
322
323     return VLC_FALSE;
324 }
325