makes destructor functions for *_free_content() optional + more documentation for UcxProperties

Wed, 02 May 2018 16:14:40 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 02 May 2018 16:14:40 +0200
changeset 277
f819fe5e20f5
parent 274
0923c036b913
child 278
7b9170c22786

makes destructor functions for *_free_content() optional + more documentation for UcxProperties

docs/src/modules.md file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
src/map.c file | annotate | diff | comparison | revisions
src/ucx/list.h file | annotate | diff | comparison | revisions
src/ucx/map.h file | annotate | diff | comparison | revisions
     1.1 --- a/docs/src/modules.md	Tue Jan 23 19:23:34 2018 +0100
     1.2 +++ b/docs/src/modules.md	Wed May 02 16:14:40 2018 +0200
     1.3 @@ -131,6 +131,38 @@
     1.4  This module provides load and store function for `*.properties` files.
     1.5  The key/value pairs are stored within an UCX Map.
     1.6  
     1.7 +### Example: Loading properties from a file
     1.8 +
     1.9 +```C
    1.10 +// Open the file as usual
    1.11 +FILE* file = fopen("myprops.properties", "r");
    1.12 +if (!file) {
    1.13 +    // error handling
    1.14 +    return 1;
    1.15 +}
    1.16 +
    1.17 +// Load the properties from the file
    1.18 +UcxMap* myprops = ucx_map_new(16);
    1.19 +if (ucx_properties_load(myprops, file)) {
    1.20 +    // error handling
    1.21 +    fclose(file);
    1.22 +    ucx_map_free(myprops);
    1.23 +    return 1;
    1.24 +}
    1.25 +
    1.26 +// Print out the key/value pairs
    1.27 +char* propval;
    1.28 +UcxMapIterator propiter = ucx_map_iterator(myprops);
    1.29 +UCX_MAP_FOREACH(key, propval, propiter) {
    1.30 +    printf("%s = %s\n", (char*)key.data, propval);
    1.31 +}
    1.32 +
    1.33 +// Don't forget to free the values before freeing the map
    1.34 +ucx_map_free_content(myprops, NULL);
    1.35 +ucx_map_free(myprops);
    1.36 +fclose(file);
    1.37 +```
    1.38 +
    1.39  <a name="stack"></a>
    1.40  
    1.41  ## Stack
     2.1 --- a/src/list.c	Tue Jan 23 19:23:34 2018 +0100
     2.2 +++ b/src/list.c	Wed May 02 16:14:40 2018 +0200
     2.3 @@ -77,6 +77,7 @@
     2.4  }
     2.5  
     2.6  void ucx_list_free_content(UcxList* list, ucx_destructor destr) {
     2.7 +    if (!destr) destr = free;
     2.8      while (list != NULL) {
     2.9          destr(list->data);
    2.10          list = list->next;
     3.1 --- a/src/map.c	Tue Jan 23 19:23:34 2018 +0100
     3.2 +++ b/src/map.c	Wed May 02 16:14:40 2018 +0200
     3.3 @@ -86,7 +86,11 @@
     3.4      UcxMapIterator iter = ucx_map_iterator(map);
     3.5      void *val;
     3.6      UCX_MAP_FOREACH(key, val, iter) {
     3.7 -        destr(val);
     3.8 +        if (destr) {
     3.9 +            destr(val);
    3.10 +        } else {
    3.11 +            map->allocator->free(val, NULL);
    3.12 +        }
    3.13      }
    3.14  }
    3.15  
     4.1 --- a/src/ucx/list.h	Tue Jan 23 19:23:34 2018 +0100
     4.2 +++ b/src/ucx/list.h	Wed May 02 16:14:40 2018 +0200
     4.3 @@ -173,9 +173,11 @@
     4.4   * 
     4.5   * Note, that the contents are not usable afterwards and the list should be
     4.6   * destroyed with ucx_list_free().
     4.7 + *
     4.8 + * If no destructor is specified (<code>NULL</code>), stdlib's free() is used.
     4.9   * 
    4.10   * @param list the list for which the contents shall be freed
    4.11 - * @param destr the destructor function (e.g. stdlib free())
    4.12 + * @param destr optional destructor function
    4.13   * @see ucx_list_free()
    4.14   */
    4.15  void ucx_list_free_content(UcxList* list, ucx_destructor destr);
     5.1 --- a/src/ucx/map.h	Tue Jan 23 19:23:34 2018 +0100
     5.2 +++ b/src/ucx/map.h	Wed May 02 16:14:40 2018 +0200
     5.3 @@ -158,7 +158,10 @@
     5.4   * Frees the contents of a hash map.
     5.5   * 
     5.6   * This is a convenience function that iterates over the map and passes all
     5.7 - * values to the specified destructor function (e.g. stdlib free()).
     5.8 + * values to the specified destructor function.
     5.9 + * 
    5.10 + * If no destructor is specified (<code>NULL</code>), the free() function of
    5.11 + * the map's own allocator is used.
    5.12   * 
    5.13   * You must ensure, that it is valid to pass each value in the map to the same
    5.14   * destructor function.
    5.15 @@ -166,7 +169,7 @@
    5.16   * You should free or clear the map afterwards, as the contents will be invalid.
    5.17   * 
    5.18   * @param map for which the contents shall be freed
    5.19 - * @param destr pointer to the destructor function
    5.20 + * @param destr optional pointer to a destructor function
    5.21   * @see ucx_map_free()
    5.22   * @see ucx_map_clear()
    5.23   */

mercurial