2 * collectd - src/utils_subst.c
3 * Copyright (C) 2008 Sebastian Harl
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.
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.
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
19 * Sebastian "tokkee" Harl <sh at tokkee.org>
23 * This module provides functions for string substitution.
29 char *subst (char *buf, size_t buflen, const char *string, int off1, int off2,
30 const char *replacement)
35 if ((NULL == buf) || (0 >= buflen) || (NULL == string)
36 || (0 > off1) || (0 > off2) || (off1 > off2)
37 || (NULL == replacement))
40 sstrncpy (buf_ptr, string, (off1 + 1 > buflen) ? buflen : off1 + 1);
47 sstrncpy (buf_ptr, replacement, len);
48 buf_ptr += strlen (replacement);
49 len -= strlen (replacement);
54 sstrncpy (buf_ptr, string + off2, len);
58 char *asubst (const char *string, int off1, int off2, const char *replacement)
65 if ((NULL == string) || (0 > off1) || (0 > off2) || (off1 > off2)
66 || (NULL ==replacement))
69 len = off1 + strlen (replacement) + strlen (string) - off2 + 1;
71 buf = (char *)malloc (len);
75 ret = subst (buf, len, string, off1, off2, replacement);
81 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */