version 1.19 | | version 1.20 |
---|
| | |
/* | | /* |
* static char *rcsid_utils_c = | | * static char *rcsid_utils_c = |
* "$Id: utils.c,v 1.19 2005/10/13 07:26:00 mwedel Exp $"; | | * "$Id: utils.c,v 1.20 2005/10/17 13:08:26 qal21 Exp $"; |
*/ | | */ |
| | |
/* | | /* |
| | |
if (!strcmp(buf+strlen(buf)-EOL_SIZE,"\n")) | | if (!strcmp(buf+strlen(buf)-EOL_SIZE,"\n")) |
buf[strlen(buf)-EOL_SIZE]='\0'; | | buf[strlen(buf)-EOL_SIZE]='\0'; |
} | | } |
| | |
| | /** |
| | * Replace in string src all occurrences of key by replacement. The resulting |
| | * string is put into result; at most resultsize characters (including the |
| | * terminating null character) will be written to result. |
| | */ |
| | void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize) |
| | { |
| | size_t resultlen; |
| | size_t keylen; |
| | |
| | /* special case to prevent infinite loop if key==replacement=="" */ |
| | if(strcmp(key, replacement) == 0) |
| | { |
| | snprintf(result, resultsize, "%s", src); |
| | return; |
| | } |
| | |
| | keylen = strlen(key); |
| | |
| | resultlen = 0; |
| | while(*src != '\0' && resultlen+1 < resultsize) |
| | { |
| | if(strncmp(src, key, keylen) == 0) |
| | { |
| | snprintf(result+resultlen, resultsize-resultlen, "%s", replacement); |
| | resultlen += strlen(result+resultlen); |
| | src += keylen; |
| | } |
| | else |
| | { |
| | result[resultlen++] = *src++; |
| | } |
| | } |
| | result[resultlen] = '\0'; |
| | } |