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