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