Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Java lang with cmake.
[simgrid.git] / tools / tesh2 / src / str_replace.c
1 #include <str_replace.h>\r
2 #include <string.h>\r
3 #include <errno.h>\r
4 #include <stdlib.h>\r
5 \r
6 #include <stdio.h>\r
7 \r
8 int\r
9 str_replace(char** str, const char* what, const char* with, const char* delimiters)\r
10 {\r
11         size_t pos, i, len;\r
12         char* begin = NULL;\r
13         char* buf;\r
14         int size;\r
15 \r
16         if(!*str || !what)\r
17         {\r
18                 errno = EINVAL;\r
19                 return -1;\r
20         }\r
21 \r
22         if(delimiters)\r
23         {\r
24                 char* delimited;\r
25 \r
26                 if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))\r
27                         return -1;\r
28 \r
29                 len = strlen(delimiters);\r
30 \r
31                 for(i = 0; i < len; i++)\r
32                 {\r
33                         memset(delimited, 0, (strlen(what) + 2));\r
34 \r
35                         sprintf(delimited,"%s%c", what, delimiters[i]);\r
36 \r
37                         if((begin = strstr(*str, delimited)))\r
38                                 break;\r
39                 }\r
40 \r
41                 free(delimited);\r
42         }\r
43         else\r
44                 begin = strstr(*str, what);\r
45 \r
46 \r
47         if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))\r
48                 begin = strstr(*str, what);\r
49 \r
50         if(!begin)\r
51         {\r
52                 errno = ESRCH;\r
53                 return -1;\r
54         }\r
55 \r
56         pos = begin - *str;\r
57 \r
58         i = 0;\r
59 \r
60         pos += strlen(what);\r
61 \r
62         if(begin == *str)\r
63         {\r
64                 if(!(buf = (char*) calloc((with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
65                         return -1;\r
66 \r
67                 if(with)\r
68                         strcpy(buf, with);\r
69 \r
70                 if(pos < strlen(*str))\r
71                         strcpy(buf + (with ? strlen(with) : 0), *str + pos);\r
72         }\r
73         else\r
74         {\r
75                 if(!(buf = (char*) calloc((begin - *str) + (with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
76                         return -1;\r
77 \r
78                 strncpy(buf, *str,  (begin - *str));\r
79 \r
80                 if(with)\r
81                         strcpy(buf + (begin - *str) , with);\r
82 \r
83                 if(pos < strlen(*str))\r
84                         strcpy(buf + (begin - *str) + (with ? strlen(with) : 0), *str + pos);\r
85         }\r
86 \r
87         free(*str);\r
88 \r
89         *str = buf;\r
90 \r
91         return 0;\r
92 \r
93 }\r
94 \r
95 int\r
96 str_replace_all(char** str, const char* what, const char* with, const char* delimiters)\r
97 {\r
98         int rv;\r
99 \r
100         while(!(rv = str_replace(str, what, with, delimiters)));\r
101 \r
102         return (errno == ESRCH) ? 0 : -1;\r
103 }\r
104 \r
105 \r
106 \r
107 \r
108 \r