]> git.proxmox.com Git - mirror_frr.git/blame - lib/frrstr.h
*: manual SPDX License ID conversions
[mirror_frr.git] / lib / frrstr.h
CommitLineData
fe011935
QY
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>
beee9b4a 25#include <sys/types.h>
061f5d1c
DA
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)
beee9b4a
DS
32#include <pcreposix.h>
33#else
fe011935 34#include <regex.h>
061f5d1c 35#endif /* HAVE_LIBPCRE2_POSIX */
2cddf2ff 36#include <stdbool.h>
fe011935
QY
37
38#include "vector.h"
39
5e244469
RW
40#ifdef __cplusplus
41extern "C" {
42#endif
43
fe011935
QY
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 */
57void frrstr_split(const char *string, const char *delimiter, char ***result,
58 int *argc);
59vector 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 */
76char *frrstr_join(const char **parts, int argc, const char *join);
77char *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 */
89void 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 */
98void frrstr_strvec_free(vector v);
99
ed1809c9
QY
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 */
121char *frrstr_replace(const char *str, const char *find, const char *replace);
90cf59ec 122
2cddf2ff
QY
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:
90cf59ec
QY
133 * true if str starts with prefix, false otherwise
134 */
135bool 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
2cddf2ff 148 */
90cf59ec 149bool frrstr_endswith(const char *str, const char *suffix);
fe011935 150
5d5ba018 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 */
160int all_digit(const char *str);
161
0c0830c5
QY
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 */
180char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num);
181
5e244469
RW
182#ifdef __cplusplus
183}
184#endif
185
fe011935 186#endif /* _FRRSTR_H_ */