]> Pileus Git - ~andy/fetchmail/blob - ucs/norm_charmap.c
Rediff patch.
[~andy/fetchmail] / ucs / norm_charmap.c
1 /*
2  * The Single Unix Specification function nl_langinfo(CODESET)
3  * returns the name of the encoding used by the currently selected
4  * locale:
5  *
6  *   http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
7  *
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
12  *
13  *   http://www.iana.org/assignments/character-sets
14  *
15  * Please extend it as needed and suggest improvements to the author.
16  *
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.
21  *
22  * Latest version:
23  *
24  *   http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
25  */
26
27 #include "config.h" /* import AC_C_CONST effects */
28 #include "norm_charmap.h"
29 #include "fetchmail.h"
30
31 #include <string.h>
32
33 #ifdef TEST
34 #include <stdio.h>
35 #include <locale.h>
36 #include <langinfo.h>
37 #endif
38
39 #define digit(x) ((x) >= '0' && (x) <= '9')
40
41 static char buf[16];
42
43 const char *norm_charmap(const char *name)
44 {
45   const char *p;
46   
47   if (!name)
48     return name;
49   
50   /* Many need no remapping, but they are listed here so you
51    * can see what output to expect, and modify for your needs
52    * as necessary. */
53   if (!strcmp(name, "UTF-8"))
54     return "UTF-8";
55   if (!strcmp(name, "EUC-JP"))
56     return "EUC-JP";
57   if (!strcmp(name, "EUC-KR"))
58     return "EUC-KR";
59   if (!strcmp(name, "EUC-TW"))
60     return "EUC-TW";
61   if (!strcmp(name, "KOI8-R"))
62     return "KOI8-R";
63   if (!strcmp(name, "KOI8-U"))
64     return "KOI8-U";
65   if (!strcmp(name, "GBK"))
66     return "GBK";
67   if (!strcmp(name, "GB2312"))
68     return "GB2312";
69   if (!strcmp(name, "GB18030"))
70     return "GB18030";
71   if (!strcmp(name, "VSCII"))
72     return "VSCII";
73   
74   /* ASCII comes in many names */
75   if (!strcmp(name, "ASCII") ||
76       !strcmp(name, "US-ASCII") ||
77       !strcmp(name, "ANSI_X3.4-1968") ||
78       !strcmp(name, "646") ||
79       !strcmp(name, "ISO646") ||
80       !strcmp(name, "ISO_646.IRV"))
81     return "US-ASCII";
82
83   /* ISO 8859 will be converted to "ISO-8859-x" */
84   if ((p = strstr(name, "8859-"))) {
85     memcpy(buf, "ISO-8859-\0\0", 12);
86     p += 5;
87     if (digit(*p)) {
88       buf[9] = *p++;
89       if (digit(*p)) buf[10] = *p;
90       return buf;
91     }
92   }
93
94   /* Windows code pages will be converted to "WINDOWS-12xx" */
95   if ((p = strstr(name, "CP12"))) {
96     memcpy(buf, "WINDOWS-12\0\0", 13);
97     p += 4;
98     if (digit(*p)) {
99       buf[10] = *p++;
100       if (digit(*p)) buf[11] = *p;
101       return buf;
102     }
103   }
104
105   /* TIS-620 comes in at least the following two forms */
106   if (!strcmp(name, "TIS-620") ||
107       !strcmp(name, "TIS620.2533"))
108     return "ISO-8859-11";
109
110   /* For some, uppercase/lowercase might differ */
111   if (!strcmp(name, "Big5") || !strcmp(name, "BIG5"))
112     return "Big5";
113   if (!strcmp(name, "Big5HKSCS") || !strcmp(name, "BIG5HKSCS"))
114     return "Big5HKSCS";
115
116   /* I don't know of any implementation of nl_langinfo(CODESET) out
117    * there that returns anything else (and I'm not even certain all of
118    * the above occur in the wild), but just in case, as a fallback,
119    * return the unmodified name. */
120 #ifdef TEST
121   printf("**** Unknown encoding name '%s'!\n", name);
122 #endif
123   return name;
124 }
125
126 /* For a demo, compile with
127  *
128  *   gcc -W -Wall -o norm_charmap -D TEST norm_charmap.c
129  *
130  * and then test it on all available locales with
131  *
132  *   for i in `locale -a` ; do printf "$i: " ; LC_ALL=$i ./norm_charmap ; done
133  */
134
135 #ifdef TEST
136 int main(int argc, char **argv)
137 {
138   char *s;
139   if (argc > 1)
140     s = argv[1];
141   else {
142     setlocale(LC_CTYPE, "");
143     s = nl_langinfo(CODESET);
144   }
145   printf("%s -> %s\n", s, norm_charmap(s));
146   return 0;
147 }
148 #endif