1 /* -*- c-file-style: "gnu" -*- */
3 * This is a quick-and-dirty emulator of the nl_langinfo(CODESET)
4 * function defined in the Single Unix Specification for those systems
5 * (FreeBSD, etc.) that don't have one yet. It behaves as if it had
6 * been called after setlocale(LC_CTYPE, ""), that is it looks at
7 * the locale environment variables.
9 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
11 * Please extend it as needed and suggest improvements to the author.
12 * This emulator will hopefully become redundant soon as
13 * nl_langinfo(CODESET) becomes more widely implemented.
15 * Since the proposed Li18nux encoding name registry is still not mature,
16 * the output follows the MIME registry where possible:
18 * http://www.iana.org/assignments/character-sets
20 * A possible autoconf test for the availability of nl_langinfo(CODESET)
23 * http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate
25 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
26 * Permission to use, copy, modify, and distribute this software
27 * for any purpose and without fee is hereby granted. The author
28 * disclaims all warranties with regard to this software.
32 * http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c
35 #include "ruby/missing.h"
38 #if defined _WIN32 || defined __CYGWIN__
40 #if defined _WIN32 && !defined strncasecmp
41 #define strncasecmp strnicmp
44 #ifdef HAVE_LANGINFO_H
48 #define C_CODESET "US-ASCII" /* Return this as the encoding of the
49 * C/POSIX locale. Could as well one day
52 #if defined _WIN32 || defined __CYGWIN__
53 #define JA_CODESET "Windows-31J"
55 #define JA_CODESET "EUC-JP"
58 #define digit(x) ((x) >= '0' && (x) <= '9')
59 #define strstart(s, n) (strncasecmp((s), (n), strlen(n)) == 0)
64 nl_langinfo_codeset(void)
69 if (((l
= getenv("LC_ALL")) && *l
) ||
70 ((l
= getenv("LC_CTYPE")) && *l
) ||
71 ((l
= getenv("LANG")) && *l
)) {
72 /* check standardized locales */
73 if (!strcmp(l
, "C") || !strcmp(l
, "POSIX"))
75 /* check for encoding name fragment */
78 if (strstart(p
, "UTF"))
80 if ((n
= 5, strstart(p
, "8859-")) || (n
= 9, strstart(p
, "ISO-8859-"))) {
83 memcpy(buf
, "ISO-8859-\0\0", 12);
85 if (digit(*p
)) buf
[10] = *p
++;
89 if (strstart(p
, "KOI8-R")) return "KOI8-R";
90 if (strstart(p
, "KOI8-U")) return "KOI8-U";
91 if (strstart(p
, "620")) return "TIS-620";
92 if (strstart(p
, "2312")) return "GB2312";
93 if (strstart(p
, "HKSCS")) return "Big5HKSCS"; /* no MIME charset */
94 if (strstart(p
, "BIG5")) return "Big5";
95 if (strstart(p
, "GBK")) return "GBK"; /* no MIME charset */
96 if (strstart(p
, "18030")) return "GB18030"; /* no MIME charset */
97 if (strstart(p
, "Shift_JIS") || strstart(p
, "SJIS")) return "Windows-31J";
98 /* check for conclusive modifier */
99 if (strstart(p
, "euro")) return "ISO-8859-15";
100 /* check for language (and perhaps country) codes */
101 if (strstart(l
, "zh_TW")) return "Big5";
102 if (strstart(l
, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */
103 if (strstart(l
, "zh")) return "GB2312";
104 if (strstart(l
, "ja")) return JA_CODESET
;
105 if (strstart(l
, "ko")) return "EUC-KR";
106 if (strstart(l
, "ru")) return "KOI8-R";
107 if (strstart(l
, "uk")) return "KOI8-U";
108 if (strstart(l
, "pl") || strstart(l
, "hr") ||
109 strstart(l
, "hu") || strstart(l
, "cs") ||
110 strstart(l
, "sk") || strstart(l
, "sl")) return "ISO-8859-2";
111 if (strstart(l
, "eo") || strstart(l
, "mt")) return "ISO-8859-3";
112 if (strstart(l
, "el")) return "ISO-8859-7";
113 if (strstart(l
, "he")) return "ISO-8859-8";
114 if (strstart(l
, "tr")) return "ISO-8859-9";
115 if (strstart(l
, "th")) return "TIS-620"; /* or ISO-8859-11 */
116 if (strstart(l
, "lt")) return "ISO-8859-13";
117 if (strstart(l
, "cy")) return "ISO-8859-14";
118 if (strstart(l
, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */
119 if (strstart(l
, "am") || strstart(l
, "vi")) return "UTF-8";
120 /* Send me further rules if you like, but don't forget that we are
121 * *only* interested in locale naming conventions on platforms
122 * that do not already provide an nl_langinfo(CODESET) implementation. */
127 #ifdef HAVE_LANGINFO_H
128 char *nl_langinfo(nl_item item
)
133 codeset
= nl_langinfo_codeset();
134 if (!codeset
) codeset
= C_CODESET
;
135 return (char *)codeset
;
139 /* For a demo, compile with "gcc -W -Wall -o langinfo -D TEST langinfo.c" */
145 printf("%s\n", nl_langinfo(CODESET
));