| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Handle dynamically growing strings |
| 4 | * |
| 5 | * (c) 1998 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 12 | * mLib is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * mLib is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with mLib; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | |
| 34 | #include "alloc.h" |
| 35 | #include "dstr.h" |
| 36 | |
| 37 | /*----- Tunable constants -------------------------------------------------*/ |
| 38 | |
| 39 | /* |
| 40 | * If the buffer is empty, it is set to @DSTR_INITSZ@ bytes in size. |
| 41 | * Otherwise, it's set to the next power of two that's large enough. This is |
| 42 | * memory-hungry, but efficient. |
| 43 | */ |
| 44 | |
| 45 | #define DSTR_INITSZ 64 /* Initial buffer size */ |
| 46 | |
| 47 | /*----- Main code ---------------------------------------------------------*/ |
| 48 | |
| 49 | /* --- @dstr_create@ --- * |
| 50 | * |
| 51 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 52 | * |
| 53 | * Returns: --- |
| 54 | * |
| 55 | * Use: Initializes a dynamic string. |
| 56 | */ |
| 57 | |
| 58 | void dstr_create(dstr *d) { DCREATE(d); } |
| 59 | |
| 60 | /* --- @dstr_destroy@ --- * |
| 61 | * |
| 62 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 63 | * |
| 64 | * Returns: --- |
| 65 | * |
| 66 | * Use: Reclaims the space used by a dynamic string. |
| 67 | */ |
| 68 | |
| 69 | void dstr_destroy(dstr *d) { DDESTROY(d); } |
| 70 | |
| 71 | /* --- @dstr_reset@ --- * |
| 72 | * |
| 73 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 74 | * |
| 75 | * Returns: --- |
| 76 | * |
| 77 | * Use: Resets a string so that new data gets put at the beginning. |
| 78 | */ |
| 79 | |
| 80 | void dstr_reset(dstr *d) { DRESET(d); } |
| 81 | |
| 82 | /* --- @dstr_ensure@ --- * |
| 83 | * |
| 84 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 85 | * @size_t sz@ = amount of free space to ensure |
| 86 | * |
| 87 | * Returns: --- |
| 88 | * |
| 89 | * Use: Ensures that at least @sz@ bytes are available in the |
| 90 | * given string. |
| 91 | */ |
| 92 | |
| 93 | void dstr_ensure(dstr *d, size_t sz) |
| 94 | { |
| 95 | size_t rq = d->len + sz; |
| 96 | size_t nsz; |
| 97 | |
| 98 | /* --- If we have enough space, just leave it --- */ |
| 99 | |
| 100 | if (rq <= d->sz) |
| 101 | return; |
| 102 | |
| 103 | /* --- Grow the buffer --- */ |
| 104 | |
| 105 | nsz = d->sz; |
| 106 | |
| 107 | if (nsz == 0) |
| 108 | nsz = (DSTR_INITSZ >> 1); |
| 109 | do nsz <<= 1; while (nsz < rq); |
| 110 | |
| 111 | if (d->buf) |
| 112 | d->buf = x_realloc(d->a, d->buf, nsz, d->sz); |
| 113 | else |
| 114 | d->buf = x_alloc(d->a, nsz); |
| 115 | d->sz = nsz; |
| 116 | } |
| 117 | |
| 118 | /* --- @dstr_putc@ --- * |
| 119 | * |
| 120 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 121 | * @int ch@ = character to append |
| 122 | * |
| 123 | * Returns: --- |
| 124 | * |
| 125 | * Use: Appends a character to a string. |
| 126 | */ |
| 127 | |
| 128 | void dstr_putc(dstr *d, int ch) { DPUTC(d, ch); } |
| 129 | |
| 130 | /* --- @dstr_putz@ --- * |
| 131 | * |
| 132 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 133 | * |
| 134 | * Returns: --- |
| 135 | * |
| 136 | * Use: Appends a null byte to a string. The null byte does not |
| 137 | * contribute to the string's length, and will be overwritten |
| 138 | * by subsequent `put' operations. |
| 139 | */ |
| 140 | |
| 141 | void dstr_putz(dstr *d) { DPUTZ(d); } |
| 142 | |
| 143 | /* --- @dstr_puts@ --- * |
| 144 | * |
| 145 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 146 | * @const char *s@ = pointer to string to append |
| 147 | * |
| 148 | * Returns: --- |
| 149 | * |
| 150 | * Use: Appends a character string to a string. A trailing null |
| 151 | * byte is added, as for @dstr_putz@. |
| 152 | */ |
| 153 | |
| 154 | void dstr_puts(dstr *d, const char *s) { DPUTS(d, s); } |
| 155 | |
| 156 | /* --- @dstr_putd@ --- * |
| 157 | * |
| 158 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 159 | * @const dstr *s@ = pointer to a dynamic string to append |
| 160 | * |
| 161 | * Returns: --- |
| 162 | * |
| 163 | * Use: Appends a dynamic string to a string. A trailing null |
| 164 | * byte is added, as for @dstr_putz@. |
| 165 | */ |
| 166 | |
| 167 | void dstr_putd(dstr *d, const dstr *s) { DPUTD(d, s); } |
| 168 | |
| 169 | /* --- @dstr_putm@ --- * |
| 170 | * |
| 171 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 172 | * @const void *p@ = pointer to a block to append |
| 173 | * @size_t sz@ = size of the block |
| 174 | * |
| 175 | * Returns: Appends an arbitrary data block to a string. No trailing |
| 176 | * null is appended. |
| 177 | */ |
| 178 | |
| 179 | void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); } |
| 180 | |
| 181 | /* --- @dstr_tidy@ --- * |
| 182 | * |
| 183 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 184 | * |
| 185 | * Returns: --- |
| 186 | * |
| 187 | * Use: Reduces the amount of memory used by a string. A trailing |
| 188 | * null byte is added, as for @dstr_putz@. |
| 189 | */ |
| 190 | |
| 191 | void dstr_tidy(dstr *d) |
| 192 | { |
| 193 | d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz); |
| 194 | d->buf[d->len] = 0; |
| 195 | d->sz = d->len + 1; |
| 196 | } |
| 197 | |
| 198 | /* --- @dstr_putline@ --- * |
| 199 | * |
| 200 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 201 | * @FILE *fp@ = a stream to read from |
| 202 | * |
| 203 | * Returns: The number of characters read into the buffer, or @EOF@ if |
| 204 | * end-of-file was reached before any characters were read. |
| 205 | * |
| 206 | * Use: Appends the next line from the given input stream to the |
| 207 | * string. A trailing newline is not added; a trailing null |
| 208 | * byte is appended, as for @dstr_putz@. |
| 209 | */ |
| 210 | |
| 211 | int dstr_putline(dstr *d, FILE *fp) |
| 212 | { |
| 213 | size_t left = d->sz - d->len; |
| 214 | size_t off = d->len; |
| 215 | int rd = 0; |
| 216 | int ch; |
| 217 | |
| 218 | for (;;) { |
| 219 | |
| 220 | /* --- Read the next byte --- */ |
| 221 | |
| 222 | ch = getc(fp); |
| 223 | |
| 224 | /* --- End-of-file when no characters read is special --- */ |
| 225 | |
| 226 | if (ch == EOF && !rd) |
| 227 | return (EOF); |
| 228 | |
| 229 | /* --- Make sure there's some buffer space --- */ |
| 230 | |
| 231 | if (!left) { |
| 232 | d->len = off; |
| 233 | dstr_ensure(d, 1); |
| 234 | left = d->sz - off; |
| 235 | } |
| 236 | |
| 237 | /* --- End-of-file or newline ends the loop --- */ |
| 238 | |
| 239 | if (ch == EOF || ch == '\n') { |
| 240 | d->buf[off] = 0; |
| 241 | d->len = off; |
| 242 | return rd; |
| 243 | } |
| 244 | |
| 245 | /* --- Append the character and continue --- */ |
| 246 | |
| 247 | d->buf[off++] = ch; |
| 248 | left--; rd++; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* --- @dstr_write@ --- * |
| 253 | * |
| 254 | * Arguments: @dstr *d@ = pointer to a dynamic string block |
| 255 | * @FILE *fp@ = a stream to write on |
| 256 | * |
| 257 | * Returns: The number of bytes written (as for @fwrite@). |
| 258 | * |
| 259 | * Use: Writes a dynamic string to a file. |
| 260 | */ |
| 261 | |
| 262 | size_t dstr_write(const dstr *d, FILE *fp) { return (DWRITE(d, fp)); } |
| 263 | |
| 264 | /*----- That's all, folks -------------------------------------------------*/ |