blob: fe952b4522d91ce44c3f7c4b67b1b79c8a64f98a [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
Mateusz Loskota6f39a32012-05-21 23:22:36 +010022#ifdef HAVE_STDARG_H
Michael Clark4504df72007-03-13 08:26:20 +000023# 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 "debug.h"
29#include "printbuf.h"
30
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050031static int printbuf_extend(struct printbuf *p, int min_size);
32
Michael Clarke8de0782009-02-25 01:45:00 +000033struct printbuf* printbuf_new(void)
Michael Clarkf0d08882007-03-13 08:26:18 +000034{
35 struct printbuf *p;
36
Michael Clarkaaec1ef2009-02-25 02:31:32 +000037 p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
38 if(!p) return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000039 p->size = 32;
40 p->bpos = 0;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000041 if(!(p->buf = (char*)malloc(p->size))) {
Michael Clarkf0d08882007-03-13 08:26:18 +000042 free(p);
43 return NULL;
44 }
45 return p;
46}
47
48
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050049/**
50 * Extend the buffer p so it has a size of at least min_size.
51 *
52 * If the current size is large enough, nothing is changed.
53 *
54 * Note: this does not check the available space! The caller
55 * is responsible for performing those calculations.
56 */
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050057static int printbuf_extend(struct printbuf *p, int min_size)
58{
59 char *t;
60 int new_size;
61
62 if (p->size >= min_size)
63 return 0;
64
Eric Haszlakiewiczd4e81f92014-05-03 22:29:10 -040065 new_size = p->size * 2;
66 if (new_size < min_size + 8)
67 new_size = min_size + 8;
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050068#ifdef PRINTBUF_DEBUG
69 MC_DEBUG("printbuf_memappend: realloc "
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050070 "bpos=%d min_size=%d old_size=%d new_size=%d\n",
71 p->bpos, min_size, p->size, new_size);
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050072#endif /* PRINTBUF_DEBUG */
73 if(!(t = (char*)realloc(p->buf, new_size)))
74 return -1;
75 p->size = new_size;
76 p->buf = t;
77 return 0;
78}
79
Michael Clark68cafad2009-01-06 22:56:57 +000080int printbuf_memappend(struct printbuf *p, const char *buf, int size)
Michael Clarkf0d08882007-03-13 08:26:18 +000081{
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050082 if (p->size <= p->bpos + size + 1) {
83 if (printbuf_extend(p, p->bpos + size + 1) < 0)
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050084 return -1;
Michael Clarkf0d08882007-03-13 08:26:18 +000085 }
86 memcpy(p->buf + p->bpos, buf, size);
87 p->bpos += size;
88 p->buf[p->bpos]= '\0';
89 return size;
90}
91
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -050092int printbuf_memset(struct printbuf *pb, int offset, int charvalue, int len)
93{
94 int size_needed;
95
96 if (offset == -1)
97 offset = pb->bpos;
98 size_needed = offset + len;
Eric Haszlakiewicz0d79b532012-04-03 14:54:25 -050099 if (pb->size < size_needed)
Eric Haszlakiewicz2d485432012-04-02 15:39:55 -0500100 {
101 if (printbuf_extend(pb, size_needed) < 0)
102 return -1;
103 }
104
105 memset(pb->buf + offset, charvalue, len);
106 if (pb->bpos < size_needed)
107 pb->bpos = size_needed;
108
109 return 0;
110}
111
Mateusz Loskota6f39a32012-05-21 23:22:36 +0100112#if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER)
Michael Clark4504df72007-03-13 08:26:20 +0000113# define vsnprintf _vsnprintf
Mateusz Loskota6f39a32012-05-21 23:22:36 +0100114#elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */
Michael Clark4504df72007-03-13 08:26:20 +0000115# error Need vsnprintf!
116#endif /* !HAVE_VSNPRINTF && defined(WIN32) */
117
Mateusz Loskota6f39a32012-05-21 23:22:36 +0100118#if !defined(HAVE_VASPRINTF)
Michael Clark4504df72007-03-13 08:26:20 +0000119/* CAW: compliant version of vasprintf */
120static int vasprintf(char **buf, const char *fmt, va_list ap)
121{
122#ifndef WIN32
123 static char _T_emptybuffer = '\0';
124#endif /* !defined(WIN32) */
125 int chars;
126 char *b;
127
128 if(!buf) { return -1; }
129
130#ifdef WIN32
131 chars = _vscprintf(fmt, ap)+1;
132#else /* !defined(WIN32) */
133 /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
134 our buffer like on some 64bit sun systems.... but hey, its time to move on */
135 chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
136 if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
137#endif /* defined(WIN32) */
138
139 b = (char*)malloc(sizeof(char)*chars);
140 if(!b) { return -1; }
141
142 if((chars = vsprintf(b, fmt, ap)) < 0)
143 {
144 free(b);
145 } else {
146 *buf = b;
147 }
148
149 return chars;
150}
151#endif /* !HAVE_VASPRINTF */
Michael Clarkf0d08882007-03-13 08:26:18 +0000152
153int sprintbuf(struct printbuf *p, const char *msg, ...)
154{
155 va_list ap;
156 char *t;
157 int size;
158 char buf[128];
159
160 /* user stack buffer first */
161 va_start(ap, msg);
162 size = vsnprintf(buf, 128, msg, ap);
163 va_end(ap);
164 /* if string is greater than stack buffer, then use dynamic string
165 with vasprintf. Note: some implementation of vsnprintf return -1
166 if output is truncated whereas some return the number of bytes that
Christopher Watford543bb142009-07-08 03:46:10 +0000167 would have been written - this code handles both cases. */
Michael Clarkf0d08882007-03-13 08:26:18 +0000168 if(size == -1 || size > 127) {
Michael Clarkf0d08882007-03-13 08:26:18 +0000169 va_start(ap, msg);
ehaszla252669c2010-12-07 18:15:35 +0000170 if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; }
Michael Clarkf0d08882007-03-13 08:26:18 +0000171 va_end(ap);
Michael Clark95f55a72009-04-27 08:16:58 +0000172 printbuf_memappend(p, t, size);
Michael Clarkf0d08882007-03-13 08:26:18 +0000173 free(t);
Michael Clark95f55a72009-04-27 08:16:58 +0000174 return size;
Michael Clarkf0d08882007-03-13 08:26:18 +0000175 } else {
Michael Clark95f55a72009-04-27 08:16:58 +0000176 printbuf_memappend(p, buf, size);
177 return size;
Michael Clarkf0d08882007-03-13 08:26:18 +0000178 }
179}
180
Michael Clarkf0d08882007-03-13 08:26:18 +0000181void printbuf_reset(struct printbuf *p)
182{
183 p->buf[0] = '\0';
184 p->bpos = 0;
185}
186
187void printbuf_free(struct printbuf *p)
188{
189 if(p) {
190 free(p->buf);
191 free(p);
192 }
193}