Silly of me to overlook it: another obvious way you might like to
[sgt/charset] / locale.c
CommitLineData
8a731dfa 1/*
2 * locale.c: try very hard to figure out the libcharset charset
3 * identifier corresponding to the current C library locale.
4 *
5 * This function works by calling nl_langinfo(CODESET) if
6 * available; failing that, it will try to figure it out manually
7 * by examining the locale environment variables. Code for the
8 * latter is adapted from Markus Kuhn's public-domain
9 * implementation of nl_langinfo(CODESET), available at
10 *
11 * http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c
12 */
13
14#include <stdlib.h>
15#include <string.h>
16#include "charset.h"
17
18#ifdef HAS_LANGINFO_CODESET
19#include "langinfo.h"
20#endif
21
22int charset_from_locale(void)
23{
24 char *l, *p;
25
26#ifdef HAS_LANGINFO_CODESET
27 int charset;
28 char const *csname;
29
30 csname = nl_langinfo(CODESET);
31 if (csname &&
32 (charset = charset_from_localenc(csname)) != CS_NONE)
33 return charset;
34#endif
35
36 if (((l = getenv("LC_ALL")) && *l) ||
37 ((l = getenv("LC_CTYPE")) && *l) ||
38 ((l = getenv("LANG")) && *l)) {
39 /* check standardized locales */
40 if (!strcmp(l, "C") || !strcmp(l, "POSIX"))
41 return CS_ASCII;
42 /* check for encoding name fragment */
43 if (strstr(l, "UTF") || strstr(l, "utf"))
44 return CS_UTF8;
45 if ((p = strstr(l, "8859-"))) {
46 char buf[16];
47 int charset;
48
49 memcpy(buf, "ISO-8859-\0\0", 12);
50 p += 5;
51 if ((*p) >= '0' && (*p) <= '9') {
52 buf[9] = *p++;
53 if ((*p) >= '0' && (*p) <= '9') buf[10] = *p++;
54 if ((charset = charset_from_localenc(buf)) != CS_NONE)
55 return charset;
56 }
57 }
58 if (strstr(l, "KOI8-RU")) return CS_KOI8_RU;
59 if (strstr(l, "KOI8-R")) return CS_KOI8_R;
60 if (strstr(l, "KOI8-U")) return CS_KOI8_U;
61 /* if (strstr(l, "620")) return "TIS-620"; */
62 if (strstr(l, "2312")) return CS_EUC_CN;
63 /* if (strstr(l, "HKSCS")) return "Big5HKSCS"; */
64 if (strstr(l, "Big5") || strstr(l, "BIG5")) return CS_BIG5;
65 /* if (strstr(l, "GBK")) return "GBK"; */
66 /* if (strstr(l, "18030")) return "GB18030"; */
67 if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return CS_SHIFT_JIS;
68 /* check for conclusive modifier */
69 if (strstr(l, "euro")) return CS_ISO8859_15;
70 /* check for language (and perhaps country) codes */
71 if (strstr(l, "zh_TW")) return CS_BIG5;
72 /* if (strstr(l, "zh_HK")) return "Big5HKSCS"; */
73 if (strstr(l, "zh")) return CS_EUC_CN;
74 if (strstr(l, "ja")) return CS_EUC_JP;
75 if (strstr(l, "ko")) return CS_EUC_KR;
76 if (strstr(l, "ru")) return CS_KOI8_R;
77 if (strstr(l, "uk")) return CS_KOI8_U;
78 if (strstr(l, "pl") || strstr(l, "hr") ||
79 strstr(l, "hu") || strstr(l, "cs") ||
80 strstr(l, "sk") || strstr(l, "sl")) return CS_ISO8859_2;
81 if (strstr(l, "eo") || strstr(l, "mt")) return CS_ISO8859_3;
82 if (strstr(l, "el")) return CS_ISO8859_7;
83 if (strstr(l, "he")) return CS_ISO8859_8;
84 if (strstr(l, "tr")) return CS_ISO8859_9;
85 /* if (strstr(l, "th")) return "TIS-620"; */
86 if (strstr(l, "lt")) return CS_ISO8859_13;
87 if (strstr(l, "cy")) return CS_ISO8859_14;
88 if (strstr(l, "ro")) return CS_ISO8859_2; /* or ISO-8859-16 */
89 if (strstr(l, "am") || strstr(l, "vi")) return CS_UTF8;
90 return CS_ISO8859_1;
91 }
92 return CS_ASCII;
93}