1 | /*
|
---|
2 | * Copyright (c) 2015 Michal Koutny
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include "conf/configuration.h"
|
---|
30 |
|
---|
31 | #include <assert.h>
|
---|
32 | #include <errno.h>
|
---|
33 | #include <str.h>
|
---|
34 |
|
---|
35 | static int config_load_item(config_item_t *config_item,
|
---|
36 | ini_item_iterator_t *it, void *dst, text_parse_t *parse)
|
---|
37 | {
|
---|
38 | size_t cnt = 0;
|
---|
39 | void *field_dst = (char *)dst + config_item->offset;
|
---|
40 | bool has_error = false;
|
---|
41 |
|
---|
42 | for (; ini_item_iterator_valid(it); ini_item_iterator_inc(it), ++cnt) {
|
---|
43 | const char *string = ini_item_iterator_value(it);
|
---|
44 | const size_t lineno = ini_item_iterator_lineno(it);
|
---|
45 | has_error = has_error ||
|
---|
46 | config_item->parse(string, field_dst, parse, lineno);
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (cnt == 0) {
|
---|
50 | if (config_item->default_value == NULL) {
|
---|
51 | return ENOENT;
|
---|
52 | }
|
---|
53 | bool result = config_item->parse(config_item->default_value,
|
---|
54 | field_dst, parse, 0);
|
---|
55 | /* Default string should be always correct */
|
---|
56 | assert(result);
|
---|
57 | }
|
---|
58 |
|
---|
59 | return has_error ? EINVAL : EOK;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Process INI section as values to a structure
|
---|
63 | *
|
---|
64 | * @param[in] specification Mark-terminated array of config_item_t specifying
|
---|
65 | * available configuration values. The mark is value
|
---|
66 | * whose name is NULL, you can use
|
---|
67 | * CONFIGURATION_ITEM_SENTINEL.
|
---|
68 | * @param[in] section INI section with raw string data
|
---|
69 | * @param[out] dst pointer to structure that holds parsed values
|
---|
70 | * @param[out] parse structure for recording any parsing errors
|
---|
71 | *
|
---|
72 | * @return EOK on success
|
---|
73 | * @return EINVAL on any parsing errors (details in parse structure)
|
---|
74 | */
|
---|
75 | int config_load_ini_section(config_item_t *specification,
|
---|
76 | ini_section_t *section, void *dst, text_parse_t *parse)
|
---|
77 | {
|
---|
78 | bool has_error = false;
|
---|
79 |
|
---|
80 | config_item_t *config_item = specification;
|
---|
81 | while (config_item->name != NULL) {
|
---|
82 | ini_item_iterator_t iterator =
|
---|
83 | ini_section_get_iterator(section, config_item->name);
|
---|
84 |
|
---|
85 | int rc = config_load_item(config_item, &iterator, dst, parse);
|
---|
86 | switch (rc) {
|
---|
87 | case ENOENT:
|
---|
88 | has_error = true;
|
---|
89 | text_parse_raise_error(parse, section->lineno,
|
---|
90 | CONFIGURATION_EMISSING_ITEM);
|
---|
91 | break;
|
---|
92 | case EINVAL:
|
---|
93 | has_error = true;
|
---|
94 | /* Parser should've raised proper errors */
|
---|
95 | break;
|
---|
96 | case EOK:
|
---|
97 | /* empty (nothing to do) */
|
---|
98 | break;
|
---|
99 | default:
|
---|
100 | assert(false);
|
---|
101 | }
|
---|
102 |
|
---|
103 | ++config_item;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return has_error ? EOK : EINVAL;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Parse string (copy) to destination
|
---|
110 | *
|
---|
111 | * @param[out] dst pointer to char * where dedicated copy will be stored
|
---|
112 | *
|
---|
113 | * @return true on success
|
---|
114 | * @return false on error (typically low memory)
|
---|
115 | */
|
---|
116 | bool config_parse_string(const char *string, void *dst, text_parse_t *parse,
|
---|
117 | size_t lineno)
|
---|
118 | {
|
---|
119 | char *my_string = str_dup(string);
|
---|
120 | if (my_string == NULL) {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | char **char_dst = dst;
|
---|
125 | *char_dst = my_string;
|
---|
126 | return true;
|
---|
127 | }
|
---|