From 5a5eb2619fed6b37178d3b2c58970788c55cb529 Mon Sep 17 00:00:00 2001 From: Dmitri Pal Date: Thu, 15 Apr 2010 17:27:04 -0400 Subject: Code restructuring Time came to split ini_config.c into many much smaller pieces. 1) ini_parse.c - will have parsing functions 2) ini_get_value.c - will have single value interpretation functions 3) ini_get_array.c - will have array interpretation functions. 4) ini_print.c - error printing 5) ini_defines.h - common constants 6) ini_parse.h header for parsing functions 7) ini_list.c - will have list processing functions --- common/ini/ini_list.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 common/ini/ini_list.c (limited to 'common/ini/ini_list.c') diff --git a/common/ini/ini_list.c b/common/ini/ini_list.c new file mode 100644 index 00000000..95894a15 --- /dev/null +++ b/common/ini/ini_list.c @@ -0,0 +1,123 @@ +/* + INI LIBRARY + + Value interpretation functions for single values + and corresponding memory cleanup functions. + + Copyright (C) Dmitri Pal 2010 + + INI Library is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + INI Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with INI Library. If not, see . +*/ + +#define _GNU_SOURCE +#include +#include +/* +#include +#include +#include +#include +#include +#include +#include +*/ +#include "config.h" +#include "trace.h" +#include "collection.h" +#include "collection_tools.h" +#include "ini_config.h" + + +/* The section array should be freed using this function */ +void free_section_list(char **section_list) +{ + TRACE_FLOW_STRING("free_section_list","Entry"); + + col_free_property_list(section_list); + + TRACE_FLOW_STRING("free_section_list","Exit"); +} + +/* The section array should be freed using this function */ +void free_attribute_list(char **section_list) +{ + TRACE_FLOW_STRING("free_attribute_list","Entry"); + + col_free_property_list(section_list); + + TRACE_FLOW_STRING("free_attribute_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) || + ((col_is_of_class(ini_config, COL_CLASS_INI_CONFIG) == 0) && + (col_is_of_class(ini_config, COL_CLASS_INI_META) == 0))) { + TRACE_ERROR_NUMBER("Invalid argument.", EINVAL); + if (error) *error = EINVAL; + return NULL; + } + + /* Pass it to the function from collection API */ + list = col_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) || + ((col_is_of_class(ini_config, COL_CLASS_INI_CONFIG) == 0) && + (col_is_of_class(ini_config, COL_CLASS_INI_META) == 0)) || + (section == NULL)) { + TRACE_ERROR_NUMBER("Invalid argument.", EINVAL); + if (error) *error = EINVAL; + return NULL; + } + + /* Fetch section */ + err = col_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 = col_collection_to_list(subcollection, size, error); + + col_destroy_collection(subcollection); + + TRACE_FLOW_STRING("get_attribute_list returning", ((list == NULL) ? "NULL" : list[0])); + return list; +} -- cgit