Browse Source

Add a printbuf_memset() function to provide an effecient way to set and append things like whitespace indentation.

tags/json-c-0.11-20130402
Eric Haszlakiewicz Keith Derrick 13 years ago
parent
commit
8310d3634c
2 changed files with 58 additions and 10 deletions
  1. +45
    -10
      printbuf.c
  2. +13
    -0
      printbuf.h

+ 45
- 10
printbuf.c View 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 */


+ 13
- 0
printbuf.h View 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, ...);



Loading…
Cancel
Save