chiark / gitweb /
Version bump.
[mLib] / dstr.h
1 /* -*-c-*-
2  *
3  * $Id: dstr.h,v 1.9 1999/12/10 23:42:04 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 /*----- Revision history --------------------------------------------------*
31  *
32  * $Log: dstr.h,v $
33  * Revision 1.9  1999/12/10 23:42:04  mdw
34  * Change header file guard names.
35  *
36  * Revision 1.8  1999/07/14 19:45:24  mdw
37  * Prevent some macros from re-evaluating their arguments.
38  *
39  * Revision 1.7  1999/05/21 22:12:12  mdw
40  * Fix the bugs in the new macros.  (Whoops.)
41  *
42  * Revision 1.6  1999/05/21 08:38:14  mdw
43  * Add some more macros, particularly for creation and destruction.
44  *
45  * Revision 1.5  1999/05/13 22:47:57  mdw
46  * Misc documentation fixes.  Change `-ise' to `-ize' throughout.
47  *
48  * Revision 1.4  1999/05/06 19:51:35  mdw
49  * Reformatted the LGPL notice a little bit.
50  *
51  * Revision 1.3  1999/05/05 18:50:31  mdw
52  * Change licensing conditions to LGPL.
53  *
54  * Revision 1.2  1998/12/15 23:53:23  mdw
55  * New functions `dstr_putf' and `dstr_vputf' which do `printf'-style
56  * formatting in a safe way.
57  *
58  * Revision 1.1.1.1  1998/06/17 23:44:42  mdw
59  * Initial version of mLib
60  *
61  */
62
63 #ifndef MLIB_DSTR_H
64 #define MLIB_DSTR_H
65
66 #ifdef __cplusplus
67   extern "C" {
68 #endif
69
70 /*----- Rationale ---------------------------------------------------------*
71  *
72  * This file declares what is hopefully a fairly useful collection of
73  * primitive string handling functions.  The idea is that the strings
74  * allocate memory for themselves as required.  The @dstr@ routines don't
75  * assume any sort of terminator character, so arbitrary binary data can
76  * be stored in a dynamic string.  With luck, this should put a stop to
77  * any buffer overflow problems.
78  */
79
80 /*----- Header files ------------------------------------------------------*/
81
82 #include <stdarg.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85
86 /*----- Data structures ---------------------------------------------------*/
87
88 typedef struct dstr {
89   char *buf;                            /* Pointer to string buffer */
90   size_t sz;                            /* Size of the buffer */
91   size_t len;                           /* Length of the string */
92 } dstr;
93
94 #define DSTR_INIT { 0, 0, 0 }           /* How to initialize one */
95
96 /*----- Functions provided ------------------------------------------------*/
97
98 /* --- @dstr_create@ --- *
99  *
100  * Arguments:   @dstr *d@ = pointer to a dynamic string block
101  *
102  * Returns:     ---
103  *
104  * Use:         Initializes a dynamic string.
105  */
106
107 extern void dstr_create(dstr */*d*/);
108
109 #define DCREATE(d) do {                                                 \
110   dstr *_dd = (d);                                                      \
111   _dd->buf = 0;                                                         \
112   _dd->sz = 0;                                                          \
113   _dd->len = 0;                                                         \
114 } while (0)
115
116 /* --- @dstr_destroy@ --- *
117  *
118  * Arguments:   @dstr *d@ = pointer to a dynamic string block
119  *
120  * Returns:     ---
121  *
122  * Use:         Reclaims the space used by a dynamic string.
123  */
124
125 extern void dstr_destroy(dstr */*d*/);
126
127 #define DDESTROY(d) do {                                                \
128   dstr *_d = (d);                                                       \
129   if (_d->buf)                                                          \
130     free(_d->buf);                                                      \
131   DCREATE(_d);                                                          \
132 } while (0)
133
134 /* --- @dstr_reset@ --- *
135  *
136  * Arguments:   @dstr *d@ = pointer to a dynamic string block
137  *
138  * Returns:     ---
139  *
140  * Use:         Resets a string so that new data gets put at the beginning.
141  */
142
143 extern void dstr_reset(dstr */*d*/);
144
145 #define DRESET(d) ((d)->len = 0)
146
147 /* --- @dstr_ensure@ --- *
148  *
149  * Arguments:   @dstr *d@ = pointer to a dynamic string block
150  *              @size_t sz@ = amount of free space to ensure
151  *
152  * Returns:     ---
153  *
154  * Use:         Ensures that at least @sz@ bytes are available in the
155  *              given string.
156  */
157
158 extern void dstr_ensure(dstr */*d*/, size_t /*sz*/);
159
160 #define DENSURE(d, rq) do {                                             \
161   dstr *_dd = (d);                                                      \
162   size_t _rq = (rq);                                                    \
163   if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq);                  \
164 } while (0)
165
166 /* --- @dstr_putc@ --- *
167  *
168  * Arguments:   @dstr *d@ = pointer to a dynamic string block
169  *              @char ch@ = character to append
170  *
171  * Returns:     ---
172  *
173  * Use:         Appends a character to a string.
174  */
175
176 extern void dstr_putc(dstr */*d*/, char /*ch*/);
177
178 #define DPUTC(d, ch) do {                                               \
179   dstr *_d = (d);                                                       \
180   DENSURE(_d, 1);                                                       \
181   _d->buf[_d->len++] = (ch);                                            \
182 } while (0)
183
184 /* --- @dstr_putz@ --- *
185  *
186  * Arguments:   @dstr *d@ = pointer to a dynamic string block
187  *
188  * Returns:     ---
189  *
190  * Use:         Appends a null byte to a string.  The null byte does not
191  *              contribute to the string's length, and will be overwritten
192  *              by subsequent `put' operations.
193  */
194
195 extern void dstr_putz(dstr */*d*/);
196
197 #define DPUTZ(d) do {                                                   \
198   dstr *_d = (d);                                                       \
199   DENSURE(_d, 1);                                                       \
200   _d->buf[_d->len] = 0;                                         \
201 } while (0)
202
203 /* --- @dstr_puts@ --- *
204  *
205  * Arguments:   @dstr *d@ = pointer to a dynamic string block
206  *              @const char *s@ = pointer to string to append
207  *
208  * Returns:     ---
209  *
210  * Use:         Appends a character string to a string.  A trailing null
211  *              byte is added, as for @dstr_putz@.
212  */
213
214 extern void dstr_puts(dstr */*d*/, const char */*s*/);
215
216 #define DPUTS(d, s) do {                                                \
217   dstr *_d = (d);                                                       \
218   const char *_s = (s);                                                 \
219   size_t _sz = strlen(s);                                               \
220   DENSURE(_d, _sz + 1);                                                 \
221   memcpy(_d->buf + _d->len, _s, _sz + 1);                               \
222   _d->len += _sz;                                                       \
223 } while (0)
224
225 /* --- @dstr_vputf@ --- *
226  *
227  * Arguments:   @dstr *d@ = pointer to a dynamic string block
228  *              @const char *p@ = pointer to @printf@-style format string
229  *              @va_list ap@ = argument handle
230  *
231  * Returns:     The number of characters written to the string.
232  *
233  * Use:         As for @dstr_putf@, but may be used as a back-end to user-
234  *              supplied functions with @printf@-style interfaces.
235  */
236
237 extern int dstr_vputf(dstr */*d*/, const char */*p*/, va_list /*ap*/);
238
239 /* --- @dstr_putf@ --- *
240  *
241  * Arguments:   @dstr *d@ = pointer to a dynamic string block
242  *              @const char *p@ = pointer to @printf@-style format string
243  *              @...@ = argument handle
244  *
245  * Returns:     The number of characters written to the string.
246  *
247  * Use:         Writes a piece of text to a dynamic string, doing @printf@-
248  *              style substitutions as it goes.  Intended to be robust if
249  *              faced with malicious arguments, but not if the format string
250  *              itself is malicious.
251  */
252
253 extern int dstr_putf(dstr */*d*/, const char */*p*/, ...);
254
255 /* --- @dstr_putd@ --- *
256  *
257  * Arguments:   @dstr *d@ = pointer to a dynamic string block
258  *              @const dstr *s@ = pointer to a dynamic string to append
259  *
260  * Returns:     ---
261  *
262  * Use:         Appends a dynamic string to a string.  A trailing null
263  *              byte is added, as for @dstr_putz@.
264  */
265
266 extern void dstr_putd(dstr */*d*/, const dstr */*s*/);
267
268 #define DPUTD(d, s) do {                                                \
269   dstr *_d = (d);                                                       \
270   const dstr *_s = (s);                                                 \
271   DENSURE(_d, _s->len + 1);                                             \
272   memcpy(_d->buf + _d->len, _s->buf, _s->len);                          \
273   _d->len += _s->len;                                                   \
274   _d->buf[_d->len] = 0;                                                 \
275 } while (0)
276
277 /* --- @dstr_putm@ --- *
278  *
279  * Arguments:   @dstr *d@ = pointer to a dynamic string block
280  *              @const void *p@ = pointer to a block to append
281  *              @size_t sz@ = size of the block
282  *
283  * Returns:     Appends an arbitrary data block to a string.  No trailing
284  *              null is appended.
285  */
286
287 extern void dstr_putm(dstr */*d*/, const void */*p*/, size_t /*sz*/);
288
289 #define DPUTM(d, p, sz) do {                                            \
290   dstr *_d = (d);                                                       \
291   size_t _sz = (sz);                                                    \
292   DENSURE(_d, _sz);                                                     \
293   memcpy(_d->buf + _d->len, (p), _sz);                                  \
294   _d->len += _sz;                                                       \
295 } while (0)
296
297 /* --- @dstr_tidy@ --- *
298  *
299  * Arguments:   @dstr *d@ = pointer to a dynamic string block
300  *
301  * Returns:     ---
302  *
303  * Use:         Reduces the amount of memory used by a string.  A trailing
304  *              null byte is added, as for @dstr_putz@.
305  */
306
307 extern void dstr_tidy(dstr */*d*/);
308
309 /* --- @dstr_putline@ --- *
310  *
311  * Arguments:   @dstr *d@ = pointer to a dynamic string block
312  *              @FILE *fp@ = a stream to read from
313  *
314  * Returns:     The number of characters read into the buffer, or @EOF@ if
315  *              end-of-file was reached before any characters were read.
316  *
317  * Use:         Appends the next line from the given input stream to the
318  *              string.  A trailing newline is not added; a trailing null
319  *              byte is appended, as for @dstr_putz@.
320  */
321
322 extern int dstr_putline(dstr */*d*/, FILE */*fp*/);
323
324 /* --- @dstr_write@ --- *
325  *
326  * Arguments:   @dstr *d@ = pointer to a dynamic string block
327  *              @FILE *fp@ = a stream to write on
328  *
329  * Returns:     The number of bytes written (as for @fwrite@).
330  *
331  * Use:         Writes a dynamic string to a file.
332  */
333
334 extern size_t dstr_write(const dstr */*d*/, FILE */*fp*/);
335
336 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
337
338 /*----- That's all, folks -------------------------------------------------*/
339
340 #ifdef __cplusplus
341   }
342 #endif
343
344 #endif