| 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"cxx . . $ `<file>/src/lib/util/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"cxx . . $ `<file>/src/common/common.h`/ |
| 29 | #include "lib/util/string_array.h"cxx . . $ `<file>/src/lib/util/string_array.h`/ |
| 30 | #include "lib/util/string.h"cxx . . $ `<file>/src/lib/util/string.h`/ |
| 31 | |
| 32 | char ** |
| 33 | string_array_createcxx . . $ string_array_create(4d3fbc76ec7ad0ba).(size_t num_items) |
| 34 | { |
| 35 | return malloc_zero_arraycxx . . $ `src/common/common.h:41:9`!(char *, num_items + 1); |
| 36 | } |
| 37 | |
| 38 | char ** |
| 39 | string_array_copycxx . . $ string_array_copy(55ba949d88614acb).(char **array, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! unique) |
| 40 | { |
| 41 | char **copy; |
| 42 | size_t len; |
| 43 | size_t i; |
| 44 | |
| 45 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 46 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 47 | } |
| 48 | |
| 49 | len = string_array_countcxx . . $ string_array_count(44350d944485fca).(array); |
| 50 | |
| 51 | copy = string_array_createcxx . . $ string_array_create(4d3fbc76ec7ad0ba).(len); |
| 52 | if (copy == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 53 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 54 | } |
| 55 | |
| 56 | for (i = 0; array[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 57 | copy = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(copy, array[i], unique); |
| 58 | if (copy == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 59 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return copy; |
| 64 | } |
| 65 | |
| 66 | char ** |
| 67 | string_array_resizecxx . . $ string_array_resize(ddb0939815488804).(char **array, size_t num_items) |
| 68 | { |
| 69 | char **reallocated; |
| 70 | size_t count; |
| 71 | size_t i; |
| 72 | |
| 73 | count = string_array_countcxx . . $ string_array_count(44350d944485fca).(array); |
| 74 | if (num_items < count) { |
| 75 | for (i = num_items; i < count; i++) { |
| 76 | freecxx . . $ free(5d22bdacc48458e8).(array[i]); |
| 77 | array[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | reallocated = realloc_arraycxx . . $ `src/common/common.h:45:9`!(array, char *, num_items + 1); |
| 82 | if (reallocated == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 83 | string_array_freecxx . . $ string_array_free(3ed91490e421178f).(array); |
| 84 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 85 | } |
| 86 | |
| 87 | reallocated[num_items] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 88 | return reallocated; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | string_array_freecxx . . $ string_array_free(3ed91490e421178f).(char **array) |
| 93 | { |
| 94 | size_t i; |
| 95 | |
| 96 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | for (i = 0; array[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 101 | freecxx . . $ free(5d22bdacc48458e8).(array[i]); |
| 102 | } |
| 103 | |
| 104 | freecxx . . $ free(5d22bdacc48458e8).(array); |
| 105 | } |
| 106 | |
| 107 | size_t |
| 108 | string_array_countcxx . . $ string_array_count(44350d944485fca).(char **array) |
| 109 | { |
| 110 | size_t count; |
| 111 | |
| 112 | for (count = 0; array[count] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; count++) { |
| 113 | /* no op */ |
| 114 | } |
| 115 | |
| 116 | return count; |
| 117 | } |
| 118 | |
| 119 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 120 | string_array_has_value_safecxx . . $ string_array_has_value_safe(a51bdd3619da8a6d).(char **array, const char *value, size_t len) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | if (value == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 125 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 126 | } |
| 127 | |
| 128 | for (i = 0; array[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 129 | if (strncmpcxx . . $ strncmp(fd278af2f295fa2e).(value, array[i], len) == 0 && array[i][len] == '\0') { |
| 130 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 135 | } |
| 136 | |
| 137 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! |
| 138 | string_array_has_valuecxx . . $ string_array_has_value(20870b8b3d19c175).(char **array, const char *value) |
| 139 | { |
| 140 | return string_array_has_value_safecxx . . $ string_array_has_value_safe(a51bdd3619da8a6d).(array, value, strlencxx . . $ strlen(751346f8406fd082).(value)); |
| 141 | } |
| 142 | |
| 143 | char ** |
| 144 | string_array_add_value_safecxx . . $ string_array_add_value_safe(dfc2d168ddb15878).(char **array, |
| 145 | const char *value, |
| 146 | size_t len, |
| 147 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! unique) |
| 148 | { |
| 149 | size_t count; |
| 150 | |
| 151 | if (unique && string_array_has_value_safecxx . . $ string_array_has_value_safe(a51bdd3619da8a6d).(array, value, len)) { |
| 152 | return array; |
| 153 | } |
| 154 | |
| 155 | count = string_array_countcxx . . $ string_array_count(44350d944485fca).(array); |
| 156 | array = string_array_resizecxx . . $ string_array_resize(ddb0939815488804).(array, count + 1); |
| 157 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 158 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 159 | } |
| 160 | |
| 161 | array[count] = strndupcxx . . $ strndup(eabdfdef77619b3d).(value, len); |
| 162 | if (array[count] == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 163 | string_array_freecxx . . $ string_array_free(3ed91490e421178f).(array); |
| 164 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 165 | } |
| 166 | |
| 167 | return array; |
| 168 | } |
| 169 | |
| 170 | char ** |
| 171 | string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(char **array, const char *value, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! unique) |
| 172 | { |
| 173 | return string_array_add_value_safecxx . . $ string_array_add_value_safe(dfc2d168ddb15878).(array, value, strlencxx . . $ strlen(751346f8406fd082).(value), unique); |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | string_array_del_valuecxx . . $ string_array_del_value(aab58f5807001017).(char **array, const char *value) |
| 178 | { |
| 179 | size_t count; |
| 180 | size_t pos; |
| 181 | size_t i; |
| 182 | |
| 183 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | count = string_array_countcxx . . $ string_array_count(44350d944485fca).(array); |
| 188 | for (i = 0; i < count; i++) { |
| 189 | if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(array[i], value) == 0) { |
| 190 | freecxx . . $ free(5d22bdacc48458e8).(array[i]); |
| 191 | array[i] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | for (i = 0, pos = 0; i < count; i++) { |
| 196 | if (array[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 197 | array[pos] = array[i]; |
| 198 | pos++; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | for (; pos < count; pos++) { |
| 203 | array[pos] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 204 | } |
| 205 | |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | char ** |
| 210 | string_array_concatcxx . . $ string_array_concat(1bde152e053d6490).(char **to, char **items, boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! unique) |
| 211 | { |
| 212 | int i; |
| 213 | |
| 214 | if (items == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 215 | return to; |
| 216 | } |
| 217 | |
| 218 | for (i = 0; items[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 219 | to = string_array_add_valuecxx . . $ string_array_add_value(db56f6d53b8c0a98).(to, items[i], unique); |
| 220 | if (to == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 221 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return to; |
| 226 | } |
| 227 | |
| 228 | static int |
| 229 | string_array_sort_callbackcxx . . $ string_array_sort_callback(9148c8c3ff42c9d8).(const void *a, const void *b) |
| 230 | { |
| 231 | return strcmpcxx . . $ strcmp(5ae29d726ae57f88).(*(char* const*)a, *(char* const*)b); |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | string_array_sortcxx . . $ string_array_sort(3ed91490e421178f).(char **array) |
| 236 | { |
| 237 | if (array == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(array, string_array_countcxx . . $ string_array_count(44350d944485fca).(array), sizeof(char *), |
| 242 | string_array_sort_callbackcxx . . $ string_array_sort_callback(9148c8c3ff42c9d8).); |
| 243 | } |
| 244 | |
| 245 | const char * |
| 246 | string_array_find_similarcxx . . $ string_array_find_similar(aae266b49c6ddb5a).(const char *value, char **array, int max_distance) |
| 247 | { |
| 248 | const char *word = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 249 | int current; |
| 250 | int best = 0; |
| 251 | int i; |
| 252 | |
| 253 | for (i = 0; array[i] != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; i++) { |
| 254 | current = string_levenshteincxx . . $ string_levenshtein(5ae29d726ae57f88).(value, array[i]); |
| 255 | if (word == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!) { |
| 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 == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! || best > max_distance) { |
| 268 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 269 | } |
| 270 | |
| 271 | return word; |
| 272 | } |