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