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