From 8a731dfa62b17746dca94840c6d81c126af45ed4 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 22 Apr 2004 17:27:57 +0000 Subject: [PATCH] Add charset_from_locale(), a best-effort attempt to return the libcharset CS_* identifier for the character set indicated by the active locale. Uses code from Markus Kuhn's website. git-svn-id: svn://svn.tartarus.org/sgt/charset@4115 cda61777-01e9-0310-a592-d414129be87e --- Makefile | 5 ++++ charset.h | 11 ++++++++ locale.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 locale.c diff --git a/Makefile b/Makefile index da58271..bf937c4 100644 --- a/Makefile +++ b/Makefile @@ -57,6 +57,7 @@ LIBCHARSET_OBJS = \ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)jisx0208.o \ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)jisx0212.o \ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)ksx1001.o \ + $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)locale.o \ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)localenc.o \ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)macenc.o \ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)mimeenc.o \ @@ -123,6 +124,10 @@ $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)ksx1001.o: \ $(LIBCHARSET_SRCDIR)ksx1001.c $(CC) $(CFLAGS) $(MD) -c -o $@ $< +$(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)locale.o: \ + $(LIBCHARSET_SRCDIR)locale.c + $(CC) $(CFLAGS) $(MD) -c -o $@ $< + $(LIBCHARSET_OBJDIR)$(LIBCHARSET_OBJPFX)localenc.o: \ $(LIBCHARSET_SRCDIR)localenc.c $(CC) $(CFLAGS) $(MD) -c -o $@ $< diff --git a/charset.h b/charset.h index 3880fbd..883dcfb 100644 --- a/charset.h +++ b/charset.h @@ -207,4 +207,15 @@ int charset_upgrade(int charset); */ int charset_contains_ascii(int charset); +/* + * This function tries to deduce the CS_* identifier of the charset + * used in the current C locale. It falls back to CS_ASCII if it + * can't figure it out at all, so it will always return a valid + * charset. + * + * (Note that you should have already called setlocale(LC_CTYPE, + * "") to guarantee that this function will do the right thing.) + */ +int charset_from_locale(void); + #endif /* charset_charset_h */ diff --git a/locale.c b/locale.c new file mode 100644 index 0000000..6710d1b --- /dev/null +++ b/locale.c @@ -0,0 +1,93 @@ +/* + * locale.c: try very hard to figure out the libcharset charset + * identifier corresponding to the current C library locale. + * + * This function works by calling nl_langinfo(CODESET) if + * available; failing that, it will try to figure it out manually + * by examining the locale environment variables. Code for the + * latter is adapted from Markus Kuhn's public-domain + * implementation of nl_langinfo(CODESET), available at + * + * http://www.cl.cam.ac.uk/~mgk25/ucs/langinfo.c + */ + +#include +#include +#include "charset.h" + +#ifdef HAS_LANGINFO_CODESET +#include "langinfo.h" +#endif + +int charset_from_locale(void) +{ + char *l, *p; + +#ifdef HAS_LANGINFO_CODESET + int charset; + char const *csname; + + csname = nl_langinfo(CODESET); + if (csname && + (charset = charset_from_localenc(csname)) != CS_NONE) + return charset; +#endif + + if (((l = getenv("LC_ALL")) && *l) || + ((l = getenv("LC_CTYPE")) && *l) || + ((l = getenv("LANG")) && *l)) { + /* check standardized locales */ + if (!strcmp(l, "C") || !strcmp(l, "POSIX")) + return CS_ASCII; + /* check for encoding name fragment */ + if (strstr(l, "UTF") || strstr(l, "utf")) + return CS_UTF8; + if ((p = strstr(l, "8859-"))) { + char buf[16]; + int charset; + + memcpy(buf, "ISO-8859-\0\0", 12); + p += 5; + if ((*p) >= '0' && (*p) <= '9') { + buf[9] = *p++; + if ((*p) >= '0' && (*p) <= '9') buf[10] = *p++; + if ((charset = charset_from_localenc(buf)) != CS_NONE) + return charset; + } + } + if (strstr(l, "KOI8-RU")) return CS_KOI8_RU; + if (strstr(l, "KOI8-R")) return CS_KOI8_R; + if (strstr(l, "KOI8-U")) return CS_KOI8_U; + /* if (strstr(l, "620")) return "TIS-620"; */ + if (strstr(l, "2312")) return CS_EUC_CN; + /* if (strstr(l, "HKSCS")) return "Big5HKSCS"; */ + if (strstr(l, "Big5") || strstr(l, "BIG5")) return CS_BIG5; + /* if (strstr(l, "GBK")) return "GBK"; */ + /* if (strstr(l, "18030")) return "GB18030"; */ + if (strstr(l, "Shift_JIS") || strstr(l, "SJIS")) return CS_SHIFT_JIS; + /* check for conclusive modifier */ + if (strstr(l, "euro")) return CS_ISO8859_15; + /* check for language (and perhaps country) codes */ + if (strstr(l, "zh_TW")) return CS_BIG5; + /* if (strstr(l, "zh_HK")) return "Big5HKSCS"; */ + if (strstr(l, "zh")) return CS_EUC_CN; + if (strstr(l, "ja")) return CS_EUC_JP; + if (strstr(l, "ko")) return CS_EUC_KR; + if (strstr(l, "ru")) return CS_KOI8_R; + if (strstr(l, "uk")) return CS_KOI8_U; + if (strstr(l, "pl") || strstr(l, "hr") || + strstr(l, "hu") || strstr(l, "cs") || + strstr(l, "sk") || strstr(l, "sl")) return CS_ISO8859_2; + if (strstr(l, "eo") || strstr(l, "mt")) return CS_ISO8859_3; + if (strstr(l, "el")) return CS_ISO8859_7; + if (strstr(l, "he")) return CS_ISO8859_8; + if (strstr(l, "tr")) return CS_ISO8859_9; + /* if (strstr(l, "th")) return "TIS-620"; */ + if (strstr(l, "lt")) return CS_ISO8859_13; + if (strstr(l, "cy")) return CS_ISO8859_14; + if (strstr(l, "ro")) return CS_ISO8859_2; /* or ISO-8859-16 */ + if (strstr(l, "am") || strstr(l, "vi")) return CS_UTF8; + return CS_ISO8859_1; + } + return CS_ASCII; +} -- 2.11.0