OGS
ConfigTreeUtil.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) OpenGeoSys Community (opengeosys.org)
2// SPDX-License-Identifier: BSD-3-Clause
3
4#include "ConfigTreeUtil.h"
5
6#include <boost/operators.hpp>
7#include <boost/property_tree/xml_parser.hpp>
8#include <ostream>
9#include <utility>
10
11#include "ConfigTree.h"
12#include "Error.h"
13#include "Logging.h"
14
15namespace BaseLib
16{
17ConfigTree makeConfigTree(const std::string& filepath, const bool be_ruthless,
18 const std::string& toplevel_tag,
19 std::stringstream& prj_stream)
20{
22
23 // note: Trimming whitespace and ignoring comments is crucial in order
24 // for our configuration tree implementation to work!
25 try
26 {
27 read_xml(prj_stream, ptree,
28 boost::property_tree::xml_parser::no_comments |
29 boost::property_tree::xml_parser::trim_whitespace);
30 }
31 catch (boost::property_tree::xml_parser_error const& e)
32 {
33 OGS_FATAL("Error while parsing XML file `{:s}' at line {:d}: {:s}.",
34 e.filename(), e.line(), e.message());
35 }
36
37 DBUG("Project configuration from file '{:s}' read.", filepath);
38
39 if (auto opt_child = ptree.get_child_optional(toplevel_tag))
40 {
41 auto const callback =
43
44 return ConfigTree(std::move(*opt_child), filepath, callback, callback);
45 }
46 OGS_FATAL("Tag <{:s}> has not been found in file `{:s}'.", toplevel_tag,
47 filepath);
48}
49
50ConfigTree makeConfigTreeFromFile(const std::string& filepath,
51 const bool be_ruthless,
52 const std::string& toplevel_tag)
53{
54 std::ifstream file(filepath);
55 if (file)
56 {
57 std::stringstream buffer;
58 buffer << file.rdbuf();
59
60 return makeConfigTree(filepath, be_ruthless, toplevel_tag, buffer);
61 }
62 else
63 {
64 OGS_FATAL("Could not read from file {:s}!", filepath);
65 }
66}
67
68} // namespace BaseLib
#define OGS_FATAL(...)
Definition Error.h:19
void DBUG(fmt::format_string< Args... > fmt, Args &&... args)
Definition Logging.h:22
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:249
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)