Imported Upstream version 0.10
[platform/upstream/json-c.git] / printbuf.c
1 /*
2  * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
3  *
4  * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5  * Michael Clark <michael@metaparadigm.com>
6  *
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.
9  *
10  *
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)
14  */
15
16 #include "config.h"
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #if HAVE_STDARG_H
23 # include <stdarg.h>
24 #else /* !HAVE_STDARG_H */
25 # error Not enough var arg support!
26 #endif /* HAVE_STDARG_H */
27
28 #include "bits.h"
29 #include "debug.h"
30 #include "printbuf.h"
31
32 static int printbuf_extend(struct printbuf *p, int min_size);
33
34 struct printbuf* printbuf_new(void)
35 {
36   struct printbuf *p;
37
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;
45   }
46   return p;
47 }
48
49
50 /**
51  * Extend the buffer p so it has a size of at least min_size.
52  *
53  * If the current size is large enough, nothing is changed.
54  *
55  * Note: this does not check the available space!  The caller
56  *  is responsible for performing those calculations.
57  */
58 static int printbuf_extend(struct printbuf *p, int min_size)
59 {
60         char *t;
61         int new_size;
62
63         if (p->size >= min_size)
64                 return 0;
65
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;
77 }
78
79 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
80 {
81   if (p->size <= p->bpos + size + 1) {
82     if (printbuf_extend(p, p->bpos + size + 1) < 0)
83       return -1;
84   }
85   memcpy(p->buf + p->bpos, buf, size);
86   p->bpos += size;
87   p->buf[p->bpos]= '\0';
88   return size;
89 }
90
91 int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
92 {
93         int size_needed;
94
95         if (offset == -1)
96                 offset = pb->bpos;
97         size_needed = offset + len;
98         if (pb->size < size_needed)
99         {
100                 if (printbuf_extend(pb, size_needed) < 0)
101                         return -1;
102         }
103
104         memset(pb->buf + offset, charvalue, len);
105         if (pb->bpos < size_needed)
106                 pb->bpos = size_needed;
107
108         return 0;
109 }
110
111 #if !HAVE_VSNPRINTF && defined(_MSC_VER)
112 # define vsnprintf _vsnprintf
113 #elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
114 # error Need vsnprintf!
115 #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
116
117 #if !HAVE_VASPRINTF
118 /* CAW: compliant version of vasprintf */
119 static int vasprintf(char **buf, const char *fmt, va_list ap)
120 {
121 #ifndef WIN32
122         static char _T_emptybuffer = '\0';
123 #endif /* !defined(WIN32) */
124         int chars;
125         char *b;
126
127         if(!buf) { return -1; }
128
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) */
137
138         b = (char*)malloc(sizeof(char)*chars);
139         if(!b) { return -1; }
140
141         if((chars = vsprintf(b, fmt, ap)) < 0)
142         {
143                 free(b);
144         } else {
145                 *buf = b;
146         }
147
148         return chars;
149 }
150 #endif /* !HAVE_VASPRINTF */
151
152 int sprintbuf(struct printbuf *p, const char *msg, ...)
153 {
154   va_list ap;
155   char *t;
156   int size;
157   char buf[128];
158
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;
177   }
178 }
179
180 void printbuf_reset(struct printbuf *p)
181 {
182   p->buf[0] = '\0';
183   p->bpos = 0;
184 }
185
186 void printbuf_free(struct printbuf *p)
187 {
188   if(p) {
189     free(p->buf);
190     free(p);
191   }
192 }