X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/2f810149832a2d855c33d0df5b02d736c2081e41..571ed94e23c9aefbd5f81f1a800eb5f1dddd8ae9:/src/xbt/xbt_strbuff.c diff --git a/src/xbt/xbt_strbuff.c b/src/xbt/xbt_strbuff.c index 688b4d9bd5..ae27f4e713 100644 --- a/src/xbt/xbt_strbuff.c +++ b/src/xbt/xbt_strbuff.c @@ -1,16 +1,11 @@ /* strbuff -- string buffers */ -/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team. +/* Copyright (c) 2007-2015. 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 @@ -29,8 +24,8 @@ XBT_INLINE void xbt_strbuff_empty(xbt_strbuff_t b) 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; @@ -42,7 +37,7 @@ xbt_strbuff_t xbt_strbuff_new(void) */ XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(const char *ctn) { - xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t)); + xbt_strbuff_t res = xbt_malloc(sizeof(s_xbt_strbuff_t)); res->data = xbt_strdup(ctn); res->used = res->size = strlen(ctn); return res; @@ -68,34 +63,19 @@ void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd) int addlen; int needed_space; - if (!b) - THROWF(arg_error, 0, "Asked to append stuff to NULL buffer"); + xbt_assert(b, "Asked to append stuff to NULL buffer"); addlen = strlen(toadd); needed_space = b->used + addlen + 1; if (needed_space > b->size) { b->size = MAX(minimal_increment + b->used, needed_space); - b->data = realloc(b->data, b->size); + b->data = xbt_realloc(b->data, b->size); } strcpy(b->data + b->used, toadd); b->used += addlen; } -XBT_INLINE void xbt_strbuff_chomp(xbt_strbuff_t b) -{ - while (b->used && b->data[b->used - 1] == '\n') { - b->used--; - b->data[b->used] = '\0'; - } -} - -XBT_INLINE void xbt_strbuff_trim(xbt_strbuff_t b) -{ - xbt_str_trim(b->data, " "); - b->used = strlen(b->data); -} - /** @brief Replaces a set of variables by their values * * @param b buffer to modify @@ -126,8 +106,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) case '\\': /* Protected char; pass the protection */ end++; - if (*end == '\0') - THROWF(arg_error, 0, "String ends with \\"); + xbt_assert(*end != '\0', "String ends with \\"); break; case '\'': @@ -166,9 +145,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) char *p = end_var + 1; while (*p != '\0' && *p != '}') p++; - if (*p == '\0') - THROWF(arg_error, 0, - "Variable default value not terminated ('}' missing)"); + xbt_assert (*p != '\0', "Variable default value not terminated ('}' missing)"); default_value = xbt_malloc(p - end_var - 1); memcpy(default_value, end_var + 2, p - end_var - 2); @@ -180,15 +157,12 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) } end_var++; } - if (*end_var == '\0') - THROWF(arg_error, 0, - "Variable name not terminated ('}' missing)"); + xbt_assert(*end_var != '\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) - THROWF(arg_error, 0, "Variable name empty (${} is not valid)"); + xbt_assert(end_var != beg_var, "Variable name empty (${} is not valid)"); } else { @@ -199,22 +173,11 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) && *end_var != '\n') end_var++; end_subst = end_var; - if (end_var == beg_var) - THROWF(arg_error, 0, "Variable name empty ($ is not valid)"); + xbt_assert (end_var != beg_var, "Variable name empty ($ is not valid)"); } -/* 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); -/* 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)"));*/ + value = xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var); if (value) value = xbt_strdup(value); @@ -225,18 +188,9 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) /* En route for the actual substitution */ val_len = strlen(value); -// 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 */ -// 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 */ -// 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 */ // XBT_DEBUG("String is now: '%s'",b->data); end = beg_subst + val_len; /* update the currently explored char in the overall loop */ @@ -245,16 +199,13 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) // 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 */ ; + 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); 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); @@ -277,6 +228,7 @@ void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns) case '\0': done = 1; + break; } end++; }