]> mj.ucw.cz Git - libucw.git/commitdiff
Added printf on fastbuf streams. The current implementation is not too
authorMartin Mares <mj@ucw.cz>
Sun, 27 Oct 2002 13:16:03 +0000 (13:16 +0000)
committerMartin Mares <mj@ucw.cz>
Sun, 27 Oct 2002 13:16:03 +0000 (13:16 +0000)
optimized for anything else than simplicity.

lib/Makefile
lib/fastbuf.h
lib/fb-printf.c [new file with mode: 0644]

index 7e1f5b83bd25dbf3b9168b13ba5c1d875ec90704..e8784f67ca3ea7b91a2cdd5e78bdb8b5e15943f9 100644 (file)
@@ -8,7 +8,7 @@ LIBSH_MODS=alloc alloc_str ctmatch db fastbuf fb-file fb-mem lists \
        prime random realloc regex timer url wildmatch \
        wordsplit str_ctype str_upper bucket conf object sorter \
        finger proctitle ipaccess profile bitsig randomkey \
-       hashfunc base64 base224 fb-temp printf fb-mmap
+       hashfunc base64 base224 fb-temp printf fb-mmap fb-printf
 LIBSH_MOD_PATHS=$(addprefix obj/lib/,$(LIBSH_MODS)) $(CUSTOM_MODULES)
 
 obj/lib/libsh.a: $(addsuffix .o,$(LIBSH_MOD_PATHS))
index ebc69955fb7720e7ebc192651745d705ba8c5d10..a370c45be8995155190b5eb61079812a685178e4 100644 (file)
@@ -15,6 +15,7 @@
 #endif
 
 #include <string.h>
+#include <stdarg.h>
 
 #include "lib/unaligned.h"
 
@@ -345,4 +346,9 @@ bdirect_write_commit(struct fastbuf *f, byte *pos)
   f->bptr = pos;
 }
 
+/* Formatted output */
+
+int bprintf(struct fastbuf *b, byte *msg, ...);
+int vbprintf(struct fastbuf *b, byte *msg, va_list args);
+
 #endif
diff --git a/lib/fb-printf.c b/lib/fb-printf.c
new file mode 100644 (file)
index 0000000..395e04b
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ *     Sherlock Library -- Printf on Fastbuf Streams
+ *
+ *     (c) 2002 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 <alloca.h>
+
+int
+vbprintf(struct fastbuf *b, byte *msg, va_list args)
+{
+  byte *buf;
+  int r, len = 256;
+
+  while (1)
+    {
+      buf = alloca(len);
+      r = vsnprintf(buf, len, msg, args);
+      if (r < 0)
+       len += len;
+      else if (r < len)
+       {
+         bwrite(b, buf, r);
+         return r;
+       }
+      else
+       len = r+1;
+    }
+}
+
+int
+bprintf(struct fastbuf *b, byte *msg, ...)
+{
+  va_list args;
+  int res;
+
+  va_start(args, msg);
+  res = vbprintf(b, msg, args);
+  va_end(args);
+  return res;
+}
+
+#ifdef TEST
+
+int main(void)
+{
+  struct fastbuf *b = bfdopen_shared(1, 65536);
+  bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str");
+  bclose(b);
+  return 0;
+}
+
+#endif