1 #ifndef LIBGEODECOMP_MISC_STRINGOPS_H 2 #define LIBGEODECOMP_MISC_STRINGOPS_H 3 4 #include <libgeodecomp/misc/stringvec.h> 5 6 #include <boost/algorithm/string.hpp> 7 #include <sstream> 8 9 // sad but true: CodeGear's C++ compiler has troubles with the + 10 // operator for strings. 11 #ifdef __CODEGEARC__ 12 namespace LibGeoDecomp { 13 std::string operator+(const std::string& a, const std::string& b) 14 { 15 return std::string(a).append(b); 16 } 17 18 } 19 #endif 20 21 namespace LibGeoDecomp { 22 23 class StringOps 24 { 25 public: 26 itoa(int i)27 static std::string itoa(int i) 28 { 29 std::stringstream buf; 30 buf << i; 31 return buf.str(); 32 } 33 atoi(const std::string & s)34 static int atoi(const std::string& s) 35 { 36 std::stringstream buf(s); 37 int ret; 38 buf >> ret; 39 return ret; 40 } 41 atof(const std::string & s)42 static double atof(const std::string& s) 43 { 44 std::stringstream buf(s); 45 double ret; 46 buf >> ret; 47 return ret; 48 } 49 tokenize(const std::string & string,const std::string & delimiters)50 static StringVec tokenize(const std::string& string, const std::string& delimiters) 51 { 52 StringVec ret; 53 boost::split(ret, string, boost::is_any_of(delimiters), boost::token_compress_on); 54 del(ret, ""); 55 56 return ret; 57 } 58 join(const StringVec & tokens,const std::string & delimiter)59 static std::string join(const StringVec& tokens, const std::string& delimiter) 60 { 61 std::stringstream buf; 62 63 for (StringVec::const_iterator i = tokens.begin(); i != tokens.end(); ++i) { 64 if (i != tokens.begin()) { 65 buf << delimiter; 66 } 67 buf << *i; 68 } 69 70 return buf.str(); 71 } 72 }; 73 74 } 75 76 #endif 77