]> diplodocus.org Git - nmh/blob - sbr/check_charset.c
a972834b7ee214197a1ba91091738600642bc287
[nmh] / sbr / check_charset.c
1 /*
2 * check_charset.c -- routines for character sets
3 *
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
7 */
8
9 #include <h/mh.h>
10
11 #include <langinfo.h>
12
13 /* Only get_charset() should use norm_charmap(), so hide its
14 declaration here. */
15 char *norm_charmap(char *);
16
17 /*
18 * Get the current character set
19 */
20 char *
21 get_charset ()
22 {
23 return norm_charmap(nl_langinfo (CODESET));
24 }
25
26
27 /*
28 * Check if we can display a given character set natively.
29 * We are passed the length of the initial part of the
30 * string to check, since we want to allow the name of the
31 * character set to be a substring of a larger string.
32 */
33
34 int
35 check_charset (char *str, int len)
36 {
37 static char *mm_charset = NULL;
38 static char *alt_charset = NULL;
39 static int mm_len;
40 static int alt_len;
41
42 /* Cache the name of our default character set */
43 if (!mm_charset) {
44 if (!(mm_charset = get_charset ()))
45 mm_charset = "US-ASCII";
46 mm_len = strlen (mm_charset);
47
48 /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
49 if (!strncasecmp("ISO-8859-", mm_charset, 9) ||
50 !strcasecmp("UTF-8", mm_charset)) {
51 alt_charset = "US-ASCII";
52 alt_len = strlen (alt_charset);
53 }
54 }
55
56 /* Check if character set is OK */
57 if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
58 return 1;
59 if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
60 return 1;
61
62 return 0;
63 }
64
65
66 /*
67 * Return the name of the character set we are
68 * using for 8bit text.
69 */
70 char *
71 write_charset_8bit (void)
72 {
73 static char *mm_charset = NULL;
74
75 /*
76 * Cache the name of the character set to
77 * use for 8bit text.
78 */
79 if (!mm_charset && !(mm_charset = get_charset ()))
80 mm_charset = "x-unknown";
81
82 return mm_charset;
83 }