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