Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove borland-compiler related cruft
[simgrid.git] / src / xbt / xbt_strbuff.c
index 4fc5f63..12045e9 100644 (file)
@@ -1,18 +1,11 @@
-/* $Id: buff.c 3483 2007-05-07 11:18:56Z mquinson $ */
-
 /* strbuff -- string buffers                                                */
 
-/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+/* Copyright (c) 2007-2014. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-/* specific to Borland Compiler */
-#ifdef __BORLANDDC__
-#pragma hdrstop
-#endif
-
 #include "xbt/strbuff.h"
 
 #define minimal_increment 512
@@ -26,14 +19,13 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff, xbt, "String buffers");
 XBT_INLINE void xbt_strbuff_empty(xbt_strbuff_t b)
 {
   b->used = 0;
-  b->data[0] = '\n';
-  b->data[1] = '\0';
+  b->data[0] = '\0';
 }
 
 xbt_strbuff_t xbt_strbuff_new(void)
 {
-  xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
-  res->data = malloc(512);
+  xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t));
+  res->data = xbt_malloc(512);
   res->size = 512;
   xbt_strbuff_empty(res);
   return res;
@@ -41,12 +33,12 @@ xbt_strbuff_t xbt_strbuff_new(void)
 
 /** @brief creates a new string buffer containing the provided string
  *
- * Beware, we store the ctn directly, not a copy of it
+ * Beware, the ctn is copied, you want to free it afterward, anyhow
  */
-XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(char *ctn)
+XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(const char *ctn)
 {
-  xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
-  res->data = ctn;
+  xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t));
+  res->data = xbt_strdup(ctn);
   res->used = res->size = strlen(ctn);
   return res;
 }
@@ -61,8 +53,7 @@ XBT_INLINE void xbt_strbuff_free_container(xbt_strbuff_t b)
 XBT_INLINE void xbt_strbuff_free(xbt_strbuff_t b)
 {
   if (b) {
-    if (b->data)
-      free(b->data);
+    free(b->data);
     free(b);
   }
 }
@@ -73,15 +64,14 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
   int needed_space;
 
   if (!b)
-    THROW0(arg_error, 0, "Asked to append stuff to NULL buffer");
+    THROWF(arg_error, 0, "Asked to append stuff to NULL buffer");
 
   addlen = strlen(toadd);
   needed_space = b->used + addlen + 1;
 
   if (needed_space > b->size) {
-    b->data =
-      realloc(b->data, MAX(minimal_increment + b->used, needed_space));
     b->size = MAX(minimal_increment + b->used, needed_space);
+    b->data = xbt_realloc(b->data, b->size);
   }
   strcpy(b->data + b->used, toadd);
   b->used += addlen;
@@ -89,10 +79,9 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
 
 XBT_INLINE void xbt_strbuff_chomp(xbt_strbuff_t b)
 {
-  while (b->data[b->used] == '\n') {
+  while (b->used && b->data[b->used - 1] == '\n') {
+    b->used--;
     b->data[b->used] = '\0';
-    if (b->used)
-      b->used--;
   }
 }
 
@@ -133,7 +122,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
       /* Protected char; pass the protection */
       end++;
       if (*end == '\0')
-        THROW0(arg_error, 0, "String ends with \\");
+        THROWF(arg_error, 0, "String ends with \\");
       break;
 
     case '\'':
@@ -173,7 +162,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
               while (*p != '\0' && *p != '}')
                 p++;
               if (*p == '\0')
-                THROW0(arg_error, 0,
+                THROWF(arg_error, 0,
                        "Variable default value not terminated ('}' missing)");
 
               default_value = xbt_malloc(p - end_var - 1);
@@ -187,14 +176,14 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
             end_var++;
           }
           if (*end_var == '\0')
-            THROW0(arg_error, 0,
+            THROWF(arg_error, 0,
                    "Variable name not terminated ('}' missing)");
 
           if (!end_subst)       /* already set if there's a default value */
             end_subst = end_var + 1;    /* also kill the } in the name */
 
           if (end_var == beg_var)
-            THROW0(arg_error, 0, "Variable name empty (${} is not valid)");
+            THROWF(arg_error, 0, "Variable name empty (${} is not valid)");
 
 
         } else {
@@ -206,18 +195,18 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
             end_var++;
           end_subst = end_var;
           if (end_var == beg_var)
-            THROW0(arg_error, 0, "Variable name empty ($ is not valid)");
+            THROWF(arg_error, 0, "Variable name empty ($ is not valid)");
         }
-/*        DEBUG5("var='%.*s'; subst='%.*s'; End_var = '%s'",
+/*        XBT_DEBUG("var='%.*s'; subst='%.*s'; End_var = '%s'",
             end_var-beg_var,beg_var,
             end_subst-beg_subst,beg_subst,
             end_var);*/
 
         /* ok, we now have the variable name. Search the dictionary for the substituted value */
         value =
-          xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var);
-/*        DEBUG1("Deal with '%s'",b->data);
-        DEBUG4("Search for %.*s, found %s (default value = %s)\n",
+            xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var);
+/*        XBT_DEBUG("Deal with '%s'",b->data);
+        XBT_DEBUG("Search for %.*s, found %s (default value = %s)\n",
             end_var-beg_var,beg_var,
             (value?value:"(no value)"),
             (default_value?default_value:"(no value)"));*/
@@ -231,35 +220,36 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
 
         /* En route for the actual substitution */
         val_len = strlen(value);
-//        DEBUG2("val_len = %d, key_len=%d",val_len,end_subst-beg_subst);
+//        XBT_DEBUG("val_len = %d, key_len=%d",val_len,end_subst-beg_subst);
         if (val_len <= end_subst - beg_subst) {
           /* enough room to do the substitute in place */
-//          DEBUG4("Substitute key name by its value: ie '%.*s' by '%.*s'",end_subst-beg_subst,beg_subst,val_len,value);
+//          XBT_DEBUG("Substitute key name by its value: ie '%.*s' by '%.*s'",end_subst-beg_subst,beg_subst,val_len,value);
           memmove(beg_subst, value, val_len);   /* substitute */
-//          DEBUG1("String is now: '%s'",b->data);
-/*          DEBUG8("Move end of string closer (%d chars moved) :\n-'%.*s%.*s'\n+'%.*s%s'",
+//          XBT_DEBUG("String is now: '%s'",b->data);
+/*          XBT_DEBUG("Move end of string closer (%d chars moved) :\n-'%.*s%.*s'\n+'%.*s%s'",
               b->used - (end_subst - b->data) + 1,
               beg_subst-b->data,b->data,
               b->used-(end_subst-b->data)+1,beg_subst+val_len,
               beg_subst-b->data,b->data,
               end_subst);*/
           memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string closer */
-//          DEBUG1("String is now: '%s'",b->data);
+//          XBT_DEBUG("String is now: '%s'",b->data);
           end = beg_subst + val_len;    /* update the currently explored char in the overall loop */
-//          DEBUG1("end of substituted section is now '%s'",end);
+//          XBT_DEBUG("end of substituted section is now '%s'",end);
           b->used -= end_subst - beg_subst - val_len;   /* update string buffer used size */
-//          DEBUG3("Used:%d end:%d ending char:%d",b->used,end-b->data,*end);
+//          XBT_DEBUG("Used:%d end:%d ending char:%d",b->used,end-b->data,*end);
         } else {
           /* we have to extend the data area */
           int tooshort =
-            val_len - (end_subst - beg_subst) + 1 /*don't forget \0 */ ;
+              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 */
-//          DEBUG2("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 */
-            char *newdata =
-              realloc(b->data, b->used + MAX(minimal_increment, tooshort));
+            char *newdata = xbt_realloc(b->data,
+                                    b->used + MAX(minimal_increment,
+                                                  tooshort));
             int offset = newdata - b->data;
             b->data = newdata;
             b->size = b->used + MAX(minimal_increment, tooshort);
@@ -267,17 +257,16 @@ 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) + 2); /* move the end of the string a bit further */
+          memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string a bit further */
           memmove(beg_subst, value, val_len);   /* substitute */
           b->used = newused;
-//          DEBUG1("String is now: %s",b->data);
+//          XBT_DEBUG("String is now: %s",b->data);
         }
         free(value);
 
-        if (default_value)
-          free(default_value);
+        free(default_value);
 
-        end--; /* compensate the next end++*/
+        end--;                  /* compensate the next end++ */
       }
       break;
 
@@ -313,14 +302,14 @@ static void mytest(const char *input, const char *patterns,
   char *str;                    /*foreach */
   xbt_strbuff_t sb;             /* what we test */
 
-  p = xbt_dict_new();
+  p = xbt_dict_new_homogeneous(free);
   dyn_patterns = xbt_str_split(patterns, " ");
   xbt_dynar_foreach(dyn_patterns, cpt, str) {
     xbt_dynar_t keyvals = xbt_str_split(str, "=");
     char *key = xbt_dynar_get_as(keyvals, 0, char *);
     char *val = xbt_dynar_get_as(keyvals, 1, char *);
     xbt_str_subst(key, '_', ' ', 0);    // to put space in names without breaking the enclosing dynar_foreach
-    xbt_dict_set(p, key, xbt_strdup(val), free);
+    xbt_dict_set(p, key, xbt_strdup(val), NULL);
     xbt_dynar_free(&keyvals);
   }
   xbt_dynar_free(&dyn_patterns);
@@ -328,101 +317,102 @@ 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_assert4(!strcmp(sb->data, expected),
+  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);
 }
 
 XBT_TEST_SUITE("xbt_strbuff", "String Buffers");
-XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute,"test the function xbt_strbuff_substitute")
+XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute, "test the function xbt_strbuff_substitute")
 {
-  xbt_test_add0("Empty");
+  xbt_test_add("Empty");
   mytest("", "", "");
 
-  xbt_test_add0("Value shorter, no braces, only variable");
+  xbt_test_add("Value shorter, no braces, only variable");
   mytest("$tutu", "tutu=t", "t");
-  xbt_test_add0("Value shorter, braces, only variable");
+  xbt_test_add("Value shorter, braces, only variable");
   mytest("${tutu}", "tutu=t", "t");
-  xbt_test_add0("Value shorter, no braces, data after");
+  xbt_test_add("Value shorter, no braces, data after");
   mytest("$tutu toto", "tutu=t", "t toto");
-  xbt_test_add0("Value shorter, braces, data after");
+  xbt_test_add("Value shorter, braces, data after");
   mytest("${tutu} toto", "tutu=t", "t toto");
-  xbt_test_add0("Value shorter, no braces, data before");
+  xbt_test_add("Value shorter, no braces, data before");
   mytest("toto $tutu", "tutu=t", "toto t");
-  xbt_test_add0("Value shorter, braces, data before");
+  xbt_test_add("Value shorter, braces, data before");
   mytest("toto ${tutu}", "tutu=t", "toto t");
-  xbt_test_add0("Value shorter, no braces, data before and after");
+  xbt_test_add("Value shorter, no braces, data before and after");
   mytest("toto $tutu tata", "tutu=t", "toto t tata");
-  xbt_test_add0("Value shorter, braces, data before and after");
+  xbt_test_add("Value shorter, braces, data before and after");
   mytest("toto ${tutu} tata", "tutu=t", "toto t tata");
 
-  xbt_test_add0("Value as long, no braces, only variable");
+  xbt_test_add("Value as long, no braces, only variable");
   mytest("$tutu", "tutu=12345", "12345");
-  xbt_test_add0("Value as long, braces, only variable");
+  xbt_test_add("Value as long, braces, only variable");
   mytest("${tutu}", "tutu=1234567", "1234567");
-  xbt_test_add0("Value as long, no braces, data after");
+  xbt_test_add("Value as long, no braces, data after");
   mytest("$tutu toto", "tutu=12345", "12345 toto");
-  xbt_test_add0("Value as long, braces, data after");
+  xbt_test_add("Value as long, braces, data after");
   mytest("${tutu} toto", "tutu=1234567", "1234567 toto");
-  xbt_test_add0("Value as long, no braces, data before");
+  xbt_test_add("Value as long, no braces, data before");
   mytest("toto $tutu", "tutu=12345", "toto 12345");
-  xbt_test_add0("Value as long, braces, data before");
+  xbt_test_add("Value as long, braces, data before");
   mytest("toto ${tutu}", "tutu=1234567", "toto 1234567");
-  xbt_test_add0("Value as long, no braces, data before and after");
+  xbt_test_add("Value as long, no braces, data before and after");
   mytest("toto $tutu tata", "tutu=12345", "toto 12345 tata");
-  xbt_test_add0("Value as long, braces, data before and after");
+  xbt_test_add("Value as long, braces, data before and after");
   mytest("toto ${tutu} tata", "tutu=1234567", "toto 1234567 tata");
 
-  xbt_test_add0("Value longer, no braces, only variable");
+  xbt_test_add("Value longer, no braces, only variable");
   mytest("$t", "t=tututu", "tututu");
-  xbt_test_add0("Value longer, braces, only variable");
+  xbt_test_add("Value longer, braces, only variable");
   mytest("${t}", "t=tututu", "tututu");
-  xbt_test_add0("Value longer, no braces, data after");
+  xbt_test_add("Value longer, no braces, data after");
   mytest("$t toto", "t=tututu", "tututu toto");
-  xbt_test_add0("Value longer, braces, data after");
+  xbt_test_add("Value longer, braces, data after");
   mytest("${t} toto", "t=tututu", "tututu toto");
-  xbt_test_add0("Value longer, no braces, data before");
+  xbt_test_add("Value longer, no braces, data before");
   mytest("toto $t", "t=tututu", "toto tututu");
-  xbt_test_add0("Value longer, braces, data before");
+  xbt_test_add("Value longer, braces, data before");
   mytest("toto ${t}", "t=tututu", "toto tututu");
-  xbt_test_add0("Value longer, no braces, data before and after");
+  xbt_test_add("Value longer, no braces, data before and after");
   mytest("toto $t tata", "t=tututu", "toto tututu tata");
-  xbt_test_add0("Value longer, braces, data before and after");
+  xbt_test_add("Value longer, braces, data before and after");
   mytest("toto ${t} tata", "t=tututu", "toto tututu tata");
 
-  xbt_test_add0("Value much longer, no braces, only variable");
+  xbt_test_add("Value much longer, no braces, only variable");
   mytest("$t", "t=" force_resize, force_resize);
-  xbt_test_add0("Value much longer, no braces, data after");
+  xbt_test_add("Value much longer, no braces, data after");
   mytest("$t toto", "t=" force_resize, force_resize " toto");
-  xbt_test_add0("Value much longer, braces, data after");
+  xbt_test_add("Value much longer, braces, data after");
   mytest("${t} toto", "t=" force_resize, force_resize " toto");
-  xbt_test_add0("Value much longer, no braces, data before");
+  xbt_test_add("Value much longer, no braces, data before");
   mytest("toto $t", "t=" force_resize, "toto " force_resize);
-  xbt_test_add0("Value much longer, braces, data before");
+  xbt_test_add("Value much longer, braces, data before");
   mytest("toto ${t}", "t=" force_resize, "toto " force_resize);
-  xbt_test_add0("Value much longer, no braces, data before and after");
+  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_add0("Value much longer, 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");
 
-  xbt_test_add0("Escaped $");
+  xbt_test_add("Escaped $");
   mytest("\\$tutu", "tutu=t", "\\$tutu");
-  xbt_test_add0("Space in var name (with braces)");
+  xbt_test_add("Space in var name (with braces)");
   mytest("${tu ti}", "tu_ti=t", "t");
 
-  xbt_test_add0("Two variables");
+  xbt_test_add("Two variables");
   mytest("$toto $tutu", "toto=1 tutu=2", "1 2");
 
   // Commented: I'm too lazy to do a memmove in var name to remove the backslash after use.
   // Users should use braces.
-  //  xbt_test_add0("Escaped space in var name", "$tu\\ ti", "tu_ti=t", "t");
+  //  xbt_test_add("Escaped space in var name", "$tu\\ ti", "tu_ti=t", "t");
 
-  xbt_test_add0("Default value");
+  xbt_test_add("Default value");
   mytest("${t:-toto}", "", "toto");
-  xbt_test_add0("Useless default value (variable already defined)");
+  xbt_test_add("Useless default value (variable already defined)");
   mytest("${t:-toto}", "t=TRUC", "TRUC");
 
 }
 
-#endif /* SIMGRID_TEST */
+#endif                          /* SIMGRID_TEST */