]>
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.
9 #include "check_charset.h"
14 static const char *norm_charmap(char *name
);
17 * Get the current character set
22 return (char *)norm_charmap(nl_langinfo(CODESET
));
27 * Check if we can display a given character set natively.
28 * We are passed the length of the initial part of the
29 * string to check, since we want to allow the name of the
30 * character set to be a substring of a larger string.
34 check_charset (char *str
, int len
)
36 static char *mm_charset
= NULL
;
37 static char *alt_charset
= NULL
;
41 /* Cache the name of our default character set */
43 if (!(mm_charset
= get_charset ()))
44 mm_charset
= "US-ASCII";
45 mm_len
= strlen (mm_charset
);
47 /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
48 if (!strncasecmp("ISO-8859-", mm_charset
, 9) ||
49 !strcasecmp("UTF-8", mm_charset
)) {
50 alt_charset
= "US-ASCII";
51 alt_len
= strlen (alt_charset
);
55 /* Check if character set is OK */
56 if ((len
== mm_len
) && !strncasecmp(str
, mm_charset
, mm_len
))
58 if (alt_charset
&& (len
== alt_len
) && !strncasecmp(str
, alt_charset
, alt_len
))
66 * Return the name of the character set we are
67 * using for 8bit text.
70 write_charset_8bit (void)
72 static char *mm_charset
= NULL
;
75 * Cache the name of the character set to
78 if (!mm_charset
&& !(mm_charset
= get_charset ()))
79 mm_charset
= "x-unknown";
84 /* The Single Unix Specification function nl_langinfo(CODESET)
85 * returns the name of the encoding used by the currently selected
88 * http://www.opengroup.org/onlinepubs/7908799/xsh/langinfo.h.html
90 * Unfortunately the encoding names are not yet standardized.
91 * This function knows about the encoding names used on many
92 * different systems and converts them where possible into
93 * the corresponding MIME charset name registered in
95 * http://www.iana.org/assignments/character-sets
97 * Please extend it as needed and suggest improvements to the author.
99 * Markus.Kuhn@cl.cam.ac.uk -- 2002-03-11
100 * Permission to use, copy, modify, and distribute this software
101 * for any purpose and without fee is hereby granted. The author
102 * disclaims all warranties with regard to this software.
106 * http://www.cl.cam.ac.uk/~mgk25/ucs/norm_charmap.c
110 norm_charmap(char *name
)
112 static const char *correct
[] = {
120 } *ali
, aliases
[] = {
121 /* Names for US-ASCII. */
122 { "ANSI_X3.4-1968", "US-ASCII" }, /* LC_ALL=C. */
123 { "ASCII", "US-ASCII" },
124 { "646", "US-ASCII" },
125 { "ISO646", "US-ASCII" },
126 { "ISO_646.IRV", "US-ASCII" },
129 { "BIG5HKSCS", "Big5HKSCS" },
130 /* Names for ISO-8859-11. */
131 { "TIS-620", "ISO-8859-11" },
132 { "TIS620.2533", "ISO-8859-11" },
138 } *sub
, substrs
[] = {
139 { "8859-1", "ISO-8859-1" },
140 { "8859-2", "ISO-8859-2" },
141 { "8859-3", "ISO-8859-3" },
142 { "8859-4", "ISO-8859-4" },
143 { "8859-5", "ISO-8859-5" },
144 { "8859-6", "ISO-8859-6" },
145 { "8859-7", "ISO-8859-7" },
146 { "8859-8", "ISO-8859-8" },
147 { "8859-9", "ISO-8859-9" },
148 { "8859-10", "ISO-8859-10" },
149 { "8859-11", "ISO-8859-11" },
150 /* 12, Latin/Devanagari, not completed. */
151 { "8859-13", "ISO-8859-13" },
152 { "8859-14", "ISO-8859-14" },
153 { "8859-15", "ISO-8859-15" },
154 { "8859-16", "ISO-8859-16" },
155 { "CP1200", "WINDOWS-1200" },
156 { "CP1201", "WINDOWS-1201" },
157 { "CP1250", "WINDOWS-1250" },
158 { "CP1251", "WINDOWS-1251" },
159 { "CP1252", "WINDOWS-1252" },
160 { "CP1253", "WINDOWS-1253" },
161 { "CP1254", "WINDOWS-1254" },
162 { "CP1255", "WINDOWS-1255" },
163 { "CP1256", "WINDOWS-1256" },
164 { "CP1257", "WINDOWS-1257" },
165 { "CP1258", "WINDOWS-1258" },
172 /* Avoid lots of tests for common correct names. */
173 for (cor
= correct
; *cor
; cor
++)
174 if (!strcmp(name
, *cor
))
177 for (ali
= aliases
; ali
->alias
; ali
++)
178 if (!strcmp(name
, ali
->alias
))
181 for (sub
= substrs
; sub
->substr
; sub
++)
182 if (strstr(name
, sub
->substr
))