Merge branch 'collectd-5.4'
[collectd.git] / src / daemon / utils_subst.c
1 /**
2  * collectd - src/utils_subst.c
3  * Copyright (C) 2008       Sebastian Harl
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Sebastian "tokkee" Harl <sh at tokkee.org>
25  **/
26
27 /*
28  * This module provides functions for string substitution.
29  */
30
31 #include "collectd.h"
32 #include "common.h"
33
34 char *subst (char *buf, size_t buflen, const char *string, size_t off1, size_t off2,
35                 const char *replacement)
36 {
37         char *out = buf;
38
39         char const *front;
40         char const *back;
41         size_t front_len;
42         size_t replacement_len;
43         size_t back_len;
44
45         if ((NULL == buf) || (0 == buflen) || (NULL == string) || (NULL == replacement))
46                 return NULL;
47
48         size_t string_len = strlen (string);
49         if ((off1 > string_len) || (off2 > string_len) || (off1 > off2))
50                 return NULL;
51
52         front = string;
53         back = string + off2;
54         front_len = off1;
55         replacement_len = strlen (replacement);
56         back_len = strlen (back);
57
58         if (front_len >= buflen) {
59                 front_len = buflen - 1;
60                 replacement_len = 0;
61                 back_len = 0;
62         } else if ((front_len + replacement_len) >= buflen) {
63                 replacement_len = buflen - (front_len + 1);
64                 back_len = 0;
65         } else if ((front_len + replacement_len + back_len) >= buflen) {
66                 back_len = buflen - (front_len + replacement_len + 1);
67         } else {
68                 buflen = front_len + replacement_len + back_len + 1;
69         }
70         assert ((front_len + replacement_len + back_len) == (buflen - 1));
71
72         if (front_len != 0) {
73                 sstrncpy (out, front, front_len + 1);
74                 out += front_len;
75         }
76
77         if (replacement_len != 0) {
78                 sstrncpy (out, replacement, replacement_len + 1);
79                 out += replacement_len;
80         }
81
82         if (back_len != 0) {
83                 sstrncpy (out, back, back_len + 1);
84                 out += back_len;
85         }
86
87         out[0] = 0;
88         return buf;
89 } /* subst */
90
91 char *asubst (const char *string, int off1, int off2, const char *replacement)
92 {
93         char *buf;
94         int   len;
95
96         char *ret;
97
98         if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
99                         || (NULL ==replacement))
100                 return NULL;
101
102         len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
103
104         buf = (char *)malloc (len);
105         if (NULL == buf)
106                 return NULL;
107
108         ret = subst (buf, len, string, off1, off2, replacement);
109         if (NULL == ret)
110                 free (buf);
111         return ret;
112 } /* asubst */
113
114 char *subst_string (char *buf, size_t buflen, const char *string,
115                 const char *needle, const char *replacement)
116 {
117         size_t needle_len;
118         size_t i;
119
120         if ((buf == NULL) || (string == NULL)
121                         || (needle == NULL) || (replacement == NULL))
122                 return (NULL);
123
124         needle_len = strlen (needle);
125         sstrncpy (buf, string, buflen);
126
127         /* Limit the loop to prevent endless loops. */
128         for (i = 0; i < buflen; i++)
129         {
130                 char temp[buflen];
131                 char *begin_ptr;
132                 size_t begin;
133
134                 /* Find `needle' in `buf'. */
135                 begin_ptr = strstr (buf, needle);
136                 if (begin_ptr == NULL)
137                         break;
138
139                 /* Calculate the start offset. */
140                 begin = begin_ptr - buf;
141
142                 /* Substitute the region using `subst'. The result is stored in
143                  * `temp'. */
144                 begin_ptr = subst (temp, buflen, buf,
145                                 begin, begin + needle_len,
146                                 replacement);
147                 if (begin_ptr == NULL)
148                 {
149                         WARNING ("subst_string: subst failed.");
150                         break;
151                 }
152
153                 /* Copy the new string in `temp' to `buf' for the next round. */
154                 strncpy (buf, temp, buflen);
155         }
156
157         if (i >= buflen)
158         {
159                 WARNING ("subst_string: Loop exited after %zu iterations: "
160                                 "string = %s; needle = %s; replacement = %s;",
161                                 i, string, needle, replacement);
162         }
163
164         return (buf);
165 } /* char *subst_string */
166
167 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
168