string-list.con commit t1301-shared-repo: fix forced modes test (0786882)
   1#include "cache.h"
   2#include "string-list.h"
   3
   4/* if there is no exact match, point to the index where the entry could be
   5 * inserted */
   6static int get_entry_index(const struct string_list *list, const char *string,
   7                int *exact_match)
   8{
   9        int left = -1, right = list->nr;
  10
  11        while (left + 1 < right) {
  12                int middle = (left + right) / 2;
  13                int compare = strcmp(string, list->items[middle].string);
  14                if (compare < 0)
  15                        right = middle;
  16                else if (compare > 0)
  17                        left = middle;
  18                else {
  19                        *exact_match = 1;
  20                        return middle;
  21                }
  22        }
  23
  24        *exact_match = 0;
  25        return right;
  26}
  27
  28/* returns -1-index if already exists */
  29static int add_entry(int insert_at, struct string_list *list, const char *string)
  30{
  31        int exact_match = 0;
  32        int index = insert_at != -1 ? insert_at : get_entry_index(list, string, &exact_match);
  33
  34        if (exact_match)
  35                return -1 - index;
  36
  37        if (list->nr + 1 >= list->alloc) {
  38                list->alloc += 32;
  39                list->items = xrealloc(list->items, list->alloc
  40                                * sizeof(struct string_list_item));
  41        }
  42        if (index < list->nr)
  43                memmove(list->items + index + 1, list->items + index,
  44                                (list->nr - index)
  45                                * sizeof(struct string_list_item));
  46        list->items[index].string = list->strdup_strings ?
  47                xstrdup(string) : (char *)string;
  48        list->items[index].util = NULL;
  49        list->nr++;
  50
  51        return index;
  52}
  53
  54struct string_list_item *string_list_insert(const char *string, struct string_list *list)
  55{
  56        return string_list_insert_at_index(-1, string, list);
  57}
  58
  59struct string_list_item *string_list_insert_at_index(int insert_at,
  60                                                     const char *string, struct string_list *list)
  61{
  62        int index = add_entry(insert_at, list, string);
  63
  64        if (index < 0)
  65                index = -1 - index;
  66
  67        return list->items + index;
  68}
  69
  70int string_list_has_string(const struct string_list *list, const char *string)
  71{
  72        int exact_match;
  73        get_entry_index(list, string, &exact_match);
  74        return exact_match;
  75}
  76
  77int string_list_find_insert_index(const struct string_list *list, const char *string,
  78                                  int negative_existing_index)
  79{
  80        int exact_match;
  81        int index = get_entry_index(list, string, &exact_match);
  82        if (exact_match)
  83                index = -1 - (negative_existing_index ? index : 0);
  84        return index;
  85}
  86
  87struct string_list_item *string_list_lookup(const char *string, struct string_list *list)
  88{
  89        int exact_match, i = get_entry_index(list, string, &exact_match);
  90        if (!exact_match)
  91                return NULL;
  92        return list->items + i;
  93}
  94
  95void string_list_clear(struct string_list *list, int free_util)
  96{
  97        if (list->items) {
  98                int i;
  99                if (list->strdup_strings) {
 100                        for (i = 0; i < list->nr; i++)
 101                                free(list->items[i].string);
 102                }
 103                if (free_util) {
 104                        for (i = 0; i < list->nr; i++)
 105                                free(list->items[i].util);
 106                }
 107                free(list->items);
 108        }
 109        list->items = NULL;
 110        list->nr = list->alloc = 0;
 111}
 112
 113void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc)
 114{
 115        if (list->items) {
 116                int i;
 117                if (clearfunc) {
 118                        for (i = 0; i < list->nr; i++)
 119                                clearfunc(list->items[i].util, list->items[i].string);
 120                }
 121                if (list->strdup_strings) {
 122                        for (i = 0; i < list->nr; i++)
 123                                free(list->items[i].string);
 124                }
 125                free(list->items);
 126        }
 127        list->items = NULL;
 128        list->nr = list->alloc = 0;
 129}
 130
 131
 132void print_string_list(const char *text, const struct string_list *p)
 133{
 134        int i;
 135        if ( text )
 136                printf("%s\n", text);
 137        for (i = 0; i < p->nr; i++)
 138                printf("%s:%p\n", p->items[i].string, p->items[i].util);
 139}
 140
 141struct string_list_item *string_list_append(const char *string, struct string_list *list)
 142{
 143        ALLOC_GROW(list->items, list->nr + 1, list->alloc);
 144        list->items[list->nr].string =
 145                list->strdup_strings ? xstrdup(string) : (char *)string;
 146        return list->items + list->nr++;
 147}
 148
 149static int cmp_items(const void *a, const void *b)
 150{
 151        const struct string_list_item *one = a;
 152        const struct string_list_item *two = b;
 153        return strcmp(one->string, two->string);
 154}
 155
 156void sort_string_list(struct string_list *list)
 157{
 158        qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
 159}
 160
 161int unsorted_string_list_has_string(struct string_list *list, const char *string)
 162{
 163        int i;
 164        for (i = 0; i < list->nr; i++)
 165                if (!strcmp(string, list->items[i].string))
 166                        return 1;
 167        return 0;
 168}
 169