X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/df2bb45e41303b5132feb6b264caaa01c31b8bb5..fc0e3a75f76a611f67a846737dabda1c64b2ecd0:/projects/sgconf/options.cpp?ds=sidebyside diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index 71f9687c..6a4eef1b 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -23,12 +23,50 @@ #include "action.h" #include "parser_state.h" +#include "stg/common.h" + +#include +#include #include #include #include +#include + +namespace +{ + +template +void ReadConfigFile(const std::string & filePath, void (C::* callback)(const std::string&, const std::string&), C * obj) +{ +std::ifstream stream(filePath.c_str()); +std::string line; +size_t num = 0; +while (std::getline(stream, line)) + { + ++num; + line = Trim(line); + std::string::size_type pos = line.find_first_of('#'); + if (pos != std::string::npos) + line = line.substr(0, pos); + if (line.empty()) + continue; + pos = line.find_first_of('='); + if (pos == std::string::npos) + { + std::ostringstream error; + error << "Bad file format, missing '=' in '" << filePath << ":" << num << "'."; + throw std::runtime_error(error.str().c_str()); + } + (obj->*callback)(Trim(line.substr(0, pos)), Trim(line.substr(pos + 1, line.length() - pos - 1))); + } +} + +} // namespace anonymous + using SGCONF::OPTION; using SGCONF::OPTION_BLOCK; +using SGCONF::OPTION_BLOCKS; using SGCONF::ACTION; using SGCONF::PARSER_STATE; @@ -55,7 +93,7 @@ OPTION::OPTION(const std::string & longName, OPTION::OPTION(const OPTION & rhs) : m_shortName(rhs.m_shortName), m_longName(rhs.m_longName), - m_action(rhs.m_action), + m_action(rhs.m_action->Clone()), m_description(rhs.m_description) { } @@ -65,6 +103,15 @@ OPTION::~OPTION() delete m_action; } +OPTION & OPTION::operator=(const OPTION & rhs) +{ +m_shortName = rhs.m_shortName; +m_longName = rhs.m_longName; +m_action = rhs.m_action->Clone(); +m_description = rhs.m_description; +return *this; +} + void OPTION::Help(size_t level) const { if (!m_action) @@ -100,32 +147,54 @@ try { return m_action->Parse(argc, argv); } +catch (const ACTION::ERROR & ex) + { + if (m_longName.empty()) + throw ERROR("-" + m_shortName + ": " + ex.what()); + else + throw ERROR("--" + m_longName + ", -" + m_shortName + ": " + ex.what()); + } +} + +void OPTION::ParseValue(const std::string & value) +{ +if (!m_action) + throw ERROR("Option is not defined."); +try + { + return m_action->ParseValue(value); + } catch (const ACTION::ERROR & ex) { throw ERROR(m_longName + ": " + ex.what()); } } -void OPTION_BLOCK::Add(const std::string & shortName, - const std::string & longName, - ACTION * action, - const std::string & description) +OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & shortName, + const std::string & longName, + ACTION * action, + const std::string & description) { m_options.push_back(OPTION(shortName, longName, action, description)); +return *this; } -void OPTION_BLOCK::Add(const std::string & longName, - ACTION * action, - const std::string & description) +OPTION_BLOCK & OPTION_BLOCK::Add(const std::string & longName, + ACTION * action, + const std::string & description) { m_options.push_back(OPTION(longName, action, description)); +return *this; } void OPTION_BLOCK::Help(size_t level) const { +if (m_options.empty()) + return; +std::cout << m_description << ":\n"; std::for_each(m_options.begin(), m_options.end(), - std::bind2nd(std::mem_fun_ref(&OPTION::Help), level)); + std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1)); } PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv) @@ -138,6 +207,44 @@ while (state.argc > 0 && !state.stop) state = it->Parse(--state.argc, ++state.argv); else break; + ++it; + } +return state; +} + +void OPTION_BLOCK::ParseFile(const std::string & filePath) +{ +if (access(filePath.c_str(), R_OK)) + throw ERROR("File '" + filePath + "' does not exists."); +ReadConfigFile(filePath, &OPTION_BLOCK::OptionCallback, this); +} + +void OPTION_BLOCK::OptionCallback(const std::string & key, const std::string & value) +{ +for (std::vector