| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: dstr.c,v 1.1 1998/06/17 23:44:42 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 General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (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 General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with mLib; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: dstr.c,v $ |
| 32 | * Revision 1.1 1998/06/17 23:44:42 mdw |
| 33 | * Initial revision |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /*----- Header files ------------------------------------------------------*/ |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | #include "alloc.h" |
| 44 | #include "dstr.h" |
| 45 | |
| 46 | /*----- Tunable constants -------------------------------------------------*/ |
| 47 | |
| 48 | #define DSTR_INITSZ 256 /* Initial buffer size */ |
| 49 | |
| 50 | /*----- Main code ---------------------------------------------------------*/ |
| 51 | |
| 52 | /* --- @dstr_create@ --- * |
| 53 | * |
| 54 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 55 | * |
| 56 | * Returns: --- |
| 57 | * |
| 58 | * Use: Initialises a dynamic string. |
| 59 | */ |
| 60 | |
| 61 | void dstr_create(dstr *d) |
| 62 | { |
| 63 | d->sz = 0; |
| 64 | d->len = 0; |
| 65 | d->buf = 0; |
| 66 | } |
| 67 | |
| 68 | /* --- @dstr_destroy@ --- * |
| 69 | * |
| 70 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 71 | * |
| 72 | * Returns: --- |
| 73 | * |
| 74 | * Use: Reclaims the space used by a dynamic string. |
| 75 | */ |
| 76 | |
| 77 | void dstr_destroy(dstr *d) |
| 78 | { |
| 79 | if (d->buf) |
| 80 | free(d->buf); |
| 81 | d->buf = 0; |
| 82 | d->len = 0; |
| 83 | d->sz = 0; |
| 84 | } |
| 85 | |
| 86 | /* --- @dstr_reset@ --- * |
| 87 | * |
| 88 | * Arguments: @dstr *d@ = pointer to a dynaimc string block |
| 89 | * |
| 90 | * Returns: --- |
| 91 | * |
| 92 | * Use: Resets a string so that new data gets put at the beginning. |
| 93 | */ |
| 94 | |
| 95 | void dstr_reset(dstr *d) |
| 96 | { |
| 97 | d->len = 0; |
| 98 | } |
| 99 | |
| 100 | /* --- @dstr_ensure@ --- * |
| 101 | * |
| 102 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 103 | * @size_t sz@ = amount of free space to ensure |
| 104 | * |
| 105 | * Returns: --- |
| 106 | * |
| 107 | * Use: Ensures that at least @sz@ bytes are available in the |
| 108 | * given string. |
| 109 | */ |
| 110 | |
| 111 | void dstr_ensure(dstr *d, size_t sz) |
| 112 | { |
| 113 | size_t rq = d->len + sz; |
| 114 | size_t nsz; |
| 115 | |
| 116 | /* --- If we have enough space, just leave it --- */ |
| 117 | |
| 118 | if (rq <= d->sz) |
| 119 | return; |
| 120 | |
| 121 | /* --- Grow the buffer --- * |
| 122 | * |
| 123 | * For small buffers, just double the size. For big buffers, make them |
| 124 | * a multiple of some suitably large chunk size. |
| 125 | */ |
| 126 | |
| 127 | nsz = d->sz; |
| 128 | |
| 129 | do { |
| 130 | if (nsz == 0) |
| 131 | nsz = DSTR_INITSZ; |
| 132 | else if (d->sz < 0x1000) |
| 133 | nsz <<= 1; |
| 134 | else |
| 135 | nsz = (rq + 0x0fff) & ~0x0fff; |
| 136 | } while (rq > nsz); |
| 137 | |
| 138 | if (d->buf) |
| 139 | d->buf = xrealloc(d->buf, nsz); |
| 140 | else |
| 141 | d->buf = xmalloc(nsz); |
| 142 | d->sz = nsz; |
| 143 | } |
| 144 | |
| 145 | /* --- @dstr_putc@ --- * |
| 146 | * |
| 147 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 148 | * @char ch@ = character to append |
| 149 | * |
| 150 | * Returns: --- |
| 151 | * |
| 152 | * Use: Appends a character to a string. |
| 153 | */ |
| 154 | |
| 155 | void dstr_putc(dstr *d, char ch) |
| 156 | { |
| 157 | DPUTC(d, ch); |
| 158 | } |
| 159 | |
| 160 | /* --- @dstr_putz@ --- * |
| 161 | * |
| 162 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 163 | * |
| 164 | * Returns: --- |
| 165 | * |
| 166 | * Use: Appends a null byte to a string. The null byte does not |
| 167 | * contribute to the string's length, and will be overwritten |
| 168 | * by subsequent `put' operations. |
| 169 | */ |
| 170 | |
| 171 | void dstr_putz(dstr *d) |
| 172 | { |
| 173 | DPUTZ(d); |
| 174 | } |
| 175 | |
| 176 | /* --- @dstr_puts@ --- * |
| 177 | * |
| 178 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 179 | * @const char *s@ = pointer to string to append |
| 180 | * |
| 181 | * Returns: --- |
| 182 | * |
| 183 | * Use: Appends a character string to a string. A trailing null |
| 184 | * byte is added, as for @dstr_putz@. |
| 185 | */ |
| 186 | |
| 187 | void dstr_puts(dstr *d, const char *s) |
| 188 | { |
| 189 | DPUTS(d, s); |
| 190 | } |
| 191 | |
| 192 | /* --- @dstr_putd@ --- * |
| 193 | * |
| 194 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 195 | * @const dstr *s@ = pointer to a dynamic string to append |
| 196 | * |
| 197 | * Returns: --- |
| 198 | * |
| 199 | * Use: Appends a dynamic string to a string. A trailing null |
| 200 | * byte is added, as for @dstr_putz@. |
| 201 | */ |
| 202 | |
| 203 | void dstr_putd(dstr *d, const dstr *s) |
| 204 | { |
| 205 | DPUTD(d, s); |
| 206 | } |
| 207 | |
| 208 | /* --- @dstr_putm@ --- * |
| 209 | * |
| 210 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 211 | * @const void *p@ = pointer to a block to append |
| 212 | * @size_t sz@ = size of the block |
| 213 | * |
| 214 | * Returns: Appends an arbitrary data block to a string. No trailing |
| 215 | * null is appended. |
| 216 | */ |
| 217 | |
| 218 | void dstr_putm(dstr *d, const void *p, size_t sz) |
| 219 | { |
| 220 | DPUTM(d, p, sz); |
| 221 | } |
| 222 | |
| 223 | /* --- @dstr_tidy@ --- * |
| 224 | * |
| 225 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 226 | * |
| 227 | * Returns: --- |
| 228 | * |
| 229 | * Use: Reduces the amount of memory used by a string. A trailing |
| 230 | * null byte is added, as for @dstr_putz@. |
| 231 | */ |
| 232 | |
| 233 | void dstr_tidy(dstr *d) |
| 234 | { |
| 235 | dstr_putz(d); |
| 236 | d->buf = xrealloc(d->buf, d->len + 1); |
| 237 | d->sz = d->len + 1; |
| 238 | } |
| 239 | |
| 240 | /* --- @dstr_putline@ --- * |
| 241 | * |
| 242 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 243 | * @FILE *fp@ = a stream to read from |
| 244 | * |
| 245 | * Returns: The number of characters read into the buffer, or @EOF@ if |
| 246 | * end-of-file was reached before any characters were read. |
| 247 | * |
| 248 | * Use: Appends the next line from the given input stream to the |
| 249 | * string. A trailing newline is not added; a trailing null |
| 250 | * byte is appended, as for @dstr_putz@. |
| 251 | */ |
| 252 | |
| 253 | int dstr_putline(dstr *d, FILE *fp) |
| 254 | { |
| 255 | size_t left = d->sz - d->len; |
| 256 | size_t off = d->len; |
| 257 | int rd = 0; |
| 258 | int ch; |
| 259 | |
| 260 | for (;;) { |
| 261 | |
| 262 | /* --- Make sure there's some buffer space --- */ |
| 263 | |
| 264 | if (!left) { |
| 265 | dstr_ensure(d, 1); |
| 266 | left = d->sz - off; |
| 267 | } |
| 268 | |
| 269 | /* --- Read the next byte --- */ |
| 270 | |
| 271 | ch = getc(fp); |
| 272 | |
| 273 | /* --- End-of-file when no characters read is special --- */ |
| 274 | |
| 275 | if (ch == EOF && !rd) |
| 276 | return (EOF); |
| 277 | |
| 278 | /* --- End-of-file or newline ends the loop --- */ |
| 279 | |
| 280 | if (ch == EOF || ch == '\n') { |
| 281 | d->buf[off] = 0; |
| 282 | d->len = off; |
| 283 | return rd; |
| 284 | } |
| 285 | |
| 286 | /* --- Append the character and continue --- */ |
| 287 | |
| 288 | d->buf[off++] = ch; |
| 289 | left--; rd++; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | /* --- @dstr_write@ --- * |
| 294 | * |
| 295 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 296 | * @FILE *fp@ = a stream to write on |
| 297 | * |
| 298 | * Returns: The number of bytes written (as for @fwrite@). |
| 299 | * |
| 300 | * Use: Writes a dynamic string to a file. |
| 301 | */ |
| 302 | |
| 303 | size_t dstr_write(dstr *d, FILE *fp) |
| 304 | { |
| 305 | return (fwrite(d->buf, 1, d->len, fp)); |
| 306 | } |
| 307 | |
| 308 | /*----- That's all, folks -------------------------------------------------*/ |