diff --git a/src/tools/Tools.cpp b/src/tools/Tools.cpp index c18073b6f9848f5a1dc582525c96b2bfc826db6d..c143889a3c1007a5ef8e63d4bf4b9c7735135282 100644 --- a/src/tools/Tools.cpp +++ b/src/tools/Tools.cpp @@ -30,7 +30,8 @@ using namespace std; namespace PLMD{ -bool Tools::convert(const string & str,int & t){ +template<class T> +bool Tools::convertToAny(const string & str,T & t){ istringstream istr(str.c_str()); bool ok=static_cast<bool>(istr>>t); if(!ok) return false; @@ -39,39 +40,27 @@ bool Tools::convert(const string & str,int & t){ return remaining.length()==0; } +bool Tools::convert(const string & str,int & t){ + return convertToAny(str,t); +} + bool Tools::convert(const string & str,long int & t){ - istringstream istr(str.c_str()); - bool ok=static_cast<bool>(istr>>t); - if(!ok) return false; - string remaining; - istr>>remaining; - return remaining.length()==0; + return convertToAny(str,t); } bool Tools::convert(const string & str,unsigned & t){ - istringstream istr(str.c_str()); - bool ok=static_cast<bool>(istr>>t); - if(!ok) return false; - string remaining; - istr>>remaining; - return remaining.length()==0; + return convertToAny(str,t); } bool Tools::convert(const string & str,AtomNumber &a){ - int i; + unsigned i; bool r=convert(str,i); if(r) a.setSerial(i); return r; } -bool Tools::convert(const string & str,float & t){ - double tt; - bool r=convert(str,tt); - t=double(tt); - return r; -} - -bool Tools::convert(const string & str,double & t){ +template<class T> +bool Tools::convertToReal(const string & str,T & t){ if(str=="PI" || str=="+PI" || str=="+pi" || str=="pi"){ t=pi; return true; } else if(str=="-PI" || str=="-pi"){ @@ -80,7 +69,7 @@ bool Tools::convert(const string & str,double & t){ std::size_t pi_start=str.find_first_of("PI"); if(str.substr(pi_start)!="PI") return false; istringstream nstr(str.substr(0,pi_start)); - double ff=0.0; bool ok=static_cast<bool>(nstr>>ff); + T ff=0.0; bool ok=static_cast<bool>(nstr>>ff); if(!ok) return false; t=ff*pi; std::string remains; nstr>>remains; @@ -89,50 +78,25 @@ bool Tools::convert(const string & str,double & t){ std::size_t pi_start=str.find_first_of("pi"); if(str.substr(pi_start)!="pi") return false; istringstream nstr(str.substr(0,pi_start)); - double ff=0.0; bool ok=static_cast<bool>(nstr>>ff); + T ff=0.0; bool ok=static_cast<bool>(nstr>>ff); if(!ok) return false; t=ff*pi; std::string remains; nstr>>remains; return remains.length()==0; } - istringstream istr(str.c_str()); - bool ok=static_cast<bool>(istr>>t); - if(!ok) return false; - string remaining; - istr>>remaining; - return remaining.length()==0; + return convertToAny(str,t); +} + +bool Tools::convert(const string & str,float & t){ + return convertToReal(str,t); +} + +bool Tools::convert(const string & str,double & t){ + return convertToReal(str,t); } bool Tools::convert(const string & str,long double & t){ - if(str=="PI" || str=="+PI" || str=="+pi" || str=="pi"){ - t=pi; return true; - } else if(str=="-PI" || str=="-pi"){ - t=-pi; return true; - } else if( str.find("PI")!=std::string::npos ){ - std::size_t pi_start=str.find_first_of("PI"); - if(str.substr(pi_start)!="PI") return false; - istringstream nstr(str.substr(0,pi_start)); - long double ff=0.0; bool ok=static_cast<bool>(nstr>>ff); - if(!ok) return false; - t=ff*pi; - std::string remains; nstr>>remains; - return remains.length()==0; - } else if( str.find("pi")!=std::string::npos ){ - std::size_t pi_start=str.find_first_of("pi"); - if(str.substr(pi_start)!="pi") return false; - istringstream nstr(str.substr(0,pi_start)); - long double ff=0.0; bool ok=static_cast<bool>(nstr>>ff); - if(!ok) return false; - t=ff*pi; - std::string remains; nstr>>remains; - return remains.length()==0; - } - istringstream istr(str.c_str()); - bool ok=static_cast<bool>(istr>>t); - if(!ok) return false; - string remaining; - istr>>remaining; - return remaining.length()==0; + return convertToReal(str,t); } bool Tools::convert(const string & str,string & t){ diff --git a/src/tools/Tools.h b/src/tools/Tools.h index f79d9a2d5e7e6f2962c88d4f073ca7918ac5c6c1..dbe7f3ded0d22878125180815a4c32d53aa18bc3 100644 --- a/src/tools/Tools.h +++ b/src/tools/Tools.h @@ -50,6 +50,13 @@ const double pi(3.14159265358979323846264338327950288419716939937510582097494459 /// \ingroup TOOLBOX /// Empty class which just contains several (static) tools class Tools{ +/// class to convert a string to a generic type T + template<class T> + static bool convertToAny(const std::string & str,T &t); +/// class to convert a string to a real type T. +/// T should be either float, double, or long double + template<class T> + static bool convertToReal(const std::string & str,T &t); public: /// Split the line in words using separators. /// It also take into account parenthesis. Outer parenthesis found are removed from