]>
diplodocus.org Git - nmh/blob - sbr/check_charset.c
1 /* check_charset.c -- routines for character sets
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
13 static const char *norm_charmap(char *name
);
16 * Get the current character set
21 return (char *)norm_charmap(nl_langinfo(CODESET
));
26 * Check if we can display a given character set natively.
27 * We are passed the length of the initial part of the
28 * string to check, since we want to allow the name of the
29 * character set to be a substring of a larger string.
33 check_charset (char *str
, int len
)
35 static char *mm_charset
= NULL
;
36 static char *alt_charset
= NULL
;
40 /* Cache the name of our default character set */
42 if (!(mm_charset
= get_charset ()))
43 mm_charset
= "US-ASCII";
44 mm_len
= strlen (mm_charset
);
46 /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
47 if (!strncasecmp("ISO-8859-", mm_charset
, 9) ||
48 !strcasecmp("UTF-8", mm_charset
)) {
49 alt_charset
= "US-ASCII";
50 alt_len
= strlen (alt_charset
);
54 /* Check if character set is OK */
55 if ((len
== mm_len
) && !strncasecmp(str
, mm_charset
, mm_len
))
57 if (alt_charset
&& (len
== alt_len
) && !strncasecmp(str
, alt_charset
, alt_len
))
65 * Return the name of the character set we are
66 * using for 8bit text.
69 write_charset_8bit (void)
71 static char *mm_charset
= NULL
;
74 * Cache the name of the character set to
77 if (!mm_charset
&& !(mm_charset
= get_charset ()))
78 mm_charset
= "x-unknown";
83 /* The Single Unix Specification function nl_langinfo(CODESET)
84 * returns the name of the encoding used by the currently selected
87 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
89 * Unfortunately the encoding names are not yet standardized.
90 * This function knows about the encoding names used on many
91 * different systems and converts them where possible into
92 * the corresponding MIME charset name registered in
94 * http://www.iana.org/assignments/character-sets
96 * Please extend it as needed and suggest improvements to the author.
98 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
99 * Permission to use, copy, modify, and distribute this software
100 * for any purpose and without fee is hereby granted. The author
101 * disclaims all warranties with regard to this software.
105 * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
108 static const char *norm_charmap(char *name
)
110 static const char *correct
[] = {
118 } *ali
, aliases
[] = {
119 /* Names for US-ASCII. */
120 { "ANSI_X3.4-1968", "US-ASCII" }, /* LC_ALL=C. */
121 { "ASCII", "US-ASCII" },
122 { "646", "US-ASCII" },
123 { "ISO646", "US-ASCII" },
124 { "ISO_646.IRV", "US-ASCII" },
127 { "BIG5HKSCS", "Big5HKSCS" },
128 /* Names for ISO-8859-11. */
129 { "TIS-620", "ISO-8859-11" },
130 { "TIS620.2533", "ISO-8859-11" },
136 } *sub
, substrs
[] = {
137 { "8859-1", "ISO-8859-1" },
138 { "8859-2", "ISO-8859-2" },
139 { "8859-3", "ISO-8859-3" },
140 { "8859-4", "ISO-8859-4" },
141 { "8859-5", "ISO-8859-5" },
142 { "8859-6", "ISO-8859-6" },
143 { "8859-7", "ISO-8859-7" },
144 { "8859-8", "ISO-8859-8" },
145 { "8859-9", "ISO-8859-9" },
146 { "8859-10", "ISO-8859-10" },
147 { "8859-11", "ISO-8859-11" },
148 /* 12, Latin/Devanagari, not completed. */
149 { "8859-13", "ISO-8859-13" },
150 { "8859-14", "ISO-8859-14" },
151 { "8859-15", "ISO-8859-15" },
152 { "8859-16", "ISO-8859-16" },
153 { "CP1200", "WINDOWS-1200" },
154 { "CP1201", "WINDOWS-1201" },
155 { "CP1250", "WINDOWS-1250" },
156 { "CP1251", "WINDOWS-1251" },
157 { "CP1252", "WINDOWS-1252" },
158 { "CP1253", "WINDOWS-1253" },
159 { "CP1254", "WINDOWS-1254" },
160 { "CP1255", "WINDOWS-1255" },
161 { "CP1256", "WINDOWS-1256" },
162 { "CP1257", "WINDOWS-1257" },
163 { "CP1258", "WINDOWS-1258" },
170 /* Avoid lots of tests for common correct names. */
171 for (cor
= correct
; *cor
; cor
++)
172 if (!strcmp(name
, *cor
))
175 for (ali
= aliases
; ali
->alias
; ali
++)
176 if (!strcmp(name
, ali
->alias
))
179 for (sub
= substrs
; sub
->substr
; sub
++)
180 if (strstr(name
, sub
->substr
))