Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

localcharset.c

00001 /* Determine a canonical name for the current locale's character encoding.
00002 
00003    Copyright (C) 2000-2002 Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify it
00006    under the terms of the GNU Library General Public License as published
00007    by the Free Software Foundation; either version 2, or (at your option)
00008    any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this program; if not, write to the Free Software
00017    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00018    USA.  */
00019 
00020 /* Written by Bruno Haible <[email protected]>.  */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 # include <config.h>
00024 #endif
00025 
00026 #if HAVE_STDDEF_H
00027 # include <stddef.h>
00028 #endif
00029 
00030 #include <stdio.h>
00031 #if HAVE_STRING_H
00032 # include <string.h>
00033 #else
00034 # include <strings.h>
00035 #endif
00036 #if HAVE_STDLIB_H
00037 # include <stdlib.h>
00038 #endif
00039 
00040 #if defined _WIN32 || defined __WIN32__
00041 # undef WIN32   /* avoid warning on mingw32 */
00042 # define WIN32
00043 #endif
00044 
00045 #if defined __EMX__
00046 /* Assume EMX program runs on OS/2, even if compiled under DOS.  */
00047 # define OS2
00048 #endif
00049 
00050 #if !defined WIN32
00051 # if HAVE_LANGINFO_CODESET
00052 #  include <langinfo.h>
00053 # else
00054 #  if HAVE_SETLOCALE
00055 #   include <locale.h>
00056 #  endif
00057 # endif
00058 #elif defined WIN32
00059 # define WIN32_LEAN_AND_MEAN
00060 # include <windows.h>
00061 #endif
00062 #if defined OS2
00063 # define INCL_DOS
00064 # include <os2.h>
00065 #endif
00066 
00067 #if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__
00068   /* Win32, OS/2, DOS */
00069 # define ISSLASH(C) ((C) == '/' || (C) == '\\')
00070 #endif
00071 
00072 #ifndef DIRECTORY_SEPARATOR
00073 # define DIRECTORY_SEPARATOR '/'
00074 #endif
00075 
00076 #ifndef ISSLASH
00077 # define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR)
00078 #endif
00079 
00080 #ifdef HAVE_GETC_UNLOCKED
00081 # undef getc
00082 # define getc getc_unlocked
00083 #endif
00084 
00085 #ifdef __cplusplus
00086 /* When compiling with "gcc -x c++", produce a function with C linkage.  */
00087 extern "C" const char * locale_charset (void);
00088 #endif
00089 
00090 /* The following static variable is declared 'volatile' to avoid a
00091    possible multithread problem in the function get_charset_aliases. If we
00092    are running in a threaded environment, and if two threads initialize
00093    'charset_aliases' simultaneously, both will produce the same value,
00094    and everything will be ok if the two assignments to 'charset_aliases'
00095    are atomic. But I don't know what will happen if the two assignments mix.  */
00096 #if __STDC__ != 1
00097 # define volatile /* empty */
00098 #endif
00099 /* Pointer to the contents of the charset.alias file, if it has already been
00100    read, else NULL.  Its format is:
00101    ALIAS_1 '\0' CANONICAL_1 '\0' ... ALIAS_n '\0' CANONICAL_n '\0' '\0'  */
00102 static const char * volatile charset_aliases;
00103 
00104 /* Return a pointer to the contents of the charset.alias file.  */
00105 static const char *
00106 get_charset_aliases ()
00107 {
00108   const char *cp;
00109 
00110   cp = charset_aliases;
00111   if (cp == NULL)
00112     {
00113 #if !defined WIN32
00114       FILE *fp;
00115       const char *dir = LIBDIR;
00116       const char *base = "charset.alias";
00117       char *file_name;
00118 
00119       /* Concatenate dir and base into freshly allocated file_name.  */
00120       {
00121         size_t dir_len = strlen (dir);
00122         size_t base_len = strlen (base);
00123         int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1]));
00124         file_name = (char *) malloc (dir_len + add_slash + base_len + 1);
00125         if (file_name != NULL)
00126           {
00127             memcpy (file_name, dir, dir_len);
00128             if (add_slash)
00129               file_name[dir_len] = DIRECTORY_SEPARATOR;
00130             memcpy (file_name + dir_len + add_slash, base, base_len + 1);
00131           }
00132       }
00133 
00134       if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL)
00135         /* Out of memory or file not found, treat it as empty.  */
00136         cp = "";
00137       else
00138         {
00139           /* Parse the file's contents.  */
00140           int c;
00141           char buf1[50+1];
00142           char buf2[50+1];
00143           char *res_ptr = NULL;
00144           size_t res_size = 0;
00145           size_t l1, l2;
00146 
00147           for (;;)
00148             {
00149               c = getc (fp);
00150               if (c == EOF)
00151                 break;
00152               if (c == '\n' || c == ' ' || c == '\t')
00153                 continue;
00154               if (c == '#')
00155                 {
00156                   /* Skip comment, to end of line.  */
00157                   do
00158                     c = getc (fp);
00159                   while (!(c == EOF || c == '\n'));
00160                   if (c == EOF)
00161                     break;
00162                   continue;
00163                 }
00164               ungetc (c, fp);
00165               if (fscanf (fp, "%50s %50s", buf1, buf2) < 2)
00166                 break;
00167               l1 = strlen (buf1);
00168               l2 = strlen (buf2);
00169               if (res_size == 0)
00170                 {
00171                   res_size = l1 + 1 + l2 + 1;
00172                   res_ptr = (char *) malloc (res_size + 1);
00173                 }
00174               else
00175                 {
00176                   res_size += l1 + 1 + l2 + 1;
00177                   res_ptr = (char *) realloc (res_ptr, res_size + 1);
00178                 }
00179               if (res_ptr == NULL)
00180                 {
00181                   /* Out of memory. */
00182                   res_size = 0;
00183                   break;
00184                 }
00185               strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1);
00186               strcpy (res_ptr + res_size - (l2 + 1), buf2);
00187             }
00188           fclose (fp);
00189           if (res_size == 0)
00190             cp = "";
00191           else
00192             {
00193               *(res_ptr + res_size) = '\0';
00194               cp = res_ptr;
00195             }
00196         }
00197 
00198       if (file_name != NULL)
00199         free (file_name);
00200 
00201 #else
00202 
00203       /* To avoid the troubles of installing a separate file in the same
00204          directory as the DLL and of retrieving the DLL's directory at
00205          runtime, simply inline the aliases here.  */
00206 
00207 # if defined WIN32
00208       cp = "CP936" "\0" "GBK" "\0"
00209            "CP1361" "\0" "JOHAB" "\0"
00210            "CP20127" "\0" "ASCII" "\0"
00211            "CP20866" "\0" "KOI8-R" "\0"
00212            "CP21866" "\0" "KOI8-RU" "\0"
00213            "CP28591" "\0" "ISO-8859-1" "\0"
00214            "CP28592" "\0" "ISO-8859-2" "\0"
00215            "CP28593" "\0" "ISO-8859-3" "\0"
00216            "CP28594" "\0" "ISO-8859-4" "\0"
00217            "CP28595" "\0" "ISO-8859-5" "\0"
00218            "CP28596" "\0" "ISO-8859-6" "\0"
00219            "CP28597" "\0" "ISO-8859-7" "\0"
00220            "CP28598" "\0" "ISO-8859-8" "\0"
00221            "CP28599" "\0" "ISO-8859-9" "\0"
00222            "CP28605" "\0" "ISO-8859-15" "\0";
00223 # endif
00224 #endif
00225 
00226       charset_aliases = cp;
00227     }
00228 
00229   return cp;
00230 }
00231 
00232 /* Determine the current locale's character encoding, and canonicalize it
00233    into one of the canonical names listed in config.charset.
00234    The result must not be freed; it is statically allocated.
00235    If the canonical name cannot be determined, the result is a non-canonical
00236    name.  */
00237 
00238 #ifdef STATIC
00239 STATIC
00240 #endif
00241 const char *
00242 locale_charset ()
00243 {
00244   const char *codeset;
00245   const char *aliases;
00246 
00247 #if !(defined WIN32 || defined OS2)
00248 
00249 # if HAVE_LANGINFO_CODESET
00250 
00251   /* Most systems support nl_langinfo (CODESET) nowadays.  */
00252   codeset = nl_langinfo (CODESET);
00253 
00254 # else
00255 
00256   /* On old systems which lack it, use setlocale or getenv.  */
00257   const char *locale = NULL;
00258 
00259   /* But most old systems don't have a complete set of locales.  Some
00260      (like SunOS 4 or DJGPP) have only the C locale.  Therefore we don't
00261      use setlocale here; it would return "C" when it doesn't support the
00262      locale name the user has set.  */
00263 #  if HAVE_SETLOCALE && 0
00264   locale = setlocale (LC_CTYPE, NULL);
00265 #  endif
00266   if (locale == NULL || locale[0] == '\0')
00267     {
00268       locale = getenv ("LC_ALL");
00269       if (locale == NULL || locale[0] == '\0')
00270         {
00271           locale = getenv ("LC_CTYPE");
00272           if (locale == NULL || locale[0] == '\0')
00273             locale = getenv ("LANG");
00274         }
00275     }
00276 
00277   /* On some old systems, one used to set locale = "iso8859_1". On others,
00278      you set it to "language_COUNTRY.charset". In any case, we resolve it
00279      through the charset.alias file.  */
00280   codeset = locale;
00281 
00282 # endif
00283 
00284 #elif defined WIN32
00285 
00286   static char buf[2 + 10 + 1];
00287 
00288   /* Woe32 has a function returning the locale's codepage as a number.  */
00289   sprintf (buf, "CP%u", GetACP ());
00290   codeset = buf;
00291 
00292 #elif defined OS2
00293 
00294   const char *locale;
00295   static char buf[2 + 10 + 1];
00296   ULONG cp[3];
00297   ULONG cplen;
00298 
00299   /* Allow user to override the codeset, as set in the operating system,
00300      with standard language environment variables.  */
00301   locale = getenv ("LC_ALL");
00302   if (locale == NULL || locale[0] == '\0')
00303     {
00304       locale = getenv ("LC_CTYPE");
00305       if (locale == NULL || locale[0] == '\0')
00306         locale = getenv ("LANG");
00307     }
00308   if (locale != NULL && locale[0] != '\0')
00309     {
00310       /* If the locale name contains an encoding after the dot, return it.  */
00311       const char *dot = strchr (locale, '.');
00312 
00313       if (dot != NULL)
00314         {
00315           const char *modifier;
00316 
00317           dot++;
00318           /* Look for the possible @... trailer and remove it, if any.  */
00319           modifier = strchr (dot, '@');
00320           if (modifier == NULL)
00321             return dot;
00322           if (modifier - dot < sizeof (buf))
00323             {
00324               memcpy (buf, dot, modifier - dot);
00325               buf [modifier - dot] = '\0';
00326               return buf;
00327             }
00328         }
00329 
00330       /* Resolve through the charset.alias file.  */
00331       codeset = locale;
00332     }
00333   else
00334     {
00335       /* OS/2 has a function returning the locale's codepage as a number.  */
00336       if (DosQueryCp (sizeof (cp), cp, &cplen))
00337         codeset = "";
00338       else
00339         {
00340           sprintf (buf, "CP%u", cp[0]);
00341           codeset = buf;
00342         }
00343     }
00344 
00345 #endif
00346 
00347   if (codeset == NULL)
00348     /* The canonical name cannot be determined.  */
00349     codeset = "";
00350 
00351   /* Resolve alias. */
00352   for (aliases = get_charset_aliases ();
00353        *aliases != '\0';
00354        aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1)
00355     if (strcmp (codeset, aliases) == 0
00356         || (aliases[0] == '*' && aliases[1] == '\0'))
00357       {
00358         codeset = aliases + strlen (aliases) + 1;
00359         break;
00360       }
00361 
00362   /* Don't return an empty string.  GNU libc and GNU libiconv interpret
00363      the empty string as denoting "the locale's character encoding",
00364      thus GNU libiconv would call this function a second time.  */
00365   if (codeset[0] == '\0')
00366     codeset = "ASCII";
00367 
00368   return codeset;
00369 }

Generated on Tue Dec 20 10:14:20 2005 for vlc-0.8.4a by  doxygen 1.4.2