/*------------------------------------------------------------------------------ string_list.c - Utilities for managing a variable length string list type Create the list with new_string_list(). When you are finished with the list, you must deallocate it with delete_string_list(). Other functions are: append_string_to_list() - Append a string to the string list Version 1.0 2015-01-30 Brian B. McGuinness ------------------------------------------------------------------------------*/ #include #include "string_list.h" #define INITIAL_CAPACITY 16 /*------------------------------------------------------------------------------ append_string_to_list - Append a string to the string list ------------------------------------------------------------------------------*/ int append_string_to_list (string_list **strlist, string *str) { if ((*strlist)->length + 1 >= (*strlist)->capacity) { int newsize = sizeof (string_list) + sizeof (string *) * ((*strlist)->capacity << 1); string_list *new = realloc (*strlist, newsize); if (new == NULL) return FALSE; *strlist = new; (*strlist)->capacity <<= 1; } (*strlist)->list[(*strlist)->length++] = clone_string (str); return TRUE; } /*------------------------------------------------------------------------------ delete_string_list - Delete a string list ------------------------------------------------------------------------------*/ void delete_string_list (string_list *strlist) { if (strlist != NULL) { for (int i = 0; i < strlist->length; i++) delete_string (strlist->list[i]); free (strlist); } } /*------------------------------------------------------------------------------ new_string_list - Create a new string list The string list must be deallocated with delete_string_list() when you are finished with it. ------------------------------------------------------------------------------*/ string_list *new_string_list () { string_list *result = malloc (sizeof (string_list) + sizeof (string *) * INITIAL_CAPACITY); if (result != NULL) { result->capacity = INITIAL_CAPACITY; result->length = 0; } return result; }