]> diplodocus.org Git - nmh/blob - sbr/check_charset.c
Formatting cleanup.
[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 #ifdef HAVE_LANGINFO_H
12 # include <langinfo.h>
13 #endif
14
15
16 /*
17 * Get the current character set
18 */
19 char *
20 get_charset ()
21 {
22 char *charset = getenv ("MM_CHARSET");
23 #if defined(HAVE_NL_LANGINFO) && defined(CODESET)
24 if (!charset)
25 charset = norm_charmap(nl_langinfo (CODESET));
26 #endif
27 return charset;
28 }
29
30
31 /*
32 * Check if we can display a given character set natively.
33 * We are passed the length of the initial part of the
34 * string to check, since we want to allow the name of the
35 * character set to be a substring of a larger string.
36 */
37
38 int
39 check_charset (char *str, int len)
40 {
41 static char *mm_charset = NULL;
42 static char *alt_charset = NULL;
43 static int mm_len;
44 static int alt_len;
45
46 /* Cache the name of our default character set */
47 if (!mm_charset) {
48 if (!(mm_charset = get_charset ()))
49 mm_charset = "US-ASCII";
50 mm_len = strlen (mm_charset);
51
52 /* US-ASCII is a subset of the ISO-8859-X and UTF-8 character sets */
53 if (!strncasecmp("ISO-8859-", mm_charset, 9) ||
54 !mh_strcasecmp("UTF-8", mm_charset)) {
55 alt_charset = "US-ASCII";
56 alt_len = strlen (alt_charset);
57 }
58 }
59
60 /* Check if character set is OK */
61 if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
62 return 1;
63 if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
64 return 1;
65
66 return 0;
67 }
68
69
70 /*
71 * Return the name of the character set we are
72 * using for 8bit text.
73 */
74 char *
75 write_charset_8bit (void)
76 {
77 static char *mm_charset = NULL;
78
79 /*
80 * Cache the name of the character set to
81 * use for 8bit text.
82 */
83 if (!mm_charset && !(mm_charset = get_charset ()))
84 mm_charset = "x-unknown";
85
86 return mm_charset;
87 }