]> mj.ucw.cz Git - libucw.git/commitdiff
Added printf-like function with output to mempool.
authorMartin Mares <mj@ucw.cz>
Thu, 10 Feb 2005 21:57:17 +0000 (21:57 +0000)
committerMartin Mares <mj@ucw.cz>
Thu, 10 Feb 2005 21:57:17 +0000 (21:57 +0000)
lib/Makefile
lib/mempool-fmt.c [new file with mode: 0644]
lib/mempool.h

index cdb02bac9790c46f9027ffc782877f6dcff52c21..c2d3e4858a2a5a10b035b5dfb2c24abf138e6251 100644 (file)
@@ -7,7 +7,7 @@ PROGS+=obj/lib/db-tool
 endif
 
 LIBUCW_MODS= \
-       alloc alloc_str realloc mempool mempool-str \
+       alloc alloc_str realloc mempool mempool-str mempool-fmt \
        mmap pagecache partmap hashfunc \
        lists sorter bitsig \
        log log-file proctitle \
diff --git a/lib/mempool-fmt.c b/lib/mempool-fmt.c
new file mode 100644 (file)
index 0000000..47bb71e
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ *     UCW Library -- Memory Pools (Formatting)
+ *
+ *     (c) 2005 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/mempool.h"
+
+#include <alloca.h>
+#include <stdio.h>
+#include <string.h>
+
+char *
+mp_vprintf(struct mempool *p, char *fmt, va_list args)
+{
+  char *ret = p->free;
+  int free = p->last - p->free;
+  if (!free)
+    {
+      ret = mp_alloc(p, 1);
+      free = p->last - p->free;
+    }
+  int cnt = vsnprintf(ret, free, fmt, args);
+  if (cnt < 0)
+    {
+      /* Our C library doesn't support C99 return value of vsnprintf, so we need to iterate */
+      uns len = 128;
+      char *buf;
+      do
+       {
+         len *= 2;
+         buf = alloca(len);
+         cnt = vsnprintf(buf, len, fmt, args);
+       }
+      while (cnt < 0);
+      ret = mp_alloc(p, cnt+1);
+      memcpy(ret, buf, cnt+1);
+    }
+  else if (cnt < free)
+    p->free += cnt + 1;
+  else
+    {
+      ret = mp_alloc(p, cnt+1);
+      int cnt2 = vsnprintf(ret, cnt+1, fmt, args);
+      ASSERT(cnt2 == cnt);
+    }
+  return ret;
+}
+
+char *
+mp_printf(struct mempool *p, char *fmt, ...)
+{
+  va_list args;
+  va_start(args, fmt);
+  char *res = mp_vprintf(p, fmt, args);
+  va_end(args);
+  return res;
+}
+
+#ifdef TEST
+
+int main(void)
+{
+  struct mempool *mp = mp_new(64);
+  char *x = mp_printf(mp, "Hello, %s!\n", "World");
+  fputs(x, stdout);
+  x = mp_printf(mp, "Hello, %100s!\n", "World");
+  fputs(x, stdout);
+  return 0;
+}
+
+#endif
index 8d964e4d30f174e60b94ab538fa40c12f3d0560e..7290c256f6865d61f0458e882ef04ad08fc5f5b5 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     UCW Library -- Memory Pools
  *
- *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2005 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -60,7 +60,7 @@ mp_end_string(struct mempool *p, void *stop)
   p->free = stop;
 }
 
-/* pool-str.c */
+/* mempool-str.c */
 
 char *mp_strdup(struct mempool *, char *);
 char *mp_multicat(struct mempool *, ...);
@@ -70,4 +70,9 @@ mp_strcat(struct mempool *mp, char *x, char *y)
   return mp_multicat(mp, x, y, NULL);
 }
 
+/* mempool-fmt.c */
+
+char *mp_printf(struct mempool *p, char *fmt, ...);
+char *mp_vprintf(struct mempool *p, char *fmt, va_list args);
+
 #endif