OGS
ConfigTreeUtil.cpp
Go to the documentation of this file.
1
11#include "ConfigTreeUtil.h"
12
13#include <boost/operators.hpp>
14#include <boost/property_tree/xml_parser.hpp>
15#include <ostream>
16#include <utility>
17
18#include "ConfigTree.h"
19#include "Error.h"
20#include "Logging.h"
21
22namespace BaseLib
23{
24ConfigTree makeConfigTree(const std::string& filepath, const bool be_ruthless,
25 const std::string& toplevel_tag,
26 std::stringstream& prj_stream)
27{
29
30 // note: Trimming whitespace and ignoring comments is crucial in order
31 // for our configuration tree implementation to work!
32 try
33 {
34 read_xml(prj_stream, ptree,
35 boost::property_tree::xml_parser::no_comments |
36 boost::property_tree::xml_parser::trim_whitespace);
37 }
38 catch (boost::property_tree::xml_parser_error const& e)
39 {
40 OGS_FATAL("Error while parsing XML file `{:s}' at line {:d}: {:s}.",
41 e.filename(), e.line(), e.message());
42 }
43
44 DBUG("Project configuration from file '{:s}' read.", filepath);
45
46 if (auto opt_child = ptree.get_child_optional(toplevel_tag))
47 {
48 auto const callback =
50
51 return ConfigTree(std::move(*opt_child), filepath, callback, callback);
52 }
53 OGS_FATAL("Tag <{:s}> has not been found in file `{:s}'.", toplevel_tag,
54 filepath);
55}
56
57ConfigTree makeConfigTreeFromFile(const std::string& filepath,
58 const bool be_ruthless,
59 const std::string& toplevel_tag)
60{
61 std::ifstream file(filepath);
62 if (file)
63 {
64 std::stringstream buffer;
65 buffer << file.rdbuf();
66
67 return makeConfigTree(filepath, be_ruthless, toplevel_tag, buffer);
68 }
69 else
70 {
71 OGS_FATAL("Could not read from file {:s}!", filepath);
72 }
73}
74
75} // namespace BaseLib
#define OGS_FATAL(...)
Definition Error.h:26
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:30
static void onerror(std::string const &filename, std::string const &path, std::string const &message)
boost::property_tree::ptree PTree
The tree being wrapped by this class.
Definition ConfigTree.h:255
static void onwarning(std::string const &filename, std::string const &path, std::string const &message)
ConfigTree makeConfigTreeFromFile(const std::string &filepath, const bool be_ruthless, const std::string &toplevel_tag)
ConfigTree makeConfigTree(const std::string &filepath, const bool be_ruthless, const std::string &toplevel_tag, std::stringstream &prj_stream)