Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 150 lines (130 sloc) 3.586 kB
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
1 /*
f6a6e48 @michaeljclark * Make headers C++ compatible by change *this to *obj
michaeljclark authored
2 * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
3 *
f6a6e48 @michaeljclark * Make headers C++ compatible by change *this to *obj
michaeljclark authored
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
5 * Michael Clark <michael@metaparadigm.com>
6 *
f6a6e48 @michaeljclark * Make headers C++ compatible by change *this to *obj
michaeljclark authored
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.
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
9 *
95f55a7 @michaeljclark optimizations to json_tokener_parse_ex(), printbuf_memappend()
michaeljclark authored
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)
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
14 */
15
4504df7 @michaeljclark * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
michaeljclark authored
16 #include "config.h"
17
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
4504df7 @michaeljclark * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
michaeljclark authored
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
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
28 #include "bits.h"
29 #include "debug.h"
30 #include "printbuf.h"
31
e8de078 @michaeljclark * Fixed warning reported by adding -Wstrict-prototypes
michaeljclark authored
32 struct printbuf* printbuf_new(void)
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
33 {
34 struct printbuf *p;
35
aaec1ef @michaeljclark * Don't use this as a variable, so we can compile with a C++ compiler
michaeljclark authored
36 p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
37 if(!p) return NULL;
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
38 p->size = 32;
39 p->bpos = 0;
aaec1ef @michaeljclark * Don't use this as a variable, so we can compile with a C++ compiler
michaeljclark authored
40 if(!(p->buf = (char*)malloc(p->size))) {
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
41 free(p);
42 return NULL;
43 }
44 return p;
45 }
46
47
68cafad @michaeljclark Add const correctness to public interfaces
michaeljclark authored
48 int printbuf_memappend(struct printbuf *p, const char *buf, int size)
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
49 {
50 char *t;
51 if(p->size - p->bpos <= size) {
7fb9b03 @michaeljclark * Rename min and max so we can never clash with C or C++ std library
michaeljclark authored
52 int new_size = json_max(p->size * 2, p->bpos + size + 8);
4504df7 @michaeljclark * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
michaeljclark authored
53 #ifdef PRINTBUF_DEBUG
dfaf670 @michaeljclark * Add macros to enable compiling out debug code
michaeljclark authored
54 MC_DEBUG("printbuf_memappend: realloc "
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
55 "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
56 p->bpos, size, p->size, new_size);
4504df7 @michaeljclark * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
michaeljclark authored
57 #endif /* PRINTBUF_DEBUG */
aaec1ef @michaeljclark * Don't use this as a variable, so we can compile with a C++ compiler
michaeljclark authored
58 if(!(t = (char*)realloc(p->buf, new_size))) return -1;
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
59 p->size = new_size;
60 p->buf = t;
61 }
62 memcpy(p->buf + p->bpos, buf, size);
63 p->bpos += size;
64 p->buf[p->bpos]= '\0';
65 return size;
66 }
67
4504df7 @michaeljclark * printbuf.c - C. Watford (christopher dot watford at gmail dot com)
michaeljclark authored
68 #if !HAVE_VSNPRINTF && defined(WIN32)
69 # define vsnprintf _vsnprintf
70 #elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
71 # error Need vsnprintf!
72 #endif /* !HAVE_VSNPRINTF && defined(WIN32) */
73
74 #if !HAVE_VASPRINTF
75 /* CAW: compliant version of vasprintf */
76 static int vasprintf(char **buf, const char *fmt, va_list ap)
77 {
78 #ifndef WIN32
79 static char _T_emptybuffer = '\0';
80 #endif /* !defined(WIN32) */
81 int chars;
82 char *b;
83
84 if(!buf) { return -1; }
85
86 #ifdef WIN32
87 chars = _vscprintf(fmt, ap)+1;
88 #else /* !defined(WIN32) */
89 /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
90 our buffer like on some 64bit sun systems.... but hey, its time to move on */
91 chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
92 if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
93 #endif /* defined(WIN32) */
94
95 b = (char*)malloc(sizeof(char)*chars);
96 if(!b) { return -1; }
97
98 if((chars = vsprintf(b, fmt, ap)) < 0)
99 {
100 free(b);
101 } else {
102 *buf = b;
103 }
104
105 return chars;
106 }
107 #endif /* !HAVE_VASPRINTF */
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
108
109 int sprintbuf(struct printbuf *p, const char *msg, ...)
110 {
111 va_list ap;
112 char *t;
113 int size;
114 char buf[128];
115
116 /* user stack buffer first */
117 va_start(ap, msg);
118 size = vsnprintf(buf, 128, msg, ap);
119 va_end(ap);
120 /* if string is greater than stack buffer, then use dynamic string
121 with vasprintf. Note: some implementation of vsnprintf return -1
122 if output is truncated whereas some return the number of bytes that
543bb14 @sixlettervariables Erik Hovland (3):
sixlettervariables authored
123 would have been written - this code handles both cases. */
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
124 if(size == -1 || size > 127) {
125 va_start(ap, msg);
252669c Simplify things by storing integer values only as int64_t's internall…
ehaszla authored
126 if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
127 va_end(ap);
95f55a7 @michaeljclark optimizations to json_tokener_parse_ex(), printbuf_memappend()
michaeljclark authored
128 printbuf_memappend(p, t, size);
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
129 free(t);
95f55a7 @michaeljclark optimizations to json_tokener_parse_ex(), printbuf_memappend()
michaeljclark authored
130 return size;
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
131 } else {
95f55a7 @michaeljclark optimizations to json_tokener_parse_ex(), printbuf_memappend()
michaeljclark authored
132 printbuf_memappend(p, buf, size);
133 return size;
f0d0888 @michaeljclark import of version 0.1
michaeljclark authored
134 }
135 }
136
137 void printbuf_reset(struct printbuf *p)
138 {
139 p->buf[0] = '\0';
140 p->bpos = 0;
141 }
142
143 void printbuf_free(struct printbuf *p)
144 {
145 if(p) {
146 free(p->buf);
147 free(p);
148 }
149 }
Something went wrong with that request. Please try again.