0875b58f |
1 | /* -*-c-*- |
2 | * |
0bfb1431 |
3 | * $Id: dstr.c,v 1.7 1999/05/21 22:14:30 mdw Exp $ |
0875b58f |
4 | * |
5 | * Handle dynamically growing strings |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
c846879c |
10 | /*----- Licensing notice --------------------------------------------------* |
0875b58f |
11 | * |
12 | * This file is part of the mLib utilities library. |
13 | * |
14 | * mLib is free software; you can redistribute it and/or modify |
c846879c |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
0875b58f |
19 | * mLib is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c846879c |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
0bd98442 |
25 | * License along with mLib; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
0875b58f |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: dstr.c,v $ |
0bfb1431 |
33 | * Revision 1.7 1999/05/21 22:14:30 mdw |
34 | * Take advantage of the new dynamic string macros. |
35 | * |
fb467548 |
36 | * Revision 1.6 1999/05/21 08:38:33 mdw |
37 | * Implement some more functions in terms of macros. |
38 | * |
96c5fe33 |
39 | * Revision 1.5 1999/05/13 22:47:57 mdw |
40 | * Misc documentation fixes. Change `-ise' to `-ize' throughout. |
41 | * |
0bd98442 |
42 | * Revision 1.4 1999/05/06 19:51:35 mdw |
43 | * Reformatted the LGPL notice a little bit. |
44 | * |
c846879c |
45 | * Revision 1.3 1999/05/05 18:50:31 mdw |
46 | * Change licensing conditions to LGPL. |
47 | * |
00c7638b |
48 | * Revision 1.2 1998/12/15 23:53:22 mdw |
49 | * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style |
50 | * formatting in a safe way. |
51 | * |
52 | * Revision 1.1.1.1 1998/06/17 23:44:42 mdw |
53 | * Initial version of mLib |
0875b58f |
54 | * |
55 | */ |
56 | |
57 | /*----- Header files ------------------------------------------------------*/ |
58 | |
00c7638b |
59 | #include <ctype.h> |
60 | #include <float.h> |
61 | #include <math.h> |
62 | #include <stdarg.h> |
0875b58f |
63 | #include <stdio.h> |
64 | #include <stdlib.h> |
65 | #include <string.h> |
66 | |
67 | #include "alloc.h" |
68 | #include "dstr.h" |
69 | |
70 | /*----- Tunable constants -------------------------------------------------*/ |
71 | |
72 | #define DSTR_INITSZ 256 /* Initial buffer size */ |
00c7638b |
73 | #define DSTR_INCSZ 4096 /* Threshhold for doubling */ |
74 | #define DSTR_PUTFSTEP 64 /* Buffer size for @putf@ */ |
0875b58f |
75 | |
76 | /*----- Main code ---------------------------------------------------------*/ |
77 | |
78 | /* --- @dstr_create@ --- * |
79 | * |
80 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
81 | * |
82 | * Returns: --- |
83 | * |
96c5fe33 |
84 | * Use: Initializes a dynamic string. |
0875b58f |
85 | */ |
86 | |
fb467548 |
87 | void dstr_create(dstr *d) { DCREATE(d); } |
0875b58f |
88 | |
89 | /* --- @dstr_destroy@ --- * |
90 | * |
91 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
92 | * |
93 | * Returns: --- |
94 | * |
95 | * Use: Reclaims the space used by a dynamic string. |
96 | */ |
97 | |
fb467548 |
98 | void dstr_destroy(dstr *d) { DDESTROY(d); } |
0875b58f |
99 | |
100 | /* --- @dstr_reset@ --- * |
101 | * |
102 | * Arguments: @dstr *d@ = pointer to a dynaimc string block |
103 | * |
104 | * Returns: --- |
105 | * |
106 | * Use: Resets a string so that new data gets put at the beginning. |
107 | */ |
108 | |
fb467548 |
109 | void dstr_reset(dstr *d) { DRESET(d); } |
0875b58f |
110 | |
111 | /* --- @dstr_ensure@ --- * |
112 | * |
113 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
114 | * @size_t sz@ = amount of free space to ensure |
115 | * |
116 | * Returns: --- |
117 | * |
118 | * Use: Ensures that at least @sz@ bytes are available in the |
119 | * given string. |
120 | */ |
121 | |
122 | void dstr_ensure(dstr *d, size_t sz) |
123 | { |
124 | size_t rq = d->len + sz; |
125 | size_t nsz; |
126 | |
127 | /* --- If we have enough space, just leave it --- */ |
128 | |
129 | if (rq <= d->sz) |
130 | return; |
131 | |
132 | /* --- Grow the buffer --- * |
133 | * |
134 | * For small buffers, just double the size. For big buffers, make them |
135 | * a multiple of some suitably large chunk size. |
136 | */ |
137 | |
138 | nsz = d->sz; |
139 | |
140 | do { |
141 | if (nsz == 0) |
142 | nsz = DSTR_INITSZ; |
143 | else if (d->sz < 0x1000) |
144 | nsz <<= 1; |
145 | else |
146 | nsz = (rq + 0x0fff) & ~0x0fff; |
147 | } while (rq > nsz); |
148 | |
149 | if (d->buf) |
150 | d->buf = xrealloc(d->buf, nsz); |
151 | else |
152 | d->buf = xmalloc(nsz); |
153 | d->sz = nsz; |
154 | } |
155 | |
156 | /* --- @dstr_putc@ --- * |
157 | * |
158 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
159 | * @char ch@ = character to append |
160 | * |
161 | * Returns: --- |
162 | * |
163 | * Use: Appends a character to a string. |
164 | */ |
165 | |
fb467548 |
166 | void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); } |
0875b58f |
167 | |
168 | /* --- @dstr_putz@ --- * |
169 | * |
170 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
171 | * |
172 | * Returns: --- |
173 | * |
174 | * Use: Appends a null byte to a string. The null byte does not |
175 | * contribute to the string's length, and will be overwritten |
176 | * by subsequent `put' operations. |
177 | */ |
178 | |
fb467548 |
179 | void dstr_putz(dstr *d) { DPUTZ(d); } |
0875b58f |
180 | |
181 | /* --- @dstr_puts@ --- * |
182 | * |
183 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
184 | * @const char *s@ = pointer to string to append |
185 | * |
186 | * Returns: --- |
187 | * |
188 | * Use: Appends a character string to a string. A trailing null |
189 | * byte is added, as for @dstr_putz@. |
190 | */ |
191 | |
fb467548 |
192 | void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); } |
0875b58f |
193 | |
00c7638b |
194 | /* --- @dstr_vputf@ --- * |
195 | * |
196 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
197 | * @const char *p@ = pointer to @printf@-style format string |
198 | * @va_list ap@ = argument handle |
199 | * |
96c5fe33 |
200 | * Returns: The number of characters written to the string. |
00c7638b |
201 | * |
202 | * Use: As for @dstr_putf@, but may be used as a back-end to user- |
203 | * supplied functions with @printf@-style interfaces. |
204 | */ |
205 | |
206 | int dstr_vputf(dstr *d, const char *p, va_list ap) |
207 | { |
208 | const char *q = p; |
209 | size_t n = d->len; |
210 | size_t sz; |
0bfb1431 |
211 | dstr dd = DSTR_INIT; |
00c7638b |
212 | |
213 | while (*p) { |
214 | unsigned f; |
215 | int wd, prec; |
00c7638b |
216 | |
217 | enum { |
218 | f_short = 1, |
219 | f_long = 2, |
220 | f_Long = 4, |
221 | f_wd = 8, |
222 | f_prec = 16 |
223 | }; |
224 | |
225 | /* --- Most stuff gets passed on through --- */ |
226 | |
227 | if (*p != '%') { |
228 | p++; |
229 | continue; |
230 | } |
231 | |
232 | /* --- Dump out what's between @q@ and @p@ --- */ |
233 | |
234 | DPUTM(d, q, p - q); |
235 | p++; |
236 | |
237 | /* --- Sort out the various silly flags and things --- */ |
238 | |
00c7638b |
239 | DPUTC(&dd, '%'); |
240 | f = 0; |
241 | sz = DSTR_PUTFSTEP; |
242 | |
243 | for (;;) { |
244 | switch (*p) { |
245 | |
246 | /* --- Various simple flags --- */ |
247 | |
248 | case '+': |
249 | case '-': |
250 | case '#': |
251 | case '0': |
252 | goto putch; |
253 | case 'h': |
254 | f |= f_short; |
255 | goto putch; |
256 | case 'l': |
257 | f |= f_long; |
258 | goto putch; |
259 | case 'L': |
260 | f |= f_Long; |
261 | goto putch; |
262 | case 0: |
263 | goto finished; |
264 | |
265 | /* --- Field widths and precision specifiers --- */ |
266 | |
267 | { |
268 | int *ip; |
269 | |
270 | case '.': |
271 | DPUTC(&dd, '.'); |
272 | ip = ≺ |
273 | f |= f_prec; |
274 | goto getnum; |
275 | case '*': |
276 | ip = &wd; |
277 | f |= f_wd; |
278 | goto getnum; |
279 | default: |
280 | if (isdigit((unsigned char)*p)) { |
281 | f |= f_prec; |
282 | ip = &wd; |
283 | goto getnum; |
284 | } |
285 | DPUTC(d, *p); |
286 | goto formatted; |
287 | getnum: |
288 | *ip = 0; |
289 | if (*p == '*') { |
290 | *ip = va_arg(ap, int); |
291 | DENSURE(&dd, DSTR_PUTFSTEP); |
292 | dd.len += sprintf(dd.buf + dd.len, "%i", *ip); |
293 | } else { |
294 | *ip = *p + '0'; |
295 | DPUTC(&dd, *p); |
296 | p++; |
297 | while (isdigit((unsigned char)*p)) { |
298 | DPUTC(&dd, *p); |
299 | *ip = 10 * *ip + *p++ + '0'; |
300 | } |
301 | } |
302 | break; |
303 | } |
304 | |
305 | /* --- Output formatting --- */ |
306 | |
307 | case 'd': case 'i': case 'x': case 'X': case 'o': case 'u': |
308 | DPUTC(&dd, *p); |
309 | DPUTZ(&dd); |
310 | if ((f & f_prec) && prec + 16 > sz) |
311 | sz = prec + 16; |
312 | if ((f & f_wd) && wd + 1> sz) |
313 | sz = wd + 1; |
314 | DENSURE(d, sz); |
315 | if (f & f_long) |
316 | d->len += sprintf(d->buf + d->len, dd.buf, |
317 | va_arg(ap, unsigned long)); |
318 | else |
319 | d->len += sprintf(d->buf + d->len, dd.buf, |
320 | va_arg(ap, unsigned int)); |
321 | goto formatted; |
322 | |
323 | case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': |
324 | DPUTC(&dd, *p); |
325 | DPUTZ(&dd); |
326 | if (*p == 'f') { |
327 | size_t mx = (f & f_Long ? LDBL_MAX_10_EXP : DBL_MAX_10_EXP) + 16; |
328 | if (mx > sz) |
329 | sz = mx; |
330 | } |
331 | if ((f & f_prec) == 0) |
332 | prec = 6; |
333 | if ((f & f_prec)) |
334 | sz += prec + 16; |
335 | if ((f & f_wd) && wd + 1 > sz) |
336 | sz = wd + 1; |
337 | DENSURE(d, sz); |
338 | if (f & f_Long) |
339 | d->len += sprintf(d->buf + d->len, dd.buf, |
340 | va_arg(ap, long double)); |
341 | else |
342 | d->len += sprintf(d->buf + d->len, dd.buf, |
343 | va_arg(ap, double)); |
344 | goto formatted; |
345 | |
346 | case 'c': |
347 | DPUTC(&dd, *p); |
348 | DPUTZ(&dd); |
349 | if ((f & f_wd) && wd + 1> sz) |
350 | sz = wd + 1; |
351 | DENSURE(d, sz); |
352 | d->len += sprintf(d->buf + d->len, dd.buf, |
353 | va_arg(ap, unsigned char)); |
354 | goto formatted; |
355 | |
356 | case 's': { |
357 | const char *s = va_arg(ap, const char *); |
358 | sz = strlen(s); |
359 | DPUTC(&dd, *p); |
360 | DPUTZ(&dd); |
361 | if (f & f_prec) |
362 | sz = prec; |
363 | if ((f & f_wd) && wd > sz) |
364 | sz = wd; |
365 | DENSURE(d, sz + 1); |
366 | d->len += sprintf(d->buf + d->len, dd.buf, s); |
367 | goto formatted; |
368 | } |
369 | |
370 | case 'p': |
371 | DPUTC(&dd, *p); |
372 | DPUTZ(&dd); |
373 | if ((f & f_prec) && prec + 16 > sz) |
374 | sz = prec + 16; |
375 | if ((f & f_wd) && wd + 1> sz) |
376 | sz = wd + 1; |
377 | DENSURE(d, sz); |
378 | d->len += sprintf(d->buf + d->len, dd.buf, |
379 | va_arg(ap, const void *)); |
380 | goto formatted; |
381 | |
382 | case 'n': |
383 | if (f & f_long) |
384 | *va_arg(ap, long *) = (long)(d->len - n); |
385 | else if (f & f_short) |
386 | *va_arg(ap, short *) = (short)(d->len - n); |
387 | else |
388 | *va_arg(ap, int *) = (int)(d->len - n); |
389 | goto formatted; |
390 | |
391 | /* --- Other random stuff --- */ |
392 | |
393 | putch: |
394 | DPUTC(&dd, *p); |
395 | p++; |
396 | break; |
397 | } |
398 | } |
399 | |
400 | formatted: |
0bfb1431 |
401 | DRESET(&dd); |
00c7638b |
402 | q = ++p; |
403 | } |
404 | |
405 | DPUTM(d, q, p - q); |
406 | finished: |
407 | DPUTZ(d); |
0bfb1431 |
408 | DDESTROY(&dd); |
00c7638b |
409 | return (d->len - n); |
410 | } |
411 | |
412 | /* --- @dstr_putf@ --- * |
413 | * |
414 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
415 | * @const char *p@ = pointer to @printf@-style format string |
416 | * @...@ = argument handle |
417 | * |
96c5fe33 |
418 | * Returns: The number of characters written to the string. |
00c7638b |
419 | * |
420 | * Use: Writes a piece of text to a dynamic string, doing @printf@- |
421 | * style substitutions as it goes. Intended to be robust if |
422 | * faced with malicious arguments, but not if the format string |
423 | * itself is malicious. |
424 | */ |
425 | |
426 | int dstr_putf(dstr *d, const char *p, ...) |
427 | { |
428 | int n; |
429 | va_list ap; |
430 | va_start(ap, p); |
431 | n = dstr_vputf(d, p, ap); |
432 | va_end(ap); |
433 | return (n); |
434 | } |
435 | |
0875b58f |
436 | /* --- @dstr_putd@ --- * |
437 | * |
438 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
439 | * @const dstr *s@ = pointer to a dynamic string to append |
440 | * |
441 | * Returns: --- |
442 | * |
443 | * Use: Appends a dynamic string to a string. A trailing null |
444 | * byte is added, as for @dstr_putz@. |
445 | */ |
446 | |
fb467548 |
447 | void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); } |
0875b58f |
448 | |
449 | /* --- @dstr_putm@ --- * |
450 | * |
451 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
452 | * @const void *p@ = pointer to a block to append |
453 | * @size_t sz@ = size of the block |
454 | * |
455 | * Returns: Appends an arbitrary data block to a string. No trailing |
456 | * null is appended. |
457 | */ |
458 | |
fb467548 |
459 | void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); } |
0875b58f |
460 | |
461 | /* --- @dstr_tidy@ --- * |
462 | * |
463 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
464 | * |
465 | * Returns: --- |
466 | * |
467 | * Use: Reduces the amount of memory used by a string. A trailing |
468 | * null byte is added, as for @dstr_putz@. |
469 | */ |
470 | |
471 | void dstr_tidy(dstr *d) |
472 | { |
473 | dstr_putz(d); |
474 | d->buf = xrealloc(d->buf, d->len + 1); |
475 | d->sz = d->len + 1; |
476 | } |
477 | |
478 | /* --- @dstr_putline@ --- * |
479 | * |
480 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
481 | * @FILE *fp@ = a stream to read from |
482 | * |
483 | * Returns: The number of characters read into the buffer, or @EOF@ if |
484 | * end-of-file was reached before any characters were read. |
485 | * |
486 | * Use: Appends the next line from the given input stream to the |
487 | * string. A trailing newline is not added; a trailing null |
488 | * byte is appended, as for @dstr_putz@. |
489 | */ |
490 | |
491 | int dstr_putline(dstr *d, FILE *fp) |
492 | { |
493 | size_t left = d->sz - d->len; |
494 | size_t off = d->len; |
495 | int rd = 0; |
496 | int ch; |
497 | |
498 | for (;;) { |
499 | |
500 | /* --- Make sure there's some buffer space --- */ |
501 | |
502 | if (!left) { |
503 | dstr_ensure(d, 1); |
504 | left = d->sz - off; |
505 | } |
506 | |
507 | /* --- Read the next byte --- */ |
508 | |
509 | ch = getc(fp); |
510 | |
511 | /* --- End-of-file when no characters read is special --- */ |
512 | |
513 | if (ch == EOF && !rd) |
514 | return (EOF); |
515 | |
516 | /* --- End-of-file or newline ends the loop --- */ |
517 | |
518 | if (ch == EOF || ch == '\n') { |
519 | d->buf[off] = 0; |
520 | d->len = off; |
521 | return rd; |
522 | } |
523 | |
524 | /* --- Append the character and continue --- */ |
525 | |
526 | d->buf[off++] = ch; |
527 | left--; rd++; |
528 | } |
529 | } |
530 | |
531 | /* --- @dstr_write@ --- * |
532 | * |
533 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
534 | * @FILE *fp@ = a stream to write on |
535 | * |
536 | * Returns: The number of bytes written (as for @fwrite@). |
537 | * |
538 | * Use: Writes a dynamic string to a file. |
539 | */ |
540 | |
fb467548 |
541 | size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); } |
0875b58f |
542 | |
543 | /*----- That's all, folks -------------------------------------------------*/ |