Thiago Farina | 0afcb5f | 2010-01-08 17:45:08 -0500 | [diff] [blame] | 1 | #ifndef STRING_LIST_H |
| 2 | #define STRING_LIST_H |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 3 | |
| 4 | struct string_list_item { |
| 5 | char *string; |
| 6 | void *util; |
| 7 | }; |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 8 | |
| 9 | typedef int (*compare_strings_fn)(const char *, const char *); |
| 10 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 11 | struct string_list { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 12 | struct string_list_item *items; |
| 13 | unsigned int nr, alloc; |
| 14 | unsigned int strdup_strings:1; |
Junio C Hamano | 8dd5afc | 2013-01-07 12:24:55 -0800 | [diff] [blame] | 15 | compare_strings_fn cmp; /* NULL uses strcmp() */ |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 16 | }; |
| 17 | |
Tanay Abhra | acb3d22 | 2014-06-03 02:13:18 -0700 | [diff] [blame] | 18 | #define STRING_LIST_INIT_NODUP { NULL, 0, 0, 0, NULL } |
| 19 | #define STRING_LIST_INIT_DUP { NULL, 0, 0, 1, NULL } |
Thiago Farina | 183113a | 2010-07-04 16:46:19 -0300 | [diff] [blame] | 20 | |
Tanay Abhra | 3ed3f5f | 2014-07-18 02:18:59 -0700 | [diff] [blame] | 21 | void string_list_init(struct string_list *list, int strdup_strings); |
| 22 | |
Julian Phillips | cb944f6 | 2010-06-26 00:41:33 +0100 | [diff] [blame] | 23 | void print_string_list(const struct string_list *p, const char *text); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 24 | void string_list_clear(struct string_list *list, int free_util); |
| 25 | |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 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 */ |
| 28 | typedef void (*string_list_clear_func_t)(void *p, const char *str); |
| 29 | void string_list_clear_func(struct string_list *list, string_list_clear_func_t clearfunc); |
| 30 | |
Alex Riesen | 8d31635 | 2010-07-03 14:40:04 +0200 | [diff] [blame] | 31 | /* Use this function or the macro below to iterate over each item */ |
Jay Soffian | c6f5a7a | 2009-02-25 03:32:18 -0500 | [diff] [blame] | 32 | typedef int (*string_list_each_func_t)(struct string_list_item *, void *); |
Julian Phillips | b684e97 | 2010-06-26 00:41:34 +0100 | [diff] [blame] | 33 | int for_each_string_list(struct string_list *list, |
| 34 | string_list_each_func_t, void *cb_data); |
Alex Riesen | 8d31635 | 2010-07-03 14:40:04 +0200 | [diff] [blame] | 35 | #define for_each_string_list_item(item,list) \ |
| 36 | for (item = (list)->items; item < (list)->items + (list)->nr; ++item) |
Jay Soffian | c6f5a7a | 2009-02-25 03:32:18 -0500 | [diff] [blame] | 37 | |
Michael Haggerty | eb5f0c7 | 2012-09-12 16:04:44 +0200 | [diff] [blame] | 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 | */ |
| 44 | void filter_string_list(struct string_list *list, int free_util, |
| 45 | string_list_each_func_t want, void *cb_data); |
| 46 | |
Michael Haggerty | f103f95 | 2012-09-12 16:04:46 +0200 | [diff] [blame] | 47 | /* |
Michael Haggerty | 6bb2a13 | 2012-11-04 08:07:06 +0100 | [diff] [blame] | 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 | */ |
| 52 | void string_list_remove_empty_items(struct string_list *list, int free_util); |
| 53 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 54 | /* Use these functions only on sorted lists: */ |
| 55 | int string_list_has_string(const struct string_list *list, const char *string); |
Marius Storm-Olsen | cfa1ee6 | 2009-02-08 15:34:28 +0100 | [diff] [blame] | 56 | int string_list_find_insert_index(const struct string_list *list, const char *string, |
| 57 | int negative_existing_index); |
Stefan Beller | fc66505 | 2014-11-24 13:22:02 -0800 | [diff] [blame] | 58 | /* |
| 59 | * Inserts the given string into the sorted list. |
| 60 | * If the string already exists, the list is not altered. |
| 61 | * Returns the string_list_item, the string is part of. |
| 62 | */ |
Julian Phillips | 78a395d | 2010-06-26 00:41:35 +0100 | [diff] [blame] | 63 | struct string_list_item *string_list_insert(struct string_list *list, const char *string); |
Stefan Beller | fc66505 | 2014-11-24 13:22:02 -0800 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Checks if the given string is part of a sorted list. If it is part of the list, |
| 67 | * return the coresponding string_list_item, NULL otherwise. |
| 68 | */ |
Julian Phillips | e8c8b71 | 2010-06-26 00:41:37 +0100 | [diff] [blame] | 69 | struct string_list_item *string_list_lookup(struct string_list *list, const char *string); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 70 | |
Michael Haggerty | 31d5451 | 2012-09-12 16:04:45 +0200 | [diff] [blame] | 71 | /* |
| 72 | * Remove all but the first of consecutive entries with the same |
| 73 | * string value. If free_util is true, call free() on the util |
| 74 | * members of any items that have to be deleted. |
| 75 | */ |
| 76 | void string_list_remove_duplicates(struct string_list *sorted_list, int free_util); |
| 77 | |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 78 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 79 | /* Use these functions only on unsorted lists: */ |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Add string to the end of list. If list->strdup_string is set, then |
| 83 | * string is copied; otherwise the new string_list_entry refers to the |
| 84 | * input string. |
| 85 | */ |
Julian Phillips | 1d2f80f | 2010-06-26 00:41:38 +0100 | [diff] [blame] | 86 | struct string_list_item *string_list_append(struct string_list *list, const char *string); |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Like string_list_append(), except string is never copied. When |
| 90 | * list->strdup_strings is set, this function can be used to hand |
| 91 | * ownership of a malloc()ed string to list without making an extra |
| 92 | * copy. |
| 93 | */ |
| 94 | struct string_list_item *string_list_append_nodup(struct string_list *list, char *string); |
| 95 | |
Michael Haggerty | 3383e19 | 2014-11-25 09:02:35 +0100 | [diff] [blame] | 96 | void string_list_sort(struct string_list *list); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 97 | int unsorted_string_list_has_string(struct string_list *list, const char *string); |
Stephen Boyd | e242148 | 2010-03-24 00:16:02 -0700 | [diff] [blame] | 98 | struct string_list_item *unsorted_string_list_lookup(struct string_list *list, |
| 99 | const char *string); |
Michael Haggerty | e448fed | 2012-09-12 16:04:42 +0200 | [diff] [blame] | 100 | |
Johannes Sixt | 86d4b52 | 2011-08-11 23:20:00 -0600 | [diff] [blame] | 101 | void unsorted_string_list_delete_item(struct string_list *list, int i, int free_util); |
Michael Haggerty | ff919f9 | 2012-09-12 16:04:43 +0200 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Split string into substrings on character delim and append the |
| 105 | * substrings to list. The input string is not modified. |
| 106 | * list->strdup_strings must be set, as new memory needs to be |
| 107 | * allocated to hold the substrings. If maxsplit is non-negative, |
| 108 | * then split at most maxsplit times. Return the number of substrings |
| 109 | * appended to list. |
| 110 | * |
| 111 | * Examples: |
| 112 | * string_list_split(l, "foo:bar:baz", ':', -1) -> ["foo", "bar", "baz"] |
| 113 | * string_list_split(l, "foo:bar:baz", ':', 0) -> ["foo:bar:baz"] |
| 114 | * string_list_split(l, "foo:bar:baz", ':', 1) -> ["foo", "bar:baz"] |
| 115 | * string_list_split(l, "foo:bar:", ':', -1) -> ["foo", "bar", ""] |
| 116 | * string_list_split(l, "", ':', -1) -> [""] |
| 117 | * string_list_split(l, ":", ':', -1) -> ["", ""] |
| 118 | */ |
| 119 | int string_list_split(struct string_list *list, const char *string, |
| 120 | int delim, int maxsplit); |
| 121 | |
| 122 | /* |
| 123 | * Like string_list_split(), except that string is split in-place: the |
| 124 | * delimiter characters in string are overwritten with NULs, and the |
| 125 | * new string_list_items point into string (which therefore must not |
| 126 | * be modified or freed while the string_list is in use). |
| 127 | * list->strdup_strings must *not* be set. |
| 128 | */ |
| 129 | int string_list_split_in_place(struct string_list *list, char *string, |
| 130 | int delim, int maxsplit); |
Thiago Farina | 0afcb5f | 2010-01-08 17:45:08 -0500 | [diff] [blame] | 131 | #endif /* STRING_LIST_H */ |