]> mj.ucw.cz Git - libucw.git/blob - charset/stk-charconv.c
Well, a simple variant of stk_conv.
[libucw.git] / charset / stk-charconv.c
1 /*
2  *      Sherlock Library -- Character Conversion with Allocation on the Stack 
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "charset/stk-charconv.h"
9 #include <string.h>
10
11 #define INITIAL_MIN_SIZE        16
12 #define INITIAL_SCALE           2
13
14 void
15 stk_conv_init(struct stk_conv_context *c, byte *s, uns in_cs, uns out_cs)
16 {
17   uns l = strlen(s);
18   if (in_cs == out_cs)
19   {
20     c->c.source = s;
21     c->c.source_end = NULL;
22     c->len = l + 1;
23     return;
24   }
25   conv_init(&c->c);
26   conv_set_charset(&c->c, in_cs, out_cs);
27   c->c.source = s;
28   c->c.source_end = s + l + 1;
29   if (l < (INITIAL_MIN_SIZE - 1) / INITIAL_SCALE)
30     c->len = INITIAL_MIN_SIZE;
31   else
32     c->len = l * INITIAL_SCALE + 1;
33   c->len = 1;
34 }
35
36 int
37 stk_conv_step(struct stk_conv_context *c, byte *buf)
38 {
39   if (!c->c.source_end)
40   {
41     memcpy(buf, c->c.source, c->len);
42     c->c.dest_start = buf;
43     return 0;
44   }
45   if (c->c.dest_start)
46   {
47     uns l = c->c.dest_end - c->c.dest_start;
48     memcpy(buf, c->c.dest_start, l);
49     c->c.dest = buf + l;
50   }
51   else
52     c->c.dest = buf;
53   c->c.dest_start = buf;
54   c->c.dest_end = buf + c->len;
55   if ((conv_run(&c->c) & CONV_SOURCE_END))
56     return 0;
57   c->len <<= 1;
58   return 1;
59 }
60