2 * The Single Unix Specification function nl_langinfo(CODESET)
3 * returns the name of the encoding used by the currently selected
6 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
8 * Unfortunately the encoding names are not yet standardized.
9 * This function knows about the encoding names used on many
10 * different systems and converts them where possible into
11 * the corresponding MIME charset name registered in
13 * http://www.iana.org/assignments/character-sets
15 * Please extend it as needed and suggest improvements to the author.
17 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
18 * Permission to use, copy, modify, and distribute this software
19 * for any purpose and without fee is hereby granted. The author
20 * disclaims all warranties with regard to this software.
24 * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
35 #define digit(x) ((x) >= '0' && (x) <= '9')
39 char *norm_charmap(char *name)
46 /* Many need no remapping, but they are listed here so you
47 * can see what output to expect, and modify for your needs
49 if (!strcmp(name, "UTF-8"))
51 if (!strcmp(name, "EUC-JP"))
53 if (!strcmp(name, "EUC-KR"))
55 if (!strcmp(name, "EUC-TW"))
57 if (!strcmp(name, "KOI8-R"))
59 if (!strcmp(name, "KOI8-U"))
61 if (!strcmp(name, "GBK"))
63 if (!strcmp(name, "GB2312"))
65 if (!strcmp(name, "GB18030"))
67 if (!strcmp(name, "VSCII"))
70 /* ASCII comes in many names */
71 if (!strcmp(name, "ASCII") ||
72 !strcmp(name, "US-ASCII") ||
73 !strcmp(name, "ANSI_X3.4-1968") ||
74 !strcmp(name, "646") ||
75 !strcmp(name, "ISO646") ||
76 !strcmp(name, "ISO_646.IRV"))
79 /* ISO 8859 will be converted to "ISO-8859-x" */
80 if ((p = strstr(name, "8859-"))) {
81 memcpy(buf, "ISO-8859-\0\0", 12);
85 if (digit(*p)) buf[10] = *p++;
90 /* Windows code pages will be converted to "WINDOWS-12xx" */
91 if ((p = strstr(name, "CP12"))) {
92 memcpy(buf, "WINDOWS-12\0\0", 13);
96 if (digit(*p)) buf[11] = *p++;
101 /* TIS-620 comes in at least the following two forms */
102 if (!strcmp(name, "TIS-620") ||
103 !strcmp(name, "TIS620.2533"))
104 return "ISO-8859-11";
106 /* For some, uppercase/lowercase might differ */
107 if (!strcmp(name, "Big5") || !strcmp(name, "BIG5"))
109 if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS"))
112 /* I don't know of any implementation of nl_langinfo(CODESET) out
113 * there that returns anything else (and I'm not even certain all of
114 * the above occur in the wild), but just in case, as a fallback,
115 * return the unmodified name. */
117 printf("**** Unknown encoding name '%s'!\n", name);
122 /* For a demo, compile with
124 * gcc -W -Wall -o norm_charmap -D TEST norm_charmap.c
126 * and then test it on all available locales with
128 * for i in `locale -a` ; do printf "$i: " ; LC_ALL=$i ./norm_charmap ; done
132 int main(int argc, char **argv)
138 setlocale(LC_CTYPE, "");
139 s = nl_langinfo(CODESET);
141 printf("%s -> %s\n", s, norm_charmap(s));