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