From 16655fcc19fc3a6cf36b646c72633bfaf07d876f Mon Sep 17 00:00:00 2001 From: Bjoern Sikora Date: Mon, 7 Sep 2009 11:48:50 +0200 Subject: [PATCH] Use boost::algorithm::string::split for splitting strings into tokens. --- src/dhs.cpp | 33 +++++---------------------------- src/dhs.h | 2 -- src/dyns.cpp | 33 +++++---------------------------- src/dyns.h | 2 -- src/easydns.cpp | 36 +++++++----------------------------- src/easydns.h | 2 -- 6 files changed, 17 insertions(+), 91 deletions(-) diff --git a/src/dhs.cpp b/src/dhs.cpp index bbb9940..befed5c 100644 --- a/src/dhs.cpp +++ b/src/dhs.cpp @@ -11,6 +11,9 @@ #include #include +#include + +namespace ba = boost::algorithm; using namespace std; @@ -108,7 +111,8 @@ string DHS::assemble_base_url(const string& hostname, const string& domain_part) */ list DHS::separate_domain_and_host_part(const string& fqdn) const { - list splitted = split(fqdn,"."); + list splitted; + ba::split(splitted,fqdn,boost::is_any_of(".")); if ( splitted.size() > 1 ) { @@ -134,33 +138,6 @@ list DHS::separate_domain_and_host_part(const string& fqdn) const /** - * Splitts a string into single tokens useing given delimiters - * @param str String to split - * @param delimiters Deliminters to use - * @return A list with the single tokens - */ -list DHS::split(const string& str,const string& delimiters) const -{ - list tokens; - // Skip delimiters at beginning. - string::size_type lastPos = str.find_first_not_of(delimiters, 0); - // Find first "non-delimiter". - string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (string::npos != pos || string::npos != lastPos) - { - // Found a token, add it to the list. - tokens.push_back(str.substr(lastPos, pos - lastPos)); - // Skip delimiters. Note the "not_of" - lastPos = str.find_first_not_of(delimiters, pos); - // Find next "non-delimiter" - pos = str.find_first_of(delimiters, lastPos); - } - return tokens; -} - - -/** * Performs the Service update. * @param ip IP Address to set. * @return 0 if all is fine, -1 otherwise. diff --git a/src/dhs.h b/src/dhs.h index 54e818b..eb70951 100644 --- a/src/dhs.h +++ b/src/dhs.h @@ -32,8 +32,6 @@ private: HTTPHelper::Ptr HTTPHelp; - std::list split(const std::string& str,const std::string& delimiters) const; - std::list separate_domain_and_host_part(const std::string& str) const; std::string assemble_base_url(const std::string& hostname, const std::string& domain_part) const; diff --git a/src/dyns.cpp b/src/dyns.cpp index 7a2229d..4ef7391 100644 --- a/src/dyns.cpp +++ b/src/dyns.cpp @@ -11,6 +11,9 @@ #include #include +#include + +namespace ba = boost::algorithm; using namespace std; @@ -142,35 +145,9 @@ int DYNS::perform_update(const std::string& ip) */ string DYNS::parse_status_code(const string& data) const { - list tokens = split(data," "); - return tokens.front(); -} - - -/** - * Splitts a string into single tokens useing given delimiters - * @param str String to split - * @param delimiters Deliminters to use - * @return A list with the single tokens - */ -list DYNS::split(const string& str,const string& delimiters) const -{ list tokens; - // Skip delimiters at beginning. - string::size_type lastPos = str.find_first_not_of(delimiters, 0); - // Find first "non-delimiter". - string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (string::npos != pos || string::npos != lastPos) - { - // Found a token, add it to the list. - tokens.push_back(str.substr(lastPos, pos - lastPos)); - // Skip delimiters. Note the "not_of" - lastPos = str.find_first_not_of(delimiters, pos); - // Find next "non-delimiter" - pos = str.find_first_of(delimiters, lastPos); - } - return tokens; + ba::split(tokens,data,boost::is_any_of(" ")); + return tokens.front(); } diff --git a/src/dyns.h b/src/dyns.h index 1650f80..f21afd9 100644 --- a/src/dyns.h +++ b/src/dyns.h @@ -36,8 +36,6 @@ private: std::string parse_status_code(const std::string& data) const; - std::list split(const std::string& str,const std::string& delimiters) const; - public: typedef boost::shared_ptr Ptr; diff --git a/src/easydns.cpp b/src/easydns.cpp index 8840776..843fb9c 100644 --- a/src/easydns.cpp +++ b/src/easydns.cpp @@ -11,6 +11,9 @@ #include #include +#include + +namespace ba = boost::algorithm; using namespace std; @@ -86,7 +89,8 @@ string EASYDNS::get_two_level_tld(const string& domain_part) const // TODO: There is a list with all two level TLD's, use it // split the domain_part - list domain_tokens = split(domain_part,"."); + list domain_tokens; + ba::split(domain_tokens,domain_part,boost::is_any_of(".")); domain_tokens.pop_front(); @@ -146,7 +150,8 @@ string EASYDNS::assemble_base_url(const string& hostname, const string& two_leve */ list EASYDNS::separate_domain_and_host_part(const string& fqdn) const { - list splitted = split(fqdn,"."); + list splitted; + ba::split(splitted,fqdn,boost::is_any_of(".")); if ( splitted.size() > 1 ) { @@ -172,33 +177,6 @@ list EASYDNS::separate_domain_and_host_part(const string& fqdn) const /** - * Splitts a string into single tokens useing given delimiters - * @param str String to split - * @param delimiters Deliminters to use - * @return A list with the single tokens - */ -list EASYDNS::split(const string& str,const string& delimiters) const -{ - list tokens; - // Skip delimiters at beginning. - string::size_type lastPos = str.find_first_not_of(delimiters, 0); - // Find first "non-delimiter". - string::size_type pos = str.find_first_of(delimiters, lastPos); - - while (string::npos != pos || string::npos != lastPos) - { - // Found a token, add it to the list. - tokens.push_back(str.substr(lastPos, pos - lastPos)); - // Skip delimiters. Note the "not_of" - lastPos = str.find_first_not_of(delimiters, pos); - // Find next "non-delimiter" - pos = str.find_first_of(delimiters, lastPos); - } - return tokens; -} - - -/** * Performs the Service update. * @param ip IP Address to set. * @return 0 if all is fine, -1 otherwise. diff --git a/src/easydns.h b/src/easydns.h index ddd0812..2e4fed7 100644 --- a/src/easydns.h +++ b/src/easydns.h @@ -32,8 +32,6 @@ private: HTTPHelper::Ptr HTTPHelp; - std::list split(const std::string& str,const std::string& delimiters) const; - std::list separate_domain_and_host_part(const std::string& str) const; std::string assemble_base_url(const std::string& hostname, const std::string& two_level_tld) const; -- 1.7.1