summaryrefslogtreecommitdiffstats
path: root/ini/ini_list_valueobj.c
blob: 8df6a1804e8046a72271d8a4fe71f9046bde368f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
    INI LIBRARY

    Value interpretation functions for single values
    and corresponding memory cleanup functions.

    Copyright (C) Dmitri Pal <dpal@redhat.com> 2012

    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 <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include "config.h"
#include "trace.h"
#include "collection.h"
#include "collection_tools.h"
#include "ini_configobj.h"
#include "ini_config_priv.h"


/* The section array should be freed using this function */
void ini_free_section_list(char **section_list)
{
    TRACE_FLOW_ENTRY();

    col_free_property_list(section_list);

    TRACE_FLOW_EXIT();
}

/* The section array should be freed using this function */
void ini_free_attribute_list(char **section_list)
{
    TRACE_FLOW_ENTRY();

    col_free_property_list(section_list);

    TRACE_FLOW_EXIT();
}


/* Get list of sections as an array of strings.
 * Function allocates memory for the array of the sections.
 */
char **ini_get_section_list(struct ini_cfgobj *ini_config, int *size, int *error)
{
    char **list;

    TRACE_FLOW_ENTRY();

    /* Do we have the configuration object ? */
    if (ini_config == NULL) {
        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->cfg, size, error);

    TRACE_FLOW_STRING("ini_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 **ini_get_attribute_list(struct ini_cfgobj *ini_config,
                              const char *section,
                              int *size,
                              int *error)
{
    struct collection_item *subcollection = NULL;
    char **list;
    int err;
    int i = 0;

    TRACE_FLOW_ENTRY();

    /* Do we have the configuration object ? */
    if (ini_config == NULL) {
        TRACE_ERROR_NUMBER("Invalid configuration object argument.", EINVAL);
        if (error) *error = EINVAL;
        return NULL;
    }

    /* Do we have the section ? */
    if (section == NULL) {
        TRACE_ERROR_NUMBER("Invalid section argument.", EINVAL);
        if (error) *error = EINVAL;
        return NULL;
    }

    /* Fetch section */
    err = col_get_collection_reference(ini_config->cfg, &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);

    /* Our list of attributes has a special extra attribute - remove it */
    if ((list != NULL) && (list[0] != NULL)) {
        free(list[0]);
        while(list[i + 1] != NULL) {
            list[i] = list[i + 1];
            i++;
        }
        list[i] = NULL;
    }

    if (size) (*size)--;

    TRACE_FLOW_STRING("ini_get_attribute_list returning", ((list == NULL) ? "NULL" : list[0]));
    return list;
}