From 9e44e5ff31421c5ecc4d0c28d98e985b84360669 Mon Sep 17 00:00:00 2001 From: Karl Ramm Date: Mon, 16 Mar 2009 03:13:50 +0000 Subject: file for charset stuff --- lib/charset.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/charset.c (limited to 'lib/charset.c') diff --git a/lib/charset.c b/lib/charset.c new file mode 100644 index 0000000..19dfb7e --- /dev/null +++ b/lib/charset.c @@ -0,0 +1,70 @@ +/* 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 +#include +#include +#include +#include + +unsigned short +ZGetCharset(char *charset) +{ + char *p; + short retval; + + if (charset == NULL) + charset = getenv("ZEPHYR_CHARSET"); + + if (charset == NULL) { + setlocale(LC_ALL, ""); + 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"; +} + -- cgit v1.2.3