From: Adam Dickmeiss Date: Fri, 5 Aug 2011 12:35:20 +0000 (+0200) Subject: New WRBUF utility: wrbuf_insert X-Git-Tag: v4.2.8~13^2~2 X-Git-Url: http://lists.indexdata.dk/?a=commitdiff_plain;h=027d8648a24b0b61b31a5f1752b29520791fd2fd;p=yaz-moved-to-github.git New WRBUF utility: wrbuf_insert --- diff --git a/include/yaz/wrbuf.h b/include/yaz/wrbuf.h index 1296538..1b11e2f 100644 --- a/include/yaz/wrbuf.h +++ b/include/yaz/wrbuf.h @@ -63,13 +63,21 @@ YAZ_EXPORT void wrbuf_destroy(WRBUF b); */ YAZ_EXPORT void wrbuf_rewind(WRBUF b); -/** \brief append constant size buffer to WRBU +/** \brief append constant size buffer to WRBUF \param b WRBUF \param buf buffer \param size size of buffer */ YAZ_EXPORT void wrbuf_write(WRBUF b, const char *buf, size_t size); +/** \brief inserts buffer into WRBUF at some position + \param b WRBUF + \param pos position (0=beginning) + \param buf buffer + \param size size of buffer + */ +YAZ_EXPORT void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size); + /** \brief appends C-string to WRBUF \param b WRBUF \param buf C-string (0-terminated) diff --git a/src/wrbuf.c b/src/wrbuf.c index 67de45e..13793ed 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -74,6 +74,17 @@ void wrbuf_write(WRBUF b, const char *buf, size_t size) b->pos += size; } +void wrbuf_insert(WRBUF b, size_t pos, const char *buf, size_t size) +{ + if (size <= 0 || pos > b->pos) + return; + if (b->pos + size >= b->size) + wrbuf_grow(b, size); + memmove(b->buf + pos + size, b->buf + pos, b->pos - pos); + memcpy(b->buf + pos, buf, size); + b->pos += size; +} + void wrbuf_puts(WRBUF b, const char *buf) { wrbuf_write(b, buf, strlen(buf));