]> git.proxmox.com Git - mirror_frr.git/blame - lib/frrstr.c
lib: fix static analysis issues, use regfree()
[mirror_frr.git] / lib / frrstr.c
CommitLineData
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
21#include <string.h>
22#include <ctype.h>
23#include <sys/types.h>
24#include <regex.h>
25
26#include "frrstr.h"
27#include "memory.h"
28#include "vector.h"
29
30void frrstr_split(const char *string, const char *delimiter, char ***result,
31 int *argc)
32{
33 if (!string)
34 return;
35
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);
40 *argc = 0;
41
42 const char *tok = NULL;
0a334343 43
fe011935
QY
44 while (copy) {
45 tok = strsep(&copy, delimiter);
46 (*result)[idx] = XSTRDUP(MTYPE_TMP, tok);
47 if (++idx == sz)
48 *result = XREALLOC(MTYPE_TMP, *result,
49 (sz *= 2) * sizeof(char *));
50 (*argc)++;
51 }
52
53 XFREE(MTYPE_TMP, copystart);
fe011935
QY
54}
55
56vector frrstr_split_vec(const char *string, const char *delimiter)
57{
58 char **result;
59 int argc;
60
5d806ec6
QY
61 if (!string)
62 return NULL;
63
fe011935
QY
64 frrstr_split(string, delimiter, &result, &argc);
65
66 vector v = array_to_vector((void **)result, argc);
0a334343 67
fe011935 68 XFREE(MTYPE_TMP, result);
0a334343 69
fe011935
QY
70 return v;
71}
72
73char *frrstr_join(const char **parts, int argc, const char *join)
74{
75 int i;
76 char *str;
77 char *p;
78 size_t len = 0;
79 size_t joinlen = join ? strlen(join) : 0;
80
81 for (i = 0; i < argc; i++)
82 len += strlen(parts[i]);
83 len += argc * joinlen + 1;
84
85 if (!len)
86 return NULL;
87
88 p = str = XMALLOC(MTYPE_TMP, len);
89
90 for (i = 0; i < argc; i++) {
91 size_t arglen = strlen(parts[i]);
0a334343 92
fe011935
QY
93 memcpy(p, parts[i], arglen);
94 p += arglen;
5d806ec6 95 if (i + 1 != argc && join) {
fe011935
QY
96 memcpy(p, join, joinlen);
97 p += joinlen;
98 }
99 }
100
101 *p = '\0';
102
103 return str;
104}
105
106char *frrstr_join_vec(vector v, const char *join)
107{
108 char **argv;
109 int argc;
110
111 vector_to_array(v, (void ***)&argv, &argc);
112
113 char *ret = frrstr_join((const char **)argv, argc, join);
114
115 XFREE(MTYPE_TMP, argv);
116
117 return ret;
118}
119
120void frrstr_filter_vec(vector v, regex_t *filter)
121{
122 regmatch_t ignored[1];
0a334343 123
fe011935
QY
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));
127 vector_unset(v, i);
128 }
129 }
130}
131
132void frrstr_strvec_free(vector v)
133{
134 unsigned int i;
135 char *cp;
136
137 if (!v)
138 return;
139
140 for (i = 0; i < vector_active(v); i++) {
141 cp = vector_slot(v, i);
142 XFREE(MTYPE_TMP, cp);
143 }
144
145 vector_free(v);
146}
147
2cddf2ff
QY
148bool begins_with(const char *str, const char *prefix)
149{
150 if (!str || !prefix)
151 return 0;
0a334343 152
2cddf2ff
QY
153 size_t lenstr = strlen(str);
154 size_t lenprefix = strlen(prefix);
0a334343 155
2cddf2ff
QY
156 if (lenprefix > lenstr)
157 return 0;
0a334343 158
2cddf2ff
QY
159 return strncmp(str, prefix, lenprefix) == 0;
160}