Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / daemon / utils_subst.h
1 /**
2  * collectd - src/utils_subst.h
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  **/
26
27 /*
28  * This module provides functions for string substitution.
29  */
30
31 #ifndef UTILS_SUBST_H
32 #define UTILS_SUBST_H 1
33
34 #include <stddef.h>
35
36 /*
37  * subst:
38  *
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.
43  *
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.
47  *
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
51  *
52  * Example:
53  *
54  *             01234567890
55  *   string = "foo_____bar"
56  *                ^    ^
57  *                |    |
58  *              off1  off2
59  *
60  *   off1 = 3
61  *   off2 = 8
62  *
63  *   replacement = " - "
64  *
65  *   -> "foo - bar"
66  *
67  * The function returns 'buf' on success, NULL else.
68  */
69 char *subst(char *buf, size_t buflen, const char *string, size_t off1,
70             size_t off2, const char *replacement);
71
72 /*
73  * asubst:
74  *
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.
78  *
79  * Returns the newly allocated result string on success, NULL else.
80  */
81 char *asubst(const char *string, int off1, int off2, const char *replacement);
82
83 /*
84  * subst_string:
85  *
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.
90  *
91  * If the substring is not found, no error will be indicated and
92  * `subst_string' works mostly like `strncpy'.
93  *
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
97  * success.
98  */
99 char *subst_string(char *buf, size_t buflen, const char *string,
100                    const char *needle, const char *replacement);
101
102 #endif /* UTILS_SUBST_H */