Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 2 | * $Id: printbuf.c,v 1.4 2005/06/14 22:41:51 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
| 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 19 | #include "config.h" |
| 20 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 25 | #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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 31 | #include "bits.h" |
| 32 | #include "debug.h" |
| 33 | #include "printbuf.h" |
| 34 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 35 | struct 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 | |
| 50 | int 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 55 | #ifdef PRINTBUF_DEBUG |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 56 | 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 59 | #endif /* PRINTBUF_DEBUG */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 60 | 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 70 | #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 */ |
| 78 | static 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 110 | |
| 111 | int 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 Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame^] | 125 | would have been writen - this code handles both cases. */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 126 | 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 139 | void printbuf_reset(struct printbuf *p) |
| 140 | { |
| 141 | p->buf[0] = '\0'; |
| 142 | p->bpos = 0; |
| 143 | } |
| 144 | |
| 145 | void printbuf_free(struct printbuf *p) |
| 146 | { |
| 147 | if(p) { |
| 148 | free(p->buf); |
| 149 | free(p); |
| 150 | } |
| 151 | } |