Initial revision
[mmh] / sbr / check_charset.c
1
2 /*
3  * check_charset.c -- routines for character sets
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9
10 /*
11  * Check if we can display a given character set natively.
12  * We are passed the length of the initial part of the
13  * string to check, since we want to allow the name of the
14  * character set to be a substring of a larger string.
15  */
16
17 int
18 check_charset (char *str, int len) 
19 {
20     static char *mm_charset = NULL;
21     static char *alt_charset = NULL;
22     static int mm_len;
23     static int alt_len;
24
25     /* Cache the name of our default character set */
26     if (!mm_charset) {
27         if (!(mm_charset = getenv ("MM_CHARSET")))
28             mm_charset = "US-ASCII";
29         mm_len = strlen (mm_charset);
30
31         /* US-ASCII is a subset of the ISO-8859-X character sets */
32         if (!strncasecmp("ISO-8859-", mm_charset, 9)) {
33             alt_charset = "US-ASCII";
34             alt_len = strlen (alt_charset);
35         }
36     }
37
38     /* Check if character set is OK */
39     if ((len == mm_len) && !strncasecmp(str, mm_charset, mm_len))
40         return 1;
41     if (alt_charset && (len == alt_len) && !strncasecmp(str, alt_charset, alt_len))
42         return 1;
43
44     return 0;
45 }
46
47
48 /*
49  * Return the name of the character set we are
50  * using for 8bit text.
51  */
52 char *
53 write_charset_8bit (void)
54 {
55     static char *mm_charset = NULL;
56
57     /*
58      * Cache the name of the character set to
59      * use for 8bit text.
60      */
61     if (!mm_charset && !(mm_charset = getenv ("MM_CHARSET")))
62             mm_charset = "x-unknown";
63
64     return mm_charset;
65 }