00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00042 # define WIN32
00043 #endif
00044
00045 #if defined __EMX__
00046
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
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
00087 extern "C" const char * locale_charset (void);
00088 #endif
00089
00090
00091
00092
00093
00094
00095
00096 #if __STDC__ != 1
00097 # define volatile
00098 #endif
00099
00100
00101
00102 static const char * volatile charset_aliases;
00103
00104
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
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
00136 cp = "";
00137 else
00138 {
00139
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
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
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
00204
00205
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
00233
00234
00235
00236
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
00252 codeset = nl_langinfo (CODESET);
00253
00254 # else
00255
00256
00257 const char *locale = NULL;
00258
00259
00260
00261
00262
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
00278
00279
00280 codeset = locale;
00281
00282 # endif
00283
00284 #elif defined WIN32
00285
00286 static char buf[2 + 10 + 1];
00287
00288
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
00300
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
00311 const char *dot = strchr (locale, '.');
00312
00313 if (dot != NULL)
00314 {
00315 const char *modifier;
00316
00317 dot++;
00318
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
00331 codeset = locale;
00332 }
00333 else
00334 {
00335
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
00349 codeset = "";
00350
00351
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
00363
00364
00365 if (codeset[0] == '\0')
00366 codeset = "ASCII";
00367
00368 return codeset;
00369 }