Debian Codegraph @

src/lib/util/string_array.h

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 #ifndef _STRING_ARRAY_H_
22 #define _STRING_ARRAY_H_
23
24 #include <stddef.h>
25 #include <stdbool.h>
26
27 /**
28 * Create NULL-terminated string array to hold @num_items.
29 *
30 * @param num_items Array capacity not including NULL terminator.
31 *
32 * @return Array on success, NULL if the allocation fails.
33 */
34 char **
35 string_array_create(size_t num_items);
36
37 /**
38 * Create copy of existing NULL-terminated string array.
39 *
40 * @param array NULL-terminated string array..
41 * @param unique If true, value will not be added if it is already present.
42 *
43 * @return Array or NULL if reallocation fails.
44 */
45 char **
46 string_array_copy(char **array, bool unique);
47
48 /**
49 * Resize NULL-terminated string array to hold @num_items. If the new capacity
50 * is smaller, the elements that do not fit into the new capacity are freed.
51 *
52 * If reallocation fails, NULL is returned and the original array is freed.
53 *
54 * @param num_items New array capacity not including NULL terminator.
55 *
56 * @return Array on success, NULL if the allocation fails.
57 */
58 char **
59 string_array_resize(char **array, size_t num_items);
60
61 /**
62 * Free NULL-terminated string array together with its elements.
63 *
64 * @param array NULL-terminated array.
65 */
66 void
67 string_array_free(char **array);
68
69 /**
70 * Count NULL-terminated string array elements.
71 *
72 * @param array NULL-terminated array.
73 *
74 * @return Number of elements stored in the array.
75 */
76 size_t
77 string_array_count(char **array);
78
79 /**
80 * Check if a value exist in NULL-terminated array.
81 *
82 * @param array NULL-terminated array.
83 * @param value Value to look for.
84 * @param len Length of the string.
85 *
86 * @return True if the array contains the value and false otherwise.
87 */
88 bool
89 string_array_has_value_safe(char **array, const char *value, size_t len);
90
91 /**
92 * Check if a value exist in NULL-terminated array.
93 *
94 * @param array NULL-terminated array.
95 * @param value Value to look for.
96 *
97 * @return True if the array contains the value and false otherwise.
98 */
99 bool
100 string_array_has_value(char **array, const char *value);
101
102 /**
103 * Add value to NULL-terminated string array to its end.
104 *
105 * If reallocation fails, NULL is returned and the original array is freed.
106 *
107 * @param array NULL-terminated array.
108 * @param value Value to append to the array.
109 * @param len Length of the string.
110 * @param unique If true, value will not be added if it is already present.
111 *
112 * @return Array or NULL if reallocation fails.
113 */
114 char **
115 string_array_add_value_safe(char **array,
116 const char *value,
117 size_t len,
118 bool unique);
119
120 /**
121 * Add value to NULL-terminated string array to its end.
122 *
123 * If reallocation fails, NULL is returned and the original array is freed.
124 *
125 * @param array NULL-terminated array.
126 * @param value Value to append to the array.
127 * @param unique If true, value will not be added if it is already present.
128 *
129 * @return Array or NULL if reallocation fails.
130 */
131 char **
132 string_array_add_value(char **array, const char *value, bool unique);
133
134 /**
135 * Remove value from NULL-terminated string array.
136 *
137 * @param array NULL-terminated array.
138 * @param value Value to remove from the array.
139 *
140 * @return Array without the value.
141 */
142 void
143 string_array_del_value(char **array, const char *value);
144
145 /**
146 * Concatenate two array. Array @items values will be appended to array @to.
147 *
148 * @param to NULL-terminated destination array.
149 * @param items NULL-terminated array to be appended into @to.
150 * @param unique If true, value will not be added if it is already present.
151 *
152 * @return Array or NULL if reallocation fails.
153 */
154 char **
155 string_array_concat(char **to, char **items, bool unique);
156
157 /**
158 * Alphabetically sort a NULL-terminated string array.
159 *
160 * @param array NULL-terminated string array.
161 */
162 void
163 string_array_sort(char **array);
164
165 /**
166 * Find similar word inside a NULL-terminated array, based on Levenshtein
167 * distance algorithm.
168 *
169 * @param value Value to search in @array.
170 * @param array NULL-terminated string array.
171 * @param max_distance Maximum distance between two strings. If the real
172 * distance is greater then this value, those string
173 * are not considered as similar.
174 *
175 * @return Most similar word that was found or NULL if non was found.
176 */
177 const char *
178 string_array_find_similar(const char *value, char **array, int max_distance);
179
180 #endif /* _STRING_ARRAY_H_ */