]> git.proxmox.com Git - mirror_frr.git/blob - lib/frrstr.c
*: style for | support
[mirror_frr.git] / lib / frrstr.c
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
30 void 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;
43
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);
54 }
55
56 vector frrstr_split_vec(const char *string, const char *delimiter)
57 {
58 char **result;
59 int argc;
60
61 frrstr_split(string, delimiter, &result, &argc);
62
63 vector v = array_to_vector((void **)result, argc);
64
65 XFREE(MTYPE_TMP, result);
66
67 return v;
68 }
69
70 char *frrstr_join(const char **parts, int argc, const char *join)
71 {
72 int i;
73 char *str;
74 char *p;
75 size_t len = 0;
76 size_t joinlen = join ? strlen(join) : 0;
77
78 for (i = 0; i < argc; i++)
79 len += strlen(parts[i]);
80 len += argc * joinlen + 1;
81
82 if (!len)
83 return NULL;
84
85 p = str = XMALLOC(MTYPE_TMP, len);
86
87 for (i = 0; i < argc; i++) {
88 size_t arglen = strlen(parts[i]);
89
90 memcpy(p, parts[i], arglen);
91 p += arglen;
92 if (i + 1 != argc) {
93 memcpy(p, join, joinlen);
94 p += joinlen;
95 }
96 }
97
98 *p = '\0';
99
100 return str;
101 }
102
103 char *frrstr_join_vec(vector v, const char *join)
104 {
105 char **argv;
106 int argc;
107
108 vector_to_array(v, (void ***)&argv, &argc);
109
110 char *ret = frrstr_join((const char **)argv, argc, join);
111
112 XFREE(MTYPE_TMP, argv);
113
114 return ret;
115 }
116
117 void frrstr_filter_vec(vector v, regex_t *filter)
118 {
119 regmatch_t ignored[1];
120
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));
124 vector_unset(v, i);
125 }
126 }
127 }
128
129 void frrstr_strvec_free(vector v)
130 {
131 unsigned int i;
132 char *cp;
133
134 if (!v)
135 return;
136
137 for (i = 0; i < vector_active(v); i++) {
138 cp = vector_slot(v, i);
139 XFREE(MTYPE_TMP, cp);
140 }
141
142 vector_free(v);
143 }
144
145 bool begins_with(const char *str, const char *prefix)
146 {
147 if (!str || !prefix)
148 return 0;
149
150 size_t lenstr = strlen(str);
151 size_t lenprefix = strlen(prefix);
152
153 if (lenprefix > lenstr)
154 return 0;
155
156 return strncmp(str, prefix, lenprefix) == 0;
157 }