3 * $Id: dstr.h,v 1.13 2004/04/08 01:36:11 mdw Exp $
5 * Handle dynamically growing strings
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
37 /*----- Rationale ---------------------------------------------------------*
39 * This file declares what is hopefully a fairly useful collection of
40 * primitive string handling functions. The idea is that the strings
41 * allocate memory for themselves as required. The @dstr@ routines don't
42 * assume any sort of terminator character, so arbitrary binary data can
43 * be stored in a dynamic string. With luck, this should put a stop to
44 * any buffer overflow problems.
47 /*----- Header files ------------------------------------------------------*/
61 /*----- Data structures ---------------------------------------------------*/
64 char *buf
; /* Pointer to string buffer */
65 size_t sz
; /* Size of the buffer */
66 size_t len
; /* Length of the string */
67 arena
*a
; /* Pointer to arena */
70 #define DSTR_INIT { 0, 0, 0, &arena_stdlib } /* How to initialize one */
72 /*----- Functions provided ------------------------------------------------*/
74 /* --- @dstr_create@ --- *
76 * Arguments: @dstr *d@ = pointer to a dynamic string block
80 * Use: Initializes a dynamic string.
83 extern void dstr_create(dstr */
*d*/
);
85 #define DCREATE(d) do { \
90 _dd->a = &arena_stdlib; \
93 /* --- @dstr_destroy@ --- *
95 * Arguments: @dstr *d@ = pointer to a dynamic string block
99 * Use: Reclaims the space used by a dynamic string.
102 extern void dstr_destroy(dstr */
*d*/
);
104 #define DDESTROY(d) do { \
107 x_free(_d->a, _d->buf); \
111 /* --- @dstr_reset@ --- *
113 * Arguments: @dstr *d@ = pointer to a dynamic string block
117 * Use: Resets a string so that new data gets put at the beginning.
120 extern void dstr_reset(dstr */
*d*/
);
122 #define DRESET(d) ((d)->len = 0)
124 /* --- @dstr_ensure@ --- *
126 * Arguments: @dstr *d@ = pointer to a dynamic string block
127 * @size_t sz@ = amount of free space to ensure
131 * Use: Ensures that at least @sz@ bytes are available in the
135 extern void dstr_ensure(dstr */
*d*/
, size_t /*sz*/);
137 #define DENSURE(d, rq) do { \
140 if (_dd->len + _rq > _dd->sz) dstr_ensure(_dd, _rq); \
143 /* --- @dstr_putc@ --- *
145 * Arguments: @dstr *d@ = pointer to a dynamic string block
146 * @char ch@ = character to append
150 * Use: Appends a character to a string.
153 extern void dstr_putc(dstr */
*d*/
, char /*ch*/);
155 #define DPUTC(d, ch) do { \
158 _d->buf[_d->len++] = (ch); \
161 /* --- @dstr_putz@ --- *
163 * Arguments: @dstr *d@ = pointer to a dynamic string block
167 * Use: Appends a null byte to a string. The null byte does not
168 * contribute to the string's length, and will be overwritten
169 * by subsequent `put' operations.
172 extern void dstr_putz(dstr */
*d*/
);
174 #define DPUTZ(d) do { \
177 _d->buf[_d->len] = 0; \
180 /* --- @dstr_puts@ --- *
182 * Arguments: @dstr *d@ = pointer to a dynamic string block
183 * @const char *s@ = pointer to string to append
187 * Use: Appends a character string to a string. A trailing null
188 * byte is added, as for @dstr_putz@.
191 extern void dstr_puts(dstr */
*d*/
, const char */
*s*/
);
193 #define DPUTS(d, s) do { \
195 const char *_s = (s); \
196 size_t _sz = strlen(_s); \
197 DENSURE(_d, _sz + 1); \
198 memcpy(_d->buf + _d->len, _s, _sz + 1); \
202 /* --- @dstr_vputf@ --- *
204 * Arguments: @dstr *d@ = pointer to a dynamic string block
205 * @const char *p@ = pointer to @printf@-style format string
206 * @va_list *ap@ = argument handle
208 * Returns: The number of characters written to the string.
210 * Use: As for @dstr_putf@, but may be used as a back-end to user-
211 * supplied functions with @printf@-style interfaces.
214 extern int dstr_vputf(dstr */
*d*/
, const char */
*p*/
, va_list */
*ap*/
);
216 /* --- @dstr_putf@ --- *
218 * Arguments: @dstr *d@ = pointer to a dynamic string block
219 * @const char *p@ = pointer to @printf@-style format string
220 * @...@ = argument handle
222 * Returns: The number of characters written to the string.
224 * Use: Writes a piece of text to a dynamic string, doing @printf@-
225 * style substitutions as it goes. Intended to be robust if
226 * faced with malicious arguments, but not if the format string
227 * itself is malicious.
230 extern int dstr_putf(dstr */
*d*/
, const char */
*p*/
, ...);
232 /* --- @dstr_putd@ --- *
234 * Arguments: @dstr *d@ = pointer to a dynamic string block
235 * @const dstr *s@ = pointer to a dynamic string to append
239 * Use: Appends a dynamic string to a string. A trailing null
240 * byte is added, as for @dstr_putz@.
243 extern void dstr_putd(dstr */
*d*/
, const dstr */
*s*/
);
245 #define DPUTD(d, s) do { \
247 const dstr *_s = (s); \
248 DENSURE(_d, _s->len + 1); \
249 memcpy(_d->buf + _d->len, _s->buf, _s->len); \
250 _d->len += _s->len; \
251 _d->buf[_d->len] = 0; \
254 /* --- @dstr_putm@ --- *
256 * Arguments: @dstr *d@ = pointer to a dynamic string block
257 * @const void *p@ = pointer to a block to append
258 * @size_t sz@ = size of the block
260 * Returns: Appends an arbitrary data block to a string. No trailing
264 extern void dstr_putm(dstr */
*d*/
, const void */
*p*/
, size_t /*sz*/);
266 #define DPUTM(d, p, sz) do { \
270 memcpy(_d->buf + _d->len, (p), _sz); \
274 /* --- @dstr_tidy@ --- *
276 * Arguments: @dstr *d@ = pointer to a dynamic string block
280 * Use: Reduces the amount of memory used by a string. A trailing
281 * null byte is added, as for @dstr_putz@.
284 extern void dstr_tidy(dstr */
*d*/
);
286 /* --- @dstr_putline@ --- *
288 * Arguments: @dstr *d@ = pointer to a dynamic string block
289 * @FILE *fp@ = a stream to read from
291 * Returns: The number of characters read into the buffer, or @EOF@ if
292 * end-of-file was reached before any characters were read.
294 * Use: Appends the next line from the given input stream to the
295 * string. A trailing newline is not added; a trailing null
296 * byte is appended, as for @dstr_putz@.
299 extern int dstr_putline(dstr */
*d*/
, FILE */
*fp*/
);
301 /* --- @dstr_write@ --- *
303 * Arguments: @dstr *d@ = pointer to a dynamic string block
304 * @FILE *fp@ = a stream to write on
306 * Returns: The number of bytes written (as for @fwrite@).
308 * Use: Writes a dynamic string to a file.
311 extern size_t dstr_write(const dstr */
*d*/
, FILE */
*fp*/
);
313 #define DWRITE(d, fp) fwrite((d)->buf, 1, (d)->len, (fp))
315 /*----- That's all, folks -------------------------------------------------*/