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