X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/dff9e15c44ab6340d27215957c56fa72fad246a2..a182030b5a8c0db05a1de18ca230b61e76363ebc:/src/xbt/xbt_strbuff.c diff --git a/src/xbt/xbt_strbuff.c b/src/xbt/xbt_strbuff.c index f734c480cc..c40e0fc19b 100644 --- a/src/xbt/xbt_strbuff.c +++ b/src/xbt/xbt_strbuff.c @@ -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) @@ -87,7 +87,7 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *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); @@ -128,7 +128,6 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) end = b->data; while (!done) { - switch (*end) { case '\\': /* Protected char; pass the protection */ @@ -209,42 +208,54 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) if (end_var == beg_var) THROW0(arg_error, 0, "Variable name empty ($ is not valid)"); } -// DEBUG1("End_var = %s",end_var); +/* DEBUG5("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); -// DEBUG4("Search for %.*s, found %s (default value = %s)\n", -// end_var-beg_var,beg_var, -// (value?value:"NULL"), -// (default_value?default_value:"NULL")); +/* DEBUG1("Deal with '%s'",b->data); + DEBUG4("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)"));*/ if (value) value = xbt_strdup(value); - else + else if (default_value) value = xbt_strdup(default_value); - - if (!value) + else value = xbt_strdup(""); /* En route for the actual substitution */ val_len = strlen(value); -// DEBUG2("val_len = %d, key_len=%d",val_len,end_subst-beg_subst); +// DEBUG2("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 */ -// INFO3("Substitute '%s' with '%s' for %d chars",beg_subst,value, val_len); +// DEBUG4("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 */ -// INFO3("Substitute '%s' with '%s' for %d chars",beg_subst+val_len,end_subst, b->used-(end_subst-b->data)+1); +// DEBUG1("String is now: '%s'",b->data); +/* DEBUG8("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); end = beg_subst + val_len; /* update the currently explored char in the overall loop */ +// DEBUG1("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); } else { /* we have to extend the data area */ 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 */ -// DEBUG2("Too short (by %d chars; %d chars left in area)",val_len- (end_subst-beg_subst), b->size - b->used); +// 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 = @@ -259,11 +270,14 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) 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, value, val_len); /* substitute */ b->used = newused; +// DEBUG1("String is now: %s",b->data); } free(value); if (default_value) free(default_value); + + end--; /* compensate the next end++*/ } break; @@ -321,8 +335,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("", "", "");