Debian Codegraph @

src/lib/util/string_array.c

raw symbols SCIP
1 /*
2 Authors:
3 Pavel Březina <[email protected]>
4
5 Copyright (C) 2018 Red Hat
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include <string.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27
28 #include "common/common.h"
29 #include "lib/util/string_array.h"
30 #include "lib/util/string.h"
31
32 char **
33 string_array_create(size_t num_items)
34 {
35 return malloc_zero_array(char *, num_items + 1);
36 }
37
38 char **
39 string_array_copy(char **array, bool unique)
40 {
41 char **copy;
42 size_t len;
43 size_t i;
44
45 if (array == NULL) {
46 return NULL;
47 }
48
49 len = string_array_count(array);
50
51 copy = string_array_create(len);
52 if (copy == NULL) {
53 return NULL;
54 }
55
56 for (i = 0; array[i] != NULL; i++) {
57 copy = string_array_add_value(copy, array[i], unique);
58 if (copy == NULL) {
59 return NULL;
60 }
61 }
62
63 return copy;
64 }
65
66 char **
67 string_array_resize(char **array, size_t num_items)
68 {
69 char **reallocated;
70 size_t count;
71 size_t i;
72
73 count = string_array_count(array);
74 if (num_items < count) {
75 for (i = num_items; i < count; i++) {
76 free(array[i]);
77 array[i] = NULL;
78 }
79 }
80
81 reallocated = realloc_array(array, char *, num_items + 1);
82 if (reallocated == NULL) {
83 string_array_free(array);
84 return NULL;
85 }
86
87 reallocated[num_items] = NULL;
88 return reallocated;
89 }
90
91 void
92 string_array_free(char **array)
93 {
94 size_t i;
95
96 if (array == NULL) {
97 return;
98 }
99
100 for (i = 0; array[i] != NULL; i++) {
101 free(array[i]);
102 }
103
104 free(array);
105 }
106
107 size_t
108 string_array_count(char **array)
109 {
110 size_t count;
111
112 for (count = 0; array[count] != NULL; count++) {
113 /* no op */
114 }
115
116 return count;
117 }
118
119 bool
120 string_array_has_value_safe(char **array, const char *value, size_t len)
121 {
122 int i;
123
124 if (value == NULL) {
125 return false;
126 }
127
128 for (i = 0; array[i] != NULL; i++) {
129 if (strncmp(value, array[i], len) == 0 && array[i][len] == '\0') {
130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 bool
138 string_array_has_value(char **array, const char *value)
139 {
140 return string_array_has_value_safe(array, value, strlen(value));
141 }
142
143 char **
144 string_array_add_value_safe(char **array,
145 const char *value,
146 size_t len,
147 bool unique)
148 {
149 size_t count;
150
151 if (unique && string_array_has_value_safe(array, value, len)) {
152 return array;
153 }
154
155 count = string_array_count(array);
156 array = string_array_resize(array, count + 1);
157 if (array == NULL) {
158 return NULL;
159 }
160
161 array[count] = strndup(value, len);
162 if (array[count] == NULL) {
163 string_array_free(array);
164 return NULL;
165 }
166
167 return array;
168 }
169
170 char **
171 string_array_add_value(char **array, const char *value, bool unique)
172 {
173 return string_array_add_value_safe(array, value, strlen(value), unique);
174 }
175
176 void
177 string_array_del_value(char **array, const char *value)
178 {
179 size_t count;
180 size_t pos;
181 size_t i;
182
183 if (array == NULL) {
184 return;
185 }
186
187 count = string_array_count(array);
188 for (i = 0; i < count; i++) {
189 if (strcmp(array[i], value) == 0) {
190 free(array[i]);
191 array[i] = NULL;
192 }
193 }
194
195 for (i = 0, pos = 0; i < count; i++) {
196 if (array[i] != NULL) {
197 array[pos] = array[i];
198 pos++;
199 }
200 }
201
202 for (; pos < count; pos++) {
203 array[pos] = NULL;
204 }
205
206 return;
207 }
208
209 char **
210 string_array_concat(char **to, char **items, bool unique)
211 {
212 int i;
213
214 if (items == NULL) {
215 return to;
216 }
217
218 for (i = 0; items[i] != NULL; i++) {
219 to = string_array_add_value(to, items[i], unique);
220 if (to == NULL) {
221 return NULL;
222 }
223 }
224
225 return to;
226 }
227
228 static int
229 string_array_sort_callback(const void *a, const void *b)
230 {
231 return strcmp(*(char* const*)a, *(char* const*)b);
232 }
233
234 void
235 string_array_sort(char **array)
236 {
237 if (array == NULL) {
238 return;
239 }
240
241 qsort(array, string_array_count(array), sizeof(char *),
242 string_array_sort_callback);
243 }
244
245 const char *
246 string_array_find_similar(const char *value, char **array, int max_distance)
247 {
248 const char *word = NULL;
249 int current;
250 int best = 0;
251 int i;
252
253 for (i = 0; array[i] != NULL; i++) {
254 current = string_levenshtein(value, array[i]);
255 if (word == NULL) {
256 best = current;
257 word = array[i];
258 continue;
259 }
260
261 if (current < best) {
262 best = current;
263 word = array[i];
264 }
265 }
266
267 if (word == NULL || best > max_distance) {
268 return NULL;
269 }
270
271 return word;
272 }