From: Martin Quinson Date: Thu, 10 Nov 2011 16:20:04 +0000 (+0100) Subject: fix the constness of xbt_strbuff_new_from(), it's too dangerous with previous prototype X-Git-Tag: exp_20120216~335 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/6f9d72501fa9d57a34e2d5309adb5d6947182eea?ds=sidebyside fix the constness of xbt_strbuff_new_from(), it's too dangerous with previous prototype --- diff --git a/include/xbt/str.h b/include/xbt/str.h index 225ef425bd..aa15a11a62 100644 --- a/include/xbt/str.h +++ b/include/xbt/str.h @@ -41,7 +41,7 @@ XBT_PUBLIC(char *) xbt_str_join_array(const char *const *strs, const char *sep); /* */ XBT_PUBLIC(void) xbt_str_subst(char *str, char from, char to, int amount); -XBT_PUBLIC(char *) xbt_str_varsubst(char *str, xbt_dict_t patterns); +XBT_PUBLIC(char *) xbt_str_varsubst(const char *str, xbt_dict_t patterns); /* */ XBT_PUBLIC(void) xbt_str_strip_spaces(char *); diff --git a/include/xbt/strbuff.h b/include/xbt/strbuff.h index 1666655730..eb1f59f29f 100644 --- a/include/xbt/strbuff.h +++ b/include/xbt/strbuff.h @@ -28,7 +28,7 @@ typedef struct { XBT_PUBLIC(void) xbt_strbuff_empty(xbt_strbuff_t b); XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new(void); -XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new_from(char *s); +XBT_PUBLIC(xbt_strbuff_t) xbt_strbuff_new_from(const char *s); XBT_PUBLIC(void) xbt_strbuff_free(xbt_strbuff_t b); XBT_PUBLIC(void) xbt_strbuff_free_container(xbt_strbuff_t b); XBT_PUBLIC(void) xbt_strbuff_append(xbt_strbuff_t b, const char *toadd); diff --git a/src/xbt/xbt_str.c b/src/xbt/xbt_str.c index 12b674aa75..6438d43742 100644 --- a/src/xbt/xbt_str.c +++ b/src/xbt/xbt_str.c @@ -191,14 +191,21 @@ void xbt_str_subst(char *str, char from, char to, int occurence) /** @brief Replaces a set of variables by their values * - * @param str where to apply the change - * @param patterns what to change + * @param str The input of the replacement process + * @param patterns The changes to apply * @return The string modified * - * Check xbt_strbuff_varsubst() for more details, and remember that the string may be reallocated (moved) in the process. + * Both '$toto' and '${toto}' are valid (and the two writing are equivalent). + * + * 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). */ -char *xbt_str_varsubst(char *str, xbt_dict_t patterns) +char *xbt_str_varsubst(const char *str, xbt_dict_t patterns) { xbt_strbuff_t buff = xbt_strbuff_new_from(str); char *res; diff --git a/src/xbt/xbt_strbuff.c b/src/xbt/xbt_strbuff.c index 16835fd961..1db44325f6 100644 --- a/src/xbt/xbt_strbuff.c +++ b/src/xbt/xbt_strbuff.c @@ -40,12 +40,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; + res->data = xbt_strdup(ctn); res->used = res->size = strlen(ctn); return res; }