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 "lib/fastbuf.h"
12 #include "charset/charconv.h"
13 #include "charset/fb-charconv.h"
22 struct conv_context ctxt;
25 #define FB_CC(f) ((struct fb_charconv *)(f)->is_fastbuf)
28 fb_cc_spout(struct fastbuf *f)
30 struct conv_context *ct = &FB_CC(f)->ctxt;
33 ct->source = f->buffer;
34 ct->source_end = f->bptr;
38 if (ct->dest > ct->dest_start)
39 bdirect_write_commit(FB_CC(f)->orig, ct->dest);
40 uns l = bdirect_write_prepare(FB_CC(f)->orig, &ct->dest_start);
41 ct->dest = ct->dest_start;
42 ct->dest_end = ct->dest + l;
44 while (!(flags & CONV_SOURCE_END));
50 fb_cc_refill(struct fastbuf *f)
52 struct conv_context *ct = &FB_CC(f)->ctxt;
55 f->bptr = f->bstop = f->buffer;
59 uns len = bdirect_read_prepare(FB_CC(f)->orig, &src);
63 ct->source_end = ct->source + len;
64 ct->dest = ct->dest_start = f->bstop;
65 ct->dest_end = f->bufend;
67 bdirect_read_commit(FB_CC(f)->orig, (byte*)ct->source);
70 while (!(flags & CONV_DEST_END));
71 return (f->bstop > f->bptr);
75 fb_cc_close(struct fastbuf *f)
77 bflush(FB_CC(f)->orig);
82 fb_wrap_charconv_out(struct fastbuf *f, int cs_from, int cs_to)
87 struct fastbuf *g = xmalloc_zero(sizeof(struct fb_charconv));
89 conv_init(&FB_CC(g)->ctxt);
90 conv_set_charset(&FB_CC(g)->ctxt, cs_from, cs_to);
91 g->name = "<charconv-out>";
92 g->spout = fb_cc_spout;
93 g->close = fb_cc_close;
94 g->buffer = g->bstop = g->bptr = FB_CC(g)->buf;
95 g->bufend = g->buffer + BUFSIZE;
100 fb_wrap_charconv_in(struct fastbuf *f, int cs_from, int cs_to)
102 if (cs_from == cs_to)
105 struct fastbuf *g = xmalloc_zero(sizeof(struct fb_charconv));
107 conv_init(&FB_CC(g)->ctxt);
108 conv_set_charset(&FB_CC(g)->ctxt, cs_from, cs_to);
109 g->name = "<charconv-in>";
110 g->refill = fb_cc_refill;
111 g->close = fb_cc_close;
112 g->buffer = g->bstop = g->bptr = FB_CC(g)->buf;
113 g->bufend = g->buffer + BUFSIZE;