2 * Copyright (C) 2008 Martin Willi
3 * Copyright (C) 2008 Thomas Kallenberg
4 * Hochschule fuer Technik Rapperswil
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 #include "uci_parser.h"
26 typedef struct private_uci_parser_t private_uci_parser_t
;
29 * Private data of an uci_parser_t object
31 struct private_uci_parser_t
{
39 * UCI package name this parser reads
45 * enumerator implementation create_section_enumerator
48 /** implements enumerator */
50 /** currently enumerated uci section */
51 struct uci_element
*current
;
52 /** all uci ipsec config sections */
53 struct uci_list
*list
;
55 struct uci_context
*ctx
;
56 /** ipsec uci package */
57 struct uci_package
*package
;
58 /** NULL terminated list of keywords */
60 } section_enumerator_t
;
63 * Implementation of section_enumerator_t.enumerate
65 static bool section_enumerator_enumerate(section_enumerator_t
*this, ...)
67 struct uci_element
*element
;
72 if (&this->current
->list
== this->list
)
79 /* name is first parameter */
80 value
= va_arg(args
, char**);
83 *value
= uci_to_section(this->current
)->type
;
86 /* followed by keyword parameters */
87 for (i
= 0; this->keywords
[i
]; i
++)
89 value
= va_arg(args
, char**);
90 if (value
&& uci_lookup(this->ctx
, &element
, this->package
,
91 this->current
->name
, this->keywords
[i
]) == UCI_OK
)
93 *value
= uci_to_option(element
)->value
;
98 this->current
= list_to_element(this->current
->list
.next
);
103 * Implementation of section_enumerator_t.public.destroy
105 static void section_enumerator_destroy(section_enumerator_t
*this)
107 uci_free_context(this->ctx
);
112 * Implementation of backend_t.create_section_enumerator.
114 static enumerator_t
* create_section_enumerator(private_uci_parser_t
*this, ...)
116 section_enumerator_t
*e
;
120 /* allocate enumerator large enought to hold keyword pointers */
122 va_start(args
, this);
123 while (va_arg(args
, char*))
128 e
= malloc(sizeof(section_enumerator_t
) + sizeof(char*) * i
);
130 va_start(args
, this);
133 e
->keywords
[i
] = va_arg(args
, char*);
135 while (e
->keywords
[i
++]);
138 e
->public.enumerate
= (void*)section_enumerator_enumerate
;
139 e
->public.destroy
= (void*)section_enumerator_destroy
;
141 /* load uci context */
142 e
->ctx
= uci_alloc_context();
143 if (uci_load(e
->ctx
, this->package
, &e
->package
) != UCI_OK
)
145 section_enumerator_destroy(e
);
148 e
->list
= &e
->package
->sections
;
149 e
->current
= list_to_element(e
->list
->next
);
150 if (e
->current
->type
!= UCI_TYPE_SECTION
)
152 section_enumerator_destroy(e
);
159 * Implementation of uci_parser_t.destroy.
161 static void destroy(private_uci_parser_t
*this)
168 * Described in header.
170 uci_parser_t
*uci_parser_create(char *package
)
172 private_uci_parser_t
*this = malloc_thing(private_uci_parser_t
);
174 this->public.create_section_enumerator
= (enumerator_t
*(*)(uci_parser_t
*, ...))create_section_enumerator
;
175 this->public.destroy
= (void(*)(uci_parser_t
*))destroy
;
177 this->package
= strdup(package
);
179 return &this->public;