libusb10: updated to 1.0.9
[tomato.git] / release / src / router / libjson-c / printbuf.c
blob9d565220007e6a887925772fe0fe20334e604cb4
1 /*
2 * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the MIT license. See COPYING for details.
11 * Copyright (c) 2008-2009 Yahoo! Inc. All rights reserved.
12 * The copyrights to the contents of this file are licensed under the MIT License
13 * (http://www.opensource.org/licenses/mit-license.php)
16 #include "config.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #ifdef HAVE_STDARG_H
23 # include <stdarg.h>
24 #else /* !HAVE_STDARG_H */
25 # error Not enough var arg support!
26 #endif /* HAVE_STDARG_H */
28 #include "bits.h"
29 #include "debug.h"
30 #include "printbuf.h"
32 static int printbuf_extend(struct printbuf *p, int min_size);
34 struct printbuf* printbuf_new(void)
36 struct printbuf *p;
38 p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
39 if(!p) return NULL;
40 p->size = 32;
41 p->bpos = 0;
42 if(!(p->buf = (char*)malloc(p->size))) {
43 free(p);
44 return NULL;
46 return p;
50 /**
51 * Extend the buffer p so it has a size of at least min_size.
53 * If the current size is large enough, nothing is changed.
55 * Note: this does not check the available space! The caller
56 * is responsible for performing those calculations.
58 static int printbuf_extend(struct printbuf *p, int min_size)
60 char *t;
61 int new_size;
63 if (p->size >= min_size)
64 return 0;
66 new_size = json_max(p->size * 2, min_size + 8);
67 #ifdef PRINTBUF_DEBUG
68 MC_DEBUG("printbuf_memappend: realloc "
69 "bpos=%d min_size=%d old_size=%d new_size=%d\n",
70 p->bpos, min_size, p->size, new_size);
71 #endif /* PRINTBUF_DEBUG */
72 if(!(t = (char*)realloc(p->buf, new_size)))
73 return -1;
74 p->size = new_size;
75 p->buf = t;
76 return 0;
79 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
81 if (p->size <= p->bpos + size + 1) {
82 if (printbuf_extend(p, p->bpos + size + 1) < 0)
83 return -1;
85 memcpy(p->buf + p->bpos, buf, size);
86 p->bpos += size;
87 p->buf[p->bpos]= '\0';
88 return size;
91 int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
93 int size_needed;
95 if (offset == -1)
96 offset = pb->bpos;
97 size_needed = offset + len;
98 if (pb->size < size_needed)
100 if (printbuf_extend(pb, size_needed) < 0)
101 return -1;
104 memset(pb->buf + offset, charvalue, len);
105 if (pb->bpos < size_needed)
106 pb->bpos = size_needed;
108 return 0;
111 #if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER)
112 # define vsnprintf _vsnprintf
113 #elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */
114 # error Need vsnprintf!
115 #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
117 #if !defined(HAVE_VASPRINTF)
118 /* CAW: compliant version of vasprintf */
119 static int vasprintf(char **buf, const char *fmt, va_list ap)
121 #ifndef WIN32
122 static char _T_emptybuffer = '\0';
123 #endif /* !defined(WIN32) */
124 int chars;
125 char *b;
127 if(!buf) { return -1; }
129 #ifdef WIN32
130 chars = _vscprintf(fmt, ap)+1;
131 #else /* !defined(WIN32) */
132 /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
133 our buffer like on some 64bit sun systems.... but hey, its time to move on */
134 chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
135 if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
136 #endif /* defined(WIN32) */
138 b = (char*)malloc(sizeof(char)*chars);
139 if(!b) { return -1; }
141 if((chars = vsprintf(b, fmt, ap)) < 0)
143 free(b);
144 } else {
145 *buf = b;
148 return chars;
150 #endif /* !HAVE_VASPRINTF */
152 int sprintbuf(struct printbuf *p, const char *msg, ...)
154 va_list ap;
155 char *t;
156 int size;
157 char buf[128];
159 /* user stack buffer first */
160 va_start(ap, msg);
161 size = vsnprintf(buf, 128, msg, ap);
162 va_end(ap);
163 /* if string is greater than stack buffer, then use dynamic string
164 with vasprintf. Note: some implementation of vsnprintf return -1
165 if output is truncated whereas some return the number of bytes that
166 would have been written - this code handles both cases. */
167 if(size == -1 || size > 127) {
168 va_start(ap, msg);
169 if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
170 va_end(ap);
171 printbuf_memappend(p, t, size);
172 free(t);
173 return size;
174 } else {
175 printbuf_memappend(p, buf, size);
176 return size;
180 void printbuf_reset(struct printbuf *p)
182 p->buf[0] = '\0';
183 p->bpos = 0;
186 void printbuf_free(struct printbuf *p)
188 if(p) {
189 free(p->buf);
190 free(p);