1#ifndef STRING_LIST_H 2#define STRING_LIST_H 3 4struct string_list_item { 5char*string; 6void*util; 7}; 8struct string_list { 9struct string_list_item *items; 10unsigned int nr, alloc; 11unsigned int strdup_strings:1; 12}; 13 14#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0 } 15#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1 } 16 17voidprint_string_list(const struct string_list *p,const char*text); 18voidstring_list_clear(struct string_list *list,int free_util); 19 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 */ 22typedefvoid(*string_list_clear_func_t)(void*p,const char*str); 23voidstring_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc); 24 25/* Use this function or the macro below to iterate over each item */ 26typedefint(*string_list_each_func_t)(struct string_list_item *,void*); 27intfor_each_string_list(struct string_list *list, 28 string_list_each_func_t,void*cb_data); 29#define for_each_string_list_item(item,list) \ 30 for (item = (list)->items; item < (list)->items + (list)->nr; ++item) 31 32 33/* Use these functions only on sorted lists: */ 34intstring_list_has_string(const struct string_list *list,const char*string); 35intstring_list_find_insert_index(const struct string_list *list,const char*string, 36int negative_existing_index); 37struct string_list_item *string_list_insert(struct string_list *list,const char*string); 38struct string_list_item *string_list_insert_at_index(struct string_list *list, 39int insert_at,const char*string); 40struct string_list_item *string_list_lookup(struct string_list *list,const char*string); 41 42 43/* Use these functions only on unsorted lists: */ 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 */ 50struct string_list_item *string_list_append(struct string_list *list,const char*string); 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 60voidsort_string_list(struct string_list *list); 61intunsorted_string_list_has_string(struct string_list *list,const char*string); 62struct string_list_item *unsorted_string_list_lookup(struct string_list *list, 63const char*string); 64 65voidunsorted_string_list_delete_item(struct string_list *list,int i,int free_util); 66 67/* 68 * Split string into substrings on character delim and append the 69 * substrings to list. The input string is not modified. 70 * list->strdup_strings must be set, as new memory needs to be 71 * allocated to hold the substrings. If maxsplit is non-negative, 72 * then split at most maxsplit times. Return the number of substrings 73 * appended to list. 74 * 75 * Examples: 76 * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"] 77 * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"] 78 * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"] 79 * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""] 80 * string_list_split(l, "", ':', -1) -> [""] 81 * string_list_split(l, ":", ':', -1) -> ["", ""] 82 */ 83intstring_list_split(struct string_list *list,const char*string, 84int delim,int maxsplit); 85 86/* 87 * Like string_list_split(), except that string is split in-place: the 88 * delimiter characters in string are overwritten with NULs, and the 89 * new string_list_items point into string (which therefore must not 90 * be modified or freed while the string_list is in use). 91 * list->strdup_strings must *not* be set. 92 */ 93intstring_list_split_in_place(struct string_list *list,char*string, 94int delim,int maxsplit); 95#endif/* STRING_LIST_H */