blob: a9c711cebad0c4eecb8eceffbbc18f4032445c65 [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
Michael Clarke8de0782009-02-25 01:45:00 +000032struct printbuf* printbuf_new(void)
Michael Clarkf0d08882007-03-13 08:26:18 +000033{
34 struct printbuf *p;
35
Michael Clarkaaec1ef2009-02-25 02:31:32 +000036 p = (struct printbuf*)calloc(1, sizeof(struct printbuf));
37 if(!p) return NULL;
Michael Clarkf0d08882007-03-13 08:26:18 +000038 p->size = 32;
39 p->bpos = 0;
Michael Clarkaaec1ef2009-02-25 02:31:32 +000040 if(!(p->buf = (char*)malloc(p->size))) {
Michael Clarkf0d08882007-03-13 08:26:18 +000041 free(p);
42 return NULL;
43 }
44 return p;
45}
46
47
Michael Clark68cafad2009-01-06 22:56:57 +000048int printbuf_memappend(struct printbuf *p, const char *buf, int size)
Michael Clarkf0d08882007-03-13 08:26:18 +000049{
50 char *t;
51 if(p->size - p->bpos <= size) {
52 int new_size = max(p->size * 2, p->bpos + size + 8);
Michael Clark4504df72007-03-13 08:26:20 +000053#ifdef PRINTBUF_DEBUG
Michael Clarkdfaf6702007-10-25 02:26:00 +000054 MC_DEBUG("printbuf_memappend: realloc "
Michael Clarkf0d08882007-03-13 08:26:18 +000055 "bpos=%d wrsize=%d old_size=%d new_size=%d\n",
56 p->bpos, size, p->size, new_size);
Michael Clark4504df72007-03-13 08:26:20 +000057#endif /* PRINTBUF_DEBUG */
Michael Clarkaaec1ef2009-02-25 02:31:32 +000058 if(!(t = (char*)realloc(p->buf, new_size))) return -1;
Michael Clarkf0d08882007-03-13 08:26:18 +000059 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
Michael Clark4504df72007-03-13 08:26:20 +000068#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 */
76static 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 */
Michael Clarkf0d08882007-03-13 08:26:18 +0000108
109int 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
Michael Clark4504df72007-03-13 08:26:20 +0000123 would have been writen - this code handles both cases. */
Michael Clarkf0d08882007-03-13 08:26:18 +0000124 if(size == -1 || size > 127) {
Michael Clarkf0d08882007-03-13 08:26:18 +0000125 va_start(ap, msg);
Michael Clark95f55a72009-04-27 08:16:58 +0000126 if((size = vasprintf(&t, msg, ap)) == -1) return -1;
Michael Clarkf0d08882007-03-13 08:26:18 +0000127 va_end(ap);
Michael Clark95f55a72009-04-27 08:16:58 +0000128 printbuf_memappend(p, t, size);
Michael Clarkf0d08882007-03-13 08:26:18 +0000129 free(t);
Michael Clark95f55a72009-04-27 08:16:58 +0000130 return size;
Michael Clarkf0d08882007-03-13 08:26:18 +0000131 } else {
Michael Clark95f55a72009-04-27 08:16:58 +0000132 printbuf_memappend(p, buf, size);
133 return size;
Michael Clarkf0d08882007-03-13 08:26:18 +0000134 }
135}
136
Michael Clarkf0d08882007-03-13 08:26:18 +0000137void printbuf_reset(struct printbuf *p)
138{
139 p->buf[0] = '\0';
140 p->bpos = 0;
141}
142
143void printbuf_free(struct printbuf *p)
144{
145 if(p) {
146 free(p->buf);
147 free(p);
148 }
149}