2 * Sherlock Library -- Charset Conversion Wrapper for Fast Buffered I/O
4 * (c) 2003--2005 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include <ucw/fastbuf.h>
12 #include <charset/charconv.h>
13 #include <charset/fb-charconv.h>
20 struct conv_context ctxt;
23 #define FB_CC(f) ((struct fb_charconv *)(f))
26 fb_cc_spout(struct fastbuf *f)
28 struct conv_context *ct = &FB_CC(f)->ctxt;
31 ct->source = f->buffer;
32 ct->source_end = f->bptr;
36 if (ct->dest > ct->dest_start)
37 bdirect_write_commit(FB_CC(f)->orig, ct->dest);
38 uint l = bdirect_write_prepare(FB_CC(f)->orig, &ct->dest_start);
39 ct->dest = ct->dest_start;
40 ct->dest_end = ct->dest + l;
42 while (!(flags & CONV_SOURCE_END));
48 fb_cc_refill(struct fastbuf *f)
50 struct conv_context *ct = &FB_CC(f)->ctxt;
53 f->bptr = f->bstop = f->buffer;
57 uint len = bdirect_read_prepare(FB_CC(f)->orig, &src);
61 ct->source_end = ct->source + len;
62 ct->dest = ct->dest_start = f->bstop;
63 ct->dest_end = f->bufend;
65 bdirect_read_commit(FB_CC(f)->orig, (byte*)ct->source);
68 while (!(flags & CONV_DEST_END));
69 return (f->bstop > f->bptr);
73 fb_cc_close(struct fastbuf *f)
75 bflush(FB_CC(f)->orig);
80 fb_wrap_charconv_out(struct fastbuf *f, int cs_from, int cs_to)
85 struct fastbuf *g = xmalloc_zero(sizeof(struct fb_charconv));
87 conv_init(&FB_CC(g)->ctxt);
88 conv_set_charset(&FB_CC(g)->ctxt, cs_from, cs_to);
89 g->name = "<charconv-out>";
90 g->spout = fb_cc_spout;
91 g->close = fb_cc_close;
92 g->buffer = g->bstop = g->bptr = FB_CC(g)->buf;
93 g->bufend = g->buffer + BUFSIZE;
98 fb_wrap_charconv_in(struct fastbuf *f, int cs_from, int cs_to)
100 if (cs_from == cs_to)
103 struct fastbuf *g = xmalloc_zero(sizeof(struct fb_charconv));
105 conv_init(&FB_CC(g)->ctxt);
106 conv_set_charset(&FB_CC(g)->ctxt, cs_from, cs_to);
107 g->name = "<charconv-in>";
108 g->refill = fb_cc_refill;
109 g->close = fb_cc_close;
110 g->buffer = g->bstop = g->bptr = FB_CC(g)->buf;
111 g->bufend = g->buffer + BUFSIZE;