aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/utils/config_parser.cpp
blob: a88a99a01eb5e443620d71881d35fd75f519ac47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0+
//

#include <uhdlib/utils/config_parser.hpp>
#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/property_tree/ini_parser.hpp>

using namespace uhd;

config_parser::config_parser(const std::string &path)
{
    if (not path.empty() and boost::filesystem::exists(path)) {
        try {
            boost::property_tree::ini_parser::read_ini(path, _pt);
        } catch (const boost::property_tree::ini_parser_error &) {
            throw uhd::runtime_error(str(
                boost::format("Unable to parse file %s")
                % path
            ));
        }
    }
}

void config_parser::read_file(const std::string &path)
{
    config_parser new_config(path);
    for (const auto& section : new_config.sections()) {
        for (const auto& key : new_config.options(section)) {
            set<std::string>(
                    section,
                    key,
                    new_config.get<std::string>(section, key)
            );
        }
    }
}

std::vector<std::string> config_parser::sections()
{
    try {
        return _options(_pt);
    } catch (const boost::property_tree::ptree_bad_path &) {
        return std::vector<std::string>{};
    }
}

std::vector<std::string> config_parser::options(const std::string &section)
{
    try {
        return _options(_pt.get_child(section));
    } catch (const boost::property_tree::ptree_bad_path &) {
        return std::vector<std::string>{};
    }
}