Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
I think I just killed a simcall
[simgrid.git] / src / xbt / xbt_strbuff.c
index c3247d2..4369aca 100644 (file)
@@ -7,27 +7,26 @@
  * 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");
 
-/**
-** Buffer code
-**/
-
-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;
 }
 
@@ -39,7 +38,8 @@ inline xbt_strbuff_t xbt_strbuff_new_from(const char *ctn)
 {
   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;
 }
 
@@ -58,6 +58,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;
@@ -72,10 +73,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
@@ -85,16 +97,15 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
  *
  * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
  *
- * You can provide a default value to use if the variable is not set in the dict by using
- * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they
- * shouldn't to respect the shell standard (:= form should set the value in the dict,
- * but does not) (BUG).
+ * You can provide a default value to use if the variable is not set in the dict by using  '${var:=default}' or
+ * '${var:-default}'. These two forms are equivalent, even if they shouldn't to respect the shell standard (:= form
+ * should set the value in the dict, but does not) (BUG).
  */
 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')
@@ -108,7 +119,6 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
       end++;
       xbt_assert(*end != '\0', "String ends with \\");
       break;
-
     case '\'':
       if (!in_double_quote) {
         /* simple quote not protected by double ones, note it */
@@ -121,16 +131,16 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
         in_double_quote = !in_double_quote;
       }
       break;
-
     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. */
@@ -152,7 +162,6 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
               default_value[p - end_var - 2] = '\0';
 
               end_subst = p + 1;        /* eat '}' */
-
               break;
             }
             end_var++;
@@ -163,14 +172,11 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
             end_subst = end_var + 1;    /* also kill the } in the name */
 
           xbt_assert(end_var != beg_var, "Variable name empty (${} is not valid)");
-
-
         } else {
           /* name given directly */
           beg_var = end;
           end_var = beg_var;
-          while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t'
-                 && *end_var != '\n')
+          while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t' && *end_var != '\n')
             end_var++;
           end_subst = end_var;
           xbt_assert (end_var != beg_var, "Variable name empty ($ is not valid)");
@@ -191,7 +197,8 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
         if (val_len <= end_subst - beg_subst) {
           /* enough room to do the substitute in place */
           memmove(beg_subst, value, val_len);   /* substitute */
-          memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string closer */
+          /* move the end of the string closer */
+          memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1);
 //          XBT_DEBUG("String is now: '%s'",b->data);
           end = beg_subst + val_len;    /* update the currently explored char in the overall loop */
 //          XBT_DEBUG("end of substituted section is now '%s'",end);
@@ -202,9 +209,10 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
           int tooshort = val_len - (end_subst - beg_subst) + 1 /* don't forget \0 */ ;
           int newused = b->used + tooshort;
           end += tooshort;      /* update the pointer of the overall loop */
-//          XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len- (end_subst-beg_subst), b->size - b->used);
+//          XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len-(end_subst-beg_subst),b->size - b->used);
           if (newused > b->size) {
-            /* We have to realloc the data area before (because b->size is too small). We have to update our pointers, too */
+            /* We have to realloc the data area before (because b->size is too small).
+             * We have to update our pointers, too */
             char *newdata = xbt_realloc(b->data, b->used + MAX(minimal_increment, tooshort));
             int offset = newdata - b->data;
             b->data = newdata;
@@ -213,19 +221,17 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
             beg_subst += offset;
             end_subst += offset;
           }
-          memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string a bit further */
+          /* move the end of the string a bit further */
+          memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1);
           memmove(beg_subst, value, val_len);   /* substitute */
           b->used = newused;
 //          XBT_DEBUG("String is now: %s",b->data);
         }
         free(value);
-
         free(default_value);
-
         end--;                  /* compensate the next end++ */
       }
       break;
-
     case '\0':
       done = 1;
       break;
@@ -237,7 +243,8 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
 #ifdef SIMGRID_TEST
 #include "xbt/strbuff.h"
 
-/* buffstr have 512 chars by default. Adding 1000 chars like this will force a resize, allowing us to test that b->used and b->size are consistent */
+/* buffstr have 512 chars by default. Adding 1000 chars like this will force a resize, allowing us to test that
+ * b->used and b->size are consistent */
 #define force_resize \
   "1.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
   "2.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
@@ -250,8 +257,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
   "9.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
   "0.........1.........2.........3.........4.........5.........6.........7.........8.........9........."
 
-static void mytest(const char *input, const char *patterns,
-                   const char *expected)
+static void mytest(const char *input, const char *patterns, const char *expected)
 {
   xbt_dynar_t dyn_patterns;     /* splited string */
   xbt_dict_t p;                 /* patterns */
@@ -274,8 +280,7 @@ static void mytest(const char *input, const char *patterns,
   xbt_strbuff_append(sb, input);
   xbt_strbuff_varsubst(sb, p);
   xbt_dict_free(&p);
-  xbt_test_assert(!strcmp(sb->data, expected),
-                   "Input (%s) with patterns (%s) leads to (%s) instead of (%s)",
+  xbt_test_assert(!strcmp(sb->data, expected), "Input (%s) with patterns (%s) leads to (%s) instead of (%s)",
                    input, patterns, sb->data, expected);
   xbt_strbuff_free(sb);
 }
@@ -350,8 +355,7 @@ XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute, "test the funct
   xbt_test_add("Value much longer, no braces, data before and after");
   mytest("toto $t tata", "t=" force_resize, "toto " force_resize " tata");
   xbt_test_add("Value much longer, braces, data before and after");
-  mytest("toto ${t} tata", "t=" force_resize,
-         "toto " force_resize " tata");
+  mytest("toto ${t} tata", "t=" force_resize, "toto " force_resize " tata");
 
   xbt_test_add("Escaped $");
   mytest("\\$tutu", "tutu=t", "\\$tutu");
@@ -369,7 +373,5 @@ XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute, "test the funct
   mytest("${t:-toto}", "", "toto");
   xbt_test_add("Useless default value (variable already defined)");
   mytest("${t:-toto}", "t=TRUC", "TRUC");
-
 }
-
 #endif                          /* SIMGRID_TEST */