src/string_list.c

changeset 44
9574a181ec26
parent 36
a7ff583e153f
child 48
0d2c13c24fd0
     1.1 --- a/src/string_list.c	Wed May 22 10:57:17 2013 +0200
     1.2 +++ b/src/string_list.c	Wed May 22 13:00:36 2013 +0200
     1.3 @@ -40,19 +40,28 @@
     1.4  }
     1.5  
     1.6  void destroy_string_list_t(string_list_t* list) {
     1.7 -  if (list->items != NULL) {
     1.8 -    free(list->items);
     1.9 -  }
    1.10 -  free(list);
    1.11 -}
    1.12 -
    1.13 -void add_string(string_list_t* list, char* item) {
    1.14 -  char** reallocated_list =
    1.15 -    realloc(list->items, sizeof(char*) * (list->count + 1));
    1.16 -  if (reallocated_list != NULL) {
    1.17 -    list->items = reallocated_list;
    1.18 -    list->items[list->count] = item;
    1.19 -    list->count++;
    1.20 +  if (list) {
    1.21 +    if (list->items) {
    1.22 +      free(list->items);
    1.23 +    }
    1.24 +    free(list);
    1.25    }
    1.26  }
    1.27  
    1.28 +/* Adds an item to the list, if a NULL-list is specified, the item will
    1.29 + * be freed. This way a NULL-list can be used as garbage bin.
    1.30 + */
    1.31 +void add_string(string_list_t* list, char* item) {
    1.32 +  if (list) {
    1.33 +    char** reallocated_list =
    1.34 +      realloc(list->items, sizeof(char*) * (list->count + 1));
    1.35 +    if (reallocated_list != NULL) {
    1.36 +      list->items = reallocated_list;
    1.37 +      list->items[list->count] = item;
    1.38 +      list->count++;
    1.39 +    }
    1.40 +  } else {
    1.41 +    free(item);
    1.42 +  }
    1.43 +}
    1.44 +

mercurial