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