]> mj.ucw.cz Git - libucw.git/blob - charset/stk-charconv.c
Merge with git+ssh://cvs.ucw.cz/projects/sherlock/GIT/sherlock.git#dev-config
[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  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "charset/stk-charconv.h"
12 #include <string.h>
13
14 #define INITIAL_MIN_SIZE        16
15 #define INITIAL_SCALE           2
16
17 uns
18 stk_strconv_init(struct conv_context *c, byte *s, uns in_cs, uns out_cs)
19 {
20   uns l = strlen(s);
21   if (in_cs == out_cs)
22   {
23     c->source = s;
24     c->source_end = NULL;
25     return l + 1;
26   }
27   conv_init(c);
28   conv_set_charset(c, in_cs, out_cs);
29   c->source = s;
30   c->source_end = s + l + 1;
31   if (l < (INITIAL_MIN_SIZE - 1) / INITIAL_SCALE)
32     return INITIAL_MIN_SIZE;
33   else
34     return l * INITIAL_SCALE + 1;
35 }
36
37 uns
38 stk_strconv_step(struct conv_context *c, byte *buf, uns len)
39 {
40   if (!c->source_end)
41   {
42     memcpy(buf, c->source, len);
43     c->dest_start = buf;
44     return 0;
45   }
46   if (c->dest_start)
47   {
48     uns l = c->dest_end - c->dest_start;
49     memcpy(buf, c->dest_start, l);
50     c->dest = buf + l;
51   }
52   else
53     c->dest = buf;
54   c->dest_start = buf;
55   c->dest_end = buf + len;
56   if (conv_run(c) & CONV_SOURCE_END)
57     return 0;
58   return len << 1;
59 }
60