* under the terms of the license (GNU LGPL) which comes with this package. */
#include "xbt/strbuff.h"
+#include <stdarg.h>
#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;
}
{
xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t));
res->data = xbt_strdup(ctn);
- res->used = res->size = strlen(ctn);
+ res->size = strlen(ctn);
+ res->used = res->size;
return res;
}
}
}
+/** @brief Adds some content at the end of the buffer */
void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
{
int addlen;
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
void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
{
char *end; /* pointers around the parsed chunk */
- int in_simple_quote = 0, in_double_quote = 0;
+ int in_simple_quote = 0;
+ int in_double_quote = 0;
int done = 0;
if (b->data[0] == '\0')
case '$':
if (!in_simple_quote) {
/* Go for the substitution. First search the variable name */
- char *beg_var, *end_var; /* variable name boundary */
- char *beg_subst, *end_subst = NULL; /* where value should be written to */
- char *value, *default_value = NULL;
+ char *beg_var;
+ char *end_var; /* variable name boundary */
+ char *beg_subst = end;
+ char *end_subst = NULL; /* where value should be written to */
+ char *value;
+ char *default_value = NULL;
int val_len;
- beg_subst = end;
if (*(++end) == '{') {
/* the variable name is enclosed in braces. */