mstring.c (264803) | mstring.c (266639) |
---|---|
1/* $Id: mstring.c,v 1.3 2014/04/08 20:37:26 tom Exp $ */ | 1/* $Id: mstring.c,v 1.6 2014/04/22 23:36:31 tom Exp $ */ |
2 3#include <stdlib.h> 4#include <stdio.h> 5#include <stdarg.h> 6#include <ctype.h> 7#include <string.h> 8#include "defs.h" 9 10/* parameters about string length. HEAD is the starting size and 11** HEAD+TAIL should be a power of two */ 12#define HEAD 24 13#define TAIL 8 14 15#if defined(YYBTYACC) | 2 3#include <stdlib.h> 4#include <stdio.h> 5#include <stdarg.h> 6#include <ctype.h> 7#include <string.h> 8#include "defs.h" 9 10/* parameters about string length. HEAD is the starting size and 11** HEAD+TAIL should be a power of two */ 12#define HEAD 24 13#define TAIL 8 14 15#if defined(YYBTYACC) |
16 17static char *buf_ptr; 18static size_t buf_len; 19 |
|
16void 17msprintf(struct mstring *s, const char *fmt,...) 18{ | 20void 21msprintf(struct mstring *s, const char *fmt,...) 22{ |
19 static char buf[4096]; /* a big static buffer */ | |
20 va_list args; 21 size_t len; | 23 va_list args; 24 size_t len; |
25#ifdef HAVE_VSNPRINTF 26 int changed; 27#endif |
|
22 23 if (!s || !s->base) 24 return; | 28 29 if (!s || !s->base) 30 return; |
31 32 if (buf_len == 0) 33 { 34 buf_ptr = malloc(buf_len = 4096); 35 } 36 if (buf_ptr == 0) 37 { 38 return; 39 } 40 41#ifdef HAVE_VSNPRINTF 42 do 43 { 44 va_start(args, fmt); 45 len = (size_t) vsnprintf(buf_ptr, buf_len, fmt, args); 46 va_end(args); 47 if ((changed = (len > buf_len)) != 0) 48 { 49 char *new_ptr = realloc(buf_ptr, (buf_len * 3) / 2); 50 if (new_ptr == 0) 51 { 52 free(buf_ptr); 53 buf_ptr = 0; 54 return; 55 } 56 buf_ptr = new_ptr; 57 } 58 } 59 while (changed); 60#else |
|
25 va_start(args, fmt); | 61 va_start(args, fmt); |
26 vsprintf(buf, fmt, args); | 62 len = (size_t) vsprintf(buf_ptr, fmt, args); |
27 va_end(args); | 63 va_end(args); |
64 if (len >= buf_len) 65 return; 66#endif |
|
28 | 67 |
29 len = strlen(buf); | |
30 if (len > (size_t) (s->end - s->ptr)) 31 { | 68 if (len > (size_t) (s->end - s->ptr)) 69 { |
70 char *new_base; |
|
32 size_t cp = (size_t) (s->ptr - s->base); 33 size_t cl = (size_t) (s->end - s->base); 34 size_t nl = cl; 35 while (len > (nl - cp)) 36 nl = nl + nl + TAIL; | 71 size_t cp = (size_t) (s->ptr - s->base); 72 size_t cl = (size_t) (s->end - s->base); 73 size_t nl = cl; 74 while (len > (nl - cp)) 75 nl = nl + nl + TAIL; |
37 if ((s->base = realloc(s->base, nl))) | 76 if ((new_base = realloc(s->base, nl))) |
38 { | 77 { |
78 s->base = new_base; |
|
39 s->ptr = s->base + cp; 40 s->end = s->base + nl; 41 } 42 else 43 { | 79 s->ptr = s->base + cp; 80 s->end = s->base + nl; 81 } 82 else 83 { |
44 s->ptr = s->end = 0; | 84 free(s->base); 85 s->base = 0; 86 s->ptr = 0; 87 s->end = 0; |
45 return; 46 } 47 } | 88 return; 89 } 90 } |
48 memcpy(s->ptr, buf, len); | 91 memcpy(s->ptr, buf_ptr, len); |
49 s->ptr += len; 50} 51#endif 52 53int 54mputchar(struct mstring *s, int ch) 55{ 56 if (!s || !s->base) --- 14 unchanged lines hidden (view full) --- 71 } 72 *s->ptr++ = (char)ch; 73 return ch; 74} 75 76struct mstring * 77msnew(void) 78{ | 92 s->ptr += len; 93} 94#endif 95 96int 97mputchar(struct mstring *s, int ch) 98{ 99 if (!s || !s->base) --- 14 unchanged lines hidden (view full) --- 114 } 115 *s->ptr++ = (char)ch; 116 return ch; 117} 118 119struct mstring * 120msnew(void) 121{ |
79 struct mstring *n = malloc(sizeof(struct mstring)); | 122 struct mstring *n = TMALLOC(struct mstring, 1); |
80 81 if (n) 82 { | 123 124 if (n) 125 { |
83 if ((n->base = n->ptr = malloc(HEAD)) != 0) | 126 if ((n->base = n->ptr = MALLOC(HEAD)) != 0) |
84 { 85 n->end = n->base + HEAD; 86 } 87 else 88 { 89 free(n); 90 n = 0; 91 } --- 53 unchanged lines hidden (view full) --- 145 { 146 if (!isspace(*s)) 147 h = (h << 5) - h + (unsigned char)*s; 148 s++; 149 } 150 return h; 151} 152#endif | 127 { 128 n->end = n->base + HEAD; 129 } 130 else 131 { 132 free(n); 133 n = 0; 134 } --- 53 unchanged lines hidden (view full) --- 188 { 189 if (!isspace(*s)) 190 h = (h << 5) - h + (unsigned char)*s; 191 s++; 192 } 193 return h; 194} 195#endif |
196 197#ifdef NO_LEAKS 198void 199mstring_leaks(void) 200{ 201#if defined(YYBTYACC) 202 free(buf_ptr); 203 buf_ptr = 0; 204 buf_len = 0; 205#endif 206} 207#endif |
|