#include <stdio.h>#include <strings.h>Go to the source code of this file.
Defines | |
| #define | DIRECTORY_SEPARATOR '/' |
| #define | ISSLASH(C) ((C) == DIRECTORY_SEPARATOR) |
| #define | volatile |
Functions | |
| const char * | get_charset_aliases () |
| const char * | locale_charset () |
Variables | |
| const char *volatile | charset_aliases |
|
|
Definition at line 73 of file localcharset.c. |
|
|
Definition at line 77 of file localcharset.c. |
|
|
Definition at line 97 of file localcharset.c. |
|
|
Definition at line 106 of file localcharset.c. 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 }
|
|
|
Definition at line 242 of file localcharset.c. 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 }
|
|
|
Definition at line 102 of file localcharset.c. |
1.3.9.1