]>
Commit | Line | Data |
---|---|---|
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 | ||
0c0830c5 | 21 | #include "zebra.h" |
b45ac5f5 | 22 | |
fe011935 QY |
23 | #include <string.h> |
24 | #include <ctype.h> | |
25 | #include <sys/types.h> | |
beee9b4a DS |
26 | #ifdef HAVE_LIBPCREPOSIX |
27 | #include <pcreposix.h> | |
28 | #else | |
fe011935 | 29 | #include <regex.h> |
beee9b4a | 30 | #endif /* HAVE_LIBPCREPOSIX */ |
fe011935 QY |
31 | |
32 | #include "frrstr.h" | |
33 | #include "memory.h" | |
34 | #include "vector.h" | |
35 | ||
36 | void frrstr_split(const char *string, const char *delimiter, char ***result, | |
37 | int *argc) | |
38 | { | |
39 | if (!string) | |
40 | return; | |
41 | ||
42 | unsigned int sz = 4, idx = 0; | |
43 | char *copy, *copystart; | |
44 | *result = XCALLOC(MTYPE_TMP, sizeof(char *) * sz); | |
45 | copystart = copy = XSTRDUP(MTYPE_TMP, string); | |
46 | *argc = 0; | |
47 | ||
48 | const char *tok = NULL; | |
0a334343 | 49 | |
fe011935 QY |
50 | while (copy) { |
51 | tok = strsep(©, delimiter); | |
52 | (*result)[idx] = XSTRDUP(MTYPE_TMP, tok); | |
53 | if (++idx == sz) | |
54 | *result = XREALLOC(MTYPE_TMP, *result, | |
55 | (sz *= 2) * sizeof(char *)); | |
56 | (*argc)++; | |
57 | } | |
58 | ||
59 | XFREE(MTYPE_TMP, copystart); | |
fe011935 QY |
60 | } |
61 | ||
62 | vector frrstr_split_vec(const char *string, const char *delimiter) | |
63 | { | |
64 | char **result; | |
65 | int argc; | |
66 | ||
5d806ec6 QY |
67 | if (!string) |
68 | return NULL; | |
69 | ||
fe011935 QY |
70 | frrstr_split(string, delimiter, &result, &argc); |
71 | ||
72 | vector v = array_to_vector((void **)result, argc); | |
0a334343 | 73 | |
fe011935 | 74 | XFREE(MTYPE_TMP, result); |
0a334343 | 75 | |
fe011935 QY |
76 | return v; |
77 | } | |
78 | ||
79 | char *frrstr_join(const char **parts, int argc, const char *join) | |
80 | { | |
81 | int i; | |
82 | char *str; | |
83 | char *p; | |
84 | size_t len = 0; | |
85 | size_t joinlen = join ? strlen(join) : 0; | |
86 | ||
2c2d5cb3 | 87 | if (!argc) |
88 | return NULL; | |
89 | ||
fe011935 QY |
90 | for (i = 0; i < argc; i++) |
91 | len += strlen(parts[i]); | |
92 | len += argc * joinlen + 1; | |
93 | ||
94 | if (!len) | |
95 | return NULL; | |
96 | ||
97 | p = str = XMALLOC(MTYPE_TMP, len); | |
98 | ||
99 | for (i = 0; i < argc; i++) { | |
100 | size_t arglen = strlen(parts[i]); | |
0a334343 | 101 | |
fe011935 QY |
102 | memcpy(p, parts[i], arglen); |
103 | p += arglen; | |
5d806ec6 | 104 | if (i + 1 != argc && join) { |
fe011935 QY |
105 | memcpy(p, join, joinlen); |
106 | p += joinlen; | |
107 | } | |
108 | } | |
109 | ||
110 | *p = '\0'; | |
111 | ||
112 | return str; | |
113 | } | |
114 | ||
115 | char *frrstr_join_vec(vector v, const char *join) | |
116 | { | |
117 | char **argv; | |
118 | int argc; | |
119 | ||
120 | vector_to_array(v, (void ***)&argv, &argc); | |
121 | ||
122 | char *ret = frrstr_join((const char **)argv, argc, join); | |
123 | ||
124 | XFREE(MTYPE_TMP, argv); | |
125 | ||
126 | return ret; | |
127 | } | |
128 | ||
129 | void frrstr_filter_vec(vector v, regex_t *filter) | |
130 | { | |
131 | regmatch_t ignored[1]; | |
0a334343 | 132 | |
fe011935 QY |
133 | for (unsigned int i = 0; i < vector_active(v); i++) { |
134 | if (regexec(filter, vector_slot(v, i), 0, ignored, 0)) { | |
135 | XFREE(MTYPE_TMP, vector_slot(v, i)); | |
136 | vector_unset(v, i); | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | void frrstr_strvec_free(vector v) | |
142 | { | |
143 | unsigned int i; | |
144 | char *cp; | |
145 | ||
146 | if (!v) | |
147 | return; | |
148 | ||
149 | for (i = 0; i < vector_active(v); i++) { | |
150 | cp = vector_slot(v, i); | |
151 | XFREE(MTYPE_TMP, cp); | |
152 | } | |
153 | ||
154 | vector_free(v); | |
155 | } | |
156 | ||
ed1809c9 QY |
157 | char *frrstr_replace(const char *str, const char *find, const char *replace) |
158 | { | |
159 | char *ch; | |
160 | char *nustr = XSTRDUP(MTYPE_TMP, str); | |
161 | ||
162 | size_t findlen = strlen(find); | |
163 | size_t repllen = strlen(replace); | |
164 | ||
165 | while ((ch = strstr(nustr, find))) { | |
166 | if (repllen > findlen) { | |
167 | size_t nusz = strlen(nustr) + repllen - findlen + 1; | |
168 | nustr = XREALLOC(MTYPE_TMP, nustr, nusz); | |
169 | ch = strstr(nustr, find); | |
170 | } | |
171 | ||
172 | size_t nustrlen = strlen(nustr); | |
173 | size_t taillen = (nustr + nustrlen) - (ch + findlen); | |
174 | ||
175 | memmove(ch + findlen + (repllen - findlen), ch + findlen, | |
176 | taillen + 1); | |
177 | memcpy(ch, replace, repllen); | |
178 | } | |
179 | ||
180 | return nustr; | |
181 | } | |
182 | ||
90cf59ec | 183 | bool frrstr_startswith(const char *str, const char *prefix) |
2cddf2ff QY |
184 | { |
185 | if (!str || !prefix) | |
b08047f8 | 186 | return false; |
0a334343 | 187 | |
2cddf2ff QY |
188 | size_t lenstr = strlen(str); |
189 | size_t lenprefix = strlen(prefix); | |
0a334343 | 190 | |
2cddf2ff | 191 | if (lenprefix > lenstr) |
b08047f8 | 192 | return false; |
0a334343 | 193 | |
2cddf2ff QY |
194 | return strncmp(str, prefix, lenprefix) == 0; |
195 | } | |
5d5ba018 | 196 | |
90cf59ec QY |
197 | bool frrstr_endswith(const char *str, const char *suffix) |
198 | { | |
199 | if (!str || !suffix) | |
200 | return false; | |
201 | ||
202 | size_t lenstr = strlen(str); | |
203 | size_t lensuffix = strlen(suffix); | |
204 | ||
205 | if (lensuffix > lenstr) | |
206 | return false; | |
207 | ||
208 | return strncmp(&str[lenstr - lensuffix], suffix, lensuffix) == 0; | |
209 | } | |
210 | ||
5d5ba018 | 211 | int all_digit(const char *str) |
212 | { | |
213 | for (; *str != '\0'; str++) | |
fefa5e0f | 214 | if (!isdigit((unsigned char)*str)) |
5d5ba018 | 215 | return 0; |
216 | return 1; | |
217 | } | |
0c0830c5 QY |
218 | |
219 | ||
220 | char *frrstr_hex(char *buff, size_t bufsiz, const uint8_t *str, size_t num) | |
221 | { | |
222 | if (bufsiz == 0) | |
223 | return buff; | |
224 | ||
225 | char tmp[3]; | |
226 | ||
227 | buff[0] = '\0'; | |
228 | ||
229 | for (size_t i = 0; i < num; i++) { | |
230 | snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); | |
231 | strlcat(buff, tmp, bufsiz); | |
232 | } | |
233 | ||
234 | return buff; | |
235 | } |