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