https://gitlab.opengeosys.org/ogs/ogs.git
Raw File
Tip revision: fa9ad3a5289541d1346184308d11513e9dbc2058 authored by Dmitry Yu. Naumov on 02 October 2023, 22:02:50 UTC
Merge branch 'FixConstructMeshesFromGeometryBulkElementIds' into 'master'
Tip revision: fa9ad3a
ConfigTreeUtil.cpp
/**
 * \file
 * \copyright
 * Copyright (c) 2012-2023, OpenGeoSys Community (http://www.opengeosys.org)
 *            Distributed under a Modified BSD License.
 *              See accompanying file LICENSE.txt or
 *              http://www.opengeosys.org/project/license
 *
 */

#include "ConfigTreeUtil.h"

#include <boost/operators.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <ostream>
#include <utility>

#include "ConfigTree.h"
#include "Error.h"
#include "Logging.h"

namespace BaseLib
{
ConfigTree makeConfigTree(const std::string& filepath, const bool be_ruthless,
                          const std::string& toplevel_tag,
                          std::stringstream& prj_stream)
{
    ConfigTree::PTree ptree;

    // note: Trimming whitespace and ignoring comments is crucial in order
    //       for our configuration tree implementation to work!
    try
    {
        read_xml(prj_stream, ptree,
                 boost::property_tree::xml_parser::no_comments |
                     boost::property_tree::xml_parser::trim_whitespace);
    }
    catch (boost::property_tree::xml_parser_error const& e)
    {
        OGS_FATAL("Error while parsing XML file `{:s}' at line {:d}: {:s}.",
                  e.filename(), e.line(), e.message());
    }

    DBUG("Project configuration from file '{:s}' read.", filepath);

    if (auto opt_child = ptree.get_child_optional(toplevel_tag))
    {
        auto const callback =
            be_ruthless ? ConfigTree::onerror : ConfigTree::onwarning;

        return ConfigTree(std::move(*opt_child), filepath, callback, callback);
    }
    OGS_FATAL("Tag <{:s}> has not been found in file `{:s}'.", toplevel_tag,
              filepath);
}

ConfigTree makeConfigTreeFromFile(const std::string& filepath,
                                  const bool be_ruthless,
                                  const std::string& toplevel_tag)
{
    std::ifstream file(filepath);
    if (file)
    {
        std::stringstream buffer;
        buffer << file.rdbuf();

        return makeConfigTree(filepath, be_ruthless, toplevel_tag, buffer);
    }
    else
    {
        OGS_FATAL("Could not read from file {:s}!", filepath);
    }
}

}  // namespace BaseLib
back to top