summaryrefslogtreecommitdiff
path: root/lib/charset.c
diff options
context:
space:
mode:
authorGravatar Karl Ramm <kcr@mit.edu>2009-03-16 03:13:50 +0000
committerGravatar Karl Ramm <kcr@mit.edu>2009-03-16 03:13:50 +0000
commit9e44e5ff31421c5ecc4d0c28d98e985b84360669 (patch)
tree75a36857f3e631501cf0b6c55471a9e0678aa262 /lib/charset.c
parent60cd1ff0635fdeda3c5a2cafee2d9a53f82cae0d (diff)
file for charset stuff
Diffstat (limited to 'lib/charset.c')
-rw-r--r--lib/charset.c70
1 files changed, 70 insertions, 0 deletions
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 <internal.h>
+#include <string.h>
+#include <locale.h>
+#include <langinfo.h>
+#include <ctype.h>
+
+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";
+}
+