diff options
-rw-r--r-- | include/salticidae/util.h | 39 | ||||
-rw-r--r-- | src/util.cpp | 43 |
2 files changed, 59 insertions, 23 deletions
diff --git a/include/salticidae/util.h b/include/salticidae/util.h index e5d653a..e51f984 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -38,6 +38,10 @@ namespace salticidae { void sec2tv(double t, struct timeval &tv); void event_add_with_timeout(struct event *ev, double timeout); +std::string trim(const std::string &s, + const std::string &space = "\t\r\n "); +std::vector<std::string> split(const std::string &s, const std::string &delim); +std::vector<std::string> trim_all(const std::vector<std::string> &ss); class SalticidaeError: public std::exception { std::string msg; @@ -142,25 +146,30 @@ class Config { }; class OptValFlag: public OptVal { - bool &val; + bool val; public: - OptValFlag(bool &val): val(val) {} + OptValFlag() = default; + OptValFlag(bool val): val(val) {} void switch_on() override { val = true; } + bool &get() { return val; } }; class OptValStr: public OptVal { - std::string &val; + std::string val; public: - OptValStr(std::string &val): val(val) {} + OptValStr() = default; + OptValStr(const std::string &val): val(val) {} void set_val(const std::string &strval) override { val = strval; } + std::string &get() { return val; } }; class OptValInt: public OptVal { - int &val; + int val; public: - OptValInt(int &val): val(val) {} + OptValInt() = default; + OptValInt(int val): val(val) {} void set_val(const std::string &strval) override { size_t idx; try { @@ -169,12 +178,14 @@ class Config { throw SalticidaeError("invalid integer"); } } + int &get() { return val; } }; class OptValDouble: public OptVal { - double &val; + double val; public: - OptValDouble(double &val): val(val) {} + OptValDouble() = default; + OptValDouble(double val): val(val) {} void set_val(const std::string &strval) override { size_t idx; try { @@ -183,15 +194,19 @@ class Config { throw SalticidaeError("invalid double"); } } + double &get() { return val; } }; class OptValStrVec: public OptVal { - std::vector<std::string> &val; + using strvec_t = std::vector<std::string>; + strvec_t val; public: - OptValStrVec(std::vector<std::string> &val): val(val) {} + OptValStrVec() = default; + OptValStrVec(const strvec_t &val): val(val) {} void append(const std::string &strval) override { val.push_back(strval); } + strvec_t &get() { return val; } }; private: @@ -221,12 +236,12 @@ class Config { conf_fname(conf_fname), opt_val_conf(this->conf_fname) { conf_idx = getopt_order.size(); - add_opt("conf", &opt_val_conf, SET_VAL); + add_opt("conf", opt_val_conf, SET_VAL); } ~Config() {} - void add_opt(const std::string &optname, OptVal *optval, Action action); + void add_opt(const std::string &optname, OptVal &optval, Action action); bool load(const std::string &fname); size_t parse(int argc, char **argv); }; 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<std::string> split(const std::string &s, const std::string &delim) { + std::vector<std::string> 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<std::string> trim_all(const std::vector<std::string> &ss) { + std::vector<std::string> res; + for (const auto &s: ss) + res.push_back(trim(s)); + return res; +} + } |