2 * collectd - src/utils_subst.h
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.
32 #define UTILS_SUBST_H 1
39 * Replace a substring of a string with the specified replacement text. The
40 * resulting string is stored in the buffer pointed to by 'buf' of length
41 * 'buflen'. Upon success, the buffer will always be null-terminated. The
42 * result may be truncated if the buffer is too small.
44 * The substring to be replaces is identified by the two offsets 'off1' and
45 * 'off2' where 'off1' specifies the offset to the beginning of the substring
46 * and 'off2' specifies the offset to the first byte after the substring.
48 * The minimum buffer size to store the complete return value (including the
49 * terminating '\0' character) thus has to be:
50 * off1 + strlen(replacement) + strlen(string) - off2 + 1
55 * string = "foo_____bar"
67 * The function returns 'buf' on success, NULL else.
69 char *subst(char *buf, size_t buflen, const char *string, size_t off1,
70 size_t off2, const char *replacement);
75 * Works like `subst', but instead of specifying start and end offsets you
76 * specify `needle', the string that is to be replaced. If `needle' is found
77 * in `string' (using strstr(3)), the offset is calculated and `subst' is
78 * called with the determined parameters.
80 * If the substring is not found, no error will be indicated and
81 * `subst_string' works mostly like `strncpy'.
83 * If the substring appears multiple times, all appearances will be replaced.
84 * If the substring has been found `buflen' times, an endless loop is assumed
85 * and the loop is broken. A warning is printed and the function returns
88 char *subst_string(char *buf, size_t buflen, const char *string,
89 const char *needle, const char *replacement);
91 #endif /* UTILS_SUBST_H */