| 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 |
* subst_string: |
| 74 |
* |
| 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. |
| 79 |
* |
| 80 |
* If the substring is not found, no error will be indicated and |
| 81 |
* `subst_string' works mostly like `strncpy'. |
| 82 |
* |
| 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 |
| 86 |
* success. |
| 87 |
*/ |
| 88 |
char *subst_string(char *buf, size_t buflen, const char *string, |
| 89 |
const char *needle, const char *replacement); |
| 90 |
|
| 91 |
#endif /* UTILS_SUBST_H */ |