]> mj.ucw.cz Git - libucw.git/blob - charset/stk-charconv.h
d8ae585c4fefa91d9e570ed77090bad63f002792
[libucw.git] / charset / stk-charconv.h
1 /*
2  *      Sherlock Library -- Character Conversion with Allocation on the Stack 
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  */
6
7 #ifndef _CHARSET_STK_CHARCONV_H
8 #define _CHARSET_STK_CHARCONV_H
9
10 #include "charset/charconv.h"
11 #include <alloca.h>
12
13 /* The following macros convert strings between given charsets (CONV_CHARSET_x). */
14
15 #define stk_conv(s, cs_in, cs_out) \
16     ({ struct stk_conv_context _c; stk_conv_init(&_c, (s), (cs_in), (cs_out)); \
17        while (stk_conv_step(&_c, alloca(_c.request))); _c.c.dest_start; })
18
19 #define stk_conv_to_utf8(s, cs_in) stk_conv(s, cs_in, CONV_CHARSET_UTF8)
20 #define stk_conv_from_utf8(s, cs_out) stk_conv(s, CONV_CHARSET_UTF8, cs_out)
21    
22 /* Internal structure and routines. */
23
24 struct stk_conv_context {
25   struct conv_context c;
26   uns count;
27   uns sum;
28   uns request;
29   byte *buf[16]; 
30   uns size[16];
31 };
32
33 void stk_conv_init(struct stk_conv_context *c, byte *s, uns cs_in, uns cs_out);
34 int stk_conv_step(struct stk_conv_context *c, byte *buf);
35
36 #endif