Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e53c505c064b9484e70dbcfa1157746a4b7764a0
[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)\r
10 {\r
11         size_t pos, i;\r
12         char* begin;\r
13         char* buf;\r
14          \r
15         if(!(begin = strstr(*str, what)))\r
16         {\r
17                 errno = ESRCH;\r
18                 return -1;\r
19         }\r
20         \r
21         pos = begin - *str;\r
22         \r
23         i = 0;\r
24         \r
25         pos += strlen(what);\r
26         \r
27         if(begin == *str)\r
28         {\r
29                 if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
30                         return -1;\r
31                         \r
32                 strcpy(buf, with);\r
33                 \r
34                 if(pos < strlen(*str))\r
35                         strcpy(buf + strlen(with), *str + pos);\r
36         }\r
37         else\r
38         {\r
39                 if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
40                         return -1;\r
41                 \r
42                 strncpy(buf, *str,  (begin - *str));\r
43                 strcpy(buf + (begin - *str) , with);\r
44                 \r
45 \r
46                 if(pos < strlen(*str))\r
47                         strcpy(buf + (begin - *str) + strlen(with), *str + pos);\r
48         }       \r
49         \r
50         free(*str);;\r
51         *str = buf;\r
52         \r
53         return 0;\r
54  \r
55\r
56 */\r
57 \r
58 /* last version int\r
59 str_replace(char** str, const char* what, const char* with, const char* delimiters)\r
60 {\r
61         size_t pos, i, len;\r
62         char* begin;\r
63         char* buf;\r
64         char* delimited;\r
65         int size;\r
66 \r
67         if(!*str || !what || !with || !delimiters)\r
68         {\r
69                 errno = EINVAL;\r
70                 return -1;\r
71         }\r
72 \r
73         \r
74         if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))\r
75                 return -1;\r
76 \r
77         len = strlen(delimiters);\r
78 \r
79         for(i = 0; i < len; i++)\r
80         {\r
81                 memset(delimited, 0, (strlen(what) + 2));\r
82 \r
83                 sprintf(delimited,"%s%c", what, delimiters[i]);\r
84                 \r
85                 if((begin = strstr(*str, delimited)))\r
86                         break;\r
87         }\r
88         \r
89         free(delimited);\r
90         \r
91         \r
92         if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))\r
93                 begin = strstr(*str, what);\r
94         \r
95         if(!begin)\r
96         {\r
97                 errno = ESRCH;\r
98                 return -1;\r
99         }\r
100         \r
101         pos = begin - *str;\r
102         \r
103         i = 0;\r
104         \r
105         pos += strlen(what);\r
106 \r
107         if(begin == *str)\r
108         {\r
109                 \r
110 \r
111                 if(!(buf = (char*) calloc(strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
112                         return -1;\r
113                         \r
114                 strcpy(buf, with);\r
115                 \r
116                 if(pos < strlen(*str))\r
117                         strcpy(buf + strlen(with), *str + pos);\r
118         }\r
119         else\r
120         {\r
121                 if(!(buf = (char*) calloc((begin - *str) + strlen(with) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
122                         return -1;\r
123                 \r
124                 strncpy(buf, *str,  (begin - *str));\r
125                 strcpy(buf + (begin - *str) , with);\r
126 \r
127                 if(pos < strlen(*str))\r
128                         strcpy(buf + (begin - *str) + strlen(with), *str + pos);\r
129         }       \r
130         \r
131         free(*str);;\r
132         *str = buf;\r
133         \r
134         return 0;\r
135  \r
136 }*/\r
137 \r
138 int\r
139 str_replace(char** str, const char* what, const char* with, const char* delimiters)\r
140 {\r
141         size_t pos, i, len;\r
142         char* begin;\r
143         char* buf;\r
144         int size;\r
145 \r
146         if(!*str || !what)\r
147         {\r
148                 errno = EINVAL;\r
149                 return -1;\r
150         }\r
151 \r
152         if(delimiters)\r
153         {\r
154                 char* delimited;\r
155 \r
156                 if(!(delimited = (char*) calloc((strlen(what) + 2) , sizeof(char))))\r
157                         return -1;\r
158 \r
159                 len = strlen(delimiters);\r
160 \r
161                 for(i = 0; i < len; i++)\r
162                 {\r
163                         memset(delimited, 0, (strlen(what) + 2));\r
164 \r
165                         sprintf(delimited,"%s%c", what, delimiters[i]);\r
166                         \r
167                         if((begin = strstr(*str, delimited)))\r
168                                 break;\r
169                 }\r
170                 \r
171                 free(delimited);\r
172         }\r
173         else\r
174                 begin = strstr(*str, what);\r
175         \r
176         \r
177         if(!begin && (size = (int)strlen(*str) - (int)strlen(what)) >= 0 && !strcmp(*str + size, what))\r
178                 begin = strstr(*str, what);\r
179         \r
180         if(!begin)\r
181         {\r
182                 errno = ESRCH;\r
183                 return -1;\r
184         }\r
185         \r
186         pos = begin - *str;\r
187         \r
188         i = 0;\r
189         \r
190         pos += strlen(what);\r
191 \r
192         if(begin == *str)\r
193         {\r
194                 if(!(buf = (char*) calloc((with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
195                         return -1;\r
196                         \r
197                 if(with)\r
198                         strcpy(buf, with);\r
199                 \r
200                 if(pos < strlen(*str))\r
201                         strcpy(buf + (with ? strlen(with) : 0), *str + pos);\r
202         }\r
203         else\r
204         {\r
205                 if(!(buf = (char*) calloc((begin - *str) + (with ? strlen(with) : 0) + ((pos < strlen(*str)) ? strlen(*str + pos) : 0) + 1, sizeof(char))))\r
206                         return -1;\r
207                 \r
208                 strncpy(buf, *str,  (begin - *str));\r
209                 \r
210                 if(with)\r
211                         strcpy(buf + (begin - *str) , with);\r
212 \r
213                 if(pos < strlen(*str))\r
214                         strcpy(buf + (begin - *str) + (with ? strlen(with) : 0), *str + pos);\r
215         }       \r
216         \r
217         free(*str);\r
218 \r
219         *str = buf;\r
220         \r
221         return 0;\r
222  \r
223\r
224 \r
225 int\r
226 str_replace_all(char** str, const char* what, const char* with, const char* delimiters)\r
227 {\r
228         int rv;\r
229         \r
230         while(!(rv = str_replace(str, what, with, delimiters)));\r
231         \r
232         return (errno == ESRCH) ? 0 : -1;\r
233 }\r
234 \r
235 \r
236 /*int\r
237 str_replace_all(char** str, const char* what, const char* with)\r
238 {\r
239         int rv;\r
240         \r
241         while(!(rv = str_replace(str, what, with)));\r
242         \r
243         return (errno == ESRCH) ? 0 : -1;\r
244 }*/\r
245 \r
246 \r
247 \r