]> mj.ucw.cz Git - libucw.git/commitdiff
Added charconv wrapper around fastbuf (currently output only).
authorMartin Mares <mj@ucw.cz>
Fri, 26 Sep 2003 14:11:55 +0000 (14:11 +0000)
committerMartin Mares <mj@ucw.cz>
Fri, 26 Sep 2003 14:11:55 +0000 (14:11 +0000)
charset/Makefile
charset/fb-charconv.c [new file with mode: 0644]
charset/fb-charconv.h [new file with mode: 0644]

index a708074557481e683ab0cf3efdd9873f4aefa77d..5d5b94e53d59f67009d6e19d1f829aeda6fcd8e3 100644 (file)
@@ -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 (file)
index 0000000..a7ba616
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ *     Sherlock Library -- Charset Conversion Wrapper for Fast Buffered I/O
+ *
+ *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *
+ *     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 <stdlib.h>
+
+#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 = "<charconv-out>";
+  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 (file)
index 0000000..8e8c148
--- /dev/null
@@ -0,0 +1,10 @@
+/*
+ *     Sherlock Library -- Charset Conversion Wrapper for Fast Buffered I/O
+ *
+ *     (c) 2003 Martin Mares <mj@ucw.cz>
+ *
+ *     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);