From ccdd4ffda70a35219fae1cdce1306f3d4ebd8dc9 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 18 Nov 2016 15:49:28 -0800 Subject: [PATCH] Move split() to OSUtils since it is not used in core. --- controller/EmbeddedNetworkController.cpp | 2 +- controller/JSONDB.cpp | 2 +- node/Utils.cpp | 44 ------------------------ node/Utils.hpp | 11 ------ one.cpp | 4 +-- osdep/OSUtils.cpp | 44 ++++++++++++++++++++++++ osdep/OSUtils.hpp | 11 ++++++ service/ControlPlane.cpp | 4 +-- 8 files changed, 61 insertions(+), 61 deletions(-) diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index 74937dd84..974936e4f 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -889,7 +889,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( json &v6m = b["v6AssignMode"]; if (!nv6m.is_object()) nv6m = json::object(); if (v6m.is_string()) { // backward compatibility - std::vector v6ms(Utils::split(_jS(v6m,"").c_str(),",","","")); + std::vector v6ms(OSUtils::split(_jS(v6m,"").c_str(),",","","")); std::sort(v6ms.begin(),v6ms.end()); v6ms.erase(std::unique(v6ms.begin(),v6ms.end()),v6ms.end()); nv6m["rfc4193"] = false; diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp index 6adc8a663..a9e9d05bc 100644 --- a/controller/JSONDB.cpp +++ b/controller/JSONDB.cpp @@ -144,7 +144,7 @@ bool JSONDB::_isValidObjectName(const std::string &n) std::string JSONDB::_genPath(const std::string &n,bool create) { - std::vector pt(Utils::split(n.c_str(),"/","","")); + std::vector pt(OSUtils::split(n.c_str(),"/","","")); if (pt.size() == 0) return std::string(); diff --git a/node/Utils.cpp b/node/Utils.cpp index 9d67fc22e..215e3b3e0 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -218,50 +218,6 @@ void Utils::getSecureRandom(void *buf,unsigned int bytes) s20.encrypt12(buf,buf,bytes); } -std::vector Utils::split(const char *s,const char *const sep,const char *esc,const char *quot) -{ - std::vector fields; - std::string buf; - - if (!esc) - esc = ""; - if (!quot) - quot = ""; - - bool escapeState = false; - char quoteState = 0; - while (*s) { - if (escapeState) { - escapeState = false; - buf.push_back(*s); - } else if (quoteState) { - if (*s == quoteState) { - quoteState = 0; - fields.push_back(buf); - buf.clear(); - } else buf.push_back(*s); - } else { - const char *quotTmp; - if (strchr(esc,*s)) - escapeState = true; - else if ((buf.size() <= 0)&&((quotTmp = strchr(quot,*s)))) - quoteState = *quotTmp; - else if (strchr(sep,*s)) { - if (buf.size() > 0) { - fields.push_back(buf); - buf.clear(); - } // else skip runs of seperators - } else buf.push_back(*s); - } - ++s; - } - - if (buf.size()) - fields.push_back(buf); - - return fields; -} - bool Utils::scopy(char *dest,unsigned int len,const char *src) { if (!len) diff --git a/node/Utils.hpp b/node/Utils.hpp index cfe565011..48c43da37 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -111,17 +111,6 @@ public: */ static void getSecureRandom(void *buf,unsigned int bytes); - /** - * Split a string by delimiter, with optional escape and quote characters - * - * @param s String to split - * @param sep One or more separators - * @param esc Zero or more escape characters - * @param quot Zero or more quote characters - * @return Vector of tokens - */ - static std::vector split(const char *s,const char *const sep,const char *esc,const char *quot); - /** * Tokenize a string (alias for strtok_r or strtok_s depending on platform) * diff --git a/one.cpp b/one.cpp index 51cda0c70..3dad2ed9b 100644 --- a/one.cpp +++ b/one.cpp @@ -696,7 +696,7 @@ static int idtool(int argc,char **argv) CertificateOfMembership com; for(int a=3;a params(Utils::split(argv[a],",","","")); + std::vector params(OSUtils::split(argv[a],",","","")); if (params.size() == 3) { uint64_t qId = Utils::hexStrToU64(params[0].c_str()); uint64_t qValue = Utils::hexStrToU64(params[1].c_str()); @@ -1084,7 +1084,7 @@ int main(int argc,char **argv) fprintf(stderr,"%s: no home path specified and no platform default available" ZT_EOL_S,argv[0]); return 1; } else { - std::vector hpsp(Utils::split(homeDir.c_str(),ZT_PATH_SEPARATOR_S,"","")); + std::vector hpsp(OSUtils::split(homeDir.c_str(),ZT_PATH_SEPARATOR_S,"","")); std::string ptmp; if (homeDir[0] == ZT_PATH_SEPARATOR) ptmp.push_back(ZT_PATH_SEPARATOR); diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 4a81625b0..65704fa36 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -315,6 +315,50 @@ bool OSUtils::writeFile(const char *path,const void *buf,unsigned int len) return false; } +std::vector OSUtils::split(const char *s,const char *const sep,const char *esc,const char *quot) +{ + std::vector fields; + std::string buf; + + if (!esc) + esc = ""; + if (!quot) + quot = ""; + + bool escapeState = false; + char quoteState = 0; + while (*s) { + if (escapeState) { + escapeState = false; + buf.push_back(*s); + } else if (quoteState) { + if (*s == quoteState) { + quoteState = 0; + fields.push_back(buf); + buf.clear(); + } else buf.push_back(*s); + } else { + const char *quotTmp; + if (strchr(esc,*s)) + escapeState = true; + else if ((buf.size() <= 0)&&((quotTmp = strchr(quot,*s)))) + quoteState = *quotTmp; + else if (strchr(sep,*s)) { + if (buf.size() > 0) { + fields.push_back(buf); + buf.clear(); + } // else skip runs of seperators + } else buf.push_back(*s); + } + ++s; + } + + if (buf.size()) + fields.push_back(buf); + + return fields; +} + std::string OSUtils::platformDefaultHomePath() { #ifdef __UNIX_LIKE__ diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index c481780b1..c73f0bd37 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -236,6 +236,17 @@ public: */ static bool writeFile(const char *path,const void *buf,unsigned int len); + /** + * Split a string by delimiter, with optional escape and quote characters + * + * @param s String to split + * @param sep One or more separators + * @param esc Zero or more escape characters + * @param quot Zero or more quote characters + * @return Vector of tokens + */ + static std::vector split(const char *s,const char *const sep,const char *esc,const char *quot); + /** * Write a block of data to disk, replacing any current file contents * diff --git a/service/ControlPlane.cpp b/service/ControlPlane.cpp index 150bba1b5..07304fc9c 100644 --- a/service/ControlPlane.cpp +++ b/service/ControlPlane.cpp @@ -266,7 +266,7 @@ unsigned int ControlPlane::handleRequest( { char json[8194]; unsigned int scode = 404; - std::vector ps(Utils::split(path.c_str(),"/","","")); + std::vector ps(OSUtils::split(path.c_str(),"/","","")); std::map urlArgs; Mutex::Lock _l(_lock); @@ -279,7 +279,7 @@ unsigned int ControlPlane::handleRequest( if (qpos != std::string::npos) { std::string args(ps[ps.size() - 1].substr(qpos + 1)); ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos); - std::vector asplit(Utils::split(args.c_str(),"&","","")); + std::vector asplit(OSUtils::split(args.c_str(),"&","","")); for(std::vector::iterator a(asplit.begin());a!=asplit.end();++a) { std::size_t eqpos = a->find('='); if (eqpos == std::string::npos)