blob: b951c7b4f417813df0d21514bfbec78c7c5027a5 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clarkf6a6e482007-03-13 08:26:23 +00002 * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00004 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
Michael Clarkf0d08882007-03-13 08:26:18 +00005 * Michael Clark <michael@metaparadigm.com>
6 *
Michael Clarkf6a6e482007-03-13 08:26:23 +00007 * 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.
Michael Clarkf0d08882007-03-13 08:26:18 +00009 *
Michael Clark95f55a72009-04-27 08:16:58 +000010 *
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)
Michael Clarkf0d08882007-03-13 08:26:18 +000014 */
15
Michael Clark4504df72007-03-13 08:26:20 +000016#include "config.h"
17
Michael Clarkf0d08882007-03-13 08:26:18 +000018#include <stdio.h>
19#include <stdlib.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000020#include <string.h>
21
Michael Clark4504df72007-03-13 08:26:20 +000022#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
Michael Clarkf0d08882007-03-13 08:26:18 +000028#include "bits.h"
29#include "debug.h"
30#include "printbuf.h"
31
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050032static int printbuf_extend(struct printbuf *p, int min_size);
33
Michael Clarke8de0782009-02-25 01:45:00 +000034struct printbuf* printbuf_new(void)
Michael Clarkf0d08882007-03-13 08:26:18 +000035{
36 struct printbuf *p;
37
Michael Clarkaaec1ef2009-02-25 02:31:32 +000038 p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
39 if(!p) return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000040 p->size = 32;
41 p->bpos = 0;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000042 if(!(p->buf = (char*)malloc(p->size))) {
Michael Clarkf0d08882007-03-13 08:26:18 +000043 free(p);
44 return NULL;
45 }
46 return p;
47}
48
49
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050050/**
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 */
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050058static 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
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050066 new_size = json_max(p->size * 2, min_size + 8);
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050067#ifdef PRINTBUF_DEBUG
68 MC_DEBUG("printbuf_memappend: realloc "
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050069 "bpos=%d min_size=%d old_size=%d new_size=%d\n",
70 p->bpos, min_size, p->size, new_size);
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050071#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
Michael Clark68cafad2009-01-06 22:56:57 +000079int printbuf_memappend(struct printbuf *p, const char *buf, int size)
Michael Clarkf0d08882007-03-13 08:26:18 +000080{
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050081 if (p->size <= p->bpos + size + 1) {
82 if (printbuf_extend(p, p->bpos + size + 1) < 0)
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050083 return -1;
Michael Clarkf0d08882007-03-13 08:26:18 +000084 }
85 memcpy(p->buf + p->bpos, buf, size);
86 p->bpos += size;
87 p->buf[p->bpos]= '\0';
88 return size;
89}
90
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050091int 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;
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050098 if (pb->size < size_needed)
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050099 {
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
John Arbash Meinel6a231e42012-02-01 09:27:49 +0100111#if !HAVE_VSNPRINTF && defined(_MSC_VER)
Michael Clark4504df72007-03-13 08:26:20 +0000112# 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 */
119static 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 */
Michael Clarkf0d08882007-03-13 08:26:18 +0000151
152int 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
Christopher Watford543bb142009-07-08 03:46:10 +0000166 would have been written - this code handles both cases. */
Michael Clarkf0d08882007-03-13 08:26:18 +0000167 if(size == -1 || size > 127) {
Michael Clarkf0d08882007-03-13 08:26:18 +0000168 va_start(ap, msg);
ehaszla252669c2010-12-07 18:15:35 +0000169 if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
Michael Clarkf0d08882007-03-13 08:26:18 +0000170 va_end(ap);
Michael Clark95f55a72009-04-27 08:16:58 +0000171 printbuf_memappend(p, t, size);
Michael Clarkf0d08882007-03-13 08:26:18 +0000172 free(t);
Michael Clark95f55a72009-04-27 08:16:58 +0000173 return size;
Michael Clarkf0d08882007-03-13 08:26:18 +0000174 } else {
Michael Clark95f55a72009-04-27 08:16:58 +0000175 printbuf_memappend(p, buf, size);
176 return size;
Michael Clarkf0d08882007-03-13 08:26:18 +0000177 }
178}
179
Michael Clarkf0d08882007-03-13 08:26:18 +0000180void printbuf_reset(struct printbuf *p)
181{
182 p->buf[0] = '\0';
183 p->bpos = 0;
184}
185
186void printbuf_free(struct printbuf *p)
187{
188 if(p) {
189 free(p->buf);
190 free(p);
191 }
192}