diff options
author | Karl Ramm <kcr@1ts.org> | 2009-04-17 14:42:18 +0000 |
---|---|---|
committer | Karl Ramm <kcr@1ts.org> | 2009-04-17 14:42:18 +0000 |
commit | 7c412165256f54ea5e499868d9ab48cd9f442147 (patch) | |
tree | 52123669b176dbadc889e71404134ff5df769a96 /lib/charset.c | |
parent | 7dffd27e0267e0585c35c5f1b59497a1431501a3 (diff) |
More i18n infrastructure
lib/charset.c
h/zephyr/zephyr.h
Refactor ZGetCharset into ZGetCharset and a function that does
all the same defaulting but returns a string.
ZTransliterate is a zephyr-metaphor wrapper for iconv.
lib/zephyr-tests.py
Wrappers for the functions in charset.c (but not tests)
lib/zephyr_tests.txt
Tests for the functions in charset.c
Diffstat (limited to 'lib/charset.c')
-rw-r--r-- | lib/charset.c | 71 |
1 files changed, 67 insertions, 4 deletions
diff --git a/lib/charset.c b/lib/charset.c index d1f64fc..34806e3 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -15,16 +15,18 @@ static const char rcsid_charset_c[] = "$Id$"; #endif /* lint */ #include <internal.h> + #include <string.h> #include <locale.h> #include <langinfo.h> #include <ctype.h> +#include <iconv.h> +#include <errno.h> -unsigned short -ZGetCharset(char *charset) +const char * +ZGetCharsetString(char *charset) { char *p; - short retval; static int once = 1; if (charset == NULL) @@ -46,6 +48,15 @@ ZGetCharset(char *charset) for (p = charset; *p; p++) *p = toupper(*p); + return charset; +} + +unsigned short +ZGetCharset(char *charset) +{ + short retval; + + charset = (char *)ZGetCharsetString(charset); if (!strcmp(charset, "NONE") || !strcmp(charset, "UNKNOWN")) retval = ZCHARSET_UNKNOWN; else if (!strcmp(charset, "ANSI_X3.4-1968")) @@ -72,4 +83,56 @@ ZCharsetToString(unsigned short charset) return "UTF-8"; return "UNKNOWN"; } - + +Code_t +ZTransliterate(char *in, int inlen, char *inset, char *outset, char **out, int *outlen) +{ + iconv_t ih; + char *outset_t, *inp, *outp; + int retval; + size_t size, inleft, outleft; + + *out = NULL; + *outlen = 0; + + outset_t = malloc(strlen(outset) + 11); + if (outset_t == NULL) + return errno; + sprintf(outset_t, "%s//TRANSLIT", outset); + + ih = iconv_open(outset_t, inset); + + free(outset_t); + + if (ih != (iconv_t)-1) { + size = inlen; /* doubling this should be enough, but.. */ + do { + size = size * 2; + + *out = malloc(size); + if (*out == NULL) { + iconv_close(ih); + return errno; + } + + inleft = inlen; + outleft = size; + + inp = in; + outp = *out; + + retval = iconv(ih, &inp, &inleft, &outp, &outleft); + if (retval < 0) + free(*out); + } while (retval < 0 && errno == E2BIG); + + iconv_close(ih); + } + + if (ih == (iconv_t)-1 || retval < 0) + return errno; + + *outlen = size - outleft; + + return ZERR_NONE; +} |