Line data Source code
1 : /*
2 : INI LIBRARY
3 :
4 : Value interpretation functions for single values
5 : and corresponding memory cleanup functions.
6 :
7 : Copyright (C) Dmitri Pal <dpal@redhat.com> 2012
8 :
9 : INI Library is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : INI Library is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with INI Library. If not, see <http://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "config.h"
24 : #include <stdio.h>
25 : #include <errno.h>
26 : #include "trace.h"
27 : #include "collection.h"
28 : #include "collection_tools.h"
29 : #include "ini_configobj.h"
30 : #include "ini_config_priv.h"
31 :
32 :
33 : /* The section array should be freed using this function */
34 2 : void ini_free_section_list(char **section_list)
35 : {
36 : TRACE_FLOW_ENTRY();
37 :
38 2 : col_free_property_list(section_list);
39 :
40 : TRACE_FLOW_EXIT();
41 2 : }
42 :
43 : /* The section array should be freed using this function */
44 1 : void ini_free_attribute_list(char **section_list)
45 : {
46 : TRACE_FLOW_ENTRY();
47 :
48 1 : col_free_property_list(section_list);
49 :
50 : TRACE_FLOW_EXIT();
51 1 : }
52 :
53 :
54 : /* Get list of sections as an array of strings.
55 : * Function allocates memory for the array of the sections.
56 : */
57 2 : char **ini_get_section_list(struct ini_cfgobj *ini_config, int *size, int *error)
58 : {
59 : char **list;
60 :
61 : TRACE_FLOW_ENTRY();
62 :
63 : /* Do we have the configuration object ? */
64 2 : if (ini_config == NULL) {
65 : TRACE_ERROR_NUMBER("Invalid argument.", EINVAL);
66 0 : if (error) *error = EINVAL;
67 : return NULL;
68 : }
69 :
70 : /* Pass it to the function from collection API */
71 2 : list = col_collection_to_list(ini_config->cfg, size, error);
72 :
73 : TRACE_FLOW_STRING("ini_get_section_list returning",
74 : ((list == NULL) ? "NULL" : list[0]));
75 2 : return list;
76 : }
77 :
78 : /* Get list of attributes in a section as an array of strings.
79 : * Function allocates memory for the array of the strings.
80 : */
81 1 : char **ini_get_attribute_list(struct ini_cfgobj *ini_config,
82 : const char *section,
83 : int *size,
84 : int *error)
85 : {
86 1 : struct collection_item *subcollection = NULL;
87 : char **list;
88 : int err;
89 1 : int i = 0;
90 :
91 : TRACE_FLOW_ENTRY();
92 :
93 : /* Do we have the configuration object ? */
94 1 : if (ini_config == NULL) {
95 : TRACE_ERROR_NUMBER("Invalid configuration object argument.", EINVAL);
96 0 : if (error) *error = EINVAL;
97 : return NULL;
98 : }
99 :
100 : /* Do we have the section ? */
101 1 : if (section == NULL) {
102 : TRACE_ERROR_NUMBER("Invalid section argument.", EINVAL);
103 0 : if (error) *error = EINVAL;
104 : return NULL;
105 : }
106 :
107 : /* Fetch section */
108 1 : err = col_get_collection_reference(ini_config->cfg, &subcollection, section);
109 : /* Check error */
110 1 : if (err && (subcollection == NULL)) {
111 : TRACE_ERROR_NUMBER("Failed to get section", err);
112 0 : if (error) *error = EINVAL;
113 : return NULL;
114 : }
115 :
116 : /* Pass it to the function from collection API */
117 1 : list = col_collection_to_list(subcollection, size, error);
118 :
119 1 : col_destroy_collection(subcollection);
120 :
121 : /* Our list of attributes has a special extra attribute - remove it */
122 1 : if ((list != NULL) && (list[0] != NULL)) {
123 1 : free(list[0]);
124 20 : while(list[i + 1] != NULL) {
125 18 : list[i] = list[i + 1];
126 18 : i++;
127 : }
128 1 : list[i] = NULL;
129 : }
130 :
131 1 : if (size) (*size)--;
132 :
133 : TRACE_FLOW_STRING("ini_get_attribute_list returning", ((list == NULL) ? "NULL" : list[0]));
134 1 : return list;
135 : }
|