Merge branch 'collectd-4.5'
[collectd.git] / src / utils_subst.c
1 /**
2  * collectd - src/utils_subst.c
3  * Copyright (C) 2008  Sebastian Harl
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Sebastian "tokkee" Harl <sh at tokkee.org>
20  **/
21
22 /*
23  * This module provides functions for string substitution.
24  */
25
26 #include "collectd.h"
27 #include "common.h"
28
29 char *subst (char *buf, size_t buflen, const char *string, int off1, int off2,
30                 const char *replacement)
31 {
32         char  *buf_ptr = buf;
33         size_t len     = buflen;
34
35         if ((NULL == buf) || (0 >= buflen) || (NULL == string)
36                         || (0 > off1) || (0 > off2) || (off1 > off2)
37                         || (NULL == replacement))
38                 return NULL;
39
40         sstrncpy (buf_ptr, string, (off1 + 1 > buflen) ? buflen : off1 + 1);
41         buf_ptr += off1;
42         len     -= off1;
43
44         if (0 >= len)
45                 return buf;
46
47         sstrncpy (buf_ptr, replacement, len);
48         buf_ptr += strlen (replacement);
49         len     -= strlen (replacement);
50
51         if (0 >= len)
52                 return buf;
53
54         sstrncpy (buf_ptr, string + off2, len);
55         return buf;
56 } /* subst */
57
58 char *asubst (const char *string, int off1, int off2, const char *replacement)
59 {
60         char *buf;
61         int   len;
62
63         char *ret;
64
65         if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
66                         || (NULL ==replacement))
67                 return NULL;
68
69         len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
70
71         buf = (char *)malloc (len);
72         if (NULL == buf)
73                 return NULL;
74
75         ret = subst (buf, len, string, off1, off2, replacement);
76         if (NULL == ret)
77                 free (buf);
78         return ret;
79 } /* asubst */
80
81 char *subst_string (char *buf, size_t buflen, const char *string,
82                 const char *needle, const char *replacement)
83 {
84         char *temp;
85         size_t needle_len;
86         size_t i;
87
88         if ((buf == NULL) || (string == NULL)
89                         || (needle == NULL) || (replacement == NULL))
90                 return (NULL);
91
92         temp = (char *) malloc (buflen);
93         if (temp == NULL)
94         {
95                 ERROR ("subst_string: malloc failed.");
96                 return (NULL);
97         }
98
99         needle_len = strlen (needle);
100         strncpy (buf, string, buflen);
101
102         /* Limit the loop to prevent endless loops. */
103         for (i = 0; i < buflen; i++)
104         {
105                 char *begin_ptr;
106                 size_t begin;
107
108                 /* Find `needle' in `buf'. */
109                 begin_ptr = strstr (buf, needle);
110                 if (begin_ptr == NULL)
111                         break;
112
113                 /* Calculate the start offset. */
114                 begin = begin_ptr - buf;
115
116                 /* Substitute the region using `subst'. The result is stored in
117                  * `temp'. */
118                 begin_ptr = subst (temp, buflen, buf,
119                                 begin, begin + needle_len,
120                                 replacement);
121                 if (begin_ptr == NULL)
122                 {
123                         WARNING ("subst_string: subst failed.");
124                         break;
125                 }
126
127                 /* Copy the new string in `temp' to `buf' for the next round. */
128                 strncpy (buf, temp, buflen);
129         }
130
131         if (i >= buflen)
132         {
133                 WARNING ("subst_string: Loop exited after %zu iterations: "
134                                 "string = %s; needle = %s; replacement = %s;",
135                                 i, string, needle, replacement);
136         }
137
138         sfree (temp);
139         return (buf);
140 } /* char *subst_string */
141
142 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
143