From 4ebfea45f32cc845d7887e552e8c6084d8ef3067 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Fri, 10 Nov 2017 21:43:09 -0800 Subject: lib: Add config_parser class This class is not publicly exported. It is meant to read config files in the INI format. Reviewed-by: Brent Stapleton --- host/lib/utils/CMakeLists.txt | 1 + host/lib/utils/config_parser.cpp | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 host/lib/utils/config_parser.cpp (limited to 'host/lib/utils') diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index 91a579853..afaf99274 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -159,6 +159,7 @@ SET_SOURCE_FILES_PROPERTIES( ######################################################################## LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/csv.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/config_parser.cpp ${CMAKE_CURRENT_SOURCE_DIR}/eeprom_utils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/gain_group.cpp ${CMAKE_CURRENT_SOURCE_DIR}/ihex.cpp diff --git a/host/lib/utils/config_parser.cpp b/host/lib/utils/config_parser.cpp new file mode 100644 index 000000000..a88a99a01 --- /dev/null +++ b/host/lib/utils/config_parser.cpp @@ -0,0 +1,59 @@ +// +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#include +#include +#include +#include + +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( + section, + key, + new_config.get(section, key) + ); + } + } +} + +std::vector config_parser::sections() +{ + try { + return _options(_pt); + } catch (const boost::property_tree::ptree_bad_path &) { + return std::vector{}; + } +} + +std::vector config_parser::options(const std::string §ion) +{ + try { + return _options(_pt.get_child(section)); + } catch (const boost::property_tree::ptree_bad_path &) { + return std::vector{}; + } +} + -- cgit v1.2.3