Cleanup: use some make_shared.
[libcxml.git] / src / cxml.cc
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libcxml.
5
6     libcxml is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libcxml is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libcxml.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "cxml.h"
22 #include <libxml++/libxml++.h>
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <cstdio>
26
27
28 using std::make_shared;
29 using std::shared_ptr;
30 using std::string;
31 using std::vector;
32 using boost::optional;
33
34
35 cxml::Node::Node ()
36         : _node (nullptr)
37 {
38
39 }
40
41 cxml::Node::Node (xmlpp::Node* node)
42         : _node (node)
43 {
44
45 }
46
47 string
48 cxml::Node::name () const
49 {
50         if (!_node) {
51                 throw Error ("No node to read name from");
52         }
53         return _node->get_name ();
54 }
55
56 shared_ptr<cxml::Node>
57 cxml::Node::node_child (string name) const
58 {
59         auto const n = node_children (name);
60         if (n.size() > 1) {
61                 throw cxml::Error ("duplicate XML tag " + name);
62         } else if (n.empty ()) {
63                 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
64         }
65
66         return n.front ();
67 }
68
69 shared_ptr<cxml::Node>
70 cxml::Node::optional_node_child (string name) const
71 {
72         auto const n = node_children (name);
73         if (n.size() > 1) {
74                 throw cxml::Error ("duplicate XML tag " + name);
75         } else if (n.empty ()) {
76                 return {};
77         }
78
79         return n.front ();
80 }
81
82 vector<shared_ptr<cxml::Node>>
83 cxml::Node::node_children () const
84 {
85         if (!_node) {
86                 throw Error ("No node to read children from");
87         }
88
89         vector<shared_ptr<cxml::Node>> n;
90         for (auto i: _node->get_children()) {
91                 n.push_back(make_shared<Node>(i));
92         }
93
94         return n;
95 }
96
97 vector<shared_ptr<cxml::Node>>
98 cxml::Node::node_children (string name) const
99 {
100         /* XXX: using find / get_path should work here, but I can't follow
101            how get_path works.
102         */
103
104         if (!_node) {
105                 throw cxml::Error("Node has no internal xmlpp node; did you forget to call a read method on cxml::Document?");
106         }
107
108         vector<shared_ptr<cxml::Node>> n;
109         for (auto i: _node->get_children()) {
110                 if (i->get_name() == name) {
111                         n.push_back(make_shared<Node>(i));
112                 }
113         }
114
115         _taken.push_back (name);
116         return n;
117 }
118
119 string
120 cxml::Node::string_child (string c) const
121 {
122         return node_child(c)->content ();
123 }
124
125 optional<string>
126 cxml::Node::optional_string_child (string c) const
127 {
128         auto const nodes = node_children (c);
129         if (nodes.size() > 1) {
130                 throw cxml::Error ("duplicate XML tag " + c);
131         }
132
133         if (nodes.empty ()) {
134                 return {};
135         }
136
137         return nodes.front()->content();
138 }
139
140 bool
141 cxml::Node::bool_child (string c) const
142 {
143         auto const s = string_child (c);
144         return (s == "1" || s == "yes" || s == "True");
145 }
146
147 optional<bool>
148 cxml::Node::optional_bool_child (string c) const
149 {
150         auto const s = optional_string_child (c);
151         if (!s) {
152                 return {};
153         }
154
155         return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
156 }
157
158 void
159 cxml::Node::ignore_child (string name) const
160 {
161         _taken.push_back (name);
162 }
163
164 string
165 cxml::Node::string_attribute (string name) const
166 {
167         auto e = dynamic_cast<const xmlpp::Element *> (_node);
168         if (!e) {
169                 throw cxml::Error ("missing attribute " + name);
170         }
171
172         auto a = e->get_attribute (name);
173         if (!a) {
174                 throw cxml::Error ("missing attribute " + name);
175         }
176
177         return a->get_value ();
178 }
179
180 optional<string>
181 cxml::Node::optional_string_attribute (string name) const
182 {
183         auto e = dynamic_cast<const xmlpp::Element *> (_node);
184         if (!e) {
185                 return {};
186         }
187
188         auto a = e->get_attribute (name);
189         if (!a) {
190                 return {};
191         }
192
193         return string (a->get_value ());
194 }
195
196 bool
197 cxml::Node::bool_attribute (string name) const
198 {
199         auto const s = string_attribute (name);
200         return (s == "1" || s == "yes");
201 }
202
203 optional<bool>
204 cxml::Node::optional_bool_attribute (string name) const
205 {
206         auto s = optional_string_attribute (name);
207         if (!s) {
208                 return {};
209         }
210
211         return (s.get() == "1" || s.get() == "yes");
212 }
213
214 void
215 cxml::Node::done () const
216 {
217         for (auto i: _node->get_children()) {
218                 if (dynamic_cast<xmlpp::Element *> (i) && find (_taken.begin(), _taken.end(), i->get_name()) == _taken.end ()) {
219                         throw cxml::Error ("unexpected XML node " + i->get_name());
220                 }
221         }
222 }
223
224 string
225 cxml::Node::content () const
226 {
227         string content;
228
229         for (auto i: _node->get_children()) {
230                 auto v = dynamic_cast<xmlpp::ContentNode const *> (i);
231                 if (v && dynamic_cast<xmlpp::TextNode const *>(v)) {
232                         content += v->get_content ();
233                 }
234         }
235
236         return content;
237 }
238
239 string
240 cxml::Node::namespace_uri () const
241 {
242         return _node->get_namespace_uri ();
243 }
244
245 string
246 cxml::Node::namespace_prefix () const
247 {
248         return _node->get_namespace_prefix ();
249 }
250
251 cxml::Document::Document (string root_name)
252         : _root_name (root_name)
253 {
254         _parser = new xmlpp::DomParser;
255 }
256
257 cxml::Document::Document (string root_name, boost::filesystem::path file)
258         : _root_name (root_name)
259 {
260         _parser = new xmlpp::DomParser ();
261         read_file (file);
262 }
263
264 cxml::Document::Document ()
265 {
266         _parser = new xmlpp::DomParser ();
267 }
268
269 cxml::Document::~Document ()
270 {
271         delete _parser;
272 }
273
274 void
275 cxml::Document::read_file (boost::filesystem::path file)
276 {
277         if (!boost::filesystem::exists (file)) {
278                 throw cxml::Error ("XML file " + file.string() + " does not exist");
279         }
280
281         _parser->parse_file (file.string ());
282         take_root_node ();
283 }
284
285 void
286 cxml::Document::read_string (string s)
287 {
288         _parser->parse_memory (s);
289         take_root_node ();
290 }
291
292 void
293 cxml::Document::take_root_node ()
294 {
295         if (!_parser) {
296                 throw cxml::Error ("could not parse XML");
297         }
298
299         _node = _parser->get_document()->get_root_node ();
300         if (!_root_name.empty() && _node->get_name() != _root_name) {
301                 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
302         } else if (_root_name.empty ()) {
303                 _root_name = _node->get_name ();
304         }
305 }
306
307 static
308 string
309 make_local (string v)
310 {
311         auto lc = localeconv ();
312         boost::algorithm::replace_all (v, ".", lc->decimal_point);
313         /* We hope it's ok not to add in thousands separators here */
314         return v;
315 }
316
317 template <typename P, typename Q>
318 P
319 locale_convert (Q x)
320 {
321         /* We can't write a generic version of locale_convert; all required
322            versions must be specialised.
323         */
324         BOOST_STATIC_ASSERT (sizeof(Q) == 0);
325 }
326
327 template<>
328 int
329 locale_convert (string x)
330 {
331         int y = 0;
332         sscanf (x.c_str(), "%d", &y);
333         return y;
334 }
335
336 template<>
337 unsigned int
338 locale_convert (string x)
339 {
340         unsigned int y = 0;
341         sscanf (x.c_str(), "%u", &y);
342         return y;
343 }
344
345 template<>
346 long int
347 locale_convert (string x)
348 {
349         long int y = 0;
350         sscanf (x.c_str(), "%ld", &y);
351         return y;
352 }
353
354 template<>
355 long unsigned int
356 locale_convert (string x)
357 {
358         long unsigned int y = 0;
359 #ifdef LIBCXML_WINDOWS
360         __mingw_sscanf (x.c_str(), "%lud", &y);
361 #else
362         sscanf (x.c_str(), "%lud", &y);
363 #endif
364         return y;
365 }
366
367 template<>
368 long long
369 locale_convert (string x)
370 {
371         long long y = 0;
372 #ifdef LIBCXML_WINDOWS
373         __mingw_sscanf (x.c_str(), "%lld", &y);
374 #else
375         sscanf (x.c_str(), "%lld", &y);
376 #endif
377         return y;
378 }
379
380 template<>
381 long long unsigned
382 locale_convert (string x)
383 {
384         long long unsigned y = 0;
385 #ifdef LIBCXML_WINDOWS
386         __mingw_sscanf (x.c_str(), "%llud", &y);
387 #else
388         sscanf (x.c_str(), "%llud", &y);
389 #endif
390         return y;
391 }
392
393 template<>
394 float
395 locale_convert (string x)
396 {
397         float y = 0;
398         sscanf (x.c_str(), "%f", &y);
399         return y;
400 }
401
402 template <>
403 double
404 locale_convert (string x)
405 {
406         double y = 0;
407         sscanf (x.c_str(), "%lf", &y);
408         return y;
409 }
410
411 template <>
412 int
413 cxml::raw_convert (string v)
414 {
415         return locale_convert<int> (make_local(v));
416 }
417
418 template <>
419 unsigned int
420 cxml::raw_convert (string v)
421 {
422         return locale_convert<unsigned int> (make_local(v));
423 }
424
425 template <>
426 long int
427 cxml::raw_convert (string v)
428 {
429         return locale_convert<long int> (make_local(v));
430 }
431
432 template <>
433 long unsigned int
434 cxml::raw_convert (string v)
435 {
436         return locale_convert<long unsigned int> (make_local(v));
437 }
438
439 template <>
440 long long
441 cxml::raw_convert (string v)
442 {
443         return locale_convert<long long> (make_local(v));
444 }
445
446 template <>
447 long long unsigned
448 cxml::raw_convert (string v)
449 {
450         return locale_convert<long long unsigned> (make_local(v));
451 }
452
453 template <>
454 float
455 cxml::raw_convert (string v)
456 {
457         return locale_convert<float> (make_local(v));
458 }
459
460 template <>
461 double
462 cxml::raw_convert (string v)
463 {
464         return locale_convert<double> (make_local(v));
465 }