| 1 |
/* |
| 2 |
* This is a quick-and-dirty emulator of the nl_langinfo(CODESET) |
| 3 |
* function defined in the Single Unix Specification for those systems |
| 4 |
* (FreeBSD, etc.) that don't have one yet. It behaves as if it had |
| 5 |
* been called after setlocale(LC_CTYPE, ""), that is it looks at |
| 6 |
* the locale environment variables. |
| 7 |
* |
| 8 |
* http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html |
| 9 |
* |
| 10 |
* Please extend it as needed and suggest improvements to the author. |
| 11 |
* This emulator will hopefully become redundant soon as |
| 12 |
* nl_langinfo(CODESET) becomes more widely implemented. |
| 13 |
* |
| 14 |
* Since the proposed Li18nux encoding name registry is still not mature, |
| 15 |
* the output follows the MIME registry where possible: |
| 16 |
* |
| 17 |
* http://www.iana.org/assignments/character-sets |
| 18 |
* |
| 19 |
* A possible autoconf test for the availability of nl_langinfo(CODESET) |
| 20 |
* can be found in |
| 21 |
* |
| 22 |
* http://www.cl.cam.ac.uk/~mgk25/unicode.html#activate |
| 23 |
* |
| 24 |
* [email protected] -- 2002-03-11 |
| 25 |
* Permission to use, copy, modify, and distribute this software |
| 26 |
* for any purpose and without fee is hereby granted. The author |
| 27 |
* disclaims all warranties with regard to this software. |
| 28 |
* |
| 29 |
* Latest version: |
| 30 |
* |
| 31 |
* http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c |
| 32 |
* |
| 33 |
* LifeLines Modifications: |
| 34 |
* |
| 35 |
* 2005/01/30: nl_langinfo() should return an empty string, not NULL |
| 36 |
* in the error case. This is implemented via the NONE |
| 37 |
* macro. |
| 38 |
*/ |
| 39 |
|
| 40 |
#include <stdlib.h> |
| 41 |
#include <string.h> |
| 42 |
#include "langinfz.h" |
| 43 |
|
| 44 |
#define C_CODESET "US-ASCII" /* Return this as the encoding of the |
| 45 |
* C/POSIX locale. Could as well one day |
| 46 |
* become "UTF-8". */ |
| 47 |
#define NONE "" |
| 48 |
|
| 49 |
#define digit(x) ((x) >= '0' && (x) <= '9') |
| 50 |
|
| 51 |
static char buf[16]; |
| 52 |
|
| 53 |
char *nl_langinfo(nl_item item) |
| 54 |
{ |
| 55 |
char *l, *p; |
| 56 |
|
| 57 |
if (item != CODESET) |
| 58 |
return NONE; |
| 59 |
|
| 60 |
if (((l = getenv("LC_ALL")) && *l) || |
| 61 |
((l = getenv("LC_CTYPE")) && *l) || |
| 62 |
((l = getenv("LANG")) && *l)) { |
| 63 |
/* check standardized locales */ |
| 64 |
if (!strcmp(l, "C") || !strcmp(l, "POSIX")) |
| 65 |
return C_CODESET; |
| 66 |
/* check for encoding name fragment */ |
| 67 |
if (strstr(l, "UTF") || strstr(l, "utf")) |
| 68 |
return "UTF-8"; |
| 69 |
if ((p = strstr(l, "8859-"))) { |
| 70 |
memcpy(buf, "ISO-8859-\0\0", 12); |
| 71 |
p += 5; |
| 72 |
if (digit(*p)) { |
| 73 |
buf[9] = *p++; |
| 74 |
if (digit(*p)) buf[10] = *p++; |
| 75 |
return buf; |
| 76 |
} |
| 77 |
} |
| 78 |
if (strstr(l, "KOI8-R")) return "KOI8-R"; |
| 79 |
if (strstr(l, "KOI8-U")) return "KOI8-U"; |
| 80 |
if (strstr(l, "620")) return "TIS-620"; |
| 81 |
if (strstr(l, "2312")) return "GB2312"; |
| 82 |
if (strstr(l, "HKSCS")) return "Big5HKSCS"; /* no MIME charset */ |
| 83 |
if (strstr(l, "Big5") || strstr(l, "BIG5")) return "Big5"; |
| 84 |
if (strstr(l, "GBK")) return "GBK"; /* no MIME charset */ |
| 85 |
if (strstr(l, "18030")) return "GB18030"; /* no MIME charset */ |
| 86 |
if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return "Shift_JIS"; |
| 87 |
/* check for conclusive modifier */ |
| 88 |
if (strstr(l, "euro")) return "ISO-8859-15"; |
| 89 |
/* check for language (and perhaps country) codes */ |
| 90 |
if (strstr(l, "zh_TW")) return "Big5"; |
| 91 |
if (strstr(l, "zh_HK")) return "Big5HKSCS"; /* no MIME charset */ |
| 92 |
if (strstr(l, "zh")) return "GB2312"; |
| 93 |
if (strstr(l, "ja")) return "EUC-JP"; |
| 94 |
if (strstr(l, "ko")) return "EUC-KR"; |
| 95 |
if (strstr(l, "ru")) return "KOI8-R"; |
| 96 |
if (strstr(l, "uk")) return "KOI8-U"; |
| 97 |
if (strstr(l, "pl") || strstr(l, "hr") || |
| 98 |
strstr(l, "hu") || strstr(l, "cs") || |
| 99 |
strstr(l, "sk") || strstr(l, "sl")) return "ISO-8859-2"; |
| 100 |
if (strstr(l, "eo") || strstr(l, "mt")) return "ISO-8859-3"; |
| 101 |
if (strstr(l, "el")) return "ISO-8859-7"; |
| 102 |
if (strstr(l, "he")) return "ISO-8859-8"; |
| 103 |
if (strstr(l, "tr")) return "ISO-8859-9"; |
| 104 |
if (strstr(l, "th")) return "TIS-620"; /* or ISO-8859-11 */ |
| 105 |
if (strstr(l, "lt")) return "ISO-8859-13"; |
| 106 |
if (strstr(l, "cy")) return "ISO-8859-14"; |
| 107 |
if (strstr(l, "ro")) return "ISO-8859-2"; /* or ISO-8859-16 */ |
| 108 |
if (strstr(l, "am") || strstr(l, "vi")) return "UTF-8"; |
| 109 |
/* Send me further rules if you like, but don't forget that we are |
| 110 |
* *only* interested in locale naming conventions on platforms |
| 111 |
* that do not already provide an nl_langinfo(CODESET) implementation. */ |
| 112 |
return "ISO-8859-1"; /* should perhaps be "UTF-8" instead */ |
| 113 |
} |
| 114 |
return C_CODESET; |
| 115 |
} |
| 116 |
|
| 117 |
/* For a demo, compile with "gcc -W -Wall -o langinfo -D TEST langinfo.c" */ |
| 118 |
|
| 119 |
#ifdef TEST |
| 120 |
#include <stdio.h> |
| 121 |
int main() |
| 122 |
{ |
| 123 |
printf("%s\n", nl_langinfo(CODESET)); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
#endif |