X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fwrbuf.c;h=6d9b145d99b4da4f954692d174fb76708db4fa2d;hp=344e5742cc00e71ba8d6ad8a1deff7892e918724;hb=dca8928db421aa8750ac9ffead1a5c09a85f4f8b;hpb=e529ed18a6825f980bb16b230e56c6d53743ced1 diff --git a/src/wrbuf.c b/src/wrbuf.c index 344e574..6d9b145 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2013 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -30,6 +31,7 @@ WRBUF wrbuf_alloc(void) n->buf = 0; n->size = 0; n->pos = 0; + wrbuf_grow(n, 1); return n; } @@ -57,9 +59,8 @@ int wrbuf_grow(WRBUF b, size_t minsize) togrow = b->size; if (togrow < minsize) togrow = minsize; - if (b->size && !(b->buf =(char *)xrealloc(b->buf, b->size += togrow))) - abort(); - else if (!b->size && !(b->buf = (char *)xmalloc(b->size = togrow))) + b->buf = (char *) xrealloc(b->buf, 1 + (b->size += togrow)); + if (!b->buf) abort(); return 0; } @@ -262,8 +263,17 @@ void wrbuf_iconv_reset(WRBUF b, yaz_iconv_t cd) const char *wrbuf_cstr(WRBUF b) { - wrbuf_putc(b, '\0'); /* add '\0' */ - (b->pos)--; /* don't include '\0' in count */ + assert(b && b->pos <= b->size); + b->buf[b->pos] = '\0'; + return b->buf; +} + +const char *wrbuf_cstr_null(WRBUF b) +{ + if (!b || b->pos == 0) + return 0; + assert(b->pos <= b->size); + b->buf[b->pos] = '\0'; return b->buf; }