From 29644f5396a22ec0d51266011f57741adb2fd171 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 11 Apr 2014 11:03:01 +0200 Subject: [PATCH] Add wrbuf_cstr_null --- include/yaz/wrbuf.h | 11 ++++++++++- src/wrbuf.c | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/include/yaz/wrbuf.h b/include/yaz/wrbuf.h index e7629d0..1d4f83b 100644 --- a/include/yaz/wrbuf.h +++ b/include/yaz/wrbuf.h @@ -242,11 +242,20 @@ YAZ_EXPORT int wrbuf_grow(WRBUF b, size_t minsize); #define wrbuf_buf(b) ((b)->buf) /** \brief returns WRBUF content as C-string - \param b WRBUF + \param b WRBUF (may not be NULL) \returns C-string */ YAZ_EXPORT const char *wrbuf_cstr(WRBUF b); +/** \brief returns WRBUF content as C-string or NULL + \param b WRBUF + \returns C-string or NULL + + This function returns NULL if either b is NULL or length of buffer is 0 +*/ +YAZ_EXPORT +const char *wrbuf_cstr_null(WRBUF b); + #define wrbuf_putc(b, c) \ ((void) ((b)->pos >= (b)->size ? wrbuf_grow(b, 1) : 0), \ (b)->buf[(b)->pos++] = (c), 0) diff --git a/src/wrbuf.c b/src/wrbuf.c index 3c63170..e57aab7 100644 --- a/src/wrbuf.c +++ b/src/wrbuf.c @@ -270,6 +270,15 @@ const char *wrbuf_cstr(WRBUF b) 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; +} + void wrbuf_cut_right(WRBUF b, size_t no_to_remove) { if (no_to_remove > b->pos) -- 1.7.10.4