]> diplodocus.org Git - nmh/blob - sbr/check_charset.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[nmh] / sbr / check_charset.c
1
2 /*
3 * check_charset.c -- routines for character sets
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11
12 #include <langinfo.h>
13
14
15 /*
16 * Get the current character set
17 */
18 char *
19 get_charset ()
20 {
21 return norm_charmap(nl_langinfo (CODESET));
22 }
23
24
25 /*
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.
30 */
31
32 int
33 check_charset (char *str, int len)
34 {
35 static char *mm_charset = NULL;
36 static char *alt_charset = NULL;
37 static int mm_len;
38 static int alt_len;
39
40 /* Cache the name of our default character set */
41 if (!mm_charset) {
42 if (!(mm_charset = get_charset ()))
43 mm_charset = "US-ASCII";
44 mm_len = strlen (mm_charset);
45
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);
51 }
52 }
53
54 /* Check if character set is OK */
55 if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
56 return 1;
57 if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
58 return 1;
59
60 return 0;
61 }
62
63
64 /*
65 * Return the name of the character set we are
66 * using for 8bit text.
67 */
68 char *
69 write_charset_8bit (void)
70 {
71 static char *mm_charset = NULL;
72
73 /*
74 * Cache the name of the character set to
75 * use for 8bit text.
76 */
77 if (!mm_charset && !(mm_charset = get_charset ()))
78 mm_charset = "x-unknown";
79
80 return mm_charset;
81 }