ESyS-Particle
4.0.1
|
00001 00002 // // 00003 // Copyright (c) 2003-2011 by The University of Queensland // 00004 // Earth Systems Science Computational Centre (ESSCC) // 00005 // http://www.uq.edu.au/esscc // 00006 // // 00007 // Primary Business: Brisbane, Queensland, Australia // 00008 // Licensed under the Open Software License version 3.0 // 00009 // http://www.opensource.org/licenses/osl-3.0.php // 00010 // // 00012 00013 00014 #ifndef ESYS_LSMSTRINGUTIL_H 00015 #define ESYS_LSMSTRINGUTIL_H 00016 00017 #include <string> 00018 #include <vector> 00019 #include <sstream> 00020 00021 namespace esys 00022 { 00023 namespace lsm 00024 { 00025 typedef std::vector<std::string> StringVector; 00029 namespace StringUtil 00030 { 00031 typedef esys::lsm::StringVector StringVector; 00032 template <class TmplIterator> 00033 class StdOStreamOp 00034 { 00035 public: 00036 std::string operator()(const TmplIterator &it) const 00037 { 00038 std::stringstream sStream; 00039 sStream << *it; 00040 00041 return sStream.str(); 00042 } 00043 }; 00044 00045 template<class TmplIterator, class TmplStringOperator> 00046 inline 00047 std::string join( 00048 TmplIterator begin, 00049 TmplIterator end, 00050 const std::string &delim, 00051 TmplStringOperator toStringOp = StdOStreamOp<TmplIterator>()) 00052 { 00053 std::string str; 00054 if (begin != end) 00055 { 00056 TmplIterator it = begin; 00057 str = toStringOp(begin); 00058 for (it++; it != end; it++) 00059 { 00060 str += delim + toStringOp(it); 00061 } 00062 } 00063 return str; 00064 } 00065 00066 template<class TmplContainer, class TmplStringOperator> 00067 inline 00068 std::string join( 00069 const TmplContainer &container, 00070 const std::string &delim, 00071 TmplStringOperator toStringOp = StdOStreamOp<typename TmplContainer::const_iterator>()) 00072 { 00073 typename TmplContainer::const_iterator it = container.begin(); 00074 typename TmplContainer::const_iterator end = container.end(); 00075 std::string str; 00076 if (it != end) 00077 { 00078 str = toStringOp(it); 00079 for (it++; it != end; it++) 00080 { 00081 str += delim + toStringOp(it); 00082 } 00083 } 00084 return str; 00085 } 00086 00087 inline 00088 std::string joinStringVector( 00089 const StringVector &container, 00090 const std::string &delim 00091 ) 00092 { 00093 StringVector::const_iterator it = container.begin(); 00094 StringVector::const_iterator end = container.end(); 00095 std::string str; 00096 if (it != end) 00097 { 00098 str = *it; 00099 for (it++; it != end; it++) 00100 { 00101 str += delim + *it; 00102 } 00103 } 00104 return str; 00105 } 00106 00107 template <class TmplData> 00108 class StdIStreamOp 00109 { 00110 public: 00111 TmplData operator()(const std::string &str) const 00112 { 00113 std::stringstream sStream(str); 00114 TmplData data; 00115 sStream >> data; 00116 00117 return data; 00118 } 00119 }; 00120 00121 template <typename TmplData> 00122 TmplData to(const std::string &str) 00123 { 00124 return StdIStreamOp<TmplData>()(str); 00125 } 00126 00127 template <typename TmplData> 00128 std::string toString(const TmplData &data) 00129 { 00130 std::stringstream sStream; 00131 sStream << data; 00132 return sStream.str(); 00133 } 00134 00135 template<class TmplData, class TmplStdStreamOp> 00136 inline 00137 std::vector<TmplData> split( 00138 const std::string &str, 00139 const std::string &delim, 00140 TmplStdStreamOp fromStringOp = StdIStreamOp<TmplData>() 00141 ) 00142 { 00143 std::vector<TmplData> vec; 00144 std::string::size_type prevPos = 0; 00145 std::string::size_type currPos = str.find(delim); 00146 while (currPos != std::string::npos) 00147 { 00148 vec.push_back( 00149 fromStringOp(str.substr(prevPos, currPos - prevPos)) 00150 ); 00151 prevPos = currPos + delim.length(); 00152 currPos = str.find(delim, prevPos); 00153 } 00154 if (prevPos < str.length()) 00155 { 00156 vec.push_back( 00157 fromStringOp(str.substr(prevPos, str.length())) 00158 ); 00159 } 00160 else if (prevPos == str.length()) 00161 { 00162 vec.push_back(fromStringOp("")); 00163 } 00164 00165 return vec; 00166 } 00167 00168 inline 00169 StringVector splitStrings(const std::string &str, const std::string &delim) 00170 { 00171 return split<std::string, StdIStreamOp<std::string> >(str, delim, StdIStreamOp<std::string>()); 00172 } 00173 00174 inline 00175 std::string trim(const std::string &str) 00176 { 00177 size_t b = 0; 00178 for (; (b < str.size()) && std::isspace(str.at(b)); b++) 00179 { 00180 // continue 00181 } 00182 00183 size_t e = 0; 00184 if (str.size() > 0) 00185 { 00186 e = str.size()-1; 00187 for (; (e > b) && std::isspace(str.at(e)); e--) 00188 { 00189 // continue 00190 } 00191 } 00192 return ((e >= b) ? str.substr(b, e-b+1) : ""); 00193 } 00194 }; 00195 }; 00196 }; 00197 00198 #endif