2 Copyright (c) 2014, Intel Corporation
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
8 * Redistributions of source code must retain the above copyright
9 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
13 the documentation and/or other materials provided with the
16 * Neither the name of Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
24 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <murphy/common.h>
35 #include "ini-parser.h"
38 int initialize_parser_buffer(ini_parser_context_t *ctx)
40 /* buffers points to the array of buffers. End points to the last
41 * character position in the buffer, and current points to the
42 * buffer position that will be written next. */
46 for (i = 0; i < 10; i++) {
47 ctx->buffers[i] = NULL;
50 ctx->buffers[0] = mrp_alloc(CHARACTER_BUFFER_SIZE * sizeof(char));
51 if (!ctx->buffers[0]) {
55 ctx->end = ctx->buffers[0] + CHARACTER_BUFFER_SIZE-1;
56 ctx->current = ctx->buffers[0];
65 char *new_parser_str(ini_parser_context_t *ctx, char *input)
67 int len = strlen(input);
70 if (ctx->buffers[ctx->buffer_i] == NULL) {
74 /* newend points to the last character position required for the
77 newend = ctx->current + len;
79 if (newend > ctx->end) {
80 /* OOM, let's allocate a new buffer */
81 if (++(ctx->buffer_i) >= MAX_BUFFERS) {
85 ctx->buffers[ctx->buffer_i] = mrp_alloc(CHARACTER_BUFFER_SIZE * sizeof(char));
86 if (ctx->buffers[ctx->buffer_i] == NULL) {
89 ctx->end = ctx->buffers[ctx->buffer_i] + CHARACTER_BUFFER_SIZE-1;
90 ctx->current = ctx->buffers[ctx->buffer_i];
92 newend = ctx->current + len;
94 if (newend > ctx->end) {
95 /* input is longer than the MAX buffer size */
100 strncpy(ctx->current, input, len);
104 ctx->current = newend + 1;
110 void delete_parser_buffer(ini_parser_context_t *ctx)
114 for (i = 0; i < MAX_BUFFERS; i++) {
115 mrp_free(ctx->buffers[i]);
116 ctx->buffers[i] = NULL;
124 void init_result(ini_parser_result_t *result)
126 /* printf("> init_result (%p)\n", result); */
127 mrp_list_init(&result->sections);
128 result->current = NULL;
132 void add_section(ini_parser_result_t *result, const char *name)
134 ini_parser_section_t *section;
139 section = mrp_allocz(sizeof(ini_parser_section_t));
143 section->name = strdup(name);
147 mrp_list_init(§ion->pairs);
148 mrp_list_init(§ion->hook);
149 mrp_list_append(&result->sections, §ion->hook);
151 result->current = section;
156 mrp_log_error("add_section ERROR (%s)", name);
161 void add_key(ini_parser_result_t *result, const char *key)
163 ini_parser_section_t *section;
164 ini_parser_keyvaluepair_t *pair;
169 section = result->current;
171 if (!section || section->current) {
172 mrp_log_error("add_key section error %p", section);
176 pair = mrp_allocz(sizeof(ini_parser_keyvaluepair_t));
180 mrp_list_init(&pair->hook);
181 mrp_list_append(§ion->pairs, &pair->hook);
183 pair->key = mrp_strdup(key);
187 section->current = pair;
192 mrp_log_error("add_key ERROR (%s)", key);
197 void add_modifier(ini_parser_result_t *result, const char *modifier)
199 ini_parser_section_t *section;
200 ini_parser_keyvaluepair_t *pair;
202 /* printf("> add_modifier (%s)\n", modifier); */
207 section = result->current;
211 pair = section->current;
215 pair->modifier = mrp_strdup(modifier);
222 mrp_log_error("add_modifier ERROR (%s)", modifier);
227 void add_value(ini_parser_result_t *result, const char *value)
229 ini_parser_section_t *section;
230 ini_parser_keyvaluepair_t *pair;
232 /* printf("> add_value (%s)\n", value); */
237 section = result->current;
241 pair = section->current;
245 pair->value = mrp_strdup(value);
249 section->current = NULL;
254 mrp_log_error("add_value ERROR (%s)", value);
259 void deinit_result(ini_parser_result_t *result)
261 mrp_list_hook_t *sp, *sn, *kp, *kn;
266 mrp_list_foreach(&result->sections, sp, sn) {
267 ini_parser_section_t *section;
269 section = mrp_list_entry(sp, typeof(*section), hook);
270 mrp_list_delete(§ion->hook);
272 mrp_list_foreach(§ion->pairs, kp, kn) {
273 ini_parser_keyvaluepair_t *pair;
275 pair = mrp_list_entry(kp, typeof(*pair), hook);
276 mrp_list_delete(&pair->hook);
279 mrp_free(pair->modifier);
280 mrp_free(pair->value);
284 mrp_free(section->name);
292 static void print_pair(ini_parser_keyvaluepair_t *pair)
294 mrp_debug("%s[%s]=%s", pair->key, pair->modifier ? pair->modifier : "",
299 void print_result(ini_parser_result_t *result)
301 mrp_list_hook_t *sp, *sn, *kp, *kn;
306 mrp_list_foreach(&result->sections, sp, sn) {
308 ini_parser_section_t *section;
310 section = mrp_list_entry(sp, typeof(*section), hook);
312 mrp_debug("[%s]", section->name);
314 mrp_list_foreach(§ion->pairs, kp, kn) {
315 ini_parser_keyvaluepair_t *pair;
317 pair = mrp_list_entry(kp, typeof(*pair), hook);