2 * collectd - src/utils_subst.c
3 * Copyright (C) 2008 Sebastian Harl
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Sebastian "tokkee" Harl <sh at tokkee.org>
28 * This module provides functions for string substitution.
34 char *subst (char *buf, size_t buflen, const char *string, int off1, int off2,
35 const char *replacement)
40 if ((NULL == buf) || (0 >= buflen) || (NULL == string)
41 || (0 > off1) || (0 > off2) || (off1 > off2)
42 || (NULL == replacement))
45 sstrncpy (buf_ptr, string,
46 ((size_t)off1 + 1 > buflen) ? buflen : (size_t)off1 + 1);
53 sstrncpy (buf_ptr, replacement, len);
54 buf_ptr += strlen (replacement);
55 len -= strlen (replacement);
60 sstrncpy (buf_ptr, string + off2, len);
64 char *asubst (const char *string, int off1, int off2, const char *replacement)
71 if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
72 || (NULL ==replacement))
75 len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
77 buf = (char *)malloc (len);
81 ret = subst (buf, len, string, off1, off2, replacement);
87 char *subst_string (char *buf, size_t buflen, const char *string,
88 const char *needle, const char *replacement)
94 if ((buf == NULL) || (string == NULL)
95 || (needle == NULL) || (replacement == NULL))
98 temp = (char *) malloc (buflen);
101 ERROR ("subst_string: malloc failed.");
105 needle_len = strlen (needle);
106 strncpy (buf, string, buflen);
108 /* Limit the loop to prevent endless loops. */
109 for (i = 0; i < buflen; i++)
114 /* Find `needle' in `buf'. */
115 begin_ptr = strstr (buf, needle);
116 if (begin_ptr == NULL)
119 /* Calculate the start offset. */
120 begin = begin_ptr - buf;
122 /* Substitute the region using `subst'. The result is stored in
124 begin_ptr = subst (temp, buflen, buf,
125 begin, begin + needle_len,
127 if (begin_ptr == NULL)
129 WARNING ("subst_string: subst failed.");
133 /* Copy the new string in `temp' to `buf' for the next round. */
134 strncpy (buf, temp, buflen);
139 WARNING ("subst_string: Loop exited after %zu iterations: "
140 "string = %s; needle = %s; replacement = %s;",
141 i, string, needle, replacement);
146 } /* char *subst_string */
148 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */