| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#if HAVE_STDDEF_H |
| 27 |
# include <stddef.h> |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#if HAVE_STRING_H |
| 32 |
# include <string.h> |
| 33 |
#else |
| 34 |
# include <strings.h> |
| 35 |
#endif |
| 36 |
#if HAVE_STDLIB_H |
| 37 |
# include <stdlib.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
#if defined _WIN32 || defined __WIN32__ |
| 41 |
# undef WIN32 |
| 42 |
# define WIN32 |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined __EMX__ |
| 46 |
|
| 47 |
# define OS2 |
| 48 |
#endif |
| 49 |
|
| 50 |
#if !defined WIN32 |
| 51 |
# if HAVE_LANGINFO_CODESET |
| 52 |
# include <langinfo.h> |
| 53 |
# else |
| 54 |
# if HAVE_SETLOCALE |
| 55 |
# include <locale.h> |
| 56 |
# endif |
| 57 |
# endif |
| 58 |
#elif defined WIN32 |
| 59 |
# define WIN32_LEAN_AND_MEAN |
| 60 |
# include <windows.h> |
| 61 |
#endif |
| 62 |
#if defined OS2 |
| 63 |
# define INCL_DOS |
| 64 |
# include <os2.h> |
| 65 |
#endif |
| 66 |
|
| 67 |
#if defined _WIN32 || defined __WIN32__ || defined __EMX__ || defined __DJGPP__ |
| 68 |
|
| 69 |
# define ISSLASH(C) ((C) == '/' || (C) == '\\') |
| 70 |
#endif |
| 71 |
|
| 72 |
#ifndef DIRECTORY_SEPARATOR |
| 73 |
# define DIRECTORY_SEPARATOR '/' |
| 74 |
#endif |
| 75 |
|
| 76 |
#ifndef ISSLASH |
| 77 |
# define ISSLASH(C) ((C) == DIRECTORY_SEPARATOR) |
| 78 |
#endif |
| 79 |
|
| 80 |
#ifdef HAVE_GETC_UNLOCKED |
| 81 |
# undef getc |
| 82 |
# define getc getc_unlocked |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifdef __cplusplus |
| 86 |
|
| 87 |
extern "C" const char * locale_charset (void); |
| 88 |
#endif |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
#if __STDC__ != 1 |
| 97 |
# define volatile |
| 98 |
#endif |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static const char * volatile charset_aliases; |
| 103 |
|
| 104 |
|
| 105 |
static const char * |
| 106 |
get_charset_aliases () |
| 107 |
{ |
| 108 |
const char *cp; |
| 109 |
|
| 110 |
cp = charset_aliases; |
| 111 |
if (cp == NULL) |
| 112 |
{ |
| 113 |
#if !defined WIN32 |
| 114 |
FILE *fp; |
| 115 |
const char *dir = LIBDIR; |
| 116 |
const char *base = "charset.alias"; |
| 117 |
char *file_name; |
| 118 |
|
| 119 |
|
| 120 |
{ |
| 121 |
size_t dir_len = strlen (dir); |
| 122 |
size_t base_len = strlen (base); |
| 123 |
int add_slash = (dir_len > 0 && !ISSLASH (dir[dir_len - 1])); |
| 124 |
file_name = (char *) malloc (dir_len + add_slash + base_len + 1); |
| 125 |
if (file_name != NULL) |
| 126 |
{ |
| 127 |
memcpy (file_name, dir, dir_len); |
| 128 |
if (add_slash) |
| 129 |
file_name[dir_len] = DIRECTORY_SEPARATOR; |
| 130 |
memcpy (file_name + dir_len + add_slash, base, base_len + 1); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (file_name == NULL || (fp = fopen (file_name, "r")) == NULL) |
| 135 |
|
| 136 |
cp = ""; |
| 137 |
else |
| 138 |
{ |
| 139 |
|
| 140 |
int c; |
| 141 |
char buf1[50+1]; |
| 142 |
char buf2[50+1]; |
| 143 |
char *res_ptr = NULL; |
| 144 |
size_t res_size = 0; |
| 145 |
size_t l1, l2; |
| 146 |
|
| 147 |
for (;;) |
| 148 |
{ |
| 149 |
c = getc (fp); |
| 150 |
if (c == EOF) |
| 151 |
break; |
| 152 |
if (c == '\n' || c == ' ' || c == '\t') |
| 153 |
continue; |
| 154 |
if (c == '#') |
| 155 |
{ |
| 156 |
|
| 157 |
do |
| 158 |
c = getc (fp); |
| 159 |
while (!(c == EOF || c == '\n')); |
| 160 |
if (c == EOF) |
| 161 |
break; |
| 162 |
continue; |
| 163 |
} |
| 164 |
ungetc (c, fp); |
| 165 |
if (fscanf (fp, "%50s %50s", buf1, buf2) < 2) |
| 166 |
break; |
| 167 |
l1 = strlen (buf1); |
| 168 |
l2 = strlen (buf2); |
| 169 |
if (res_size == 0) |
| 170 |
{ |
| 171 |
res_size = l1 + 1 + l2 + 1; |
| 172 |
res_ptr = (char *) malloc (res_size + 1); |
| 173 |
} |
| 174 |
else |
| 175 |
{ |
| 176 |
res_size += l1 + 1 + l2 + 1; |
| 177 |
res_ptr = (char *) realloc (res_ptr, res_size + 1); |
| 178 |
} |
| 179 |
if (res_ptr == NULL) |
| 180 |
{ |
| 181 |
|
| 182 |
res_size = 0; |
| 183 |
break; |
| 184 |
} |
| 185 |
strcpy (res_ptr + res_size - (l2 + 1) - (l1 + 1), buf1); |
| 186 |
strcpy (res_ptr + res_size - (l2 + 1), buf2); |
| 187 |
} |
| 188 |
fclose (fp); |
| 189 |
if (res_size == 0) |
| 190 |
cp = ""; |
| 191 |
else |
| 192 |
{ |
| 193 |
*(res_ptr + res_size) = '\0'; |
| 194 |
cp = res_ptr; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if (file_name != NULL) |
| 199 |
free (file_name); |
| 200 |
|
| 201 |
#else |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
# if defined WIN32 |
| 208 |
cp = "CP936" "\0" "GBK" "\0" |
| 209 |
"CP1361" "\0" "JOHAB" "\0" |
| 210 |
"CP20127" "\0" "ASCII" "\0" |
| 211 |
"CP20866" "\0" "KOI8-R" "\0" |
| 212 |
"CP21866" "\0" "KOI8-RU" "\0" |
| 213 |
"CP28591" "\0" "ISO-8859-1" "\0" |
| 214 |
"CP28592" "\0" "ISO-8859-2" "\0" |
| 215 |
"CP28593" "\0" "ISO-8859-3" "\0" |
| 216 |
"CP28594" "\0" "ISO-8859-4" "\0" |
| 217 |
"CP28595" "\0" "ISO-8859-5" "\0" |
| 218 |
"CP28596" "\0" "ISO-8859-6" "\0" |
| 219 |
"CP28597" "\0" "ISO-8859-7" "\0" |
| 220 |
"CP28598" "\0" "ISO-8859-8" "\0" |
| 221 |
"CP28599" "\0" "ISO-8859-9" "\0" |
| 222 |
"CP28605" "\0" "ISO-8859-15" "\0"; |
| 223 |
# endif |
| 224 |
#endif |
| 225 |
|
| 226 |
charset_aliases = cp; |
| 227 |
} |
| 228 |
|
| 229 |
return cp; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
#ifdef STATIC |
| 239 |
STATIC |
| 240 |
#endif |
| 241 |
const char * |
| 242 |
locale_charset () |
| 243 |
{ |
| 244 |
const char *codeset; |
| 245 |
const char *aliases; |
| 246 |
|
| 247 |
#if !(defined WIN32 || defined OS2) |
| 248 |
|
| 249 |
# if HAVE_LANGINFO_CODESET |
| 250 |
|
| 251 |
|
| 252 |
codeset = nl_langinfo (CODESET); |
| 253 |
|
| 254 |
# else |
| 255 |
|
| 256 |
|
| 257 |
const char *locale = NULL; |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
# if HAVE_SETLOCALE && 0 |
| 264 |
locale = setlocale (LC_CTYPE, NULL); |
| 265 |
# endif |
| 266 |
if (locale == NULL || locale[0] == '\0') |
| 267 |
{ |
| 268 |
locale = getenv ("LC_ALL"); |
| 269 |
if (locale == NULL || locale[0] == '\0') |
| 270 |
{ |
| 271 |
locale = getenv ("LC_CTYPE"); |
| 272 |
if (locale == NULL || locale[0] == '\0') |
| 273 |
locale = getenv ("LANG"); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
codeset = locale; |
| 281 |
|
| 282 |
# endif |
| 283 |
|
| 284 |
#elif defined WIN32 |
| 285 |
|
| 286 |
static char buf[2 + 10 + 1]; |
| 287 |
|
| 288 |
|
| 289 |
sprintf (buf, "CP%u", GetACP ()); |
| 290 |
codeset = buf; |
| 291 |
|
| 292 |
#elif defined OS2 |
| 293 |
|
| 294 |
const char *locale; |
| 295 |
static char buf[2 + 10 + 1]; |
| 296 |
ULONG cp[3]; |
| 297 |
ULONG cplen; |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
locale = getenv ("LC_ALL"); |
| 302 |
if (locale == NULL || locale[0] == '\0') |
| 303 |
{ |
| 304 |
locale = getenv ("LC_CTYPE"); |
| 305 |
if (locale == NULL || locale[0] == '\0') |
| 306 |
locale = getenv ("LANG"); |
| 307 |
} |
| 308 |
if (locale != NULL && locale[0] != '\0') |
| 309 |
{ |
| 310 |
|
| 311 |
const char *dot = strchr (locale, '.'); |
| 312 |
|
| 313 |
if (dot != NULL) |
| 314 |
{ |
| 315 |
const char *modifier; |
| 316 |
|
| 317 |
dot++; |
| 318 |
|
| 319 |
modifier = strchr (dot, '@'); |
| 320 |
if (modifier == NULL) |
| 321 |
return dot; |
| 322 |
if (modifier - dot < sizeof (buf)) |
| 323 |
{ |
| 324 |
memcpy (buf, dot, modifier - dot); |
| 325 |
buf [modifier - dot] = '\0'; |
| 326 |
return buf; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
codeset = locale; |
| 332 |
} |
| 333 |
else |
| 334 |
{ |
| 335 |
|
| 336 |
if (DosQueryCp (sizeof (cp), cp, &cplen)) |
| 337 |
codeset = ""; |
| 338 |
else |
| 339 |
{ |
| 340 |
sprintf (buf, "CP%u", cp[0]); |
| 341 |
codeset = buf; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
#endif |
| 346 |
|
| 347 |
if (codeset == NULL) |
| 348 |
|
| 349 |
codeset = ""; |
| 350 |
|
| 351 |
|
| 352 |
for (aliases = get_charset_aliases (); |
| 353 |
*aliases != '\0'; |
| 354 |
aliases += strlen (aliases) + 1, aliases += strlen (aliases) + 1) |
| 355 |
if (strcmp (codeset, aliases) == 0 |
| 356 |
|| (aliases[0] == '*' && aliases[1] == '\0')) |
| 357 |
{ |
| 358 |
codeset = aliases + strlen (aliases) + 1; |
| 359 |
break; |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
if (codeset[0] == '\0') |
| 366 |
codeset = "ASCII"; |
| 367 |
|
| 368 |
return codeset; |
| 369 |
} |