X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/1c622be53c551a86b0fbf5ce8f2ee2d776f421b2..21988c314858eeaf92a8b81be8e6ce6ee2243607:/tools/tesh2/src/str_replace.c diff --git a/tools/tesh2/src/str_replace.c b/tools/tesh2/src/str_replace.c index e53c505c06..efb916bd07 100644 --- a/tools/tesh2/src/str_replace.c +++ b/tools/tesh2/src/str_replace.c @@ -11,26 +11,26 @@ str_replace(char** str, const char* what, const char* with) size_t pos, i; char* begin; char* buf; - + if(!(begin = strstr(*str, what))) { errno = ESRCH; return -1; } - + pos = begin - *str; - + i = 0; - + pos += strlen(what); - + if(begin == *str) { if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char)))) return -1; - + strcpy(buf, with); - + if(pos < strlen(*str)) strcpy(buf + strlen(with), *str + pos); } @@ -38,21 +38,21 @@ str_replace(char** str, const char* what, const char* with) { if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char)))) return -1; - + strncpy(buf, *str, (begin - *str)); strcpy(buf + (begin - *str) , with); - + if(pos < strlen(*str)) strcpy(buf + (begin - *str) + strlen(with), *str + pos); - } - + } + free(*str);; *str = buf; - + return 0; - -} + +} */ /* last version int @@ -70,7 +70,7 @@ str_replace(char** str, const char* what, const char* with, const char* delimite return -1; } - + if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char)))) return -1; @@ -81,38 +81,38 @@ str_replace(char** str, const char* what, const char* with, const char* delimite memset(delimited, 0, (strlen(what) + 2)); sprintf(delimited,"%s%c", what, delimiters[i]); - + if((begin = strstr(*str, delimited))) break; } - + free(delimited); - - + + if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what)) begin = strstr(*str, what); - + if(!begin) { errno = ESRCH; return -1; } - + pos = begin - *str; - + i = 0; - + pos += strlen(what); if(begin == *str) { - + if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char)))) return -1; - + strcpy(buf, with); - + if(pos < strlen(*str)) strcpy(buf + strlen(with), *str + pos); } @@ -120,26 +120,26 @@ str_replace(char** str, const char* what, const char* with, const char* delimite { if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char)))) return -1; - + strncpy(buf, *str, (begin - *str)); strcpy(buf + (begin - *str) , with); if(pos < strlen(*str)) strcpy(buf + (begin - *str) + strlen(with), *str + pos); - } - + } + free(*str);; *str = buf; - + return 0; - + }*/ int str_replace(char** str, const char* what, const char* with, const char* delimiters) { size_t pos, i, len; - char* begin; + char* begin = NULL; char* buf; int size; @@ -163,40 +163,40 @@ str_replace(char** str, const char* what, const char* with, const char* delimite memset(delimited, 0, (strlen(what) + 2)); sprintf(delimited,"%s%c", what, delimiters[i]); - + if((begin = strstr(*str, delimited))) break; } - + free(delimited); } else begin = strstr(*str, what); - - + + if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what)) begin = strstr(*str, what); - + if(!begin) { errno = ESRCH; return -1; } - + pos = begin - *str; - + i = 0; - + pos += strlen(what); if(begin == *str) { if(!(buf = (char*) calloc((with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char)))) return -1; - + if(with) strcpy(buf, with); - + if(pos < strlen(*str)) strcpy(buf + (with ? strlen(with) : 0), *str + pos); } @@ -204,31 +204,31 @@ str_replace(char** str, const char* what, const char* with, const char* delimite { if(!(buf = (char*) calloc((begin - *str) + (with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char)))) return -1; - + strncpy(buf, *str, (begin - *str)); - + if(with) strcpy(buf + (begin - *str) , with); if(pos < strlen(*str)) strcpy(buf + (begin - *str) + (with ? strlen(with) : 0), *str + pos); - } - + } + free(*str); *str = buf; - + return 0; - -} + +} int str_replace_all(char** str, const char* what, const char* with, const char* delimiters) { int rv; - + while(!(rv = str_replace(str, what, with, delimiters))); - + return (errno == ESRCH) ? 0 : -1; } @@ -237,9 +237,9 @@ str_replace_all(char** str, const char* what, const char* with, const char* deli str_replace_all(char** str, const char* what, const char* with) { int rv; - + while(!(rv = str_replace(str, what, with))); - + return (errno == ESRCH) ? 0 : -1; }*/