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