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