chiark / gitweb /
Fix name of DOUINTSZ.
[mLib] / dstr.c
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: dstr.c,v 1.16 2004/04/08 01:36:11 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/*----- Header files ------------------------------------------------------*/
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "alloc.h"
37#include "dstr.h"
38
39/*----- Tunable constants -------------------------------------------------*/
40
41/*
42 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
43 * Otherwise, it's set to the next power of two that's large enough. This is
44 * memory-hungry, but efficient.
45 */
46
47#define DSTR_INITSZ 64 /* Initial buffer size */
48
49/*----- Main code ---------------------------------------------------------*/
50
51/* --- @dstr_create@ --- *
52 *
53 * Arguments: @dstr *d@ = pointer to a dynamic string block
54 *
55 * Returns: ---
56 *
57 * Use: Initializes a dynamic string.
58 */
59
60void dstr_create(dstr *d) { DCREATE(d); }
61
62/* --- @dstr_destroy@ --- *
63 *
64 * Arguments: @dstr *d@ = pointer to a dynamic string block
65 *
66 * Returns: ---
67 *
68 * Use: Reclaims the space used by a dynamic string.
69 */
70
71void dstr_destroy(dstr *d) { DDESTROY(d); }
72
73/* --- @dstr_reset@ --- *
74 *
75 * Arguments: @dstr *d@ = pointer to a dynamic string block
76 *
77 * Returns: ---
78 *
79 * Use: Resets a string so that new data gets put at the beginning.
80 */
81
82void dstr_reset(dstr *d) { DRESET(d); }
83
84/* --- @dstr_ensure@ --- *
85 *
86 * Arguments: @dstr *d@ = pointer to a dynamic string block
87 * @size_t sz@ = amount of free space to ensure
88 *
89 * Returns: ---
90 *
91 * Use: Ensures that at least @sz@ bytes are available in the
92 * given string.
93 */
94
95void dstr_ensure(dstr *d, size_t sz)
96{
97 size_t rq = d->len + sz;
98 size_t nsz;
99
100 /* --- If we have enough space, just leave it --- */
101
102 if (rq <= d->sz)
103 return;
104
105 /* --- Grow the buffer --- */
106
107 nsz = d->sz;
108
109 if (nsz == 0)
110 nsz = (DSTR_INITSZ >> 1);
111 do nsz <<= 1; while (nsz < rq);
112
113 if (d->buf)
114 d->buf = x_realloc(d->a, d->buf, nsz, d->sz);
115 else
116 d->buf = x_alloc(d->a, nsz);
117 d->sz = nsz;
118}
119
120/* --- @dstr_putc@ --- *
121 *
122 * Arguments: @dstr *d@ = pointer to a dynamic string block
123 * @char ch@ = character to append
124 *
125 * Returns: ---
126 *
127 * Use: Appends a character to a string.
128 */
129
130void dstr_putc(dstr *d, char ch) { DPUTC(d, ch); }
131
132/* --- @dstr_putz@ --- *
133 *
134 * Arguments: @dstr *d@ = pointer to a dynamic string block
135 *
136 * Returns: ---
137 *
138 * Use: Appends a null byte to a string. The null byte does not
139 * contribute to the string's length, and will be overwritten
140 * by subsequent `put' operations.
141 */
142
143void dstr_putz(dstr *d) { DPUTZ(d); }
144
145/* --- @dstr_puts@ --- *
146 *
147 * Arguments: @dstr *d@ = pointer to a dynamic string block
148 * @const char *s@ = pointer to string to append
149 *
150 * Returns: ---
151 *
152 * Use: Appends a character string to a string. A trailing null
153 * byte is added, as for @dstr_putz@.
154 */
155
156void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
157
158/* --- @dstr_putd@ --- *
159 *
160 * Arguments: @dstr *d@ = pointer to a dynamic string block
161 * @const dstr *s@ = pointer to a dynamic string to append
162 *
163 * Returns: ---
164 *
165 * Use: Appends a dynamic string to a string. A trailing null
166 * byte is added, as for @dstr_putz@.
167 */
168
169void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
170
171/* --- @dstr_putm@ --- *
172 *
173 * Arguments: @dstr *d@ = pointer to a dynamic string block
174 * @const void *p@ = pointer to a block to append
175 * @size_t sz@ = size of the block
176 *
177 * Returns: Appends an arbitrary data block to a string. No trailing
178 * null is appended.
179 */
180
181void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
182
183/* --- @dstr_tidy@ --- *
184 *
185 * Arguments: @dstr *d@ = pointer to a dynamic string block
186 *
187 * Returns: ---
188 *
189 * Use: Reduces the amount of memory used by a string. A trailing
190 * null byte is added, as for @dstr_putz@.
191 */
192
193void dstr_tidy(dstr *d)
194{
195 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
196 d->buf[d->len] = 0;
197 d->sz = d->len + 1;
198}
199
200/* --- @dstr_putline@ --- *
201 *
202 * Arguments: @dstr *d@ = pointer to a dynamic string block
203 * @FILE *fp@ = a stream to read from
204 *
205 * Returns: The number of characters read into the buffer, or @EOF@ if
206 * end-of-file was reached before any characters were read.
207 *
208 * Use: Appends the next line from the given input stream to the
209 * string. A trailing newline is not added; a trailing null
210 * byte is appended, as for @dstr_putz@.
211 */
212
213int dstr_putline(dstr *d, FILE *fp)
214{
215 size_t left = d->sz - d->len;
216 size_t off = d->len;
217 int rd = 0;
218 int ch;
219
220 for (;;) {
221
222 /* --- Read the next byte --- */
223
224 ch = getc(fp);
225
226 /* --- End-of-file when no characters read is special --- */
227
228 if (ch == EOF && !rd)
229 return (EOF);
230
231 /* --- Make sure there's some buffer space --- */
232
233 if (!left) {
234 d->len = off;
235 dstr_ensure(d, 1);
236 left = d->sz - off;
237 }
238
239 /* --- End-of-file or newline ends the loop --- */
240
241 if (ch == EOF || ch == '\n') {
242 d->buf[off] = 0;
243 d->len = off;
244 return rd;
245 }
246
247 /* --- Append the character and continue --- */
248
249 d->buf[off++] = ch;
250 left--; rd++;
251 }
252}
253
254/* --- @dstr_write@ --- *
255 *
256 * Arguments: @dstr *d@ = pointer to a dynamic string block
257 * @FILE *fp@ = a stream to write on
258 *
259 * Returns: The number of bytes written (as for @fwrite@).
260 *
261 * Use: Writes a dynamic string to a file.
262 */
263
264size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
265
266/*----- That's all, folks -------------------------------------------------*/