diff options
author | Dmitri Pal <dpal@redhat.com> | 2009-04-10 11:30:59 -0400 |
---|---|---|
committer | Simo Sorce <ssorce@redhat.com> | 2009-04-10 16:59:24 -0400 |
commit | 15221b649ccff0a7b70592b604c8bdef905afd76 (patch) | |
tree | f7b5ee74fba4bfcef7db19220be5e6fbe3583b6b /ini | |
parent | 7a4629f520bb3da6e349909f90489cac16745b0e (diff) | |
download | ding-libs-15221b649ccff0a7b70592b604c8bdef905afd76.tar.gz ding-libs-15221b649ccff0a7b70592b604c8bdef905afd76.tar.xz ding-libs-15221b649ccff0a7b70592b604c8bdef905afd76.zip |
Added functions to create list of sections and attributes.
Diffstat (limited to 'ini')
-rw-r--r-- | ini/ini_config.c | 79 | ||||
-rw-r--r-- | ini/ini_config.h | 16 | ||||
-rw-r--r-- | ini/ini_config_ut.c | 44 |
3 files changed, 139 insertions, 0 deletions
diff --git a/ini/ini_config.c b/ini/ini_config.c index b4b4b25..8d19b27 100644 --- a/ini/ini_config.c +++ b/ini/ini_config.c @@ -1443,3 +1443,82 @@ inline void free_double_config_array(double *array) { if (array != NULL) free(array); } + +/* The section array should be freed using this function */ +inline void free_section_list(char **section_list) +{ + TRACE_FLOW_STRING("free_section_list","Entry"); + + free_property_list(section_list); + + TRACE_FLOW_STRING("free_section_list","Exit"); +} + +/* The section array should be freed using this function */ +inline void free_attribute_list(char **section_list) +{ + TRACE_FLOW_STRING("free_section_list","Entry"); + + free_property_list(section_list); + + TRACE_FLOW_STRING("free_section_list","Exit"); +} + + +/* Get list of sections as an array of strings. + * Function allocates memory for the array of the sections. + */ +char **get_section_list(struct collection_item *ini_config, int *size, int *error) +{ + char **list; + + TRACE_FLOW_STRING("get_section_list","Entry"); + /* Do we have the item ? */ + if ((ini_config == NULL) || + !is_of_class(ini_config, COL_CLASS_INI_CONFIG)) { + TRACE_ERROR_NUMBER("Invalid argument.", EINVAL); + if (error) *error = EINVAL; + return NULL; + } + + /* Pass it to the function from collection API */ + list = collection_to_list(ini_config, size, error); + + TRACE_FLOW_STRING("get_section_list returning", list == NULL ? "NULL" : list[0]); + return list; +} + +/* Get list of attributes in a section as an array of strings. + * Function allocates memory for the array of the strings. + */ +char **get_attribute_list(struct collection_item *ini_config, const char *section, int *size, int *error) +{ + struct collection_item *subcollection = NULL; + char **list; + int err; + + TRACE_FLOW_STRING("get_attribute_list","Entry"); + /* Do we have the item ? */ + if ((ini_config == NULL) || + !is_of_class(ini_config, COL_CLASS_INI_CONFIG) || + (section == NULL)) { + TRACE_ERROR_NUMBER("Invalid argument.", EINVAL); + if (error) *error = EINVAL; + return NULL; + } + + /* Fetch section */ + err = get_collection_reference(ini_config, &subcollection, section); + /* Check error */ + if (err && (subcollection == NULL)) { + TRACE_ERROR_NUMBER("Failed to get section", err); + if (error) *error = EINVAL; + return NULL; + } + + /* Pass it to the function from collection API */ + list = collection_to_list(subcollection, size, error); + + TRACE_FLOW_STRING("get_attribute_list returning", list == NULL ? "NULL" : list[0]); + return list; +} diff --git a/ini/ini_config.h b/ini/ini_config.h index 2a9aa52..95cd524 100644 --- a/ini/ini_config.h +++ b/ini/ini_config.h @@ -96,6 +96,22 @@ void print_file_parsing_errors(FILE *file, /* File to void print_config_parsing_errors(FILE *file, /* File to send errors to */ struct collection_item *error_list); /* Collection of collections of errors */ +/* Get list of sections from the config collection as an array of strings. + * Function allocates memory for the array of the sections. + */ +char **get_section_list(struct collection_item *ini_config, int *size, int *error); + +/* The section array should be freed using this function */ +void free_section_list(char **section_list); + +/* Get list of attributes in a section as an array of strings. + * Function allocates memory for the array of attributes. + */ +char **get_attribute_list(struct collection_item *ini_config, const char *section, int *size, int *error); + +/* The attribute array should be freed using this function */ +void free_attribute_list(char **attr_list); + /* Get a configuration item form the configuration */ int get_config_item(const char *section, /* Section. If NULL assumed default */ const char *name, /* Name of the property to look up */ diff --git a/ini/ini_config_ut.c b/ini/ini_config_ut.c index 4968a0a..28560e5 100644 --- a/ini/ini_config_ut.c +++ b/ini/ini_config_ut.c @@ -227,6 +227,7 @@ int get_test() int size; long *array; double *darray; + char **prop_array; printf("\n\n===== GET TEST START ======\n"); printf("Reading collection\n"); @@ -712,6 +713,49 @@ int get_test() free_double_config_array(darray); + printf("\n\nSection list - no size\n"); + + /* Do not care about the error or size */ + prop_array = get_section_list(ini_config,NULL,NULL); + if (prop_array == NULL) { + printf("Expect success got error.\n"); + destroy_collection(ini_config); + return -1; + } + + i = 0; + while (prop_array[i]) { + printf("Section: [%s]\n", prop_array[i]); + i++; + } + free_section_list(prop_array); + + printf("\n\nSection list - with size\n"); + + /* Do not care about the error or size */ + prop_array = get_section_list(ini_config, &size, NULL); + if (prop_array == NULL) { + printf("Expect success got error.\n"); + destroy_collection(ini_config); + return -1; + } + + for (i=0;i<size;i++) printf("Section: [%s]\n", prop_array[i]); + free_section_list(prop_array); + + printf("\n\nAttributes in the section - with size and error\n"); + + /* Do not care about the error or size */ + prop_array = get_attribute_list(ini_config, "domains/EXAMPLE.COM", &size, &error); + if (prop_array == NULL) { + printf("Expect success got error.\n"); + destroy_collection(ini_config); + return -1; + } + + for (i=0;i<size;i++) printf("Section: [%s]\n", prop_array[i]); + free_attribute_list(prop_array); + printf("Done with get test!\n"); return EOK; } |