2f28eb9b141fabbdaf452b227727dfa3f1b40bd5
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 **/
27 /*
28 * This module provides functions for string substitution.
29 */
31 #include "collectd.h"
32 #include "common.h"
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;
40 if ((NULL == buf) || (0 >= buflen) || (NULL == string)
41 || (0 > off1) || (0 > off2) || (off1 > off2)
42 || (NULL == replacement))
43 return NULL;
45 sstrncpy (buf_ptr, string,
46 ((size_t)off1 + 1 > buflen) ? buflen : (size_t)off1 + 1);
47 buf_ptr += off1;
48 len -= off1;
50 if (0 >= len)
51 return buf;
53 sstrncpy (buf_ptr, replacement, len);
54 buf_ptr += strlen (replacement);
55 len -= strlen (replacement);
57 if (0 >= len)
58 return buf;
60 sstrncpy (buf_ptr, string + off2, len);
61 return buf;
62 } /* subst */
64 char *asubst (const char *string, int off1, int off2, const char *replacement)
65 {
66 char *buf;
67 int len;
69 char *ret;
71 if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
72 || (NULL ==replacement))
73 return NULL;
75 len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
77 buf = (char *)malloc (len);
78 if (NULL == buf)
79 return NULL;
81 ret = subst (buf, len, string, off1, off2, replacement);
82 if (NULL == ret)
83 free (buf);
84 return ret;
85 } /* asubst */
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;
94 if ((buf == NULL) || (string == NULL)
95 || (needle == NULL) || (replacement == NULL))
96 return (NULL);
98 temp = (char *) malloc (buflen);
99 if (temp == NULL)
100 {
101 ERROR ("subst_string: malloc failed.");
102 return (NULL);
103 }
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++)
110 {
111 char *begin_ptr;
112 size_t begin;
114 /* Find `needle' in `buf'. */
115 begin_ptr = strstr (buf, needle);
116 if (begin_ptr == NULL)
117 break;
119 /* Calculate the start offset. */
120 begin = begin_ptr - buf;
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 }
133 /* Copy the new string in `temp' to `buf' for the next round. */
134 strncpy (buf, temp, buflen);
135 }
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 }
144 sfree (temp);
145 return (buf);
146 } /* char *subst_string */
148 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */