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