]>
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
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
)
61 frrstr_split(string
, delimiter
, &result
, &argc
);
63 vector v
= array_to_vector((void **)result
, argc
);
65 XFREE(MTYPE_TMP
, result
);
70 char *frrstr_join(const char **parts
, int argc
, const char *join
)
76 size_t joinlen
= join
? strlen(join
) : 0;
78 for (i
= 0; i
< argc
; i
++)
79 len
+= strlen(parts
[i
]);
80 len
+= argc
* joinlen
+ 1;
85 p
= str
= XMALLOC(MTYPE_TMP
, len
);
87 for (i
= 0; i
< argc
; i
++) {
88 size_t arglen
= strlen(parts
[i
]);
90 memcpy(p
, parts
[i
], arglen
);
93 memcpy(p
, join
, joinlen
);
103 char *frrstr_join_vec(vector v
, const char *join
)
108 vector_to_array(v
, (void ***)&argv
, &argc
);
110 char *ret
= frrstr_join((const char **)argv
, argc
, join
);
112 XFREE(MTYPE_TMP
, argv
);
117 void frrstr_filter_vec(vector v
, regex_t
*filter
)
119 regmatch_t ignored
[1];
121 for (unsigned int i
= 0; i
< vector_active(v
); i
++) {
122 if (regexec(filter
, vector_slot(v
, i
), 0, ignored
, 0)) {
123 XFREE(MTYPE_TMP
, vector_slot(v
, i
));
129 void frrstr_strvec_free(vector v
)
137 for (i
= 0; i
< vector_active(v
); i
++) {
138 cp
= vector_slot(v
, i
);
139 XFREE(MTYPE_TMP
, cp
);
145 bool begins_with(const char *str
, const char *prefix
)
150 size_t lenstr
= strlen(str
);
151 size_t lenprefix
= strlen(prefix
);
153 if (lenprefix
> lenstr
)
156 return strncmp(str
, prefix
, lenprefix
) == 0;