summaryrefslogtreecommitdiff
path: root/printbuf.c
diff options
context:
space:
mode:
authorMichael Clark <michael@metaparadigm.com>2007-03-13 08:26:20 +0000
committerMichael Clark <michael@metaparadigm.com>2007-03-13 08:26:20 +0000
commit4504df71178fe7a0eb8e7c37fa548b7c853a800f (patch)
treeb5c804143276475f469429ad2f7e6c7de9584743 /printbuf.c
parentf0d08887b857fce1fe95a68d29eb7a07cd527d7c (diff)
* printbuf.c - C. Watford (christopher dot watford at gmail dot com)
Added a Win32/Win64 compliant implementation of vasprintf * debug.c - C. Watford (christopher dot watford at gmail dot com) Removed usage of vsyslog on Win32/Win64 systems, needs to be handled by a configure script * json_object.c - C. Watford (christopher dot watford at gmail dot com) Added scope operator to wrap usage of json_object_object_foreach, this needs to be rethought to be more ANSI C friendly * json_object.h - C. Watford (christopher dot watford at gmail dot com) Added Microsoft C friendly version of json_object_object_foreach * json_tokener.c - C. Watford (christopher dot watford at gmail dot com) Added a Win32/Win64 compliant implementation of strndup * json_util.c - C. Watford (christopher dot watford at gmail dot com) Added cast and mask to suffice size_t v. unsigned int conversion correctness * json_tokener.c - sign reversal issue on error info for nested object parse spotted by Johan Bj�rklund (johbjo09 at kth.se) * json_object.c - escape " in json_escape_str * Change to automake and libtool to build shared and static library Michael Clark <michael@metaparadigm.com> git-svn-id: http://svn.metaparadigm.com/svn/json-c/trunk@4 327403b1-1117-474d-bef2-5cb71233fd97
Diffstat (limited to 'printbuf.c')
-rw-r--r--printbuf.c59
1 files changed, 52 insertions, 7 deletions
diff --git a/printbuf.c b/printbuf.c
index 76c04e4..7e70fb0 100644
--- a/printbuf.c
+++ b/printbuf.c
@@ -1,5 +1,5 @@
/*
- * $Id: printbuf.c,v 1.3 2004/08/07 03:12:21 mclark Exp $
+ * $Id: printbuf.c,v 1.4 2005/06/14 22:41:51 mclark Exp $
*
* Copyright Metaparadigm Pte. Ltd. 2004.
* Michael Clark <michael@metaparadigm.com>
@@ -16,16 +16,22 @@
*
*/
+#include "config.h"
+
#include <stdio.h>
#include <stdlib.h>
-#include <stdarg.h>
#include <string.h>
+#if HAVE_STDARG_H
+# include <stdarg.h>
+#else /* !HAVE_STDARG_H */
+# error Not enough var arg support!
+#endif /* HAVE_STDARG_H */
+
#include "bits.h"
#include "debug.h"
#include "printbuf.h"
-
struct printbuf* printbuf_new()
{
struct printbuf *p;
@@ -46,11 +52,11 @@ int printbuf_memappend(struct printbuf *p, char *buf, int size)
char *t;
if(p->size - p->bpos <= size) {
int new_size = max(p->size * 2, p->bpos + size + 8);
-#if 0
+#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);
-#endif
+#endif /* PRINTBUF_DEBUG */
if(!(t = realloc(p->buf, new_size))) return -1;
p->size = new_size;
p->buf = t;
@@ -61,6 +67,46 @@ int printbuf_memappend(struct printbuf *p, char *buf, int size)
return size;
}
+#if !HAVE_VSNPRINTF && defined(WIN32)
+# define vsnprintf _vsnprintf
+#elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
+# error Need vsnprintf!
+#endif /* !HAVE_VSNPRINTF && defined(WIN32) */
+
+#if !HAVE_VASPRINTF
+/* CAW: compliant version of vasprintf */
+static int vasprintf(char **buf, const char *fmt, va_list ap)
+{
+#ifndef WIN32
+ static char _T_emptybuffer = '\0';
+#endif /* !defined(WIN32) */
+ int chars;
+ char *b;
+
+ if(!buf) { return -1; }
+
+#ifdef WIN32
+ chars = _vscprintf(fmt, ap)+1;
+#else /* !defined(WIN32) */
+ /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
+ our buffer like on some 64bit sun systems.... but hey, its time to move on */
+ chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
+ if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
+#endif /* defined(WIN32) */
+
+ b = (char*)malloc(sizeof(char)*chars);
+ if(!b) { return -1; }
+
+ if((chars = vsprintf(b, fmt, ap)) < 0)
+ {
+ free(b);
+ } else {
+ *buf = b;
+ }
+
+ return chars;
+}
+#endif /* !HAVE_VASPRINTF */
int sprintbuf(struct printbuf *p, const char *msg, ...)
{
@@ -76,7 +122,7 @@ int sprintbuf(struct printbuf *p, const char *msg, ...)
/* if string is greater than stack buffer, then use dynamic string
with vasprintf. Note: some implementation of vsnprintf return -1
if output is truncated whereas some return the number of bytes that
- would have been writeen - this code handles both cases. */
+ would have been writen - this code handles both cases. */
if(size == -1 || size > 127) {
int ret;
va_start(ap, msg);
@@ -90,7 +136,6 @@ int sprintbuf(struct printbuf *p, const char *msg, ...)
}
}
-
void printbuf_reset(struct printbuf *p)
{
p->buf[0] = '\0';