1 #include <str_replace.h>
\r
9 str_replace(char** str, const char* what, const char* with)
\r
15 if(!(begin = strstr(*str, what)))
\r
25 pos += strlen(what);
\r
29 if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
34 if(pos < strlen(*str))
\r
35 strcpy(buf + strlen(with), *str + pos);
\r
39 if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
42 strncpy(buf, *str, (begin - *str));
\r
43 strcpy(buf + (begin - *str) , with);
\r
46 if(pos < strlen(*str))
\r
47 strcpy(buf + (begin - *str) + strlen(with), *str + pos);
\r
59 str_replace(char** str, const char* what, const char* with, const char* delimiters)
\r
67 if(!*str || !what || !with || !delimiters)
\r
74 if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))
\r
77 len = strlen(delimiters);
\r
79 for(i = 0; i < len; i++)
\r
81 memset(delimited, 0, (strlen(what) + 2));
\r
83 sprintf(delimited,"%s%c", what, delimiters[i]);
\r
85 if((begin = strstr(*str, delimited)))
\r
92 if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))
\r
93 begin = strstr(*str, what);
\r
101 pos = begin - *str;
\r
105 pos += strlen(what);
\r
111 if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
116 if(pos < strlen(*str))
\r
117 strcpy(buf + strlen(with), *str + pos);
\r
121 if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
124 strncpy(buf, *str, (begin - *str));
\r
125 strcpy(buf + (begin - *str) , with);
\r
127 if(pos < strlen(*str))
\r
128 strcpy(buf + (begin - *str) + strlen(with), *str + pos);
\r
139 str_replace(char** str, const char* what, const char* with, const char* delimiters)
\r
141 size_t pos, i, len;
\r
156 if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))
\r
159 len = strlen(delimiters);
\r
161 for(i = 0; i < len; i++)
\r
163 memset(delimited, 0, (strlen(what) + 2));
\r
165 sprintf(delimited,"%s%c", what, delimiters[i]);
\r
167 if((begin = strstr(*str, delimited)))
\r
174 begin = strstr(*str, what);
\r
177 if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))
\r
178 begin = strstr(*str, what);
\r
186 pos = begin - *str;
\r
190 pos += strlen(what);
\r
194 if(!(buf = (char*) calloc((with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
200 if(pos < strlen(*str))
\r
201 strcpy(buf + (with ? strlen(with) : 0), *str + pos);
\r
205 if(!(buf = (char*) calloc((begin - *str) + (with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
\r
208 strncpy(buf, *str, (begin - *str));
\r
211 strcpy(buf + (begin - *str) , with);
\r
213 if(pos < strlen(*str))
\r
214 strcpy(buf + (begin - *str) + (with ? strlen(with) : 0), *str + pos);
\r
226 str_replace_all(char** str, const char* what, const char* with, const char* delimiters)
\r
230 while(!(rv = str_replace(str, what, with, delimiters)));
\r
232 return (errno == ESRCH) ? 0 : -1;
\r
237 str_replace_all(char** str, const char* what, const char* with)
\r
241 while(!(rv = str_replace(str, what, with)));
\r
243 return (errno == ESRCH) ? 0 : -1;
\r