string_list: add function string_list_append_nodup()
[git/git.git] / string-list.h
CommitLineData
0afcb5f7
TF
1#ifndef STRING_LIST_H
2#define STRING_LIST_H
c455c87c
JS
3
4struct string_list_item {
5 char *string;
6 void *util;
7};
9cba13ca 8struct string_list {
c455c87c
JS
9 struct string_list_item *items;
10 unsigned int nr, alloc;
11 unsigned int strdup_strings:1;
12};
13
183113a5
TF
14#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 }
15#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1 }
16
cb944f6b 17void print_string_list(const struct string_list *p, const char *text);
c455c87c
JS
18void string_list_clear(struct string_list *list, int free_util);
19
cfa1ee6b
MSO
20/* Use this function to call a custom clear function on each util pointer */
21/* The string associated with the util pointer is passed as the second argument */
22typedef void (*string_list_clear_func_t)(void *p, const char *str);
23void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc);
24
8d31635c 25/* Use this function or the macro below to iterate over each item */
c6f5a7a9 26typedef int (*string_list_each_func_t)(struct string_list_item *, void *);
b684e977
JP
27int for_each_string_list(struct string_list *list,
28 string_list_each_func_t, void *cb_data);
8d31635c
AR
29#define for_each_string_list_item(item,list) \
30 for (item = (list)->items; item < (list)->items + (list)->nr; ++item)
c6f5a7a9 31
e448fed8 32
c455c87c
JS
33/* Use these functions only on sorted lists: */
34int string_list_has_string(const struct string_list *list, const char *string);
cfa1ee6b
MSO
35int string_list_find_insert_index(const struct string_list *list, const char *string,
36 int negative_existing_index);
78a395d3 37struct string_list_item *string_list_insert(struct string_list *list, const char *string);
aadceea6
JP
38struct string_list_item *string_list_insert_at_index(struct string_list *list,
39 int insert_at, const char *string);
e8c8b713 40struct string_list_item *string_list_lookup(struct string_list *list, const char *string);
c455c87c 41
e448fed8 42
c455c87c 43/* Use these functions only on unsorted lists: */
e448fed8
MH
44
45/*
46 * Add string to the end of list. If list->strdup_string is set, then
47 * string is copied; otherwise the new string_list_entry refers to the
48 * input string.
49 */
1d2f80fa 50struct string_list_item *string_list_append(struct string_list *list, const char *string);
e448fed8
MH
51
52/*
53 * Like string_list_append(), except string is never copied. When
54 * list->strdup_strings is set, this function can be used to hand
55 * ownership of a malloc()ed string to list without making an extra
56 * copy.
57 */
58struct string_list_item *string_list_append_nodup(struct string_list *list, char *string);
59
c455c87c
JS
60void sort_string_list(struct string_list *list);
61int unsorted_string_list_has_string(struct string_list *list, const char *string);
e2421480
SB
62struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
63 const char *string);
e448fed8 64
86d4b528 65void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util);
0afcb5f7 66#endif /* STRING_LIST_H */