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