Replaces all instances of 'search' with 'replace' Returns number of sub-strings modified, or -1 on error
| 158 | ///Replaces all instances of 'search' with 'replace' |
| 159 | ///Returns number of sub-strings modified, or -1 on error |
| 160 | int str_replace(char *str, const char *search, const char *replace) { |
| 161 | unsigned int i=0,j=0; //mbg merge 7/17/06 changed to unsigned int |
| 162 | int searchlen,replacelen; |
| 163 | char *astr; |
| 164 | |
| 165 | searchlen = strlen(search); |
| 166 | replacelen = strlen(replace); |
| 167 | if ((!strlen(str)) || (!searchlen)) return -1; //note: allow *replace to have a length of zero! |
| 168 | if (!(astr = (char*)malloc(strlen(str)+1))) return -1; |
| 169 | while (i < strlen(str)) { |
| 170 | if (!strncmp(str+i,search,searchlen)) { |
| 171 | if (replacelen) memcpy(astr+j,replace,replacelen); |
| 172 | i += searchlen; |
| 173 | j += replacelen; |
| 174 | } |
| 175 | else astr[j++] = str[i++]; |
| 176 | } |
| 177 | astr[j] = 0; |
| 178 | strcpy(str,astr); |
| 179 | free(astr); |
| 180 | return j; |
| 181 | } |
| 182 | |
| 183 | static const struct Base64Table |
| 184 | { |