Merge branch 'collectd-5.4' into collectd-5.5
[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 #include "utils_subst.h"
34
35 char *subst (char *buf, size_t buflen, const char *string, int off1, int off2,
36                 const char *replacement)
37 {
38         char  *buf_ptr = buf;
39         size_t len     = buflen;
40
41         if ((NULL == buf) || (0 >= buflen) || (NULL == string)
42                         || (0 > off1) || (0 > off2) || (off1 > off2)
43                         || (NULL == replacement))
44                 return NULL;
45
46         sstrncpy (buf_ptr, string,
47                         ((size_t)off1 + 1 > buflen) ? buflen : (size_t)off1 + 1);
48         buf_ptr += off1;
49         len     -= off1;
50
51         if (0 >= len)
52                 return buf;
53
54         sstrncpy (buf_ptr, replacement, len);
55         buf_ptr += strlen (replacement);
56         len     -= strlen (replacement);
57
58         if (0 >= len)
59                 return buf;
60
61         sstrncpy (buf_ptr, string + off2, len);
62         return buf;
63 } /* subst */
64
65 char *asubst (const char *string, int off1, int off2, const char *replacement)
66 {
67         char *buf;
68         int   len;
69
70         char *ret;
71
72         if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
73                         || (NULL ==replacement))
74                 return NULL;
75
76         len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
77
78         buf = (char *)malloc (len);
79         if (NULL == buf)
80                 return NULL;
81
82         ret = subst (buf, len, string, off1, off2, replacement);
83         if (NULL == ret)
84                 free (buf);
85         return ret;
86 } /* asubst */
87
88 char *subst_string (char *buf, size_t buflen, const char *string,
89                 const char *needle, const char *replacement)
90 {
91         char *temp;
92         size_t needle_len;
93         size_t i;
94
95         if ((buf == NULL) || (string == NULL)
96                         || (needle == NULL) || (replacement == NULL))
97                 return (NULL);
98
99         temp = (char *) malloc (buflen);
100         if (temp == NULL)
101         {
102                 ERROR ("subst_string: malloc failed.");
103                 return (NULL);
104         }
105
106         needle_len = strlen (needle);
107         strncpy (buf, string, buflen);
108
109         /* Limit the loop to prevent endless loops. */
110         for (i = 0; i < buflen; i++)
111         {
112                 char *begin_ptr;
113                 size_t begin;
114
115                 /* Find `needle' in `buf'. */
116                 begin_ptr = strstr (buf, needle);
117                 if (begin_ptr == NULL)
118                         break;
119
120                 /* Calculate the start offset. */
121                 begin = begin_ptr - buf;
122
123                 /* Substitute the region using `subst'. The result is stored in
124                  * `temp'. */
125                 begin_ptr = subst (temp, buflen, buf,
126                                 begin, begin + needle_len,
127                                 replacement);
128                 if (begin_ptr == NULL)
129                 {
130                         WARNING ("subst_string: subst failed.");
131                         break;
132                 }
133
134                 /* Copy the new string in `temp' to `buf' for the next round. */
135                 strncpy (buf, temp, buflen);
136         }
137
138         if (i >= buflen)
139         {
140                 WARNING ("subst_string: Loop exited after %zu iterations: "
141                                 "string = %s; needle = %s; replacement = %s;",
142                                 i, string, needle, replacement);
143         }
144
145         sfree (temp);
146         return (buf);
147 } /* char *subst_string */
148
149 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
150