Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "path-list.h" |
| 3 | |
| 4 | /* if there is no exact match, point to the index where the entry could be |
| 5 | * inserted */ |
| 6 | static int get_entry_index(const struct path_list *list, const char *path, |
| 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(path, list->items[middle].path); |
| 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 */ |
| 29 | static int add_entry(struct path_list *list, const char *path) |
| 30 | { |
| 31 | int exact_match; |
| 32 | int index = get_entry_index(list, path, &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 path_list_item)); |
| 41 | } |
| 42 | if (index < list->nr) |
| 43 | memmove(list->items + index + 1, list->items + index, |
| 44 | (list->nr - index) |
| 45 | * sizeof(struct path_list_item)); |
| 46 | list->items[index].path = list->strdup_paths ? |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 47 | xstrdup(path) : (char *)path; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 48 | list->items[index].util = NULL; |
| 49 | list->nr++; |
| 50 | |
| 51 | return index; |
| 52 | } |
| 53 | |
| 54 | struct path_list_item *path_list_insert(const char *path, struct path_list *list) |
| 55 | { |
| 56 | int index = add_entry(list, path); |
| 57 | |
| 58 | if (index < 0) |
Junio C Hamano | 057bc80 | 2006-11-11 14:45:35 -0800 | [diff] [blame] | 59 | index = -1 - index; |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 60 | |
| 61 | return list->items + index; |
| 62 | } |
| 63 | |
| 64 | int path_list_has_path(const struct path_list *list, const char *path) |
| 65 | { |
| 66 | int exact_match; |
| 67 | get_entry_index(list, path, &exact_match); |
| 68 | return exact_match; |
| 69 | } |
| 70 | |
| 71 | struct path_list_item *path_list_lookup(const char *path, struct path_list *list) |
| 72 | { |
| 73 | int exact_match, i = get_entry_index(list, path, &exact_match); |
| 74 | if (!exact_match) |
| 75 | return NULL; |
| 76 | return list->items + i; |
| 77 | } |
| 78 | |
| 79 | void path_list_clear(struct path_list *list, int free_items) |
| 80 | { |
| 81 | if (list->items) { |
| 82 | int i; |
| 83 | if (free_items) |
| 84 | for (i = 0; i < list->nr; i++) { |
| 85 | if (list->strdup_paths) |
| 86 | free(list->items[i].path); |
Junio C Hamano | 4cac42b | 2006-08-27 21:19:39 -0700 | [diff] [blame] | 87 | free(list->items[i].util); |
Johannes Schindelin | 8fd2cb4 | 2006-07-25 21:32:18 -0700 | [diff] [blame] | 88 | } |
| 89 | free(list->items); |
| 90 | } |
| 91 | list->items = NULL; |
| 92 | list->nr = list->alloc = 0; |
| 93 | } |
| 94 | |
| 95 | void print_path_list(const char *text, const struct path_list *p) |
| 96 | { |
| 97 | int i; |
| 98 | if ( text ) |
| 99 | printf("%s\n", text); |
| 100 | for (i = 0; i < p->nr; i++) |
| 101 | printf("%s:%p\n", p->items[i].path, p->items[i].util); |
| 102 | } |