#include "debug.h"
#include "printbuf.h"
+static int printbuf_extend(struct printbuf *p, int min_size);
+
struct printbuf* printbuf_new(void)
{
struct printbuf *p;
}
-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;
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 */
} 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, ...);