/* -*-c-*-
*
- * $Id: dstr.c,v 1.10 1999/10/04 21:44:47 mdw Exp $
+ * $Id: dstr.c,v 1.12 1999/12/10 23:42:04 mdw Exp $
*
* Handle dynamically growing strings
*
/*----- Revision history --------------------------------------------------*
*
* $Log: dstr.c,v $
+ * 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.
*
* memory-hungry, but efficient.
*/
-#define DSTR_INITSZ 256 /* Initial buffer size */
+#define DSTR_INITSZ 64 /* Initial buffer size */
/*----- Main code ---------------------------------------------------------*/
/* --- @dstr_reset@ --- *
*
- * Arguments: @dstr *d@ = pointer to a dynaimc string block
+ * Arguments: @dstr *d@ = pointer to a dynamic string block
*
* Returns: ---
*
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);
void dstr_tidy(dstr *d)
{
- dstr_putz(d);
d->buf = xrealloc(d->buf, d->len + 1);
+ d->buf[d->len] = 0;
d->sz = d->len + 1;
}
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);
if (ch == EOF && !rd)
return (EOF);
+ /* --- Make sure there's some buffer space --- */
+
+ if (!left) {
+ dstr_ensure(d, 1);
+ left = d->sz - off;
+ }
+
/* --- End-of-file or newline ends the loop --- */
if (ch == EOF || ch == '\n') {