X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/002eaee36ed12caf6cf7f2a7188111240a427d80..78b1464e4a78dc364ecc8aebcfd5ec13b3f6bb07:/dstr.c diff --git a/dstr.c b/dstr.c index 82f0805..273c53a 100644 --- a/dstr.c +++ b/dstr.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: dstr.c,v 1.10 1999/10/04 21:44:47 mdw Exp $ + * $Id: dstr.c,v 1.15 2000/07/16 12:29:16 mdw Exp $ * * Handle dynamically growing strings * @@ -30,6 +30,21 @@ /*----- Revision history --------------------------------------------------* * * $Log: dstr.c,v $ + * Revision 1.15 2000/07/16 12:29:16 mdw + * Change to arena `realloc' interface, to fix a design bug. + * + * Revision 1.14 2000/06/17 10:37:39 mdw + * Add support for arena management. + * + * Revision 1.13 1999/12/22 15:39:28 mdw + * Fix overflow in dstr_putline. + * + * Revision 1.12 1999/12/10 23:42:04 mdw + * Change header file guard names. + * + * Revision 1.11 1999/10/28 22:05:29 mdw + * Modify and debug allocation routines. + * * Revision 1.10 1999/10/04 21:44:47 mdw * Move `dstr_putf' and `dstr_vputf' into a separate source file. * @@ -80,7 +95,7 @@ * memory-hungry, but efficient. */ -#define DSTR_INITSZ 256 /* Initial buffer size */ +#define DSTR_INITSZ 64 /* Initial buffer size */ /*----- Main code ---------------------------------------------------------*/ @@ -108,7 +123,7 @@ void dstr_destroy(dstr *d) { DDESTROY(d); } /* --- @dstr_reset@ --- * * - * Arguments: @dstr *d@ = pointer to a dynaimc string block + * Arguments: @dstr *d@ = pointer to a dynamic string block * * Returns: --- * @@ -142,15 +157,14 @@ void dstr_ensure(dstr *d, size_t sz) nsz = d->sz; - if (nsz == 0 && rq < DSTR_INITSZ) - nsz = DSTR_INITSZ; - else - do nsz <<= 1; while (nsz < rq); + if (nsz == 0) + nsz = (DSTR_INITSZ >> 1); + do nsz <<= 1; while (nsz < rq); if (d->buf) - d->buf = xrealloc(d->buf, nsz); + d->buf = x_realloc(d->a, d->buf, nsz, d->sz); else - d->buf = xmalloc(nsz); + d->buf = x_alloc(d->a, nsz); d->sz = nsz; } @@ -229,8 +243,8 @@ void dstr_putm(dstr *d, const void *p, size_t sz) { DPUTM(d, p, sz); } void dstr_tidy(dstr *d) { - dstr_putz(d); - d->buf = xrealloc(d->buf, d->len + 1); + d->buf = x_realloc(d->a, d->buf, d->len + 1, d->sz); + d->buf[d->len] = 0; d->sz = d->len + 1; } @@ -256,13 +270,6 @@ int dstr_putline(dstr *d, FILE *fp) for (;;) { - /* --- Make sure there's some buffer space --- */ - - if (!left) { - dstr_ensure(d, 1); - left = d->sz - off; - } - /* --- Read the next byte --- */ ch = getc(fp); @@ -272,6 +279,14 @@ int dstr_putline(dstr *d, FILE *fp) if (ch == EOF && !rd) return (EOF); + /* --- Make sure there's some buffer space --- */ + + if (!left) { + d->len = off; + dstr_ensure(d, 1); + left = d->sz - off; + } + /* --- End-of-file or newline ends the loop --- */ if (ch == EOF || ch == '\n') {