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 | 24967bd826ad5437a11b1431c8289b1881b52a17 (patch) | |
tree | 2bd20ac84fcba597abc0e492e23ccebb3940b340 /common/ini | |
parent | baf568c235c4f8e6e733c47bc0c76bf84b55b9b0 (diff) | |
download | sssd-24967bd826ad5437a11b1431c8289b1881b52a17.tar.gz sssd-24967bd826ad5437a11b1431c8289b1881b52a17.tar.bz2 sssd-24967bd826ad5437a11b1431c8289b1881b52a17.zip |
Added functions to create list of sections and attributes.
Diffstat (limited to 'common/ini')
-rw-r--r-- | common/ini/ini_config.c | 79 | ||||
-rw-r--r-- | common/ini/ini_config.h | 16 | ||||
-rw-r--r-- | common/ini/ini_config_ut.c | 44 |
3 files changed, 139 insertions, 0 deletions
diff --git a/common/ini/ini_config.c b/common/ini/ini_config.c index b4b4b25f..8d19b272 100644 --- a/common/ini/ini_config.c +++ b/common/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/common/ini/ini_config.h b/common/ini/ini_config.h index 2a9aa52a..95cd5241 100644 --- a/common/ini/ini_config.h +++ b/common/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/common/ini/ini_config_ut.c b/common/ini/ini_config_ut.c index 4968a0ac..28560e54 100644 --- a/common/ini/ini_config_ut.c +++ b/common/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; } |