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