From 483dfb7c5021d7dc9e2c7f10c9b76101441c7203 Mon Sep 17 00:00:00 2001 From: imarom Date: Sun, 3 Jul 2016 15:38:59 +0300 Subject: slowpath features bit to avoid multiple IFs --- src/utl_yaml.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/utl_yaml.cpp') diff --git a/src/utl_yaml.cpp b/src/utl_yaml.cpp index 66c83ac8..8352e887 100755 --- a/src/utl_yaml.cpp +++ b/src/utl_yaml.cpp @@ -22,6 +22,8 @@ limitations under the License. */ #include +#include +#include "common/basic_utils.h" #define INADDRSZ 4 @@ -157,6 +159,27 @@ static bool mac2uint64(const std::string &mac_str, uint64_t &mac_num) { * YAML Parser Wrapper * ***********************/ +void +YAMLParserWrapper::load(YAML::Node &root) { + std::stringstream ss; + + /* first check file exists */ + if (!utl_is_file_exists(m_filename)){ + ss << "file '" << m_filename << "' does not exists"; + throw std::runtime_error(ss.str()); + } + + std::ifstream fin(m_filename); + + try { + YAML::Parser base_parser(fin); + base_parser.GetNextDocument(root); + + } catch (const YAML::Exception &e) { + parse_err(e.what()); + } +} + bool YAMLParserWrapper::parse_bool(const YAML::Node &node, const std::string &name, bool def) { if (!node.FindValue(name)) { @@ -322,7 +345,7 @@ void YAMLParserWrapper::parse_err(const std::string &err, const YAML::Node &node) const { std::stringstream ss; - ss << "'" << m_header << "' - YAML parsing error at line " << node.GetMark().line << ": "; + ss << "'" << m_filename << "' - YAML parsing error at line " << node.GetMark().line << ": "; ss << err; throw std::runtime_error(ss.str()); @@ -332,7 +355,7 @@ void YAMLParserWrapper::parse_err(const std::string &err) const { std::stringstream ss; - ss << "'" << m_header << "' - YAML parsing error: " << err; + ss << "'" << m_filename << "' - YAML parsing error: " << err; throw std::runtime_error(ss.str()); } -- cgit 1.2.3-korg