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