]> git.proxmox.com Git - mirror_frr.git/blob - lib/frrstr.h
Merge pull request #12798 from donaldsharp/rib_match_multicast
[mirror_frr.git] / lib / frrstr.h
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * FRR string processing utilities.
4 * Copyright (C) 2018 Cumulus Networks, Inc.
5 * Quentin Young
6 */
7
8 #ifndef _FRRSTR_H_
9 #define _FRRSTR_H_
10
11 #include <sys/types.h>
12 #include <sys/types.h>
13 #ifdef HAVE_LIBPCRE2_POSIX
14 #ifndef _FRR_PCRE2_POSIX
15 #define _FRR_PCRE2_POSIX
16 #include <pcre2posix.h>
17 #endif /* _FRR_PCRE2_POSIX */
18 #elif defined(HAVE_LIBPCREPOSIX)
19 #include <pcreposix.h>
20 #else
21 #include <regex.h>
22 #endif /* HAVE_LIBPCRE2_POSIX */
23 #include <stdbool.h>
24
25 #include "vector.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /*
32 * Tokenizes a string, storing tokens in a vector. Whitespace is ignored.
33 * Delimiter characters are not included.
34 *
35 * string
36 * The string to split
37 *
38 * delimiter
39 * Delimiter string, as used in strsep()
40 *
41 * Returns:
42 * The split string. Each token is allocated with MTYPE_TMP.
43 */
44 void frrstr_split(const char *string, const char *delimiter, char ***result,
45 int *argc);
46 vector frrstr_split_vec(const char *string, const char *delimiter);
47
48 /*
49 * Concatenate string array into a single string.
50 *
51 * argv
52 * array of string pointers to concatenate
53 *
54 * argc
55 * array length
56 *
57 * join
58 * string to insert between each part, or NULL for nothing
59 *
60 * Returns:
61 * the joined string, allocated with MTYPE_TMP
62 */
63 char *frrstr_join(const char **parts, int argc, const char *join);
64 char *frrstr_join_vec(vector v, const char *join);
65
66 /*
67 * Filter string vector.
68 * Removes lines that do not contain a match for the provided regex.
69 *
70 * v
71 * The vector to filter.
72 *
73 * filter
74 * Regex to filter with.
75 */
76 void frrstr_filter_vec(vector v, regex_t *filter);
77
78 /*
79 * Free allocated string vector.
80 * Assumes each item is allocated with MTYPE_TMP.
81 *
82 * v
83 * the vector to free
84 */
85 void frrstr_strvec_free(vector v);
86
87 /*
88 * Given a string, replaces all occurrences of a substring with a different
89 * string. The result is a new string. The original string is not modified.
90 *
91 * If 'replace' is longer than 'find', this function performs N+1 allocations,
92 * where N is the number of times 'find' occurs in 'str'. If 'replace' is equal
93 * in length or shorter than 'find', only 1 allocation is performed.
94 *
95 * str
96 * String to perform replacement on.
97 *
98 * find
99 * Substring to replace.
100 *
101 * replace
102 * String to replace 'find' with.
103 *
104 * Returns:
105 * A new string, allocated with MTYPE_TMP, that is the result of performing
106 * the replacement on 'str'. This must be freed by the caller.
107 */
108 char *frrstr_replace(const char *str, const char *find, const char *replace);
109
110 /*
111 * Prefix match for string.
112 *
113 * str
114 * string to check for prefix match
115 *
116 * prefix
117 * prefix to look for
118 *
119 * Returns:
120 * true if str starts with prefix, false otherwise
121 */
122 bool frrstr_startswith(const char *str, const char *prefix);
123
124 /*
125 * Suffix match for string.
126 *
127 * str
128 * string to check for suffix match
129 *
130 * suffix
131 * suffix to look for
132 *
133 * Returns:
134 * true if str ends with suffix, false otherwise
135 */
136 bool frrstr_endswith(const char *str, const char *suffix);
137
138 /*
139 * Check the string only contains digit characters.
140 *
141 * str
142 * string to check for digits
143 *
144 * Returns:
145 * 1 str only contains digit characters, 0 otherwise
146 */
147 int all_digit(const char *str);
148
149 /*
150 * Copy the hexadecimal representation of the string to a buffer.
151 *
152 * buff
153 * Buffer to copy result into with size of at least (2 * num) + 1.
154 *
155 * bufsiz
156 * Size of destination buffer.
157 *
158 * str
159 * String to represent as hexadecimal.
160 *
161 * num
162 * Number of characters to copy.
163 *
164 * Returns:
165 * Pointer to buffer containing resulting hexadecimal representation.
166 */
167 char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num);
168
169 #ifdef __cplusplus
170 }
171 #endif
172
173 #endif /* _FRRSTR_H_ */