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