]>
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
25 #include <sys/types.h>
26 #ifdef HAVE_LIBPCREPOSIX
27 #include <pcreposix.h>
30 #endif /* HAVE_LIBPCREPOSIX */
36 void frrstr_split(const char *string
, const char *delimiter
, char ***result
,
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
);
48 const char *tok
= NULL
;
51 tok
= strsep(©
, delimiter
);
52 (*result
)[idx
] = XSTRDUP(MTYPE_TMP
, tok
);
54 *result
= XREALLOC(MTYPE_TMP
, *result
,
55 (sz
*= 2) * sizeof(char *));
59 XFREE(MTYPE_TMP
, copystart
);
62 vector
frrstr_split_vec(const char *string
, const char *delimiter
)
70 frrstr_split(string
, delimiter
, &result
, &argc
);
72 vector v
= array_to_vector((void **)result
, argc
);
74 XFREE(MTYPE_TMP
, result
);
79 char *frrstr_join(const char **parts
, int argc
, const char *join
)
85 size_t joinlen
= join
? strlen(join
) : 0;
90 for (i
= 0; i
< argc
; i
++)
91 len
+= strlen(parts
[i
]);
92 len
+= argc
* joinlen
+ 1;
97 p
= str
= XMALLOC(MTYPE_TMP
, len
);
99 for (i
= 0; i
< argc
; i
++) {
100 size_t arglen
= strlen(parts
[i
]);
102 memcpy(p
, parts
[i
], arglen
);
104 if (i
+ 1 != argc
&& join
) {
105 memcpy(p
, join
, joinlen
);
115 char *frrstr_join_vec(vector v
, const char *join
)
120 vector_to_array(v
, (void ***)&argv
, &argc
);
122 char *ret
= frrstr_join((const char **)argv
, argc
, join
);
124 XFREE(MTYPE_TMP
, argv
);
129 void frrstr_filter_vec(vector v
, regex_t
*filter
)
131 regmatch_t ignored
[1];
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
));
141 void frrstr_strvec_free(vector v
)
149 for (i
= 0; i
< vector_active(v
); i
++) {
150 cp
= vector_slot(v
, i
);
151 XFREE(MTYPE_TMP
, cp
);
157 char *frrstr_replace(const char *str
, const char *find
, const char *replace
)
160 char *nustr
= XSTRDUP(MTYPE_TMP
, str
);
162 size_t findlen
= strlen(find
);
163 size_t repllen
= strlen(replace
);
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
);
172 size_t nustrlen
= strlen(nustr
);
173 size_t taillen
= (nustr
+ nustrlen
) - (ch
+ findlen
);
175 memmove(ch
+ findlen
+ (repllen
- findlen
), ch
+ findlen
,
177 memcpy(ch
, replace
, repllen
);
183 bool frrstr_startswith(const char *str
, const char *prefix
)
188 size_t lenstr
= strlen(str
);
189 size_t lenprefix
= strlen(prefix
);
191 if (lenprefix
> lenstr
)
194 return strncmp(str
, prefix
, lenprefix
) == 0;
197 bool frrstr_endswith(const char *str
, const char *suffix
)
202 size_t lenstr
= strlen(str
);
203 size_t lensuffix
= strlen(suffix
);
205 if (lensuffix
> lenstr
)
208 return strncmp(&str
[lenstr
- lensuffix
], suffix
, lensuffix
) == 0;
211 int all_digit(const char *str
)
213 for (; *str
!= '\0'; str
++)
214 if (!isdigit((unsigned char)*str
))
220 char *frrstr_hex(char *buff
, size_t bufsiz
, const uint8_t *str
, size_t num
)
229 for (size_t i
= 0; i
< num
; i
++) {
230 snprintf(tmp
, sizeof(tmp
), "%02x", (unsigned char)str
[i
]);
231 strlcat(buff
, tmp
, bufsiz
);