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