@@@ timeout wip
[mLib] / struct / dstr.h
CommitLineData
0875b58f 1/* -*-c-*-
2 *
0875b58f 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
c6e0eaf0 28#ifndef MLIB_DSTR_H
29#define MLIB_DSTR_H
0875b58f 30
31#ifdef __cplusplus
32 extern "C" {
33#endif
34
35/*----- Rationale ---------------------------------------------------------*
36 *
37 * This file declares what is hopefully a fairly useful collection of
38 * primitive string handling functions. The idea is that the strings
39 * allocate memory for themselves as required. The @dstr@ routines don't
40 * assume any sort of terminator character, so arbitrary binary data can
41 * be stored in a dynamic string. With luck, this should put a stop to
42 * any buffer overflow problems.
43 */
44
45/*----- Header files ------------------------------------------------------*/
46
00c7638b 47#include <stdarg.h>
0875b58f 48#include <stdio.h>
c44b6bde 49#include <stdlib.h>
e63124bc 50#include <string.h>
0875b58f 51
20eb516f 52#ifndef MLIB_ALLOC_H
53# include "alloc.h"
54#endif
55
56#ifndef MLIB_ARENA_H
57# include "arena.h"
58#endif
59
3832000d
MW
60#ifndef MLIB_MACROS_H
61# include "macros.h"
62#endif
63
0875b58f 64/*----- Data structures ---------------------------------------------------*/
65
66typedef struct dstr {
67 char *buf; /* Pointer to string buffer */
68 size_t sz; /* Size of the buffer */
69 size_t len; /* Length of the string */
20eb516f 70 arena *a; /* Pointer to arena */
0875b58f 71} dstr;
72
20eb516f 73#define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */
c44b6bde 74
e63124bc
MW
75extern const struct gprintf_ops dstr_printops;
76
0875b58f 77/*----- Functions provided ------------------------------------------------*/
78
79/* --- @dstr_create@ --- *
80 *
81 * Arguments: @dstr *d@ = pointer to a dynamic string block
82 *
83 * Returns: ---
84 *
96c5fe33 85 * Use: Initializes a dynamic string.
0875b58f 86 */
87
88extern void dstr_create(dstr */*d*/);
89
c44b6bde 90#define DCREATE(d) do { \
077c6597 91 dstr *_dd = (d); \
92 _dd->buf = 0; \
93 _dd->sz = 0; \
94 _dd->len = 0; \
20eb516f 95 _dd->a = &arena_stdlib; \
c44b6bde 96} while (0)
97
0875b58f 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
107extern void dstr_destroy(dstr */*d*/);
108
c44b6bde 109#define DDESTROY(d) do { \
077c6597 110 dstr *_d = (d); \
111 if (_d->buf) \
20eb516f 112 x_free(_d->a, _d->buf); \
077c6597 113 DCREATE(_d); \
c44b6bde 114} while (0)
115
0875b58f 116/* --- @dstr_reset@ --- *
117 *
c6e0eaf0 118 * Arguments: @dstr *d@ = pointer to a dynamic string block
0875b58f 119 *
120 * Returns: ---
121 *
122 * Use: Resets a string so that new data gets put at the beginning.
123 */
124
125extern void dstr_reset(dstr */*d*/);
126
bd3d316a 127#define DRESET(d) ((d)->len = 0)
c44b6bde 128
0875b58f 129/* --- @dstr_ensure@ --- *
130 *
131 * Arguments: @dstr *d@ = pointer to a dynamic string block
132 * @size_t sz@ = amount of free space to ensure
133 *
134 * Returns: ---
135 *
136 * Use: Ensures that at least @sz@ bytes are available in the
137 * given string.
138 */
139
140extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
141
142#define DENSURE(d, rq) do { \
077c6597 143 dstr *_dd = (d); \
144 size_t _rq = (rq); \
145 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
0875b58f 146} while (0)
147
148/* --- @dstr_putc@ --- *
149 *
150 * Arguments: @dstr *d@ = pointer to a dynamic string block
2be33c7c 151 * @int ch@ = character to append
0875b58f 152 *
153 * Returns: ---
154 *
155 * Use: Appends a character to a string.
156 */
157
2be33c7c 158extern void dstr_putc(dstr */*d*/, int /*ch*/);
0875b58f 159
160#define DPUTC(d, ch) do { \
077c6597 161 dstr *_d = (d); \
162 DENSURE(_d, 1); \
2be33c7c 163 *((unsigned char *)_d->buf + _d->len++) = (ch); \
0875b58f 164} while (0)
165
166/* --- @dstr_putz@ --- *
167 *
168 * Arguments: @dstr *d@ = pointer to a dynamic string block
169 *
170 * Returns: ---
171 *
172 * Use: Appends a null byte to a string. The null byte does not
173 * contribute to the string's length, and will be overwritten
174 * by subsequent `put' operations.
175 */
176
177extern void dstr_putz(dstr */*d*/);
178
179#define DPUTZ(d) do { \
077c6597 180 dstr *_d = (d); \
181 DENSURE(_d, 1); \
182 _d->buf[_d->len] = 0; \
0875b58f 183} while (0)
184
185/* --- @dstr_puts@ --- *
186 *
187 * Arguments: @dstr *d@ = pointer to a dynamic string block
188 * @const char *s@ = pointer to string to append
189 *
190 * Returns: ---
191 *
192 * Use: Appends a character string to a string. A trailing null
193 * byte is added, as for @dstr_putz@.
194 */
195
196extern void dstr_puts(dstr */*d*/, const char */*s*/);
197
198#define DPUTS(d, s) do { \
077c6597 199 dstr *_d = (d); \
200 const char *_s = (s); \
c53c8c86 201 size_t _sz = strlen(_s); \
077c6597 202 DENSURE(_d, _sz + 1); \
203 memcpy(_d->buf + _d->len, _s, _sz + 1); \
204 _d->len += _sz; \
0875b58f 205} while (0)
206
00c7638b 207/* --- @dstr_vputf@ --- *
208 *
209 * Arguments: @dstr *d@ = pointer to a dynamic string block
210 * @const char *p@ = pointer to @printf@-style format string
5a18a126 211 * @va_list *ap@ = argument handle
00c7638b 212 *
96c5fe33 213 * Returns: The number of characters written to the string.
00c7638b 214 *
215 * Use: As for @dstr_putf@, but may be used as a back-end to user-
216 * supplied functions with @printf@-style interfaces.
217 */
218
5a18a126 219extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list */*ap*/);
00c7638b 220
221/* --- @dstr_putf@ --- *
222 *
223 * Arguments: @dstr *d@ = pointer to a dynamic string block
224 * @const char *p@ = pointer to @printf@-style format string
225 * @...@ = argument handle
226 *
96c5fe33 227 * Returns: The number of characters written to the string.
00c7638b 228 *
229 * Use: Writes a piece of text to a dynamic string, doing @printf@-
230 * style substitutions as it goes. Intended to be robust if
231 * faced with malicious arguments, but not if the format string
232 * itself is malicious.
233 */
234
31d0247c 235extern PRINTF_LIKE(2, 3) int dstr_putf(dstr */*d*/, const char */*p*/, ...);
00c7638b 236
0875b58f 237/* --- @dstr_putd@ --- *
238 *
239 * Arguments: @dstr *d@ = pointer to a dynamic string block
240 * @const dstr *s@ = pointer to a dynamic string to append
241 *
242 * Returns: ---
243 *
244 * Use: Appends a dynamic string to a string. A trailing null
245 * byte is added, as for @dstr_putz@.
246 */
247
248extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
249
250#define DPUTD(d, s) do { \
077c6597 251 dstr *_d = (d); \
252 const dstr *_s = (s); \
253 DENSURE(_d, _s->len + 1); \
254 memcpy(_d->buf + _d->len, _s->buf, _s->len); \
255 _d->len += _s->len; \
256 _d->buf[_d->len] = 0; \
0875b58f 257} while (0)
258
259/* --- @dstr_putm@ --- *
260 *
261 * Arguments: @dstr *d@ = pointer to a dynamic string block
262 * @const void *p@ = pointer to a block to append
263 * @size_t sz@ = size of the block
264 *
265 * Returns: Appends an arbitrary data block to a string. No trailing
266 * null is appended.
267 */
268
269extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
270
271#define DPUTM(d, p, sz) do { \
077c6597 272 dstr *_d = (d); \
273 size_t _sz = (sz); \
274 DENSURE(_d, _sz); \
275 memcpy(_d->buf + _d->len, (p), _sz); \
276 _d->len += _sz; \
0875b58f 277} while (0)
278
279/* --- @dstr_tidy@ --- *
280 *
281 * Arguments: @dstr *d@ = pointer to a dynamic string block
282 *
283 * Returns: ---
284 *
285 * Use: Reduces the amount of memory used by a string. A trailing
286 * null byte is added, as for @dstr_putz@.
287 */
288
289extern void dstr_tidy(dstr */*d*/);
290
291/* --- @dstr_putline@ --- *
292 *
293 * Arguments: @dstr *d@ = pointer to a dynamic string block
294 * @FILE *fp@ = a stream to read from
295 *
296 * Returns: The number of characters read into the buffer, or @EOF@ if
297 * end-of-file was reached before any characters were read.
298 *
299 * Use: Appends the next line from the given input stream to the
300 * string. A trailing newline is not added; a trailing null
301 * byte is appended, as for @dstr_putz@.
302 */
303
304extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
305
306/* --- @dstr_write@ --- *
307 *
308 * Arguments: @dstr *d@ = pointer to a dynamic string block
309 * @FILE *fp@ = a stream to write on
310 *
311 * Returns: The number of bytes written (as for @fwrite@).
312 *
313 * Use: Writes a dynamic string to a file.
314 */
315
96c5fe33 316extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/);
0875b58f 317
c44b6bde 318#define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
319
0875b58f 320/*----- That's all, folks -------------------------------------------------*/
321
322#ifdef __cplusplus
323 }
324#endif
325
326#endif