/* * Data structure for dynamic string lists that works * similar to the stralloc library. * Return values: 1 if successful, 0 in case of failures. * afb 4/2003 */ #include #include "strlist.h" /* assure that there is at least room for len list entries */ int strlist_ready(strlist* list, unsigned int len) { if (list->allocated < len) { unsigned int wanted = len + (len>>3) + 8; char** newlist = (char**) realloc(list->list, sizeof(char*) * wanted); if (newlist == 0) return 0; list->list = newlist; list->allocated = wanted; } return 1; } /* assure that there is room for len additional list entries */ int strlist_readyplus(strlist* list, unsigned int len) { return strlist_ready(list, list->len + len); } /* truncate the list to zero length */ int strlist_clear(strlist* list) { list->len = 0; return 1; } /* append the string pointer to the list */ int strlist_push(strlist* list, char* string) { if (!strlist_ready(list, list->len + 1)) return 0; list->list[list->len++] = string; return 1; } /* free the strlist data structure but not the strings */ int strlist_free(strlist* list) { free(list->list); list->list = 0; list->allocated = 0; list->len = 0; return 1; }