2 * Copyright (c) 1995-2003, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: wrbuf.c,v 1.12 2003-01-06 08:20:28 adam Exp $
10 * Growing buffer for writing various stuff.
22 #include <yaz/wrbuf.h>
24 WRBUF wrbuf_alloc(void)
28 if (!(n = (WRBUF)xmalloc(sizeof(*n))))
36 void wrbuf_free(WRBUF b, int free_buf)
38 if (free_buf && b->buf)
43 void wrbuf_rewind(WRBUF b)
48 int wrbuf_grow(WRBUF b, int minsize)
58 if (b->size && !(b->buf =(char *)xrealloc(b->buf, b->size += togrow)))
60 else if (!b->size && !(b->buf = (char *)xmalloc(b->size = togrow)))
65 int wrbuf_write(WRBUF b, const char *buf, int size)
69 if (b->pos + size >= b->size)
71 memcpy(b->buf + b->pos, buf, size);
76 int wrbuf_puts(WRBUF b, const char *buf)
78 wrbuf_write(b, buf, strlen(buf)+1); /* '\0'-terminate as well */
79 (b->pos)--; /* don't include '\0' in count */
83 void wrbuf_printf(WRBUF b, const char *fmt, ...)
90 _vsnprintf(buf, sizeof(buf)-1, fmt, ap);
94 vsnprintf(buf, sizeof(buf)-1, fmt, ap);
96 vsprintf(buf, fmt, ap);