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