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