5 /* if there is no exact match, point to the index where the entry could be
7 static int get_entry_index(const struct path_list
*list
, const char *path
,
10 int left
= -1, right
= list
->nr
;
12 while (left
+ 1 < right
) {
13 int middle
= (left
+ right
) / 2;
14 int compare
= strcmp(path
, list
->items
[middle
].path
);
29 /* returns -1-index if already exists */
30 static int add_entry(struct path_list
*list
, const char *path
)
33 int index
= get_entry_index(list
, path
, &exact_match
);
38 if (list
->nr
+ 1 >= list
->alloc
) {
40 list
->items
= xrealloc(list
->items
, list
->alloc
41 * sizeof(struct path_list_item
));
44 memmove(list
->items
+ index
+ 1, list
->items
+ index
,
46 * sizeof(struct path_list_item
));
47 list
->items
[index
].path
= list
->strdup_paths
?
48 strdup(path
) : (char *)path
;
49 list
->items
[index
].util
= NULL
;
55 struct path_list_item
*path_list_insert(const char *path
, struct path_list
*list
)
57 int index
= add_entry(list
, path
);
62 return list
->items
+ index
;
65 int path_list_has_path(const struct path_list
*list
, const char *path
)
68 get_entry_index(list
, path
, &exact_match
);
72 struct path_list_item
*path_list_lookup(const char *path
, struct path_list
*list
)
74 int exact_match
, i
= get_entry_index(list
, path
, &exact_match
);
77 return list
->items
+ i
;
80 void path_list_clear(struct path_list
*list
, int free_items
)
85 for (i
= 0; i
< list
->nr
; i
++) {
86 if (list
->strdup_paths
)
87 free(list
->items
[i
].path
);
88 if (list
->items
[i
].util
)
89 free(list
->items
[i
].util
);
94 list
->nr
= list
->alloc
= 0;
97 void print_path_list(const char *text
, const struct path_list
*p
)
101 printf("%s\n", text
);
102 for (i
= 0; i
< p
->nr
; i
++)
103 printf("%s:%p\n", p
->items
[i
].path
, p
->items
[i
].util
);