Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 1 | /* |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 2 | * $Id: printbuf.c,v 1.5 2006/01/26 02:16:28 mclark Exp $ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 3 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 4 | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 5 | * Michael Clark <michael@metaparadigm.com> |
| 6 | * |
Michael Clark | f6a6e48 | 2007-03-13 08:26:23 +0000 | [diff] [blame] | 7 | * 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 9 | * |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 10 | * |
| 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
Mateusz Loskot | a6f39a3 | 2012-05-21 23:22:36 +0100 | [diff] [blame] | 22 | #ifdef HAVE_STDARG_H |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 23 | # include <stdarg.h> |
| 24 | #else /* !HAVE_STDARG_H */ |
| 25 | # error Not enough var arg support! |
| 26 | #endif /* HAVE_STDARG_H */ |
| 27 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 28 | #include "debug.h" |
| 29 | #include "printbuf.h" |
| 30 | |
Eric Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 31 | static int printbuf_extend(struct printbuf *p, int min_size); |
| 32 | |
Michael Clark | e8de078 | 2009-02-25 01:45:00 +0000 | [diff] [blame] | 33 | struct printbuf* printbuf_new(void) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 34 | { |
| 35 | struct printbuf *p; |
| 36 | |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 37 | p = (struct printbuf*)calloc(1, sizeof(struct printbuf)); |
| 38 | if(!p) return NULL; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 39 | p->size = 32; |
| 40 | p->bpos = 0; |
Michael Clark | aaec1ef | 2009-02-25 02:31:32 +0000 | [diff] [blame] | 41 | if(!(p->buf = (char*)malloc(p->size))) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 42 | free(p); |
| 43 | return NULL; |
| 44 | } |
| 45 | return p; |
| 46 | } |
| 47 | |
| 48 | |
Eric Haszlakiewicz | 0d79b53 | 2012-04-03 14:54:25 -0500 | [diff] [blame] | 49 | /** |
| 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 Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 57 | static 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 Haszlakiewicz | d4e81f9 | 2014-05-03 22:29:10 -0400 | [diff] [blame] | 65 | new_size = p->size * 2; |
| 66 | if (new_size < min_size + 8) |
| 67 | new_size = min_size + 8; |
Eric Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 68 | #ifdef PRINTBUF_DEBUG |
| 69 | MC_DEBUG("printbuf_memappend: realloc " |
Eric Haszlakiewicz | 0d79b53 | 2012-04-03 14:54:25 -0500 | [diff] [blame] | 70 | "bpos=%d min_size=%d old_size=%d new_size=%d\n", |
| 71 | p->bpos, min_size, p->size, new_size); |
Eric Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 72 | #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 Clark | 68cafad | 2009-01-06 22:56:57 +0000 | [diff] [blame] | 80 | int printbuf_memappend(struct printbuf *p, const char *buf, int size) |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 81 | { |
Eric Haszlakiewicz | 0d79b53 | 2012-04-03 14:54:25 -0500 | [diff] [blame] | 82 | if (p->size <= p->bpos + size + 1) { |
| 83 | if (printbuf_extend(p, p->bpos + size + 1) < 0) |
Eric Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 84 | return -1; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 85 | } |
| 86 | memcpy(p->buf + p->bpos, buf, size); |
| 87 | p->bpos += size; |
| 88 | p->buf[p->bpos]= '\0'; |
| 89 | return size; |
| 90 | } |
| 91 | |
Eric Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 92 | int 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 Haszlakiewicz | 0d79b53 | 2012-04-03 14:54:25 -0500 | [diff] [blame] | 99 | if (pb->size < size_needed) |
Eric Haszlakiewicz | 2d48543 | 2012-04-02 15:39:55 -0500 | [diff] [blame] | 100 | { |
| 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 Loskot | a6f39a3 | 2012-05-21 23:22:36 +0100 | [diff] [blame] | 112 | #if !defined(HAVE_VSNPRINTF) && defined(_MSC_VER) |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 113 | # define vsnprintf _vsnprintf |
Mateusz Loskot | a6f39a3 | 2012-05-21 23:22:36 +0100 | [diff] [blame] | 114 | #elif !defined(HAVE_VSNPRINTF) /* !HAVE_VSNPRINTF */ |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 115 | # error Need vsnprintf! |
| 116 | #endif /* !HAVE_VSNPRINTF && defined(WIN32) */ |
| 117 | |
Mateusz Loskot | a6f39a3 | 2012-05-21 23:22:36 +0100 | [diff] [blame] | 118 | #if !defined(HAVE_VASPRINTF) |
Michael Clark | 4504df7 | 2007-03-13 08:26:20 +0000 | [diff] [blame] | 119 | /* CAW: compliant version of vasprintf */ |
| 120 | static 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 Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 152 | |
| 153 | int 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 Watford | 543bb14 | 2009-07-08 03:46:10 +0000 | [diff] [blame] | 167 | would have been written - this code handles both cases. */ |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 168 | if(size == -1 || size > 127) { |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 169 | va_start(ap, msg); |
ehaszla | 252669c | 2010-12-07 18:15:35 +0000 | [diff] [blame] | 170 | if((size = vasprintf(&t, msg, ap)) < 0) { va_end(ap); return -1; } |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 171 | va_end(ap); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 172 | printbuf_memappend(p, t, size); |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 173 | free(t); |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 174 | return size; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 175 | } else { |
Michael Clark | 95f55a7 | 2009-04-27 08:16:58 +0000 | [diff] [blame] | 176 | printbuf_memappend(p, buf, size); |
| 177 | return size; |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Michael Clark | f0d0888 | 2007-03-13 08:26:18 +0000 | [diff] [blame] | 181 | void printbuf_reset(struct printbuf *p) |
| 182 | { |
| 183 | p->buf[0] = '\0'; |
| 184 | p->bpos = 0; |
| 185 | } |
| 186 | |
| 187 | void printbuf_free(struct printbuf *p) |
| 188 | { |
| 189 | if(p) { |
| 190 | free(p->buf); |
| 191 | free(p); |
| 192 | } |
| 193 | } |