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, size_t off2,
70 const char *replacement);
75 * This function is very similar to subst(). It differs in that it
76 * automatically allocates the memory required for the return value which the
77 * user then has to free himself.
79 * Returns the newly allocated result string on success, NULL else.
81 char *asubst (const char *string, int off1, int off2, const char *replacement);
86 * Works like `subst', but instead of specifying start and end offsets you
87 * specify `needle', the string that is to be replaced. If `needle' is found
88 * in `string' (using strstr(3)), the offset is calculated and `subst' is
89 * called with the determined parameters.
91 * If the substring is not found, no error will be indicated and
92 * `subst_string' works mostly like `strncpy'.
94 * If the substring appears multiple times, all appearances will be replaced.
95 * If the substring has been found `buflen' times, an endless loop is assumed
96 * and the loop is broken. A warning is printed and the function returns
99 char *subst_string (char *buf, size_t buflen, const char *string,
100 const char *needle, const char *replacement);
102 #endif /* UTILS_SUBST_H */
104 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */