chiark / gitweb /
Various bug fixes: understand requests for help properly, and fix the
[mLib] / dstr.c
CommitLineData
0875b58f 1/* -*-c-*-
2 *
b5ea4de3 3 * $Id: dstr.c,v 1.15 2000/07/16 12:29:16 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 $
b5ea4de3 33 * Revision 1.15 2000/07/16 12:29:16 mdw
34 * Change to arena `realloc' interface, to fix a design bug.
35 *
20eb516f 36 * Revision 1.14 2000/06/17 10:37:39 mdw
37 * Add support for arena management.
38 *
9994cbdc 39 * Revision 1.13 1999/12/22 15:39:28 mdw
40 * Fix overflow in dstr_putline.
41 *
c6e0eaf0 42 * Revision 1.12 1999/12/10 23:42:04 mdw
43 * Change header file guard names.
44 *
f8509853 45 * Revision 1.11 1999/10/28 22:05:29 mdw
46 * Modify and debug allocation routines.
47 *
002eaee3 48 * Revision 1.10 1999/10/04 21:44:47 mdw
49 * Move `dstr_putf' and `dstr_vputf' into a separate source file.
50 *
3ca4c9f8 51 * Revision 1.9 1999/07/06 19:16:06 mdw
52 * Simplify buffer-growing algorithm. Just double it each time.
53 *
0fbb1412 54 * Revision 1.8 1999/06/01 09:47:52 mdw
55 * Fix nasty bugs in `dstr_vputf'.
56 *
0bfb1431 57 * Revision 1.7 1999/05/21 22:14:30 mdw
58 * Take advantage of the new dynamic string macros.
59 *
fb467548 60 * Revision 1.6 1999/05/21 08:38:33 mdw
61 * Implement some more functions in terms of macros.
62 *
96c5fe33 63 * Revision 1.5 1999/05/13 22:47:57 mdw
64 * Misc documentation fixes. Change `-ise' to `-ize' throughout.
65 *
0bd98442 66 * Revision 1.4 1999/05/06 19:51:35 mdw
67 * Reformatted the LGPL notice a little bit.
68 *
c846879c 69 * Revision 1.3 1999/05/05 18:50:31 mdw
70 * Change licensing conditions to LGPL.
71 *
00c7638b 72 * Revision 1.2 1998/12/15 23:53:22 mdw
73 * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
74 * formatting in a safe way.
75 *
76 * Revision 1.1.1.1 1998/06/17 23:44:42 mdw
77 * Initial version of mLib
0875b58f 78 *
79 */
80
81/*----- Header files ------------------------------------------------------*/
82
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86
87#include "alloc.h"
88#include "dstr.h"
89
90/*----- Tunable constants -------------------------------------------------*/
91
002eaee3 92/*
3ca4c9f8 93 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
94 * Otherwise, it's set to the next power of two that's large enough. This is
95 * memory-hungry, but efficient.
96 */
97
f8509853 98#define DSTR_INITSZ 64 /* Initial buffer size */
3ca4c9f8 99
0875b58f 100/*----- Main code ---------------------------------------------------------*/
101
102/* --- @dstr_create@ --- *
103 *
104 * Arguments: @dstr *d@ = pointer to a dynamic string block
105 *
106 * Returns: ---
107 *
96c5fe33 108 * Use: Initializes a dynamic string.
0875b58f 109 */
110
fb467548 111void dstr_create(dstr *d) { DCREATE(d); }
0875b58f 112
113/* --- @dstr_destroy@ --- *
114 *
115 * Arguments: @dstr *d@ = pointer to a dynamic string block
116 *
117 * Returns: ---
118 *
119 * Use: Reclaims the space used by a dynamic string.
120 */
121
fb467548 122void dstr_destroy(dstr *d) { DDESTROY(d); }
0875b58f 123
124/* --- @dstr_reset@ --- *
125 *
c6e0eaf0 126 * Arguments: @dstr *d@ = pointer to a dynamic string block
0875b58f 127 *
128 * Returns: ---
129 *
130 * Use: Resets a string so that new data gets put at the beginning.
131 */
132
fb467548 133void dstr_reset(dstr *d) { DRESET(d); }
0875b58f 134
135/* --- @dstr_ensure@ --- *
136 *
137 * Arguments: @dstr *d@ = pointer to a dynamic string block
138 * @size_t sz@ = amount of free space to ensure
139 *
140 * Returns: ---
141 *
142 * Use: Ensures that at least @sz@ bytes are available in the
143 * given string.
144 */
145
146void dstr_ensure(dstr *d, size_t sz)
147{
148 size_t rq = d->len + sz;
149 size_t nsz;
150
151 /* --- If we have enough space, just leave it --- */
152
153 if (rq <= d->sz)
154 return;
155
3ca4c9f8 156 /* --- Grow the buffer --- */
0875b58f 157
158 nsz = d->sz;
159
f8509853 160 if (nsz == 0)
161 nsz = (DSTR_INITSZ >> 1);
162 do nsz <<= 1; while (nsz < rq);
0875b58f 163
164 if (d->buf)
b5ea4de3 165 d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
0875b58f 166 else
20eb516f 167 d->buf = x_alloc(d->a, nsz);
0875b58f 168 d->sz = nsz;
169}
170
171/* --- @dstr_putc@ --- *
172 *
173 * Arguments: @dstr *d@ = pointer to a dynamic string block
174 * @char ch@ = character to append
175 *
176 * Returns: ---
177 *
178 * Use: Appends a character to a string.
179 */
180
fb467548 181void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
0875b58f 182
183/* --- @dstr_putz@ --- *
184 *
185 * Arguments: @dstr *d@ = pointer to a dynamic string block
186 *
187 * Returns: ---
188 *
189 * Use: Appends a null byte to a string. The null byte does not
190 * contribute to the string's length, and will be overwritten
191 * by subsequent `put' operations.
192 */
193
fb467548 194void dstr_putz(dstr *d) { DPUTZ(d); }
0875b58f 195
196/* --- @dstr_puts@ --- *
197 *
198 * Arguments: @dstr *d@ = pointer to a dynamic string block
199 * @const char *s@ = pointer to string to append
200 *
201 * Returns: ---
202 *
203 * Use: Appends a character string to a string. A trailing null
204 * byte is added, as for @dstr_putz@.
205 */
206
fb467548 207void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
0875b58f 208
209/* --- @dstr_putd@ --- *
210 *
211 * Arguments: @dstr *d@ = pointer to a dynamic string block
212 * @const dstr *s@ = pointer to a dynamic string to append
213 *
214 * Returns: ---
215 *
216 * Use: Appends a dynamic string to a string. A trailing null
217 * byte is added, as for @dstr_putz@.
218 */
219
fb467548 220void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
0875b58f 221
222/* --- @dstr_putm@ --- *
223 *
224 * Arguments: @dstr *d@ = pointer to a dynamic string block
225 * @const void *p@ = pointer to a block to append
226 * @size_t sz@ = size of the block
227 *
228 * Returns: Appends an arbitrary data block to a string. No trailing
229 * null is appended.
230 */
231
fb467548 232void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
0875b58f 233
234/* --- @dstr_tidy@ --- *
235 *
236 * Arguments: @dstr *d@ = pointer to a dynamic string block
237 *
238 * Returns: ---
239 *
240 * Use: Reduces the amount of memory used by a string. A trailing
241 * null byte is added, as for @dstr_putz@.
242 */
243
244void dstr_tidy(dstr *d)
245{
b5ea4de3 246 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
f8509853 247 d->buf[d->len] = 0;
0875b58f 248 d->sz = d->len + 1;
249}
250
251/* --- @dstr_putline@ --- *
252 *
253 * Arguments: @dstr *d@ = pointer to a dynamic string block
254 * @FILE *fp@ = a stream to read from
255 *
256 * Returns: The number of characters read into the buffer, or @EOF@ if
257 * end-of-file was reached before any characters were read.
258 *
259 * Use: Appends the next line from the given input stream to the
260 * string. A trailing newline is not added; a trailing null
261 * byte is appended, as for @dstr_putz@.
262 */
263
264int dstr_putline(dstr *d, FILE *fp)
265{
266 size_t left = d->sz - d->len;
267 size_t off = d->len;
268 int rd = 0;
269 int ch;
270
271 for (;;) {
272
0875b58f 273 /* --- Read the next byte --- */
274
275 ch = getc(fp);
276
277 /* --- End-of-file when no characters read is special --- */
278
279 if (ch == EOF && !rd)
280 return (EOF);
281
f8509853 282 /* --- Make sure there's some buffer space --- */
283
284 if (!left) {
9994cbdc 285 d->len = off;
f8509853 286 dstr_ensure(d, 1);
287 left = d->sz - off;
288 }
289
0875b58f 290 /* --- End-of-file or newline ends the loop --- */
291
292 if (ch == EOF || ch == '\n') {
293 d->buf[off] = 0;
294 d->len = off;
295 return rd;
296 }
297
298 /* --- Append the character and continue --- */
299
300 d->buf[off++] = ch;
301 left--; rd++;
302 }
303}
304
305/* --- @dstr_write@ --- *
306 *
307 * Arguments: @dstr *d@ = pointer to a dynamic string block
308 * @FILE *fp@ = a stream to write on
309 *
310 * Returns: The number of bytes written (as for @fwrite@).
311 *
312 * Use: Writes a dynamic string to a file.
313 */
314
fb467548 315size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
0875b58f 316
317/*----- That's all, folks -------------------------------------------------*/