\r
#include <stdio.h>\r
\r
-int\r
+/*int\r
str_replace(char** str, const char* what, const char* with)\r
{\r
size_t pos, i;\r
return 0;\r
\r
} \r
+*/\r
+\r
+/* last version int\r
+str_replace(char** str, const char* what, const char* with, const char* delimiters)\r
+{\r
+ size_t pos, i, len;\r
+ char* begin;\r
+ char* buf;\r
+ char* delimited;\r
+ int size;\r
+\r
+ if(!*str || !what || !with || !delimiters)\r
+ {\r
+ errno = EINVAL;\r
+ return -1;\r
+ }\r
+\r
+ \r
+ if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))\r
+ return -1;\r
+\r
+ len = strlen(delimiters);\r
+\r
+ for(i = 0; i < len; i++)\r
+ {\r
+ memset(delimited, 0, (strlen(what) + 2));\r
+\r
+ sprintf(delimited,"%s%c", what, delimiters[i]);\r
+ \r
+ if((begin = strstr(*str, delimited)))\r
+ break;\r
+ }\r
+ \r
+ free(delimited);\r
+ \r
+ \r
+ if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))\r
+ begin = strstr(*str, what);\r
+ \r
+ if(!begin)\r
+ {\r
+ errno = ESRCH;\r
+ return -1;\r
+ }\r
+ \r
+ pos = begin - *str;\r
+ \r
+ i = 0;\r
+ \r
+ pos += strlen(what);\r
+\r
+ if(begin == *str)\r
+ {\r
+ \r
+\r
+ if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
+ return -1;\r
+ \r
+ strcpy(buf, with);\r
+ \r
+ if(pos < strlen(*str))\r
+ strcpy(buf + strlen(with), *str + pos);\r
+ }\r
+ else\r
+ {\r
+ if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
+ return -1;\r
+ \r
+ strncpy(buf, *str, (begin - *str));\r
+ strcpy(buf + (begin - *str) , with);\r
+\r
+ if(pos < strlen(*str))\r
+ strcpy(buf + (begin - *str) + strlen(with), *str + pos);\r
+ } \r
+ \r
+ free(*str);;\r
+ *str = buf;\r
+ \r
+ return 0;\r
+ \r
+}*/\r
\r
int\r
+str_replace(char** str, const char* what, const char* with, const char* delimiters)\r
+{\r
+ size_t pos, i, len;\r
+ char* begin;\r
+ char* buf;\r
+ int size;\r
+\r
+ if(!*str || !what)\r
+ {\r
+ errno = EINVAL;\r
+ return -1;\r
+ }\r
+\r
+ if(delimiters)\r
+ {\r
+ char* delimited;\r
+\r
+ if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))\r
+ return -1;\r
+\r
+ len = strlen(delimiters);\r
+\r
+ for(i = 0; i < len; i++)\r
+ {\r
+ memset(delimited, 0, (strlen(what) + 2));\r
+\r
+ sprintf(delimited,"%s%c", what, delimiters[i]);\r
+ \r
+ if((begin = strstr(*str, delimited)))\r
+ break;\r
+ }\r
+ \r
+ free(delimited);\r
+ }\r
+ else\r
+ begin = strstr(*str, what);\r
+ \r
+ \r
+ if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))\r
+ begin = strstr(*str, what);\r
+ \r
+ if(!begin)\r
+ {\r
+ errno = ESRCH;\r
+ return -1;\r
+ }\r
+ \r
+ pos = begin - *str;\r
+ \r
+ i = 0;\r
+ \r
+ pos += strlen(what);\r
+\r
+ if(begin == *str)\r
+ {\r
+ if(!(buf = (char*) calloc((with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
+ return -1;\r
+ \r
+ if(with)\r
+ strcpy(buf, with);\r
+ \r
+ if(pos < strlen(*str))\r
+ strcpy(buf + (with ? strlen(with) : 0), *str + pos);\r
+ }\r
+ else\r
+ {\r
+ if(!(buf = (char*) calloc((begin - *str) + (with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
+ return -1;\r
+ \r
+ strncpy(buf, *str, (begin - *str));\r
+ \r
+ if(with)\r
+ strcpy(buf + (begin - *str) , with);\r
+\r
+ if(pos < strlen(*str))\r
+ strcpy(buf + (begin - *str) + (with ? strlen(with) : 0), *str + pos);\r
+ } \r
+ \r
+ free(*str);\r
+\r
+ *str = buf;\r
+ \r
+ return 0;\r
+ \r
+} \r
+\r
+int\r
+str_replace_all(char** str, const char* what, const char* with, const char* delimiters)\r
+{\r
+ int rv;\r
+ \r
+ while(!(rv = str_replace(str, what, with, delimiters)));\r
+ \r
+ return (errno == ESRCH) ? 0 : -1;\r
+}\r
+\r
+\r
+/*int\r
str_replace_all(char** str, const char* what, const char* with)\r
{\r
int rv;\r
while(!(rv = str_replace(str, what, with)));\r
\r
return (errno == ESRCH) ? 0 : -1;\r
-}\r
+}*/\r
+\r
\r
\r