From 4d2e7a23ebff97c9208f538ecf7d2218aa046130 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 6 Jul 2018 20:10:14 -0400 Subject: ... --- src/util.cpp | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/util.cpp b/src/util.cpp index 9ea3b83..19ec01c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -146,24 +146,15 @@ Config::Opt::Opt(const std::string &optname, OptVal *optval, Action action, int opt.val = idx; } -void Config::add_opt(const std::string &optname, OptVal *optval, Action action) { +void Config::add_opt(const std::string &optname, OptVal &optval, Action action) { if (conf.count(optname)) throw SalticidaeError("option name already exists"); auto it = conf.insert( std::make_pair(optname, - Opt(optname, optval, action, getopt_order.size()))).first; + Opt(optname, &optval, action, getopt_order.size()))).first; getopt_order.push_back(&it->second); } -std::string trim(const std::string &str, - const std::string &space = "\t\r\n ") { - const auto new_begin = str.find_first_not_of(space); - if (new_begin == std::string::npos) - return ""; - const auto new_end = str.find_last_not_of(space); - return str.substr(new_begin, new_end - new_begin + 1); -} - void Config::update(Opt &p, const char *optval) { switch (p.action) { @@ -241,4 +232,34 @@ size_t Config::parse(int argc, char **argv) { return optind; } +std::vector split(const std::string &s, const std::string &delim) { + std::vector res; + size_t lastpos = 0; + for (;;) + { + size_t pos = s.find(delim, lastpos); + if (pos == std::string::npos) + break; + res.push_back(s.substr(lastpos, pos - lastpos)); + lastpos = pos + delim.length(); + } + res.push_back(s.substr(lastpos, s.length() - lastpos)); + return res; +} + +std::string trim(const std::string &s, const std::string &space) { + const auto new_begin = s.find_first_not_of(space); + if (new_begin == std::string::npos) + return ""; + const auto new_end = s.find_last_not_of(space); + return s.substr(new_begin, new_end - new_begin + 1); +} + +std::vector trim_all(const std::vector &ss) { + std::vector res; + for (const auto &s: ss) + res.push_back(trim(s)); + return res; +} + } -- cgit v1.2.3-70-g09d2