X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/7410b72db09489e8b9d3ee3cb087f35882397d93..1d04f47ddcc46f37eca622ca5a343c7197115990:/src/xbt/xbt_strbuff.c diff --git a/src/xbt/xbt_strbuff.c b/src/xbt/xbt_strbuff.c index 1d3067c542..9aead1ac34 100644 --- a/src/xbt/xbt_strbuff.c +++ b/src/xbt/xbt_strbuff.c @@ -7,23 +7,26 @@ * under the terms of the license (GNU LGPL) which comes with this package. */ #include "xbt/strbuff.h" +#include #define minimal_increment 512 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff, xbt, "String buffers"); -inline void xbt_strbuff_empty(xbt_strbuff_t b) +/** @brief Remove any content from the buffer */ +inline void xbt_strbuff_clear(xbt_strbuff_t b) { b->used = 0; b->data[0] = '\0'; } +/** @brief Constructor */ xbt_strbuff_t xbt_strbuff_new(void) { xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t)); res->data = xbt_malloc(512); res->size = 512; - xbt_strbuff_empty(res); + xbt_strbuff_clear(res); return res; } @@ -54,6 +57,7 @@ inline void xbt_strbuff_free(xbt_strbuff_t b) } } +/** @brief Adds some content at the end of the buffer */ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd) { int addlen; @@ -68,10 +72,21 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd) b->size = MAX(minimal_increment + b->used, needed_space); b->data = xbt_realloc(b->data, b->size); } - strcpy(b->data + b->used, toadd); + strncpy(b->data + b->used, toadd, b->size-b->used); b->used += addlen; } +/** @brief format some content and push it at the end of the buffer */ +void xbt_strbuff_printf(xbt_strbuff_t b, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + char *data = bvprintf(fmt, ap); + xbt_strbuff_append(b, data); + xbt_free(data); + va_end(ap); +} + /** @brief Replaces a set of variables by their values * * @param b buffer to modify