]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrstr.c
2 * FRR string processing utilities.
3 * Copyright (C) 2018 Cumulus Networks, Inc.
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)
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
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
27 #include <sys/types.h>
34 void frrstr_split(const char *string
, const char *delimiter
, char ***result
,
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
);
46 const char *tok
= NULL
;
49 tok
= strsep(©
, delimiter
);
50 (*result
)[idx
] = XSTRDUP(MTYPE_TMP
, tok
);
52 *result
= XREALLOC(MTYPE_TMP
, *result
,
53 (sz
*= 2) * sizeof(char *));
57 XFREE(MTYPE_TMP
, copystart
);
60 vector
frrstr_split_vec(const char *string
, const char *delimiter
)
68 frrstr_split(string
, delimiter
, &result
, &argc
);
70 vector v
= array_to_vector((void **)result
, argc
);
72 XFREE(MTYPE_TMP
, result
);
77 char *frrstr_join(const char **parts
, int argc
, const char *join
)
83 size_t joinlen
= join
? strlen(join
) : 0;
88 for (i
= 0; i
< argc
; i
++)
89 len
+= strlen(parts
[i
]);
90 len
+= argc
* joinlen
+ 1;
95 p
= str
= XMALLOC(MTYPE_TMP
, len
);
97 for (i
= 0; i
< argc
; i
++) {
98 size_t arglen
= strlen(parts
[i
]);
100 memcpy(p
, parts
[i
], arglen
);
102 if (i
+ 1 != argc
&& join
) {
103 memcpy(p
, join
, joinlen
);
113 char *frrstr_join_vec(vector v
, const char *join
)
118 vector_to_array(v
, (void ***)&argv
, &argc
);
120 char *ret
= frrstr_join((const char **)argv
, argc
, join
);
122 XFREE(MTYPE_TMP
, argv
);
127 void frrstr_filter_vec(vector v
, regex_t
*filter
)
129 regmatch_t ignored
[1];
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
));
139 void frrstr_strvec_free(vector v
)
147 for (i
= 0; i
< vector_active(v
); i
++) {
148 cp
= vector_slot(v
, i
);
149 XFREE(MTYPE_TMP
, cp
);
155 char *frrstr_replace(const char *str
, const char *find
, const char *replace
)
158 char *nustr
= XSTRDUP(MTYPE_TMP
, str
);
160 size_t findlen
= strlen(find
);
161 size_t repllen
= strlen(replace
);
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
);
170 size_t nustrlen
= strlen(nustr
);
171 size_t taillen
= (nustr
+ nustrlen
) - (ch
+ findlen
);
173 memmove(ch
+ findlen
+ (repllen
- findlen
), ch
+ findlen
,
175 memcpy(ch
, replace
, repllen
);
181 bool begins_with(const char *str
, const char *prefix
)
186 size_t lenstr
= strlen(str
);
187 size_t lenprefix
= strlen(prefix
);
189 if (lenprefix
> lenstr
)
192 return strncmp(str
, prefix
, lenprefix
) == 0;
195 int all_digit(const char *str
)
197 for (; *str
!= '\0'; str
++)
198 if (!isdigit((int)*str
))