Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
s/NOTRECEIPT/NOTRECEIVED/ (plus some reindent)
[simgrid.git] / tools / tesh2 / src / str_replace.c
index f4c275a..efb916b 100644 (file)
-#include <str_replace.h>
-#include <string.h>
-#include <errno.h>
-#include <stdlib.h>
-
-#include <stdio.h>
-
-int
-str_replace(char** str, const char* what, const char* with)
-{
-       int pos, i;
-       char* begin;
-       char* buf;
-        
-       if(!(begin = strstr(*str, what)))
-       {
-               errno = ESRCH;
-               return -1;
-       }
-       
-       pos = begin - *str;
-       
-       i = 0;
-       
-       /*while(begin[i] != ' ' && begin[i] != '\n' && begin[i] != '\r' && begin[i] != '\0')
-               i++;
-       
-       pos += i;
-       */
-       
-       pos += strlen(what);
-       
-       if(begin == *str)
-       {
-               if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
-                       return -1;
-                       
-               strcpy(buf, with);
-               
-               if(pos < strlen(*str))
-                       strcpy(buf + strlen(with), *str + pos);
-       }
-       else
-       {
-               if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))
-                       return -1;
-               
-               strncpy(buf, *str,  (begin - *str));
-               strcpy(buf + (begin - *str) , with);
-               
-
-               if(pos < strlen(*str))
-                       strcpy(buf + (begin - *str) + strlen(with), *str + pos);
-       }       
-       
-       free(*str);;
-       *str = buf;
-       
-       return 0;
-} 
-
-int
-str_replace_all(char** str, const char* what, const char* with)
-{
-       int rv;
-       
-       while(!(rv = str_replace(str, what, with)));
-       
-       return (errno == ESRCH) ? 0 : -1;
-}
-
-
+#include <str_replace.h>\r
+#include <string.h>\r
+#include <errno.h>\r
+#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
+\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 = NULL;\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
+\r
+       while(!(rv = str_replace(str, what, with)));\r
+\r
+       return (errno == ESRCH) ? 0 : -1;\r
+}*/\r
+\r
+\r
+\r