Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sanitize the use of logs in unit testing of parmap to fix supernovae
[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 \rint \r
8 str_replace(char **str, const char *what, const char *with,
9             const char *delimiters) \r
10 {
11   \rsize_t pos, i, len;
12   \rchar *begin = NULL;
13   \rchar *buf;
14   \rint size;
15   \r\rif (!*str || !what)
16     \r {
17     \rerrno = EINVAL;
18     \rreturn -1;
19     \r}
20   \r\rif (delimiters)
21     \r {
22     \rchar *delimited;
23     \r\rif (!(delimited = (char *) calloc((strlen(what) + 2), sizeof(char))))
24       \rreturn -1;
25     \r\rlen = strlen(delimiters);
26     \r\rfor (i = 0; i < len; i++)
27       \r {
28       \rmemset(delimited, 0, (strlen(what) + 2));
29       \r\rsprintf(delimited, "%s%c", what, delimiters[i]);
30       \r\rif ((begin = strstr(*str, delimited)))
31         \rbreak;
32       \r}
33     \r\rfree(delimited);
34     \r}
35   \r
36   else
37     \rbegin = strstr(*str, what);
38   \r\r\rif (!begin && (size = (int) strlen(*str) - (int) strlen(what)) >= 0
39          && !strcmp(*str + size, what))
40     \rbegin = strstr(*str, what);
41   \r\rif (!begin)
42     \r {
43     \rerrno = ESRCH;
44     \rreturn -1;
45     \r}
46   \r\rpos = begin - *str;
47   \r\ri = 0;
48   \r\rpos += strlen(what);
49   \r\rif (begin == *str)
50     \r {
51     \rif (!
52          (buf =
53           (char *) calloc((with ? strlen(with) : 0) +
54                           ((pos <
55                             strlen(*str)) ? strlen(*str + pos) : 0) + 1,
56                           sizeof(char))))
57       \rreturn -1;
58     \r\rif (with)
59       \rstrcpy(buf, with);
60     \r\rif (pos < strlen(*str))
61       \rstrcpy(buf + (with ? strlen(with) : 0), *str + pos);
62     \r}
63   \r
64   else
65     \r {
66     \rif (!
67          (buf =
68           (char *) calloc((begin - *str) + (with ? strlen(with) : 0) +
69                           ((pos <
70                             strlen(*str)) ? strlen(*str + pos) : 0) + 1,
71                           sizeof(char))))
72       \rreturn -1;
73     \r\rstrncpy(buf, *str, (begin - *str));
74     \r\rif (with)
75       \rstrcpy(buf + (begin - *str), with);
76     \r\rif (pos < strlen(*str))
77       \rstrcpy(buf + (begin - *str) + (with ? strlen(with) : 0),
78               *str + pos);
79     \r}
80   \r\rfree(*str);
81   \r\r*str = buf;
82   \r\rreturn 0;
83 \r\r}
84
85 \r\rint \r
86 str_replace_all(char **str, const char *what, const char *with,
87                 const char *delimiters) \r
88 {
89   \rint rv;
90   \r\rwhile (!(rv = str_replace(str, what, with, delimiters)));
91   \r\rreturn (errno == ESRCH) ? 0 : -1;
92 \r}
93
94 \r\r\r\r\r\r