src/string_list.c

changeset 82
44b3332dfe03
parent 81
54515e5d60f5
equal deleted inserted replaced
81:54515e5d60f5 82:44b3332dfe03
44 } 44 }
45 free(list); 45 free(list);
46 } 46 }
47 } 47 }
48 48
49 /* Adds an item to the list, if a NULL-list is specified, the item will
50 * be freed. This way a NULL-list can be used as garbage bin.
51 */
52 void add_string(string_list_t* list, char* item) { 49 void add_string(string_list_t* list, char* item) {
53 if (list) { 50 assert(list != NULL);
54 if (list->count < list->capacity) { 51 if (list->count < list->capacity) {
55 list->items[list->count++] = item;
56 return;
57 }
58 size_t newCapacity = list->capacity * 2;
59 char** reallocated_list =
60 realloc(list->items, sizeof(char*) * newCapacity);
61 assert(reallocated_list != NULL);
62 list->capacity = newCapacity;
63 list->items = reallocated_list;
64 list->items[list->count++] = item; 52 list->items[list->count++] = item;
65 } else { 53 return;
66 free(item);
67 } 54 }
55 size_t newCapacity = list->capacity * 2;
56 char** reallocated_list =
57 realloc(list->items, sizeof(char*) * newCapacity);
58 assert(reallocated_list != NULL);
59 list->capacity = newCapacity;
60 list->items = reallocated_list;
61 list->items[list->count++] = item;
68 } 62 }
69 63

mercurial