blob: 7e70fb0d1b409df1d87f7988bd1046b73a7da2c1 [file] [log] [blame]
Michael Clarkf0d08882007-03-13 08:26:18 +00001/*
Michael Clark4504df72007-03-13 08:26:20 +00002 * $Id: printbuf.c,v 1.4 2005/06/14 22:41:51 mclark Exp $
Michael Clarkf0d08882007-03-13 08:26:18 +00003 *
4 * Copyright Metaparadigm Pte. Ltd. 2004.
5 * Michael Clark <michael@metaparadigm.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public (LGPL)
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details: http://www.gnu.org/
16 *
17 */
18
Michael Clark4504df72007-03-13 08:26:20 +000019#include "config.h"
20
Michael Clarkf0d08882007-03-13 08:26:18 +000021#include <stdio.h>
22#include <stdlib.h>
Michael Clarkf0d08882007-03-13 08:26:18 +000023#include <string.h>
24
Michael Clark4504df72007-03-13 08:26:20 +000025#if HAVE_STDARG_H
26# include <stdarg.h>
27#else /* !HAVE_STDARG_H */
28# error Not enough var arg support!
29#endif /* HAVE_STDARG_H */
30
Michael Clarkf0d08882007-03-13 08:26:18 +000031#include "bits.h"
32#include "debug.h"
33#include "printbuf.h"
34
Michael Clarkf0d08882007-03-13 08:26:18 +000035struct printbuf* printbuf_new()
36{
37 struct printbuf *p;
38
39 if(!(p = calloc(1, sizeof(struct printbuf)))) return NULL;
40 p->size = 32;
41 p->bpos = 0;
42 if(!(p->buf = malloc(p->size))) {
43 free(p);
44 return NULL;
45 }
46 return p;
47}
48
49
50int printbuf_memappend(struct printbuf *p, char *buf, int size)
51{
52 char *t;
53 if(p->size - p->bpos <= size) {
54 int new_size = max(p->size * 2, p->bpos + size + 8);
Michael Clark4504df72007-03-13 08:26:20 +000055#ifdef PRINTBUF_DEBUG
Michael Clarkf0d08882007-03-13 08:26:18 +000056 mc_debug("printbuf_memappend: realloc "
57 "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
58 p->bpos, size, p->size, new_size);
Michael Clark4504df72007-03-13 08:26:20 +000059#endif /* PRINTBUF_DEBUG */
Michael Clarkf0d08882007-03-13 08:26:18 +000060 if(!(t = realloc(p->buf, new_size))) return -1;
61 p->size = new_size;
62 p->buf = t;
63 }
64 memcpy(p->buf + p->bpos, buf, size);
65 p->bpos += size;
66 p->buf[p->bpos]= '\0';
67 return size;
68}
69
Michael Clark4504df72007-03-13 08:26:20 +000070#if !HAVE_VSNPRINTF && defined(WIN32)
71# define vsnprintf _vsnprintf
72#elif !HAVE_VSNPRINTF /* !HAVE_VSNPRINTF */
73# error Need vsnprintf!
74#endif /* !HAVE_VSNPRINTF && defined(WIN32) */
75
76#if !HAVE_VASPRINTF
77/* CAW: compliant version of vasprintf */
78static int vasprintf(char **buf, const char *fmt, va_list ap)
79{
80#ifndef WIN32
81 static char _T_emptybuffer = '\0';
82#endif /* !defined(WIN32) */
83 int chars;
84 char *b;
85
86 if(!buf) { return -1; }
87
88#ifdef WIN32
89 chars = _vscprintf(fmt, ap)+1;
90#else /* !defined(WIN32) */
91 /* CAW: RAWR! We have to hope to god here that vsnprintf doesn't overwrite
92 our buffer like on some 64bit sun systems.... but hey, its time to move on */
93 chars = vsnprintf(&_T_emptybuffer, 0, fmt, ap)+1;
94 if(chars < 0) { chars *= -1; } /* CAW: old glibc versions have this problem */
95#endif /* defined(WIN32) */
96
97 b = (char*)malloc(sizeof(char)*chars);
98 if(!b) { return -1; }
99
100 if((chars = vsprintf(b, fmt, ap)) < 0)
101 {
102 free(b);
103 } else {
104 *buf = b;
105 }
106
107 return chars;
108}
109#endif /* !HAVE_VASPRINTF */
Michael Clarkf0d08882007-03-13 08:26:18 +0000110
111int sprintbuf(struct printbuf *p, const char *msg, ...)
112{
113 va_list ap;
114 char *t;
115 int size;
116 char buf[128];
117
118 /* user stack buffer first */
119 va_start(ap, msg);
120 size = vsnprintf(buf, 128, msg, ap);
121 va_end(ap);
122 /* if string is greater than stack buffer, then use dynamic string
123 with vasprintf. Note: some implementation of vsnprintf return -1
124 if output is truncated whereas some return the number of bytes that
Michael Clark4504df72007-03-13 08:26:20 +0000125 would have been writen - this code handles both cases. */
Michael Clarkf0d08882007-03-13 08:26:18 +0000126 if(size == -1 || size > 127) {
127 int ret;
128 va_start(ap, msg);
129 if((size = vasprintf(&t, msg, ap)) == -1) return -1;
130 va_end(ap);
131 ret = printbuf_memappend(p, t, size);
132 free(t);
133 return ret;
134 } else {
135 return printbuf_memappend(p, buf, size);
136 }
137}
138
Michael Clarkf0d08882007-03-13 08:26:18 +0000139void printbuf_reset(struct printbuf *p)
140{
141 p->buf[0] = '\0';
142 p->bpos = 0;
143}
144
145void printbuf_free(struct printbuf *p)
146{
147 if(p) {
148 free(p->buf);
149 free(p);
150 }
151}