]> diplodocus.org Git - nmh/blob - sbr/check_charset.c
uip/mhcachesbr.c: Move exported symbols to existing h/mhcachesbr.h.
[nmh] / sbr / check_charset.c
1 /* check_charset.c -- routines for character sets
2 *
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.
6 */
7
8 #include <h/mh.h>
9
10 #include <langinfo.h>
11
12 /* Only get_charset() should use norm_charmap(), so hide its
13 declaration here. */
14 char *norm_charmap(char *);
15
16 /*
17 * Get the current character set
18 */
19 char *
20 get_charset(void)
21 {
22 return norm_charmap(nl_langinfo (CODESET));
23 }
24
25
26 /*
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.
31 */
32
33 int
34 check_charset (char *str, int len)
35 {
36 static char *mm_charset = NULL;
37 static char *alt_charset = NULL;
38 static int mm_len;
39 static int alt_len;
40
41 /* Cache the name of our default character set */
42 if (!mm_charset) {
43 if (!(mm_charset = get_charset ()))
44 mm_charset = "US-ASCII";
45 mm_len = strlen (mm_charset);
46
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);
52 }
53 }
54
55 /* Check if character set is OK */
56 if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
57 return 1;
58 if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
59 return 1;
60
61 return 0;
62 }
63
64
65 /*
66 * Return the name of the character set we are
67 * using for 8bit text.
68 */
69 char *
70 write_charset_8bit (void)
71 {
72 static char *mm_charset = NULL;
73
74 /*
75 * Cache the name of the character set to
76 * use for 8bit text.
77 */
78 if (!mm_charset && !(mm_charset = get_charset ()))
79 mm_charset = "x-unknown";
80
81 return mm_charset;
82 }