diff options
Diffstat (limited to 'string-list.c')
-rw-r--r-- | string-list.c | 75 |
1 files changed, 64 insertions, 11 deletions
diff --git a/string-list.c b/string-list.c index ddd83c8c76..51681189e8 100644 --- a/string-list.c +++ b/string-list.c @@ -26,10 +26,10 @@ static int get_entry_index(const struct string_list *list, const char *string, } /* returns -1-index if already exists */ -static int add_entry(struct string_list *list, const char *string) +static int add_entry(int insert_at, struct string_list *list, const char *string) { - int exact_match; - int index = get_entry_index(list, string, &exact_match); + int exact_match = 0; + int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match); if (exact_match) return -1 - index; @@ -51,9 +51,15 @@ static int add_entry(struct string_list *list, const char *string) return index; } -struct string_list_item *string_list_insert(const char *string, struct string_list *list) +struct string_list_item *string_list_insert(struct string_list *list, const char *string) +{ + return string_list_insert_at_index(list, -1, string); +} + +struct string_list_item *string_list_insert_at_index(struct string_list *list, + int insert_at, const char *string) { - int index = add_entry(list, string); + int index = add_entry(insert_at, list, string); if (index < 0) index = -1 - index; @@ -68,7 +74,17 @@ int string_list_has_string(const struct string_list *list, const char *string) return exact_match; } -struct string_list_item *string_list_lookup(const char *string, struct string_list *list) +int string_list_find_insert_index(const struct string_list *list, const char *string, + int negative_existing_index) +{ + int exact_match; + int index = get_entry_index(list, string, &exact_match); + if (exact_match) + index = -1 - (negative_existing_index ? index : 0); + return index; +} + +struct string_list_item *string_list_lookup(struct string_list *list, const char *string) { int exact_match, i = get_entry_index(list, string, &exact_match); if (!exact_match) @@ -76,6 +92,16 @@ struct string_list_item *string_list_lookup(const char *string, struct string_li return list->items + i; } +int for_each_string_list(struct string_list *list, + string_list_each_func_t fn, void *cb_data) +{ + int i, ret = 0; + for (i = 0; i < list->nr; i++) + if ((ret = fn(&list->items[i], cb_data))) + break; + return ret; +} + void string_list_clear(struct string_list *list, int free_util) { if (list->items) { @@ -94,7 +120,26 @@ void string_list_clear(struct string_list *list, int free_util) list->nr = list->alloc = 0; } -void print_string_list(const char *text, const struct string_list *p) +void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc) +{ + if (list->items) { + int i; + if (clearfunc) { + for (i = 0; i < list->nr; i++) + clearfunc(list->items[i].util, list->items[i].string); + } + if (list->strdup_strings) { + for (i = 0; i < list->nr; i++) + free(list->items[i].string); + } + free(list->items); + } + list->items = NULL; + list->nr = list->alloc = 0; +} + + +void print_string_list(const struct string_list *p, const char *text) { int i; if ( text ) @@ -103,11 +148,12 @@ void print_string_list(const char *text, const struct string_list *p) printf("%s:%p\n", p->items[i].string, p->items[i].util); } -struct string_list_item *string_list_append(const char *string, struct string_list *list) +struct string_list_item *string_list_append(struct string_list *list, const char *string) { ALLOC_GROW(list->items, list->nr + 1, list->alloc); list->items[list->nr].string = list->strdup_strings ? xstrdup(string) : (char *)string; + list->items[list->nr].util = NULL; return list->items + list->nr++; } @@ -123,12 +169,19 @@ void sort_string_list(struct string_list *list) qsort(list->items, list->nr, sizeof(*list->items), cmp_items); } -int unsorted_string_list_has_string(struct string_list *list, const char *string) +struct string_list_item *unsorted_string_list_lookup(struct string_list *list, + const char *string) { int i; for (i = 0; i < list->nr; i++) if (!strcmp(string, list->items[i].string)) - return 1; - return 0; + return list->items + i; + return NULL; +} + +int unsorted_string_list_has_string(struct string_list *list, + const char *string) +{ + return unsorted_string_list_lookup(list, string) != NULL; } |