]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | while (copy) { | |
44 | tok = strsep(©, delimiter); | |
45 | (*result)[idx] = XSTRDUP(MTYPE_TMP, tok); | |
46 | if (++idx == sz) | |
47 | *result = XREALLOC(MTYPE_TMP, *result, | |
48 | (sz *= 2) * sizeof(char *)); | |
49 | (*argc)++; | |
50 | } | |
51 | ||
52 | XFREE(MTYPE_TMP, copystart); | |
53 | ||
54 | return; | |
55 | } | |
56 | ||
57 | vector frrstr_split_vec(const char *string, const char *delimiter) | |
58 | { | |
59 | char **result; | |
60 | int argc; | |
61 | ||
62 | frrstr_split(string, delimiter, &result, &argc); | |
63 | ||
64 | vector v = array_to_vector((void **)result, argc); | |
65 | XFREE(MTYPE_TMP, result); | |
66 | return v; | |
67 | } | |
68 | ||
69 | char *frrstr_join(const char **parts, int argc, const char *join) | |
70 | { | |
71 | int i; | |
72 | char *str; | |
73 | char *p; | |
74 | size_t len = 0; | |
75 | size_t joinlen = join ? strlen(join) : 0; | |
76 | ||
77 | for (i = 0; i < argc; i++) | |
78 | len += strlen(parts[i]); | |
79 | len += argc * joinlen + 1; | |
80 | ||
81 | if (!len) | |
82 | return NULL; | |
83 | ||
84 | p = str = XMALLOC(MTYPE_TMP, len); | |
85 | ||
86 | for (i = 0; i < argc; i++) { | |
87 | size_t arglen = strlen(parts[i]); | |
88 | memcpy(p, parts[i], arglen); | |
89 | p += arglen; | |
90 | if (i + 1 != argc) { | |
91 | memcpy(p, join, joinlen); | |
92 | p += joinlen; | |
93 | } | |
94 | } | |
95 | ||
96 | *p = '\0'; | |
97 | ||
98 | return str; | |
99 | } | |
100 | ||
101 | char *frrstr_join_vec(vector v, const char *join) | |
102 | { | |
103 | char **argv; | |
104 | int argc; | |
105 | ||
106 | vector_to_array(v, (void ***)&argv, &argc); | |
107 | ||
108 | char *ret = frrstr_join((const char **)argv, argc, join); | |
109 | ||
110 | XFREE(MTYPE_TMP, argv); | |
111 | ||
112 | return ret; | |
113 | } | |
114 | ||
115 | void frrstr_filter_vec(vector v, regex_t *filter) | |
116 | { | |
117 | regmatch_t ignored[1]; | |
118 | for (unsigned int i = 0; i < vector_active(v); i++) { | |
119 | if (regexec(filter, vector_slot(v, i), 0, ignored, 0)) { | |
120 | XFREE(MTYPE_TMP, vector_slot(v, i)); | |
121 | vector_unset(v, i); | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | void frrstr_strvec_free(vector v) | |
127 | { | |
128 | unsigned int i; | |
129 | char *cp; | |
130 | ||
131 | if (!v) | |
132 | return; | |
133 | ||
134 | for (i = 0; i < vector_active(v); i++) { | |
135 | cp = vector_slot(v, i); | |
136 | XFREE(MTYPE_TMP, cp); | |
137 | } | |
138 | ||
139 | vector_free(v); | |
140 | } | |
141 |