@@@ fltfmt mess
[mLib] / struct / dstr.c
1 /* -*-c-*-
2 *
3 * Handle dynamically growing strings
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "alloc.h"
35 #include "dstr.h"
36 #include "growbuf.h"
37
38 /*----- Tunable constants -------------------------------------------------*/
39
40 /*
41 * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size.
42 * Otherwise, it's set to the next power of two that's large enough. This is
43 * memory-hungry, but efficient.
44 */
45
46 #define DSTR_INITSZ 64 /* Initial buffer size */
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 /* --- @dstr_create@ --- *
51 *
52 * Arguments: @dstr *d@ = pointer to a dynamic string block
53 *
54 * Returns: ---
55 *
56 * Use: Initializes a dynamic string.
57 */
58
59 void dstr_create(dstr *d) { DCREATE(d); }
60
61 /* --- @dstr_destroy@ --- *
62 *
63 * Arguments: @dstr *d@ = pointer to a dynamic string block
64 *
65 * Returns: ---
66 *
67 * Use: Reclaims the space used by a dynamic string.
68 */
69
70 void dstr_destroy(dstr *d) { DDESTROY(d); }
71
72 /* --- @dstr_reset@ --- *
73 *
74 * Arguments: @dstr *d@ = pointer to a dynamic string block
75 *
76 * Returns: ---
77 *
78 * Use: Resets a string so that new data gets put at the beginning.
79 */
80
81 void dstr_reset(dstr *d) { DRESET(d); }
82
83 /* --- @dstr_ensure@ --- *
84 *
85 * Arguments: @dstr *d@ = pointer to a dynamic string block
86 * @size_t sz@ = amount of free space to ensure
87 *
88 * Returns: ---
89 *
90 * Use: Ensures that at least @sz@ bytes are available in the
91 * given string.
92 */
93
94 void dstr_ensure(dstr *d, size_t sz)
95 {
96 GROWBUF_EXTEND(size_t, d->a, d->buf, d->sz, d->len + sz,
97 DSTR_INITSZ, 1);
98 }
99
100 /* --- @dstr_putc@ --- *
101 *
102 * Arguments: @dstr *d@ = pointer to a dynamic string block
103 * @int ch@ = character to append
104 *
105 * Returns: ---
106 *
107 * Use: Appends a character to a string.
108 */
109
110 void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); }
111
112 /* --- @dstr_putz@ --- *
113 *
114 * Arguments: @dstr *d@ = pointer to a dynamic string block
115 *
116 * Returns: ---
117 *
118 * Use: Appends a null byte to a string. The null byte does not
119 * contribute to the string's length, and will be overwritten
120 * by subsequent `put' operations.
121 */
122
123 void dstr_putz(dstr *d) { DPUTZ(d); }
124
125 /* --- @dstr_puts@ --- *
126 *
127 * Arguments: @dstr *d@ = pointer to a dynamic string block
128 * @const char *s@ = pointer to string to append
129 *
130 * Returns: ---
131 *
132 * Use: Appends a character string to a string. A trailing null
133 * byte is added, as for @dstr_putz@.
134 */
135
136 void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); }
137
138 /* --- @dstr_putd@ --- *
139 *
140 * Arguments: @dstr *d@ = pointer to a dynamic string block
141 * @const dstr *s@ = pointer to a dynamic string to append
142 *
143 * Returns: ---
144 *
145 * Use: Appends a dynamic string to a string. A trailing null
146 * byte is added, as for @dstr_putz@.
147 */
148
149 void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); }
150
151 /* --- @dstr_putm@ --- *
152 *
153 * Arguments: @dstr *d@ = pointer to a dynamic string block
154 * @const void *p@ = pointer to a block to append
155 * @size_t sz@ = size of the block
156 *
157 * Returns: Appends an arbitrary data block to a string. No trailing
158 * null is appended.
159 */
160
161 void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); }
162
163 /* --- @dstr_tidy@ --- *
164 *
165 * Arguments: @dstr *d@ = pointer to a dynamic string block
166 *
167 * Returns: ---
168 *
169 * Use: Reduces the amount of memory used by a string. A trailing
170 * null byte is added, as for @dstr_putz@.
171 */
172
173 void dstr_tidy(dstr *d)
174 {
175 d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz);
176 d->buf[d->len] = 0;
177 d->sz = d->len + 1;
178 }
179
180 /* --- @dstr_putline@ --- *
181 *
182 * Arguments: @dstr *d@ = pointer to a dynamic string block
183 * @FILE *fp@ = a stream to read from
184 *
185 * Returns: The number of characters read into the buffer, or @EOF@ if
186 * end-of-file was reached before any characters were read.
187 *
188 * Use: Appends the next line from the given input stream to the
189 * string. A trailing newline is not added; a trailing null
190 * byte is appended, as for @dstr_putz@.
191 */
192
193 int dstr_putline(dstr *d, FILE *fp)
194 {
195 size_t left = d->sz - d->len;
196 size_t off = d->len;
197 int rd = 0;
198 int ch;
199
200 for (;;) {
201
202 /* --- Read the next byte --- */
203
204 ch = getc(fp);
205
206 /* --- End-of-file when no characters read is special --- */
207
208 if (ch == EOF && !rd)
209 return (EOF);
210
211 /* --- Make sure there's some buffer space --- */
212
213 if (!left) {
214 d->len = off;
215 dstr_ensure(d, 1);
216 left = d->sz - off;
217 }
218
219 /* --- End-of-file or newline ends the loop --- */
220
221 if (ch == EOF || ch == '\n') {
222 d->buf[off] = 0;
223 d->len = off;
224 return rd;
225 }
226
227 /* --- Append the character and continue --- */
228
229 d->buf[off++] = ch;
230 left--; rd++;
231 }
232 }
233
234 /* --- @dstr_write@ --- *
235 *
236 * Arguments: @dstr *d@ = pointer to a dynamic string block
237 * @FILE *fp@ = a stream to write on
238 *
239 * Returns: The number of bytes written (as for @fwrite@).
240 *
241 * Use: Writes a dynamic string to a file.
242 */
243
244 size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); }
245
246 /*----- That's all, folks -------------------------------------------------*/