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.
33 #include "utils_subst.h"
35 char *subst (char *buf, size_t buflen, const char *string, size_t off1, size_t off2,
36 const char *replacement)
43 size_t replacement_len;
46 if ((NULL == buf) || (0 == buflen) || (NULL == string) || (NULL == replacement))
49 size_t string_len = strlen (string);
50 if ((off1 > string_len) || (off2 > string_len) || (off1 > off2))
56 replacement_len = strlen (replacement);
57 back_len = strlen (back);
59 if (front_len >= buflen) {
60 front_len = buflen - 1;
63 } else if ((front_len + replacement_len) >= buflen) {
64 replacement_len = buflen - (front_len + 1);
66 } else if ((front_len + replacement_len + back_len) >= buflen) {
67 back_len = buflen - (front_len + replacement_len + 1);
69 buflen = front_len + replacement_len + back_len + 1;
71 assert ((front_len + replacement_len + back_len) == (buflen - 1));
74 sstrncpy (out, front, front_len + 1);
78 if (replacement_len != 0) {
79 sstrncpy (out, replacement, replacement_len + 1);
80 out += replacement_len;
84 sstrncpy (out, back, back_len + 1);
92 char *asubst (const char *string, int off1, int off2, const char *replacement)
99 if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
100 || (NULL ==replacement))
103 len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
109 ret = subst (buf, len, string, off1, off2, replacement);
115 char *subst_string (char *buf, size_t buflen, const char *string,
116 const char *needle, const char *replacement)
121 if ((buf == NULL) || (string == NULL)
122 || (needle == NULL) || (replacement == NULL))
125 needle_len = strlen (needle);
126 sstrncpy (buf, string, buflen);
128 /* Limit the loop to prevent endless loops. */
129 for (i = 0; i < buflen; i++)
135 /* Find `needle' in `buf'. */
136 begin_ptr = strstr (buf, needle);
137 if (begin_ptr == NULL)
140 /* Calculate the start offset. */
141 begin = begin_ptr - buf;
143 /* Substitute the region using `subst'. The result is stored in
145 begin_ptr = subst (temp, buflen, buf,
146 begin, begin + needle_len,
148 if (begin_ptr == NULL)
150 WARNING ("subst_string: subst failed.");
154 /* Copy the new string in `temp' to `buf' for the next round. */
155 strncpy (buf, temp, buflen);
160 WARNING ("subst_string: Loop exited after %zu iterations: "
161 "string = %s; needle = %s; replacement = %s;",
162 i, string, needle, replacement);
166 } /* char *subst_string */
168 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */