|
Packit |
ea8578 |
/*
|
|
Packit |
ea8578 |
* $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
|
|
Packit |
ea8578 |
*
|
|
Packit |
ea8578 |
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
|
|
Packit |
ea8578 |
* Michael Clark <michael@metaparadigm.com>
|
|
Packit |
ea8578 |
*
|
|
Packit |
ea8578 |
* This library is free software; you can redistribute it and/or modify
|
|
Packit |
ea8578 |
* it under the terms of the MIT license. See COPYING for details.
|
|
Packit |
ea8578 |
*
|
|
Packit |
ea8578 |
*
|
|
Packit |
ea8578 |
* Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
|
|
Packit |
ea8578 |
* The copyrights to the contents of this file are licensed under the MIT License
|
|
Packit |
ea8578 |
* (http://www.opensource.org/licenses/mit-license.php)
|
|
Packit |
ea8578 |
*/
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
#include "config.h"
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
#include <stdio.h>
|
|
Packit |
ea8578 |
#include <stdlib.h>
|
|
Packit |
ea8578 |
#include <string.h>
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
#ifdef HAVE_STDARG_H
|
|
Packit |
ea8578 |
# include <stdarg.h>
|
|
Packit |
ea8578 |
#else /* !HAVE_STDARG_H */
|
|
Packit |
ea8578 |
# error Not enough var arg support!
|
|
Packit |
ea8578 |
#endif /* HAVE_STDARG_H */
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
#include "debug.h"
|
|
Packit |
ea8578 |
#include "printbuf.h"
|
|
Packit |
ea8578 |
#include "snprintf_compat.h"
|
|
Packit |
ea8578 |
#include "vasprintf_compat.h"
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
static int printbuf_extend(struct printbuf *p, int min_size);
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
struct printbuf* printbuf_new(void)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
struct printbuf *p;
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
|
|
Packit |
ea8578 |
if(!p) return NULL;
|
|
Packit |
ea8578 |
p->size = 32;
|
|
Packit |
ea8578 |
p->bpos = 0;
|
|
Packit |
ea8578 |
if(!(p->buf = (char*)malloc(p->size))) {
|
|
Packit |
ea8578 |
free(p);
|
|
Packit |
ea8578 |
return NULL;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
p->buf[0]= '\0';
|
|
Packit |
ea8578 |
return p;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
/**
|
|
Packit |
ea8578 |
* Extend the buffer p so it has a size of at least min_size.
|
|
Packit |
ea8578 |
*
|
|
Packit |
ea8578 |
* If the current size is large enough, nothing is changed.
|
|
Packit |
ea8578 |
*
|
|
Packit |
ea8578 |
* Note: this does not check the available space! The caller
|
|
Packit |
ea8578 |
* is responsible for performing those calculations.
|
|
Packit |
ea8578 |
*/
|
|
Packit |
ea8578 |
static int printbuf_extend(struct printbuf *p, int min_size)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
char *t;
|
|
Packit |
ea8578 |
int new_size;
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
if (p->size >= min_size)
|
|
Packit |
ea8578 |
return 0;
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
new_size = p->size * 2;
|
|
Packit |
ea8578 |
if (new_size < min_size + 8)
|
|
Packit |
ea8578 |
new_size = min_size + 8;
|
|
Packit |
ea8578 |
#ifdef PRINTBUF_DEBUG
|
|
Packit |
ea8578 |
MC_DEBUG("printbuf_memappend: realloc "
|
|
Packit |
ea8578 |
"bpos=%d min_size=%d old_size=%d new_size=%d\n",
|
|
Packit |
ea8578 |
p->bpos, min_size, p->size, new_size);
|
|
Packit |
ea8578 |
#endif /* PRINTBUF_DEBUG */
|
|
Packit |
ea8578 |
if(!(t = (char*)realloc(p->buf, new_size)))
|
|
Packit |
ea8578 |
return -1;
|
|
Packit |
ea8578 |
p->size = new_size;
|
|
Packit |
ea8578 |
p->buf = t;
|
|
Packit |
ea8578 |
return 0;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
int printbuf_memappend(struct printbuf *p, const char *buf, int size)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
if (p->size <= p->bpos + size + 1) {
|
|
Packit |
ea8578 |
if (printbuf_extend(p, p->bpos + size + 1) < 0)
|
|
Packit |
ea8578 |
return -1;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
memcpy(p->buf + p->bpos, buf, size);
|
|
Packit |
ea8578 |
p->bpos += size;
|
|
Packit |
ea8578 |
p->buf[p->bpos]= '\0';
|
|
Packit |
ea8578 |
return size;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
int size_needed;
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
if (offset == -1)
|
|
Packit |
ea8578 |
offset = pb->bpos;
|
|
Packit |
ea8578 |
size_needed = offset + len;
|
|
Packit |
ea8578 |
if (pb->size < size_needed)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
if (printbuf_extend(pb, size_needed) < 0)
|
|
Packit |
ea8578 |
return -1;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
memset(pb->buf + offset, charvalue, len);
|
|
Packit |
ea8578 |
if (pb->bpos < size_needed)
|
|
Packit |
ea8578 |
pb->bpos = size_needed;
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
return 0;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
int sprintbuf(struct printbuf *p, const char *msg, ...)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
va_list ap;
|
|
Packit |
ea8578 |
char *t;
|
|
Packit |
ea8578 |
int size;
|
|
Packit |
ea8578 |
char buf[128];
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
/* user stack buffer first */
|
|
Packit |
ea8578 |
va_start(ap, msg);
|
|
Packit |
ea8578 |
size = vsnprintf(buf, 128, msg, ap);
|
|
Packit |
ea8578 |
va_end(ap);
|
|
Packit |
ea8578 |
/* if string is greater than stack buffer, then use dynamic string
|
|
Packit |
ea8578 |
with vasprintf. Note: some implementation of vsnprintf return -1
|
|
Packit |
ea8578 |
if output is truncated whereas some return the number of bytes that
|
|
Packit |
ea8578 |
would have been written - this code handles both cases. */
|
|
Packit |
ea8578 |
if(size == -1 || size > 127) {
|
|
Packit |
ea8578 |
va_start(ap, msg);
|
|
Packit |
ea8578 |
if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
|
|
Packit |
ea8578 |
va_end(ap);
|
|
Packit |
ea8578 |
printbuf_memappend(p, t, size);
|
|
Packit |
ea8578 |
free(t);
|
|
Packit |
ea8578 |
return size;
|
|
Packit |
ea8578 |
} else {
|
|
Packit |
ea8578 |
printbuf_memappend(p, buf, size);
|
|
Packit |
ea8578 |
return size;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
void printbuf_reset(struct printbuf *p)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
p->buf[0] = '\0';
|
|
Packit |
ea8578 |
p->bpos = 0;
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
|
|
Packit |
ea8578 |
void printbuf_free(struct printbuf *p)
|
|
Packit |
ea8578 |
{
|
|
Packit |
ea8578 |
if(p) {
|
|
Packit |
ea8578 |
free(p->buf);
|
|
Packit |
ea8578 |
free(p);
|
|
Packit |
ea8578 |
}
|
|
Packit |
ea8578 |
}
|