1 #include <str_replace.h>
\r
9 str_replace(char** str, const char* what, const char* with, const char* delimiters)
\r
26 if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))
\r
29 len = strlen(delimiters);
\r
31 for(i = 0; i < len; i++)
\r
33 memset(delimited, 0, (strlen(what) + 2));
\r
35 sprintf(delimited,"%s%c", what, delimiters[i]);
\r
37 if((begin = strstr(*str, delimited)))
\r
44 begin = strstr(*str, what);
\r
47 if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))
\r
48 begin = strstr(*str, what);
\r
60 pos += strlen(what);
\r
64 if(!(buf = (char*) calloc((with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
70 if(pos < strlen(*str))
\r
71 strcpy(buf + (with ? strlen(with) : 0), *str + pos);
\r
75 if(!(buf = (char*) calloc((begin - *str) + (with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
78 strncpy(buf, *str, (begin - *str));
\r
81 strcpy(buf + (begin - *str) , with);
\r
83 if(pos < strlen(*str))
\r
84 strcpy(buf + (begin - *str) + (with ? strlen(with) : 0), *str + pos);
\r
96 str_replace_all(char** str, const char* what, const char* with, const char* delimiters)
\r
100 while(!(rv = str_replace(str, what, with, delimiters)));
\r
102 return (errno == ESRCH) ? 0 : -1;
\r