From d4bd861a91d3e9d9ed08336e553d5b9d851f1e64 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 26 Sep 2003 14:11:55 +0000 Subject: [PATCH] Added charconv wrapper around fastbuf (currently output only). --- charset/Makefile | 2 +- charset/fb-charconv.c | 72 +++++++++++++++++++++++++++++++++++++++++++ charset/fb-charconv.h | 10 ++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 charset/fb-charconv.c create mode 100644 charset/fb-charconv.h diff --git a/charset/Makefile b/charset/Makefile index a7080745..5d5b94e5 100644 --- a/charset/Makefile +++ b/charset/Makefile @@ -3,7 +3,7 @@ DIRS+=charset LIBCHARSET_MODS=toupper tolower tocat utf8 unaccent strlen debug \ - charconv setnames unistream + charconv setnames unistream fb-charconv LIBCHARSET=obj/charset/libcharset.$(LS) obj/charset/libcharset.a: $(addsuffix .o,$(addprefix obj/charset/,$(LIBCHARSET_MODS))) diff --git a/charset/fb-charconv.c b/charset/fb-charconv.c new file mode 100644 index 00000000..a7ba616f --- /dev/null +++ b/charset/fb-charconv.c @@ -0,0 +1,72 @@ +/* + * Sherlock Library -- Charset Conversion Wrapper for Fast Buffered I/O + * + * (c) 2003 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +#include "lib/lib.h" +#include "lib/fastbuf.h" +#include "charset/charconv.h" +#include "charset/fb-charconv.h" + +#include + +#define BUFSIZE 1024 + +struct fb_charconv { + struct fastbuf fb; + struct fastbuf *out; + struct conv_context ctxt; + byte buf[BUFSIZE]; +}; +#define FB_CC(f) ((struct fb_charconv *)(f)->is_fastbuf) + +static void +fb_cc_spout(struct fastbuf *f) +{ + struct conv_context *ct = &FB_CC(f)->ctxt; + int flags; + + ct->source = f->buffer; + ct->source_end = f->bptr; + do + { + flags = conv_run(ct); + if (ct->dest > ct->dest_start) + bdirect_write_commit(FB_CC(f)->out, ct->dest); + uns l = bdirect_write_prepare(FB_CC(f)->out, &ct->dest_start); + ct->dest = ct->dest_start; + ct->dest_end = ct->dest + l; + } + while (!(flags & CONV_SOURCE_END)); + + f->bptr = f->buffer; +} + +static void +fb_cc_close(struct fastbuf *f) +{ + bclose(FB_CC(f)->out); + xfree(f); +} + +struct fastbuf * +fb_wrap_charconv_out(struct fastbuf *f, int cs_from, int cs_to) +{ + if (cs_from == cs_to) + return f; + + struct fastbuf *g = xmalloc_zero(sizeof(struct fb_charconv)); + FB_CC(g)->out = f; + conv_init(&FB_CC(g)->ctxt); + conv_set_charset(&FB_CC(g)->ctxt, cs_from, cs_to); + g->name = ""; + g->spout = fb_cc_spout; + g->close = fb_cc_close; + g->buffer = g->bstop = g->bptr = FB_CC(g)->buf; + g->bufend = g->buffer + BUFSIZE; + return g; +} diff --git a/charset/fb-charconv.h b/charset/fb-charconv.h new file mode 100644 index 00000000..8e8c1486 --- /dev/null +++ b/charset/fb-charconv.h @@ -0,0 +1,10 @@ +/* + * Sherlock Library -- Charset Conversion Wrapper for Fast Buffered I/O + * + * (c) 2003 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + +struct fastbuf *fb_wrap_charconv_out(struct fastbuf *f, int cs_from, int cs_to); -- 2.39.5