4 /* if there is no exact match, point to the index where the entry could be
6 static int get_entry_index(const struct path_list
*list
, const char *path
,
9 int left
= -1, right
= list
->nr
;
11 while (left
+ 1 < right
) {
12 int middle
= (left
+ right
) / 2;
13 int compare
= strcmp(path
, list
->items
[middle
].path
);
28 /* returns -1-index if already exists */
29 static int add_entry(struct path_list
*list
, const char *path
)
32 int index
= get_entry_index(list
, path
, &exact_match
);
37 if (list
->nr
+ 1 >= list
->alloc
) {
39 list
->items
= xrealloc(list
->items
, list
->alloc
40 * sizeof(struct path_list_item
));
43 memmove(list
->items
+ index
+ 1, list
->items
+ index
,
45 * sizeof(struct path_list_item
));
46 list
->items
[index
].path
= list
->strdup_paths
?
47 xstrdup(path
) : (char *)path
;
48 list
->items
[index
].util
= NULL
;
54 struct path_list_item
*path_list_insert(const char *path
, struct path_list
*list
)
56 int index
= add_entry(list
, path
);
61 return list
->items
+ index
;
64 int path_list_has_path(const struct path_list
*list
, const char *path
)
67 get_entry_index(list
, path
, &exact_match
);
71 struct path_list_item
*path_list_lookup(const char *path
, struct path_list
*list
)
73 int exact_match
, i
= get_entry_index(list
, path
, &exact_match
);
76 return list
->items
+ i
;
79 void path_list_clear(struct path_list
*list
, int free_util
)
83 if (list
->strdup_paths
) {
84 for (i
= 0; i
< list
->nr
; i
++)
85 free(list
->items
[i
].path
);
88 for (i
= 0; i
< list
->nr
; i
++)
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
);
106 struct path_list_item
*path_list_append(const char *path
, struct path_list
*list
)
108 ALLOC_GROW(list
->items
, list
->nr
+ 1, list
->alloc
);
109 list
->items
[list
->nr
].path
=
110 list
->strdup_paths
? xstrdup(path
) : (char *)path
;
111 return list
->items
+ list
->nr
++;
114 static int cmp_items(const void *a
, const void *b
)
116 const struct path_list_item
*one
= a
;
117 const struct path_list_item
*two
= b
;
118 return strcmp(one
->path
, two
->path
);
121 void sort_path_list(struct path_list
*list
)
123 qsort(list
->items
, list
->nr
, sizeof(*list
->items
), cmp_items
);
126 int unsorted_path_list_has_path(struct path_list
*list
, const char *path
)
129 for (i
= 0; i
< list
->nr
; i
++)
130 if (!strcmp(path
, list
->items
[i
].path
))