diff options
author | waker <wakeroid@gmail.com> | 2011-01-24 19:44:45 +0100 |
---|---|---|
committer | waker <wakeroid@gmail.com> | 2011-01-24 19:44:45 +0100 |
commit | 966bf6d2f2fbddb7eb70ac0fd18e0ba8157dd1cb (patch) | |
tree | e0ca019a7bcc8d7000eb38210c5c0a6ae23f3fd0 /junklib.c | |
parent | 553d1790372f98a589550e1f221db00ccf9220ab (diff) |
adding shift-jis->utf8 converter WIP
Diffstat (limited to 'junklib.c')
-rw-r--r-- | junklib.c | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -32,6 +32,9 @@ #else #define DDB_RECODE #include "ConvertUTF/ConvertUTF.h" +uint16_t sj_to_unicode[] = { + #include "sj_to_unicode.h" +}; #endif #include <limits.h> #include <errno.h> @@ -354,6 +357,54 @@ int ddb_iconv (const char *cs_out, const char *cs_in, char *out, int outlen, con len = target - out; } } + else if (!strcasecmp (cs_in, "SHIFT-JIS")) { + int len = 0; + while (inlen > 0 && len < outlen) { + if (*in > 0) { + *out++ = *in++; + inlen--; + len++; + } + else if (inlen < 2) { + return -1; + } + else { + // find character in table + uint16_t c = (((uint8_t*)in)[0] << 8) | ((uint8_t*)in)[1]; + for (int i = 0; sj_to_unicode[i]; i += 2) { + if (c == sj_to_unicode[i]) { + break; + } + } + if (sj_to_unicode[i]) { + // slow conversion! + char unicode_val[2] = { (sj_to_unicode[i+1] & 0xff00) >> 8, sj_to_unicode[i+1] & 0xff }; + char utf8_val[5]; + char *src = unicode_val, *dst = utf8_val; + + ConversionResult res = ConvertUTF16toUTF32 ((UTF16**)&src, src+2, (UTF8**)&dst, dst+5, strictConversion); + if (res == conversionOK) { + if (src - utf8_val < outlen-len) { + memcpy (out, utf8_val, src - utf8_val); + out += src - utf8_val; + len += src - utf8_val; + inlen -= 2; + in += 2; + } + else { + return -1; + } + } + else { + return -1; + } + } + else { + return -1; // error + } + } + } + } else { fprintf (stderr, "invalid conversion request: %s -> %s\n", cs_in, cs_out); } |