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