Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
update lua(bypass) examples
[simgrid.git] / tools / tesh2 / src / str_replace.c
index 5c868333b9231e0f3d5459bfe421a25912de2caa..976d93ca2ba09b6a4a7e2f0f98d0f0ac451ed79f 100644 (file)
@@ -4,64 +4,91 @@
 #include <stdlib.h>\r
 \r
 #include <stdio.h>\r
-\r
-int\r
-str_replace(char** str, const char* what, const char* with)\r
-{\r
-       size_t pos, i;\r
-       char* begin;\r
-       char* buf;\r
-        \r
-       if(!(begin = strstr(*str, what)))\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(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
-\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_all(char** str, const char* what, const char* with)\r
-{\r
-       int rv;\r
-       \r
-       while(!(rv = str_replace(str, what, with)));\r
-       \r
-       return (errno == ESRCH) ? 0 : -1;\r
-}\r
-\r
-\r
+\rint \r
+str_replace(char **str, const char *what, const char *with,
+            const char *delimiters) \r
+{
+  \rsize_t pos, i, len;
+  \rchar *begin = NULL;
+  \rchar *buf;
+  \rint size;
+  \r\rif (!*str || !what)
+    \r {
+    \rerrno = EINVAL;
+    \rreturn -1;
+    \r}
+  \r\rif (delimiters)
+    \r {
+    \rchar *delimited;
+    \r\rif (!(delimited = (char *) calloc((strlen(what) + 2), sizeof(char))))
+      \rreturn -1;
+    \r\rlen = strlen(delimiters);
+    \r\rfor (i = 0; i < len; i++)
+      \r {
+      \rmemset(delimited, 0, (strlen(what) + 2));
+      \r\rsprintf(delimited, "%s%c", what, delimiters[i]);
+      \r\rif ((begin = strstr(*str, delimited)))
+        \rbreak;
+      \r}
+    \r\rfree(delimited);
+    \r}
+  \r
+  else
+    \rbegin = strstr(*str, what);
+  \r\r\rif (!begin && (size = (int) strlen(*str) - (int) strlen(what)) >= 0
+         && !strcmp(*str + size, what))
+    \rbegin = strstr(*str, what);
+  \r\rif (!begin)
+    \r {
+    \rerrno = ESRCH;
+    \rreturn -1;
+    \r}
+  \r\rpos = begin - *str;
+  \r\ri = 0;
+  \r\rpos += strlen(what);
+  \r\rif (begin == *str)
+    \r {
+    \rif (!
+         (buf =
+          (char *) calloc((with ? strlen(with) : 0) +
+                          ((pos <
+                            strlen(*str)) ? strlen(*str + pos) : 0) + 1,
+                          sizeof(char))))
+      \rreturn -1;
+    \r\rif (with)
+      \rstrcpy(buf, with);
+    \r\rif (pos < strlen(*str))
+      \rstrcpy(buf + (with ? strlen(with) : 0), *str + pos);
+    \r}
+  \r
+  else
+    \r {
+    \rif (!
+         (buf =
+          (char *) calloc((begin - *str) + (with ? strlen(with) : 0) +
+                          ((pos <
+                            strlen(*str)) ? strlen(*str + pos) : 0) + 1,
+                          sizeof(char))))
+      \rreturn -1;
+    \r\rstrncpy(buf, *str, (begin - *str));
+    \r\rif (with)
+      \rstrcpy(buf + (begin - *str), with);
+    \r\rif (pos < strlen(*str))
+      \rstrcpy(buf + (begin - *str) + (with ? strlen(with) : 0),
+              *str + pos);
+    \r}
+  \r\rfree(*str);
+  \r\r*str = buf;
+  \r\rreturn 0;
+\r\r}
+
+\r\rint \r
+str_replace_all(char **str, const char *what, const char *with,
+                const char *delimiters) \r
+{
+  \rint rv;
+  \r\rwhile (!(rv = str_replace(str, what, with, delimiters)));
+  \r\rreturn (errno == ESRCH) ? 0 : -1;
+\r}
+
+\r\r\r\r\r\r