| 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc., Durham, North Carolina. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: |
| 20 | * Lukas Kuklinek <[email protected]> |
| 21 | */ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <assert.h> |
| 28 | #include <string.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdarg.h> |
| 31 | |
| 32 | #include "list.h" |
| 33 | static inline bool _oscap_iterator_has_more_internal(const struct oscap_iterator *it); |
| 34 | |
| 35 | struct oscap_list *oscap_list_new(void) |
| 36 | { |
| 37 | struct oscap_list *list = calloc(1, sizeof(struct oscap_list)); |
| 38 | return list; |
| 39 | } |
| 40 | |
| 41 | void oscap_create_lists(struct oscap_list **first, ...) |
| 42 | { |
| 43 | va_list ap; |
| 44 | va_start(ap, first); |
| 45 | for (struct oscap_list **cur = first; cur != NULL; cur = va_arg(ap, struct oscap_list **)) |
| 46 | *cur = oscap_list_new(); |
| 47 | va_end(ap); |
| 48 | } |
| 49 | |
| 50 | bool oscap_list_add(struct oscap_list * list, void *value) |
| 51 | { |
| 52 | __attribute__nonnull__(list); |
| 53 | if (value == NULL) return false; |
| 54 | |
| 55 | struct oscap_list_item *item = malloc(sizeof(struct oscap_list_item)); |
| 56 | item->next = NULL; |
| 57 | item->data = value; |
| 58 | ++list->itemcount; |
| 59 | |
| 60 | if (list->last == NULL) |
| 61 | list->first = list->last = item; |
| 62 | else { |
| 63 | list->last->next = item; |
| 64 | list->last = item; |
| 65 | } |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | bool oscap_list_prepend(struct oscap_list * list, void *value) |
| 70 | { |
| 71 | __attribute__nonnull__(list); |
| 72 | if (value == NULL) return false; |
| 73 | |
| 74 | struct oscap_list_item *item = malloc(sizeof(struct oscap_list_item)); |
| 75 | item->next = NULL; |
| 76 | item->data = value; |
| 77 | ++list->itemcount; |
| 78 | |
| 79 | if (list->first == NULL) { |
| 80 | list->last = list->first = item; |
| 81 | } else { |
| 82 | item->next = list->first; |
| 83 | list->first = item; |
| 84 | } |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | bool oscap_list_push(struct oscap_list *list, void *value) |
| 89 | { |
| 90 | return oscap_list_add(list,value); |
| 91 | } |
| 92 | |
| 93 | bool oscap_list_pop(struct oscap_list *list, oscap_destruct_func destructor) |
| 94 | { |
| 95 | if (list == NULL || list->first == NULL) return false; |
| 96 | struct oscap_list_item *cur = list->first, *prev = NULL; |
| 97 | |
| 98 | while (cur != list->last) { |
| 99 | prev = cur; |
| 100 | cur = cur->next; |
| 101 | } |
| 102 | |
| 103 | if (destructor) destructor(cur->data); |
| 104 | free(cur); |
| 105 | |
| 106 | list->last = prev; |
| 107 | if (prev) prev->next = NULL; |
| 108 | else list->first = NULL; |
| 109 | |
| 110 | --list->itemcount; |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | bool oscap_list_remove(struct oscap_list *list, void *value, oscap_cmp_func compare, oscap_destruct_func destructor) |
| 116 | { |
| 117 | if (list == NULL || list->first == NULL) return false; |
| 118 | struct oscap_list_item *cur = list->first, *prev = NULL; |
| 119 | |
| 120 | while (cur != list->last && !compare(cur->data, value)) { |
| 121 | prev = cur; |
| 122 | cur = cur->next; |
| 123 | } |
| 124 | |
| 125 | if (compare(cur->data, value)) { |
| 126 | if (prev) |
| 127 | prev->next = cur->next; |
| 128 | else |
| 129 | list->first = cur->next; |
| 130 | |
| 131 | if (cur == list->last) |
| 132 | list->last = prev; |
| 133 | |
| 134 | if (destructor) destructor(cur->data); |
| 135 | free(cur); |
| 136 | |
| 137 | --list->itemcount; |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | struct oscap_list * oscap_list_clone(const struct oscap_list * list, oscap_clone_func cloner) |
| 145 | { |
| 146 | if (list == NULL) |
| 147 | return NULL; |
| 148 | |
| 149 | struct oscap_list * copy = oscap_list_new(); |
| 150 | struct oscap_list_item * item; |
| 151 | |
| 152 | item = list->first; |
| 153 | while (item != NULL) { |
| 154 | if (cloner) |
| 155 | oscap_list_add(copy, cloner(item->data)); |
| 156 | else oscap_list_add(copy, item->data); |
| 157 | item = item->next; |
| 158 | } |
| 159 | |
| 160 | return copy; |
| 161 | } |
| 162 | |
| 163 | struct oscap_list *oscap_list_destructive_join(struct oscap_list *list1, struct oscap_list *list2) |
| 164 | { |
| 165 | if (list1->first == NULL) |
| 166 | list1->first = list2->first; |
| 167 | else list1->last->next = list2->first; |
| 168 | if (list2->last != NULL) list1->last = list2->last; |
| 169 | list1->itemcount += list2->itemcount; |
| 170 | free(list2); |
| 171 | return list1; |
| 172 | } |
| 173 | |
| 174 | int oscap_list_get_itemcount(struct oscap_list *list) |
| 175 | { |
| 176 | __attribute__nonnull__(list); |
| 177 | return list->itemcount; |
| 178 | } |
| 179 | |
| 180 | void oscap_list_free(struct oscap_list *list, oscap_destruct_func destructor) |
| 181 | { |
| 182 | struct oscap_list_item *item, *to_del; |
| 183 | if (list) { |
| 184 | item = list->first; |
| 185 | while (item != NULL) { |
| 186 | to_del = item; |
| 187 | item = item->next; |
| 188 | if (destructor) |
| 189 | destructor(to_del->data); |
| 190 | free(to_del); |
| 191 | } |
| 192 | free(list); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void oscap_list_free0(struct oscap_list *list) |
| 197 | { |
| 198 | oscap_list_free(list, NULL); |
| 199 | } |
| 200 | |
| 201 | bool oscap_ptr_cmp(void *node1, void *node2) |
| 202 | { |
| 203 | return node1 == node2; |
| 204 | } |
| 205 | |
| 206 | void* oscap_list_find(struct oscap_list *list, void *what, oscap_cmp_func compare) |
| 207 | { |
| 208 | if (list == NULL) return false; |
| 209 | if (compare == NULL) compare = oscap_ptr_cmp; |
| 210 | |
| 211 | struct oscap_iterator *it = oscap_iterator_new(list); |
| 212 | |
| 213 | while (_oscap_iterator_has_more_internal(it)) { |
| 214 | void *item = oscap_iterator_next(it); |
| 215 | if (compare(item, what)) { |
| 216 | oscap_iterator_free(it); |
| 217 | return item; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | oscap_iterator_free(it); |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | bool oscap_list_contains(struct oscap_list *list, void *what, oscap_cmp_func compare) |
| 226 | { |
| 227 | return oscap_list_find(list, what, compare) != NULL; |
| 228 | } |
| 229 | |
| 230 | void oscap_list_dump(struct oscap_list *list, oscap_dump_func dumper, int depth) |
| 231 | { |
| 232 | if (list == NULL) { |
| 233 | printf(" (NULL list)\n"); |
| 234 | return; |
| 235 | } |
| 236 | printf(" (list, %u item%s)\n", (unsigned)list->itemcount, (list->itemcount == 1 ? "" : "s")); |
| 237 | struct oscap_list_item *item = list->first; |
| 238 | while (item) { |
| 239 | dumper(item->data, depth); |
| 240 | item = item->next; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | static bool oscap_iterator_no_filter(void *foo, void *bar) |
| 245 | { |
| 246 | return true; |
| 247 | } |
| 248 | |
| 249 | static inline void oscap_iterator_find_nearest(struct oscap_iterator *it) |
| 250 | { |
| 251 | __attribute__nonnull__(it); |
| 252 | __attribute__nonnull__(it->list); |
| 253 | |
| 254 | do { |
| 255 | it->cur = (it->cur ? it->cur->next : it->list->first); |
| 256 | } while (it->cur && !it->filter(it->cur->data, it->user_data) && _oscap_iterator_has_more_internal(it)); |
| 257 | } |
| 258 | |
| 259 | void *oscap_iterator_new(struct oscap_list *list) |
| 260 | { |
| 261 | struct oscap_iterator *it = calloc(1, sizeof(struct oscap_iterator)); |
| 262 | it->cur = NULL; |
| 263 | it->filter = oscap_iterator_no_filter; |
| 264 | it->list = list; |
| 265 | return it; |
| 266 | } |
| 267 | |
| 268 | void *oscap_iterator_new_filter(struct oscap_list *list, oscap_filter_func filter, void *user_data) |
| 269 | { |
| 270 | struct oscap_iterator *it = oscap_iterator_new(list); |
| 271 | it->filter = filter; |
| 272 | it->user_data = user_data; |
| 273 | return it; |
| 274 | } |
| 275 | |
| 276 | size_t oscap_iterator_get_itemcount(const struct oscap_iterator * it) |
| 277 | { |
| 278 | __attribute__nonnull__(it); |
| 279 | return it->list->itemcount; |
| 280 | } |
| 281 | |
| 282 | void *oscap_iterator_detach(struct oscap_iterator *it) |
| 283 | { |
| 284 | __attribute__nonnull__(it); |
| 285 | |
| 286 | if (!it->cur) |
| 287 | return NULL; |
| 288 | |
| 289 | struct oscap_list_item *item = it->cur; |
| 290 | void *value = item->data; |
| 291 | |
| 292 | assert(it->list->first != NULL && it->list->last != NULL); |
| 293 | |
| 294 | if (it->list->first == it->list->last) { |
| 295 | assert(it->list->first == it->list->last); |
| 296 | assert(it->list->first == item); |
| 297 | it->list->first = it->list->last = NULL; |
| 298 | it->cur = NULL; |
| 299 | } else if (item == it->list->first) { |
| 300 | assert(it->list->first != it->list->last); |
| 301 | assert(item->next != NULL); |
| 302 | it->list->first = item->next; |
| 303 | it->cur = NULL; |
| 304 | } else { |
| 305 | struct oscap_list_item *cur = it->list->first; |
| 306 | while (cur->next != item) { |
| 307 | assert(cur->next != NULL); |
| 308 | cur = cur->next; |
| 309 | } |
| 310 | assert(cur->next == item); |
| 311 | cur->next = item->next; |
| 312 | if (item == it->list->last) |
| 313 | it->list->last = cur; |
| 314 | it->cur = cur; |
| 315 | } |
| 316 | |
| 317 | free(item); |
| 318 | --it->list->itemcount; |
| 319 | return value; |
| 320 | } |
| 321 | |
| 322 | void oscap_iterator_free(struct oscap_iterator *it) |
| 323 | { |
| 324 | free(it); |
| 325 | } |
| 326 | |
| 327 | void *oscap_iterator_next(struct oscap_iterator *it) |
| 328 | { |
| 329 | __attribute__nonnull__(it); |
| 330 | oscap_iterator_find_nearest(it); |
| 331 | return it->cur->data; |
| 332 | } |
| 333 | |
| 334 | static inline bool _oscap_iterator_has_more_internal(const struct oscap_iterator *it) |
| 335 | { |
| 336 | return (!it->cur && it->list->first) || (it->cur && it->cur->next); |
| 337 | } |
| 338 | |
| 339 | bool oscap_iterator_has_more(struct oscap_iterator *it) |
| 340 | { |
| 341 | if (!it || !it->list || !it->list->first) |
| 342 | return false; |
| 343 | if (it->cur == NULL) { |
| 344 | if (it->filter(it->list->first->data, it->user_data)) |
| 345 | return true; |
| 346 | else |
| 347 | it->cur = it->list->first; |
| 348 | } |
| 349 | while (it->cur->next && !it->filter(it->cur->next->data, it->user_data)) |
| 350 | it->cur = it->cur->next; |
| 351 | return it->cur->next != NULL; |
| 352 | } |
| 353 | |
| 354 | void oscap_iterator_reset(struct oscap_iterator * it) |
| 355 | { |
| 356 | it->cur = NULL; |
| 357 | } |
| 358 | |
| 359 | struct oscap_stringlist *oscap_stringlist_clone(struct oscap_stringlist *list) |
| 360 | { |
| 361 | void *clone = oscap_list_new(); // oscap_stringlist (or oscap_list) |
| 362 | OSCAP_FOR_STR(str, oscap_stringlist_get_strings(list)) |
| 363 | oscap_list_add(clone, oscap_strdup(str)); |
| 364 | return clone; |
| 365 | } |
| 366 | |
| 367 | struct oscap_string_iterator *oscap_stringlist_get_strings(const struct oscap_stringlist* list) |
| 368 | { |
| 369 | return oscap_iterator_new((struct oscap_list *) list); |
| 370 | } |
| 371 | |
| 372 | bool oscap_stringlist_add_string(struct oscap_stringlist* list, const char *str) |
| 373 | { |
| 374 | return oscap_list_add((struct oscap_list *) list, oscap_strdup(str)); |
| 375 | } |
| 376 | |
| 377 | struct oscap_stringlist * oscap_stringlist_new(void) |
| 378 | { |
| 379 | return (struct oscap_stringlist *) oscap_list_new(); |
| 380 | } |
| 381 | |
| 382 | void oscap_stringlist_free(struct oscap_stringlist *list) |
| 383 | { |
| 384 | oscap_list_free((struct oscap_list *) list, free); |
| 385 | } |
| 386 | |
| 387 | OSCAP_ITERATOR_GEN_T(const char *, oscap_string) |
| 388 | OSCAP_ITERATOR_REMOVE_T(const char *, oscap_string, free) |
| 389 | OSCAP_ITERATOR_GEN(oscap_stringlist) |
| 390 | OSCAP_ITERATOR_REMOVE(oscap_stringlist, oscap_stringlist_free) |
| 391 | /*OSCAP_ITERATOR_RESET(oscap_string)*/ |
| 392 | |
| 393 | |
| 394 | #define OSCAP_DEFAULT_HSIZE 389 |
| 395 | static inline unsigned int oscap_htable_hash(const char *str, size_t htable_size) |
| 396 | { |
| 397 | unsigned h = 0; |
| 398 | unsigned char *p; |
| 399 | for (p = (unsigned char *)str; *p != '\0'; p++) |
| 400 | h = (97 * h) + *p; |
| 401 | return h % htable_size; |
| 402 | } |
| 403 | |
| 404 | struct oscap_htable *oscap_htable_new1(oscap_compare_func cmp, size_t hsize) |
| 405 | { |
| 406 | struct oscap_htable *t; |
| 407 | |
| 408 | assert(hsize > 0); |
| 409 | |
| 410 | t = malloc(sizeof(struct oscap_htable)); |
| 411 | if (t == NULL) |
| 412 | return NULL; |
| 413 | t->hsize = hsize; |
| 414 | t->itemcount = 0; |
| 415 | t->table = calloc(hsize, sizeof(struct oscap_htable_item *)); |
| 416 | if (t->table == NULL) { |
| 417 | free(t); |
| 418 | return NULL; |
| 419 | } |
| 420 | t->cmp = cmp; |
| 421 | return t; |
| 422 | } |
| 423 | |
| 424 | struct oscap_htable * oscap_htable_clone(const struct oscap_htable * table, oscap_clone_func cloner) |
| 425 | { |
| 426 | struct oscap_htable *t = oscap_htable_new(); |
| 427 | if (t == NULL) |
| 428 | return NULL; |
| 429 | |
| 430 | for (size_t i = 0; i < table->hsize; ++i) { |
| 431 | struct oscap_htable_item *item = table->table[i]; |
| 432 | while (item != NULL) { |
| 433 | oscap_htable_add(t, item->key, (void *) cloner(item->value)); |
| 434 | item = item->next; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return t; |
| 439 | } |
| 440 | |
| 441 | static int oscap_htable_cmp(const char *s1, const char *s2) |
| 442 | { |
| 443 | if (s1 == NULL) |
| 444 | return -1; |
| 445 | if (s2 == NULL) |
| 446 | return 1; |
| 447 | return strcmp(s1, s2); |
| 448 | } |
| 449 | |
| 450 | struct oscap_htable *oscap_htable_new(void) |
| 451 | { |
| 452 | return oscap_htable_new1(oscap_htable_cmp, OSCAP_DEFAULT_HSIZE); |
| 453 | } |
| 454 | |
| 455 | static struct oscap_htable_item *oscap_htable_lookup(struct oscap_htable *htable, const char *key) |
| 456 | { |
| 457 | __attribute__nonnull__(htable); |
| 458 | if (key == NULL) |
| 459 | return NULL; |
| 460 | unsigned int hashcode = oscap_htable_hash(key, htable->hsize); |
| 461 | struct oscap_htable_item *htitem = htable->table[hashcode]; |
| 462 | while (htitem != NULL) { |
| 463 | if (htable->cmp(htitem->key, key) == 0) |
| 464 | return htitem; |
| 465 | htitem = htitem->next; |
| 466 | } |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | bool oscap_htable_add(struct oscap_htable * htable, const char *key, void *item) |
| 471 | { |
| 472 | __attribute__nonnull__(htable); |
| 473 | /* |
| 474 | unsigned int hashcode = oscap_htable_hash(key, htable->hsize); |
| 475 | struct oscap_htable_item* htitem = htable->table[hashcode]; |
| 476 | while(htitem != NULL) |
| 477 | { |
| 478 | if( (*(htable->cmp))(htitem->key, key) == 0) return false; |
| 479 | if(htitem->next == NULL) break; |
| 480 | htitem = htitem->next; |
| 481 | } |
| 482 | // htitem points to the last item |
| 483 | */ |
| 484 | if (oscap_htable_lookup(htable, key) != NULL) |
| 485 | return false; |
| 486 | unsigned int hashcode = oscap_htable_hash(key, htable->hsize); |
| 487 | struct oscap_htable_item *newhtitem; |
| 488 | newhtitem = malloc(sizeof(struct oscap_htable_item)); |
| 489 | newhtitem->key = oscap_strdup(key); |
| 490 | newhtitem->value = item; |
| 491 | newhtitem->next = htable->table[hashcode]; |
| 492 | htable->table[hashcode] = newhtitem; |
| 493 | htable->itemcount++; |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | void *oscap_htable_detach(struct oscap_htable *htable, const char *key) |
| 498 | { |
| 499 | struct oscap_htable_item *htitem = oscap_htable_lookup(htable, key); |
| 500 | if (htitem) { |
| 501 | void *val = htitem->value; |
| 502 | free(htitem->key); |
| 503 | htitem->key = NULL; |
| 504 | htitem->value = NULL; |
| 505 | htable->itemcount--; |
| 506 | return val; |
| 507 | } |
| 508 | return NULL; |
| 509 | } |
| 510 | |
| 511 | void *oscap_htable_get(struct oscap_htable *htable, const char *key) |
| 512 | { |
| 513 | __attribute__nonnull__(htable); |
| 514 | struct oscap_htable_item *htitem = oscap_htable_lookup(htable, key); |
| 515 | return htitem ? htitem->value : NULL; |
| 516 | } |
| 517 | |
| 518 | size_t oscap_htable_itemcount(struct oscap_htable *htable) |
| 519 | { |
| 520 | return htable->itemcount; |
| 521 | } |
| 522 | |
| 523 | void oscap_print_depth(int); |
| 524 | |
| 525 | void oscap_htable_dump(struct oscap_htable *htable, oscap_dump_func dumper, int depth) |
| 526 | { |
| 527 | if (htable == NULL) { |
| 528 | printf(" (NULL hash table)\n"); |
| 529 | return; |
| 530 | } |
| 531 | printf(" (hash table, %u item%s)\n", (unsigned)htable->itemcount, (htable->itemcount == 1 ? "" : "s")); |
| 532 | int i; |
| 533 | for (i = 0; i < (int)htable->hsize; ++i) { |
| 534 | struct oscap_htable_item *item = htable->table[i]; |
| 535 | while (item) { |
| 536 | oscap_print_depth(depth); |
| 537 | printf("'%s':\n", item->key); |
| 538 | dumper(item->value, depth + 1); |
| 539 | item = item->next; |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | void oscap_htable_free(struct oscap_htable *htable, oscap_destruct_func destructor) |
| 545 | { |
| 546 | if (htable) { |
| 547 | size_t ht; |
| 548 | struct oscap_htable_item *cur, *next; |
| 549 | |
| 550 | for (ht = 0; ht < htable->hsize; ++ht) { |
| 551 | cur = htable->table[ht]; |
| 552 | while (cur) { |
| 553 | next = cur->next; |
| 554 | free(cur->key); |
| 555 | if (destructor) |
| 556 | destructor(cur->value); |
| 557 | free(cur); |
| 558 | cur = next; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | free(htable->table); |
| 563 | free(htable); |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | void oscap_htable_free0(struct oscap_htable *htable) |
| 568 | { |
| 569 | oscap_htable_free(htable, NULL); |
| 570 | } |
| 571 | |
| 572 | struct oscap_htable_iterator { |
| 573 | struct oscap_htable *htable; // Table we iterate through |
| 574 | struct oscap_htable_item *cur; // The current item |
| 575 | size_t hpos; // Line on which we are now |
| 576 | }; |
| 577 | |
| 578 | struct oscap_htable_iterator * |
| 579 | oscap_htable_iterator_new(struct oscap_htable *htable) |
| 580 | { |
| 581 | struct oscap_htable_iterator *hit = calloc(1, sizeof(struct oscap_htable_iterator)); |
| 582 | hit->htable = htable; |
| 583 | hit->cur = NULL; |
| 584 | hit->hpos = 0; |
| 585 | return hit; |
| 586 | } |
| 587 | |
| 588 | bool |
| 589 | oscap_htable_iterator_has_more(struct oscap_htable_iterator *hit) |
| 590 | { |
| 591 | __attribute__nonnull__(hit); |
| 592 | if (hit->htable == NULL) |
| 593 | return false; |
| 594 | size_t i = hit->hpos; |
| 595 | if (hit->cur != NULL) { |
| 596 | if (hit->cur->next != NULL) |
| 597 | return true; |
| 598 | if (i + 1 >= hit->htable->hsize) |
| 599 | return false; |
| 600 | i++; |
| 601 | } |
| 602 | for (; i < hit->htable->hsize; i++) { |
| 603 | if (hit->htable->table[i] == NULL) |
| 604 | continue; |
| 605 | if (i != hit->hpos) |
| 606 | hit->hpos = i - 1; |
| 607 | return true; |
| 608 | } |
| 609 | hit->hpos = i; |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | const struct oscap_htable_item * |
| 614 | oscap_htable_iterator_next(struct oscap_htable_iterator *hit) |
| 615 | { |
| 616 | __attribute__nonnull__(hit); |
| 617 | if (hit->cur != NULL) { |
| 618 | if (hit->cur->next != NULL) { |
| 619 | hit->cur = hit->cur->next; |
| 620 | return hit->cur; |
| 621 | } |
| 622 | if (hit->hpos + 1 >= hit->htable->hsize) { |
| 623 | assert(false); |
| 624 | return NULL; |
| 625 | } |
| 626 | hit->hpos++; |
| 627 | } |
| 628 | for (; hit->hpos < hit->htable->hsize; hit->hpos++) { |
| 629 | if (hit->htable->table[hit->hpos] == NULL) |
| 630 | continue; |
| 631 | hit->cur = hit->htable->table[hit->hpos]; |
| 632 | return hit->cur; |
| 633 | } |
| 634 | assert(false); // no more item found |
| 635 | return NULL; |
| 636 | } |
| 637 | |
| 638 | const char * |
| 639 | oscap_htable_iterator_next_key(struct oscap_htable_iterator *hit) |
| 640 | { |
| 641 | const struct oscap_htable_item *item = oscap_htable_iterator_next(hit); |
| 642 | return (item == NULL) ? NULL : item->key; |
| 643 | } |
| 644 | |
| 645 | void * |
| 646 | oscap_htable_iterator_next_value(struct oscap_htable_iterator *hit) |
| 647 | { |
| 648 | const struct oscap_htable_item *item = oscap_htable_iterator_next(hit); |
| 649 | return (item == NULL) ? NULL : item->value; |
| 650 | } |
| 651 | |
| 652 | void oscap_htable_iterator_next_kv(struct oscap_htable_iterator *hit, const char **key, void **value) |
| 653 | { |
| 654 | const struct oscap_htable_item *item = oscap_htable_iterator_next(hit); |
| 655 | if (item == NULL) |
| 656 | return; |
| 657 | |
| 658 | *key = item->key; |
| 659 | *value = item->value; |
| 660 | } |
| 661 | |
| 662 | void |
| 663 | oscap_htable_iterator_reset(struct oscap_htable_iterator *hit) |
| 664 | { |
| 665 | __attribute__nonnull__(hit); |
| 666 | hit->cur = NULL; |
| 667 | hit->hpos = 0; |
| 668 | } |
| 669 | |
| 670 | void |
| 671 | oscap_htable_iterator_free(struct oscap_htable_iterator *hit) |
| 672 | { |
| 673 | free(hit); |
| 674 | } |
| 675 | |
| 676 | void oscap_print_depth(int depth) |
| 677 | { |
| 678 | while (depth--) |
| 679 | printf(" "); |
| 680 | } |