Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix sem_init testing.
[simgrid.git] / src / xbt / xbt_strbuff.c
index b19cb4f..098827b 100644 (file)
@@ -2,8 +2,8 @@
 
 /* strbuff -- string buffers                                                */
 
-/* Copyright (c) 2007 Martin Quinson.                                       */
-/* All rights reserved.                                                     */
+/* Copyright (c) 2007, 2008, 2009, 2010. 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. */
@@ -23,7 +23,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff, xbt, "String buffers");
 ** Buffer code
 **/
 
-void xbt_strbuff_empty(xbt_strbuff_t b)
+XBT_INLINE void xbt_strbuff_empty(xbt_strbuff_t b)
 {
   b->used = 0;
   b->data[0] = '\n';
@@ -43,7 +43,7 @@ xbt_strbuff_t xbt_strbuff_new(void)
  *
  * Beware, we store the ctn directly, not a copy of it
  */
-xbt_strbuff_t xbt_strbuff_new_from(char *ctn)
+XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(char *ctn)
 {
   xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
   res->data = ctn;
@@ -52,13 +52,13 @@ xbt_strbuff_t xbt_strbuff_new_from(char *ctn)
 }
 
 /** @brief frees only the container without touching to the contained string */
-void xbt_strbuff_free_container(xbt_strbuff_t b)
+XBT_INLINE void xbt_strbuff_free_container(xbt_strbuff_t b)
 {
   free(b);
 }
 
 /** @brief frees the buffer and its content */
-void xbt_strbuff_free(xbt_strbuff_t b)
+XBT_INLINE void xbt_strbuff_free(xbt_strbuff_t b)
 {
   if (b) {
     if (b->data)
@@ -80,14 +80,14 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
 
   if (needed_space > b->size) {
     b->data =
-      realloc(b->data, MAX(minimal_increment + b->used, needed_space));
+        realloc(b->data, MAX(minimal_increment + b->used, needed_space));
     b->size = MAX(minimal_increment + b->used, needed_space);
   }
   strcpy(b->data + b->used, toadd);
   b->used += addlen;
 }
 
-void xbt_strbuff_chomp(xbt_strbuff_t b)
+XBT_INLINE void xbt_strbuff_chomp(xbt_strbuff_t b)
 {
   while (b->data[b->used] == '\n') {
     b->data[b->used] = '\0';
@@ -96,7 +96,7 @@ void xbt_strbuff_chomp(xbt_strbuff_t b)
   }
 }
 
-void xbt_strbuff_trim(xbt_strbuff_t b)
+XBT_INLINE void xbt_strbuff_trim(xbt_strbuff_t b)
 {
   xbt_str_trim(b->data, " ");
   b->used = strlen(b->data);
@@ -215,7 +215,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
 
         /* 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);
+            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",
             end_var-beg_var,beg_var,
@@ -252,14 +252,15 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
         } 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);
           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 = 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,7 +268,7 @@ 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);
@@ -277,7 +278,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
         if (default_value)
           free(default_value);
 
-        end--; /* compensate the next end++*/
+        end--;                  /* compensate the next end++ */
       }
       break;
 
@@ -335,7 +336,7 @@ static void mytest(const char *input, const char *patterns,
 }
 
 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");
   mytest("", "", "");
@@ -404,7 +405,8 @@ XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute,"test the functi
   xbt_test_add0("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");
+  mytest("toto ${t} tata", "t=" force_resize,
+         "toto " force_resize " tata");
 
   xbt_test_add0("Escaped $");
   mytest("\\$tutu", "tutu=t", "\\$tutu");
@@ -425,4 +427,4 @@ XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute,"test the functi
 
 }
 
-#endif /* SIMGRID_TEST */
+#endif                          /* SIMGRID_TEST */