]>
git.proxmox.com Git - mirror_frr.git/blob - lib/frrstr.c
d003590ba6d2b0937bf000bf53da618059d43226
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
23 #include <sys/types.h>
30 void frrstr_split(const char *string
, const char *delimiter
, char ***result
,
36 unsigned int sz
= 4, idx
= 0;
37 char *copy
, *copystart
;
38 *result
= XCALLOC(MTYPE_TMP
, sizeof(char *) * sz
);
39 copystart
= copy
= XSTRDUP(MTYPE_TMP
, string
);
42 const char *tok
= NULL
;
45 tok
= strsep(©
, delimiter
);
46 (*result
)[idx
] = XSTRDUP(MTYPE_TMP
, tok
);
48 *result
= XREALLOC(MTYPE_TMP
, *result
,
49 (sz
*= 2) * sizeof(char *));
53 XFREE(MTYPE_TMP
, copystart
);
56 vector
frrstr_split_vec(const char *string
, const char *delimiter
)
64 frrstr_split(string
, delimiter
, &result
, &argc
);
66 vector v
= array_to_vector((void **)result
, argc
);
68 XFREE(MTYPE_TMP
, result
);
73 char *frrstr_join(const char **parts
, int argc
, const char *join
)
79 size_t joinlen
= join
? strlen(join
) : 0;
81 for (i
= 0; i
< argc
; i
++)
82 len
+= strlen(parts
[i
]);
83 len
+= argc
* joinlen
+ 1;
88 p
= str
= XMALLOC(MTYPE_TMP
, len
);
90 for (i
= 0; i
< argc
; i
++) {
91 size_t arglen
= strlen(parts
[i
]);
93 memcpy(p
, parts
[i
], arglen
);
95 if (i
+ 1 != argc
&& join
) {
96 memcpy(p
, join
, joinlen
);
106 char *frrstr_join_vec(vector v
, const char *join
)
111 vector_to_array(v
, (void ***)&argv
, &argc
);
113 char *ret
= frrstr_join((const char **)argv
, argc
, join
);
115 XFREE(MTYPE_TMP
, argv
);
120 void frrstr_filter_vec(vector v
, regex_t
*filter
)
122 regmatch_t ignored
[1];
124 for (unsigned int i
= 0; i
< vector_active(v
); i
++) {
125 if (regexec(filter
, vector_slot(v
, i
), 0, ignored
, 0)) {
126 XFREE(MTYPE_TMP
, vector_slot(v
, i
));
132 void frrstr_strvec_free(vector v
)
140 for (i
= 0; i
< vector_active(v
); i
++) {
141 cp
= vector_slot(v
, i
);
142 XFREE(MTYPE_TMP
, cp
);
148 bool begins_with(const char *str
, const char *prefix
)
153 size_t lenstr
= strlen(str
);
154 size_t lenprefix
= strlen(prefix
);
156 if (lenprefix
> lenstr
)
159 return strncmp(str
, prefix
, lenprefix
) == 0;