1#ifndef STRING_LIST_H 2#define STRING_LIST_H 3 4struct string_list_item { 5char*string; 6void*util; 7}; 8 9typedefint(*compare_strings_fn)(const char*,const char*); 10 11struct string_list { 12struct string_list_item *items; 13unsigned int nr, alloc; 14unsigned int strdup_strings:1; 15 compare_strings_fn cmp;/* NULL uses strcmp() */ 16}; 17 18#define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL } 19#define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL } 20 21voidstring_list_init(struct string_list *list,int strdup_strings); 22 23voidprint_string_list(const struct string_list *p,const char*text); 24voidstring_list_clear(struct string_list *list,int free_util); 25 26/* Use this function to call a custom clear function on each util pointer */ 27/* The string associated with the util pointer is passed as the second argument */ 28typedefvoid(*string_list_clear_func_t)(void*p,const char*str); 29voidstring_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc); 30 31/* Use this function or the macro below to iterate over each item */ 32typedefint(*string_list_each_func_t)(struct string_list_item *,void*); 33intfor_each_string_list(struct string_list *list, 34 string_list_each_func_t,void*cb_data); 35#define for_each_string_list_item(item,list) \ 36 for (item = (list)->items; item < (list)->items + (list)->nr; ++item) 37 38/* 39 * Apply want to each item in list, retaining only the ones for which 40 * the function returns true. If free_util is true, call free() on 41 * the util members of any items that have to be deleted. Preserve 42 * the order of the items that are retained. 43 */ 44voidfilter_string_list(struct string_list *list,int free_util, 45 string_list_each_func_t want,void*cb_data); 46 47/* 48 * Remove any empty strings from the list. If free_util is true, call 49 * free() on the util members of any items that have to be deleted. 50 * Preserve the order of the items that are retained. 51 */ 52voidstring_list_remove_empty_items(struct string_list *list,int free_util); 53 54/* Use these functions only on sorted lists: */ 55intstring_list_has_string(const struct string_list *list,const char*string); 56intstring_list_find_insert_index(const struct string_list *list,const char*string, 57int negative_existing_index); 58struct string_list_item *string_list_insert(struct string_list *list,const char*string); 59struct string_list_item *string_list_insert_at_index(struct string_list *list, 60int insert_at,const char*string); 61struct string_list_item *string_list_lookup(struct string_list *list,const char*string); 62 63/* 64 * Remove all but the first of consecutive entries with the same 65 * string value. If free_util is true, call free() on the util 66 * members of any items that have to be deleted. 67 */ 68voidstring_list_remove_duplicates(struct string_list *sorted_list,int free_util); 69 70 71/* Use these functions only on unsorted lists: */ 72 73/* 74 * Add string to the end of list. If list->strdup_string is set, then 75 * string is copied; otherwise the new string_list_entry refers to the 76 * input string. 77 */ 78struct string_list_item *string_list_append(struct string_list *list,const char*string); 79 80/* 81 * Like string_list_append(), except string is never copied. When 82 * list->strdup_strings is set, this function can be used to hand 83 * ownership of a malloc()ed string to list without making an extra 84 * copy. 85 */ 86struct string_list_item *string_list_append_nodup(struct string_list *list,char*string); 87 88voidsort_string_list(struct string_list *list); 89intunsorted_string_list_has_string(struct string_list *list,const char*string); 90struct string_list_item *unsorted_string_list_lookup(struct string_list *list, 91const char*string); 92 93voidunsorted_string_list_delete_item(struct string_list *list,int i,int free_util); 94 95/* 96 * Split string into substrings on character delim and append the 97 * substrings to list. The input string is not modified. 98 * list->strdup_strings must be set, as new memory needs to be 99 * allocated to hold the substrings. If maxsplit is non-negative, 100 * then split at most maxsplit times. Return the number of substrings 101 * appended to list. 102 * 103 * Examples: 104 * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"] 105 * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"] 106 * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"] 107 * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""] 108 * string_list_split(l, "", ':', -1) -> [""] 109 * string_list_split(l, ":", ':', -1) -> ["", ""] 110 */ 111intstring_list_split(struct string_list *list,const char*string, 112int delim,int maxsplit); 113 114/* 115 * Like string_list_split(), except that string is split in-place: the 116 * delimiter characters in string are overwritten with NULs, and the 117 * new string_list_items point into string (which therefore must not 118 * be modified or freed while the string_list is in use). 119 * list->strdup_strings must *not* be set. 120 */ 121intstring_list_split_in_place(struct string_list *list,char*string, 122int delim,int maxsplit); 123#endif/* STRING_LIST_H */