The path_list API offers a data structure and functions to handle sorted and unsorted string lists.

The name is a bit misleading, a path_list may store not only paths but strings in general.

The caller:

  1. Allocates and clears a struct path_list variable.

  2. Initializes the members. You might want to set the flag strdup_paths if the strings should be strdup()ed. For example, this is necessary when you add something like git_path("…"), since that function returns a static buffer that will change with the next call to git_path().

    If you need something advanced, you can manually malloc() the items member (you need this if you add things later) and you should set the nr and alloc members in that case, too.

  3. Adds new items to the list, using path_list_append or path_list_insert.

  4. Can check if a string is in the list using path_list_has_path or unsorted_path_list_has_path and get it from the list using path_list_lookup for sorted lists.

  5. Can sort an unsorted list using sort_path_list.

  6. Finally it should free the list using path_list_clear.

Example:

struct path_list list;
int i;

memset(&list, 0, sizeof(struct path_list));
path_list_append("foo", &list);
path_list_append("bar", &list);
for (i = 0; i < list.nr; i++)
        printf("%s\n", list.items[i].path)
Note
It is more efficient to build an unsorted list and sort it afterwards, instead of building a sorted list (O(n log n) instead of O(n^2)).

+ However, if you use the list to check if a certain string was added already, you should not do that (using unsorted_path_list_has_path()), because the complexity would be quadratic again (but with a worse factor).

Functions

Data structures

Represents an item of the list. The path member is a pointer to the string, and you may use the util member for any purpose, if you want.

Represents the list itself.

  1. The array of items are available via the items member.

  2. The nr member contains the number of items stored in the list.

  3. The alloc member is used to avoid reallocating at every insertion. You should not tamper with it.

  4. Setting the strdup_paths member to 1 will strdup() the strings before adding them, see above.