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 **/
22 /*
23 * This module provides functions for string substitution.
24 */
26 #include "collectd.h"
27 #include "common.h"
28 #include "utils_subst.h"
30 char *subst (char *buf, size_t buflen, const char *string, int off1, int off2,
31 const char *replacement)
32 {
33 char *buf_ptr = buf;
34 size_t len = buflen;
36 if ((NULL == buf) || (0 >= buflen) || (NULL == string)
37 || (0 > off1) || (0 > off2) || (off1 > off2)
38 || (NULL == replacement))
39 return NULL;
41 sstrncpy (buf_ptr, string,
42 ((size_t)off1 + 1 > buflen) ? buflen : (size_t)off1 + 1);
43 buf_ptr += off1;
44 len -= off1;
46 if (0 >= len)
47 return buf;
49 sstrncpy (buf_ptr, replacement, len);
50 buf_ptr += strlen (replacement);
51 len -= strlen (replacement);
53 if (0 >= len)
54 return buf;
56 sstrncpy (buf_ptr, string + off2, len);
57 return buf;
58 } /* subst */
60 char *asubst (const char *string, int off1, int off2, const char *replacement)
61 {
62 char *buf;
63 int len;
65 char *ret;
67 if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
68 || (NULL ==replacement))
69 return NULL;
71 len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
73 buf = (char *)malloc (len);
74 if (NULL == buf)
75 return NULL;
77 ret = subst (buf, len, string, off1, off2, replacement);
78 if (NULL == ret)
79 free (buf);
80 return ret;
81 } /* asubst */
83 char *subst_string (char *buf, size_t buflen, const char *string,
84 const char *needle, const char *replacement)
85 {
86 char *temp;
87 size_t needle_len;
88 size_t i;
90 if ((buf == NULL) || (string == NULL)
91 || (needle == NULL) || (replacement == NULL))
92 return (NULL);
94 temp = (char *) malloc (buflen);
95 if (temp == NULL)
96 {
97 ERROR ("subst_string: malloc failed.");
98 return (NULL);
99 }
101 needle_len = strlen (needle);
102 strncpy (buf, string, buflen);
104 /* Limit the loop to prevent endless loops. */
105 for (i = 0; i < buflen; i++)
106 {
107 char *begin_ptr;
108 size_t begin;
110 /* Find `needle' in `buf'. */
111 begin_ptr = strstr (buf, needle);
112 if (begin_ptr == NULL)
113 break;
115 /* Calculate the start offset. */
116 begin = begin_ptr - buf;
118 /* Substitute the region using `subst'. The result is stored in
119 * `temp'. */
120 begin_ptr = subst (temp, buflen, buf,
121 begin, begin + needle_len,
122 replacement);
123 if (begin_ptr == NULL)
124 {
125 WARNING ("subst_string: subst failed.");
126 break;
127 }
129 /* Copy the new string in `temp' to `buf' for the next round. */
130 strncpy (buf, temp, buflen);
131 }
133 if (i >= buflen)
134 {
135 WARNING ("subst_string: Loop exited after %zu iterations: "
136 "string = %s; needle = %s; replacement = %s;",
137 i, string, needle, replacement);
138 }
140 sfree (temp);
141 return (buf);
142 } /* char *subst_string */
144 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */