1 | /* |
---|
2 | * Copyright (c) 2014, Chris Johns <chrisj@rtems.org> |
---|
3 | * |
---|
4 | * Permission to use, copy, modify, and/or distribute this software for any |
---|
5 | * purpose with or without fee is hereby granted, provided that the above |
---|
6 | * copyright notice and this permission notice appear in all copies. |
---|
7 | * |
---|
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
15 | */ |
---|
16 | /** |
---|
17 | * @file |
---|
18 | * |
---|
19 | * @ingroup rtems_rld |
---|
20 | * |
---|
21 | * @brief INI Configuration reader. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #if !defined (_RLD_CONFIG_H_) |
---|
26 | #define _RLD_CONFIG_H_ |
---|
27 | |
---|
28 | #include <string> |
---|
29 | #include <list> |
---|
30 | #include <vector> |
---|
31 | |
---|
32 | #include <rld.h> |
---|
33 | |
---|
34 | namespace rld |
---|
35 | { |
---|
36 | namespace config |
---|
37 | { |
---|
38 | /** |
---|
39 | * The configuration item. This is a data component of a record contained |
---|
40 | * in a section. |
---|
41 | */ |
---|
42 | struct item |
---|
43 | { |
---|
44 | std::string text; /**< The text as read from the configuration. */ |
---|
45 | |
---|
46 | /** |
---|
47 | * Construct an item. |
---|
48 | */ |
---|
49 | item (const std::string& text); |
---|
50 | item (const char* text); |
---|
51 | }; |
---|
52 | |
---|
53 | /** |
---|
54 | * Configuration item container. |
---|
55 | */ |
---|
56 | typedef std::vector < item > items; |
---|
57 | |
---|
58 | /** |
---|
59 | * Configuration record is a line in a section. There can be multiple |
---|
60 | * records with the same key in a section. Keys are specific to a section. |
---|
61 | */ |
---|
62 | struct record |
---|
63 | { |
---|
64 | std::string name; //< Name of the record. |
---|
65 | items items; //< The record's items. |
---|
66 | |
---|
67 | /** |
---|
68 | * Return true if there is only one item. |
---|
69 | */ |
---|
70 | bool single () const { |
---|
71 | return items.size () == 1; |
---|
72 | } |
---|
73 | }; |
---|
74 | |
---|
75 | /** |
---|
76 | * Configuration record container. |
---|
77 | */ |
---|
78 | typedef std::list < record > records; |
---|
79 | |
---|
80 | /** |
---|
81 | * Configuration section. A section contains a number of records and the |
---|
82 | * records contain [1..n] items. |
---|
83 | */ |
---|
84 | struct section |
---|
85 | { |
---|
86 | std::string name; //< Name of the section. |
---|
87 | records recs; //< The section's records. |
---|
88 | |
---|
89 | /** |
---|
90 | * Find a record and throw an error if not found. |
---|
91 | */ |
---|
92 | const record& get_record (const std::string& name) const; |
---|
93 | }; |
---|
94 | |
---|
95 | /** |
---|
96 | * Configuration section container. |
---|
97 | */ |
---|
98 | typedef std::list < section > sections; |
---|
99 | |
---|
100 | /** |
---|
101 | * Container of configuration file paths loaded. |
---|
102 | */ |
---|
103 | typedef std::vector < std::string > paths; |
---|
104 | |
---|
105 | /** |
---|
106 | * The configuration. |
---|
107 | */ |
---|
108 | class config |
---|
109 | { |
---|
110 | public: |
---|
111 | /** |
---|
112 | * Construct an empty configuration. |
---|
113 | */ |
---|
114 | config(); |
---|
115 | virtual ~config(); |
---|
116 | |
---|
117 | /** |
---|
118 | * Clear the current configuration. |
---|
119 | */ |
---|
120 | void clear (); |
---|
121 | |
---|
122 | /** |
---|
123 | * Load a configuration. |
---|
124 | */ |
---|
125 | void load (const std::string& name); |
---|
126 | |
---|
127 | /** |
---|
128 | * Process any include records in the section named. If the section has |
---|
129 | * any records named 'include' split the items and include the |
---|
130 | * configuration files. |
---|
131 | */ |
---|
132 | void includes (const section& sec, bool must_exist = false); |
---|
133 | |
---|
134 | /** |
---|
135 | * Get the section and throw an error if not found. |
---|
136 | */ |
---|
137 | const section& get_section (const std::string& name) const; |
---|
138 | |
---|
139 | /** |
---|
140 | * Get the paths of loaded configuration files. |
---|
141 | */ |
---|
142 | const paths& get_paths () const; |
---|
143 | |
---|
144 | private: |
---|
145 | |
---|
146 | paths paths; /**< The path's of the loaded files. */ |
---|
147 | sections secs; /**< The sections loaded from configuration files */ |
---|
148 | }; |
---|
149 | |
---|
150 | /** |
---|
151 | * Return the items from a record. |
---|
152 | */ |
---|
153 | template < typename T > |
---|
154 | void parse_items (const rld::config::record& record, T& items) |
---|
155 | { |
---|
156 | items.clear (); |
---|
157 | for (rld::config::items::const_iterator ii = record.items.begin (); |
---|
158 | ii != record.items.end (); |
---|
159 | ++ii) |
---|
160 | { |
---|
161 | rld::strings ss; |
---|
162 | rld::split (ss, (*ii).text, ','); |
---|
163 | std::copy (ss.begin (), ss.end (), std::back_inserter (items)); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Return the items from a record in a section. Optionally raise an error |
---|
169 | * if the record is not found and it is to be present. |
---|
170 | */ |
---|
171 | template < typename T > |
---|
172 | void parse_items (const rld::config::section& section, |
---|
173 | const std::string& name, |
---|
174 | T& items, |
---|
175 | bool present = false) |
---|
176 | { |
---|
177 | items.clear (); |
---|
178 | const rld::config::record* rec = 0; |
---|
179 | try |
---|
180 | { |
---|
181 | const rld::config::record& rr = section.get_record (name); |
---|
182 | rec = &rr; |
---|
183 | } |
---|
184 | catch (rld::error re) |
---|
185 | { |
---|
186 | /* |
---|
187 | * Ignore the error if it does not need to exist. |
---|
188 | */ |
---|
189 | if (present) |
---|
190 | throw rld::error ("not found", "record: " + section.name + name); |
---|
191 | } |
---|
192 | |
---|
193 | if (rec) |
---|
194 | parse_items (*rec, items); |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * Return the items from a record in a section in the |
---|
199 | * configuration. Optionally raise an error if the section is not found and |
---|
200 | * it is to be present. |
---|
201 | */ |
---|
202 | template < typename T > |
---|
203 | void parse_items (const rld::config::config& config, |
---|
204 | const std::string& section, |
---|
205 | const std::string& record, |
---|
206 | T& items, |
---|
207 | bool present = false) |
---|
208 | { |
---|
209 | items.clear (); |
---|
210 | const rld::config::section* sec = 0; |
---|
211 | try |
---|
212 | { |
---|
213 | const rld::config::section& sr = config.get_section (section); |
---|
214 | sec = &sr; |
---|
215 | } |
---|
216 | catch (rld::error re) |
---|
217 | { |
---|
218 | /* |
---|
219 | * Ignore the error if it does not need to exist. |
---|
220 | */ |
---|
221 | if (present) |
---|
222 | throw rld::error ("not found", "section: " + section); |
---|
223 | } |
---|
224 | |
---|
225 | if (sec) |
---|
226 | parse_items (*sec, record, items); |
---|
227 | } |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | #endif |
---|