summaryrefslogtreecommitdiff
path: root/lib/charset.c
blob: d1f64fc1489694738ea8a24f42eb2fd5ef04c309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains source for the ZGetCharset function.
 *
 *	Created by:	Karl Ramm
 *
 *	$Id$
 *
 *	Copyright (c) 2009 by the Massachusetts Institute of Technology.
 *	For copying and distribution information, see the file
 *	"mit-copyright.h". 
 */

#ifndef lint
static const char rcsid_charset_c[] = "$Id$";
#endif /* lint */

#include <internal.h>
#include <string.h>
#include <locale.h>
#include <langinfo.h>
#include <ctype.h>

unsigned short
ZGetCharset(char *charset)
{
    char *p;
    short retval;
    static int once = 1;
	
    if (charset == NULL)
	charset = getenv("ZEPHYR_CHARSET");

    if (charset == NULL) {
	if (once) {
	    setlocale(LC_ALL, "");
	    once = 0;
	}
	charset = nl_langinfo(CODESET);
    }

    if (charset == NULL)
	return ZCHARSET_UNKNOWN;

    charset = strdup(charset);

    for (p = charset; *p; p++)
	*p = toupper(*p);

    if (!strcmp(charset, "NONE") || !strcmp(charset, "UNKNOWN"))
	retval = ZCHARSET_UNKNOWN;
    else if (!strcmp(charset, "ANSI_X3.4-1968"))
	retval = ZCHARSET_ISO_8859_1; /* A hack. */
    else if (!strcmp(charset, "ISO-8859-1"))
	retval = ZCHARSET_ISO_8859_1;
    else if (!strcmp(charset, "UTF-8"))
	retval = ZCHARSET_UTF_8;
    else
	retval = ZCHARSET_UNKNOWN;

    free(charset);
    return retval;
}

const char *
ZCharsetToString(unsigned short charset)
{
    if (charset == ZCHARSET_UNKNOWN)
	return "UNKNOWN";
    else if (charset == ZCHARSET_ISO_8859_1)
	return "ISO-8859-1";
    else if (charset == ZCHARSET_UTF_8)
	return "UTF-8";
    return "UNKNOWN";
}