diff options
Diffstat (limited to 'ucs')
-rw-r--r-- | ucs/langinfo.c | 119 | ||||
-rw-r--r-- | ucs/norm_charmap.c | 144 |
2 files changed, 263 insertions, 0 deletions
diff --git a/ucs/langinfo.c b/ucs/langinfo.c new file mode 100644 index 00000000..3d7d64d7 --- /dev/null +++ b/ucs/langinfo.c @@ -0,0 +1,119 @@ +/* + * This is a quick-and-dirty emulator of the nl_langinfo(CODESET) + * function defined in the Single Unix Specification for those systems + * (FreeBSD, etc.) that don't have one yet. It behaves as if it had + * been called after setlocale(LC_CTYPE, ""), that is it looks at + * the locale environment variables. + * + * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html + * + * Please extend it as needed and suggest improvements to the author. + * This emulator will hopefully become redundant soon as + * nl_langinfo(CODESET) becomes more widely implemented. + * + * Since the proposed Li18nux encoding name registry is still not mature, + * the output follows the MIME registry where possible: + * + * http://www.iana.org/assignments/character-sets + * + * A possible autoconf test for the availability of nl_langinfo(CODESET) + * can be found in + * + * http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate + * + * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11 + * Permission to use, copy, modify, and distribute this software + * for any purpose and without fee is hereby granted. The author + * disclaims all warranties with regard to this software. + * + * Latest version: + * + * http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c + */ + +#include <stdlib.h> +#include <string.h> +#include "langinfo.h" + +#define C_CODESET "US-ASCII" /* Return this as the encoding of the + * C/POSIX locale. Could as well one day + * become "UTF-8". */ + +#define digit(x) ((x) >= '0' && (x) <= '9') + +static char buf[16]; + +char *nl_langinfo(nl_item item) +{ + char *l, *p; + + if (item != CODESET) + return NULL; + + if (((l = getenv("LC_ALL")) && *l) || + ((l = getenv("LC_CTYPE")) && *l) || + ((l = getenv("LANG")) && *l)) { + /* check standardized locales */ + if (!strcmp(l, "C") || !strcmp(l, "POSIX")) + return C_CODESET; + /* check for encoding name fragment */ + if (strstr(l, "UTF") || strstr(l, "utf")) + return "UTF-8"; + if ((p = strstr(l, "8859-"))) { + memcpy(buf, "ISO-8859-\0\0", 12); + p += 5; + if (digit(*p)) { + buf[9] = *p++; + if (digit(*p)) buf[10] = *p++; + return buf; + } + } + if (strstr(l, "KOI8-R")) return "KOI8-R"; + if (strstr(l, "KOI8-U")) return "KOI8-U"; + if (strstr(l, "620")) return "TIS-620"; + if (strstr(l, "2312")) return "GB2312"; + if (strstr(l, "HKSCS")) return "Big5HKSCS"; /* no MIME charset */ + if (strstr(l, "Big5") || strstr(l, "BIG5")) return "Big5"; + if (strstr(l, "GBK")) return "GBK"; /* no MIME charset */ + if (strstr(l, "18030")) return "GB18030"; /* no MIME charset */ + if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return "Shift_JIS"; + /* check for conclusive modifier */ + if (strstr(l, "euro")) return "ISO-8859-15"; + /* check for language (and perhaps country) codes */ + if (strstr(l, "zh_TW")) return "Big5"; + if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */ + if (strstr(l, "zh")) return "GB2312"; + if (strstr(l, "ja")) return "EUC-JP"; + if (strstr(l, "ko")) return "EUC-KR"; + if (strstr(l, "ru")) return "KOI8-R"; + if (strstr(l, "uk")) return "KOI8-U"; + if (strstr(l, "pl") || strstr(l, "hr") || + strstr(l, "hu") || strstr(l, "cs") || + strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2"; + if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3"; + if (strstr(l, "el")) return "ISO-8859-7"; + if (strstr(l, "he")) return "ISO-8859-8"; + if (strstr(l, "tr")) return "ISO-8859-9"; + if (strstr(l, "th")) return "TIS-620"; /* or ISO-8859-11 */ + if (strstr(l, "lt")) return "ISO-8859-13"; + if (strstr(l, "cy")) return "ISO-8859-14"; + if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */ + if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8"; + /* Send me further rules if you like, but don't forget that we are + * *only* interested in locale naming conventions on platforms + * that do not already provide an nl_langinfo(CODESET) implementation. */ + return "ISO-8859-1"; /* should perhaps be "UTF-8" instead */ + } + return C_CODESET; +} + +/* For a demo, compile with "gcc -W -Wall -o langinfo -D TEST langinfo.c" */ + +#ifdef TEST +#include <stdio.h> +int main() +{ + printf("%s\n", nl_langinfo(CODESET)); + return 0; +} +#endif diff --git a/ucs/norm_charmap.c b/ucs/norm_charmap.c new file mode 100644 index 00000000..30204871 --- /dev/null +++ b/ucs/norm_charmap.c @@ -0,0 +1,144 @@ +/* + * The Single Unix Specification function nl_langinfo(CODESET) + * returns the name of the encoding used by the currently selected + * locale: + * + * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html + * + * Unfortunately the encoding names are not yet standardized. + * This function knows about the encoding names used on many + * different systems and converts them where possible into + * the corresponding MIME charset name registered in + * + * http://www.iana.org/assignments/character-sets + * + * Please extend it as needed and suggest improvements to the author. + * + * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11 + * Permission to use, copy, modify, and distribute this software + * for any purpose and without fee is hereby granted. The author + * disclaims all warranties with regard to this software. + * + * Latest version: + * + * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c + */ + +#include <string.h> + +#ifdef TEST +#include <stdio.h> +#include <locale.h> +#include <langinfo.h> +#endif + +#define digit(x) ((x) >= '0' && (x) <= '9') + +static char buf[16]; + +char *norm_charmap(char *name) +{ + char *p; + + if (!name) + return name; + + /* Many need no remapping, but they are listed here so you + * can see what output to expect, and modify for your needs + * as necessary. */ + if (!strcmp(name, "UTF-8")) + return "UTF-8"; + if (!strcmp(name, "EUC-JP")) + return "EUC-JP"; + if (!strcmp(name, "EUC-KR")) + return "EUC-KR"; + if (!strcmp(name, "EUC-TW")) + return "EUC-TW"; + if (!strcmp(name, "KOI8-R")) + return "KOI8-R"; + if (!strcmp(name, "KOI8-U")) + return "KOI8-U"; + if (!strcmp(name, "GBK")) + return "GBK"; + if (!strcmp(name, "GB2312")) + return "GB2312"; + if (!strcmp(name, "GB18030")) + return "GB18030"; + if (!strcmp(name, "VSCII")) + return "VSCII"; + + /* ASCII comes in many names */ + if (!strcmp(name, "ASCII") || + !strcmp(name, "US-ASCII") || + !strcmp(name, "ANSI_X3.4-1968") || + !strcmp(name, "646") || + !strcmp(name, "ISO646") || + !strcmp(name, "ISO_646.IRV")) + return "US-ASCII"; + + /* ISO 8859 will be converted to "ISO-8859-x" */ + if ((p = strstr(name, "8859-"))) { + memcpy(buf, "ISO-8859-\0\0", 12); + p += 5; + if (digit(*p)) { + buf[9] = *p++; + if (digit(*p)) buf[10] = *p++; + return buf; + } + } + + /* Windows code pages will be converted to "WINDOWS-12xx" */ + if ((p = strstr(name, "CP12"))) { + memcpy(buf, "WINDOWS-12\0\0", 13); + p += 4; + if (digit(*p)) { + buf[10] = *p++; + if (digit(*p)) buf[11] = *p++; + return buf; + } + } + + /* TIS-620 comes in at least the following two forms */ + if (!strcmp(name, "TIS-620") || + !strcmp(name, "TIS620.2533")) + return "ISO-8859-11"; + + /* For some, uppercase/lowercase might differ */ + if (!strcmp(name, "Big5") || !strcmp(name, "BIG5")) + return "Big5"; + if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS")) + return "Big5HKSCS"; + + /* I don't know of any implementation of nl_langinfo(CODESET) out + * there that returns anything else (and I'm not even certain all of + * the above occur in the wild), but just in case, as a fallback, + * return the unmodified name. */ +#ifdef TEST + printf("**** Unknown encoding name '%s'!\n", name); +#endif + return name; +} + +/* For a demo, compile with + * + * gcc -W -Wall -o norm_charmap -D TEST norm_charmap.c + * + * and then test it on all available locales with + * + * for i in `locale -a` ; do printf "$i: " ; LC_ALL=$i ./norm_charmap ; done + */ + +#ifdef TEST +int main(int argc, char **argv) +{ + char *s; + if (argc > 1) + s = argv[1]; + else { + setlocale(LC_CTYPE, ""); + s = nl_langinfo(CODESET); + } + printf("%s -> %s\n", s, norm_charmap(s)); + return 0; +} +#endif |