Add a printbuf_memset() function to provide an effecient way to set and append things...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Mon, 2 Apr 2012 20:39:55 +0000 (15:39 -0500)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Mon, 2 Apr 2012 20:39:55 +0000 (15:39 -0500)
printbuf.c
printbuf.h

index e8902c8..b4f7955 100644 (file)
@@ -29,6 +29,8 @@
 #include "debug.h"
 #include "printbuf.h"
 
+static int printbuf_extend(struct printbuf *p, int min_size);
+
 struct printbuf* printbuf_new(void)
 {
   struct printbuf *p;
@@ -45,19 +47,32 @@ struct printbuf* printbuf_new(void)
 }
 
 
-int printbuf_memappend(struct printbuf *p, const char *buf, int size)
+static int printbuf_extend(struct printbuf *p, int min_size)
 {
-  char *t;
-  if(p->size - p->bpos <= size) {
-    int new_size = json_max(p->size * 2, p->bpos + size + 8);
+       char *t;
+       int new_size;
+
+       if (p->size >= min_size)
+               return 0;
+
+       new_size = json_max(p->size * 2, p->bpos + min_size + 8);
 #ifdef PRINTBUF_DEBUG
-    MC_DEBUG("printbuf_memappend: realloc "
-            "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
-            p->bpos, size, p->size, new_size);
+       MC_DEBUG("printbuf_memappend: realloc "
+         "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
+         p->bpos, size, p->size, new_size);
 #endif /* PRINTBUF_DEBUG */
-    if(!(t = (char*)realloc(p->buf, new_size))) return -1;
-    p->size = new_size;
-    p->buf = t;
+       if(!(t = (char*)realloc(p->buf, new_size)))
+               return -1;
+       p->size = new_size;
+       p->buf = t;
+       return 0;
+}
+
+int printbuf_memappend(struct printbuf *p, const char *buf, int size)
+{
+  if(p->size - p->bpos <= size) {
+    if (printbuf_extend(p, size) < 0)
+      return -1;
   }
   memcpy(p->buf + p->bpos, buf, size);
   p->bpos += size;
@@ -65,6 +80,26 @@ int printbuf_memappend(struct printbuf *p, const char *buf, int size)
   return size;
 }
 
+int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
+{
+       int size_needed;
+
+       if (offset == -1)
+               offset = pb->bpos;
+       size_needed = offset + len;
+       if(pb->size - pb->bpos <= size_needed)
+       {
+               if (printbuf_extend(pb, size_needed) < 0)
+                       return -1;
+       }
+
+       memset(pb->buf + offset, charvalue, len);
+       if (pb->bpos < size_needed)
+               pb->bpos = size_needed;
+
+       return 0;
+}
+
 #if !HAVE_VSNPRINTF && defined(_MSC_VER)
 # define vsnprintf _vsnprintf
 #elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
index 5d4963f..fc8ac61 100644 (file)
@@ -50,6 +50,19 @@ do {                                                         \
   } else {  printbuf_memappend(p, (bufptr), bufsize); }      \
 } while (0)
 
+#define printbuf_length(p) ((p)->bpos)
+
+/**
+ * Set len bytes of the buffer to charvalue, starting at offset offset.
+ * Similar to calling memset(x, charvalue, len);
+ *
+ * The memory allocated for the buffer is extended as necessary.
+ *
+ * If offset is -1, this starts at the end of the current data in the buffer.
+ */
+extern int
+printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len);
+
 extern int
 sprintbuf(struct printbuf *p, const char *msg, ...);