X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=charset%2Fmp-charconv.c;h=ef0adf951c83a0b5de0a026763bccb5b1c7f0a06;hb=9c5d20f9afd457a9d16e681475c6ea59c33de2d0;hp=0235ae7693257fd00037d38ea06f78a2902f8b4f;hpb=32ffcf1473c7bbba040a4f067e272fd9e5e85274;p=libucw.git diff --git a/charset/mp-charconv.c b/charset/mp-charconv.c index 0235ae76..ef0adf95 100644 --- a/charset/mp-charconv.c +++ b/charset/mp-charconv.c @@ -1,16 +1,50 @@ /* - * Sherlock Library -- Character Conversion with Allocation on a Memory Pool + * Sherlock Library -- Character Conversion with Allocation on a Memory Pool * * (c) 2006 Pavel Charvat + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ #include "lib/lib.h" -#include "lib/mempool.h" #include "charset/mp-charconv.h" -#include "charset/stk-charconv.h" +#include +#include byte * -mp_conv(struct mempool *mp, byte *s, uns in_cs, uns out_cs) +mp_strconv(struct mempool *mp, byte *s, uns in_cs, uns out_cs) { - return mp_strdup(mp, stk_conv(s, in_cs, out_cs)); + if (in_cs == out_cs) + return mp_strdup(mp, s); + + struct conv_context c; + char *b[32]; + uns bs[32], n = 0, sum = 0; + uns l = strlen(s) + 1; + + conv_init(&c); + conv_set_charset(&c, in_cs, out_cs); + c.source = s; + c.source_end = s + l; + + for (;;) + { + l <<= 1; + c.dest_start = c.dest = b[n] = alloca(l); + c.dest_end = c.dest_start+ l; + uns r = conv_run(&c); + sum += bs[n++] = c.dest - c.dest_start; + if (r & CONV_SOURCE_END) + { + c.dest_start = c.dest = mp_alloc(mp, sum); + for (uns i = 0; i < n; i++) + { + memcpy(c.dest, b[i], bs[i]); + c.dest += bs[i]; + } + return c.dest_start; + } + } } +