00001 #ifndef FUNC_H
00002 #define FUNC_H
00003
00004 #include <iostream>
00005 #include <sstream>
00006 #include <string>
00007 #include <typeinfo>
00008 #include <stdexcept>
00009 #include <vector>
00010
00011 using namespace std;
00012
00032 class BadConversion : public std::runtime_error {
00033 public:
00034 BadConversion(const std::string& s) : std::runtime_error(s) { }
00035 };
00036
00058 inline std::string stringify(const char* c)
00059 {
00060 std::ostringstream o;
00061 if (!(o << c))
00062 throw BadConversion("stringify(const char*)");
00063 return o.str();
00064 }
00065
00090 template<typename T>
00091 inline void convert(const std::string& s, T& x, bool failIfLeftoverChars = true) {
00092 std::istringstream i(s);
00093 char c;
00094 if (!(i >> x) || (failIfLeftoverChars && i.get(c))) {
00095 throw BadConversion(s);
00096 }
00097 }
00098
00123 template<typename T>
00124 inline T convertTo(const std::string& s, bool failIfLeftoverChars = true) {
00125 T x;
00126 convert(s, x, failIfLeftoverChars);
00127 return x;
00128 }
00129
00154 void inline Tokenize(const string& str, vector<string>& tokens, const string& delimiters = " ") {
00155
00156 string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00157
00158 string::size_type pos = str.find_first_of(delimiters, lastPos);
00159
00160 while (string::npos != pos || string::npos != lastPos)
00161 {
00162
00163 string tmp = str.substr(lastPos, pos - lastPos);
00164 string::size_type loc = tmp.find( "\n", 0 );
00165 if(loc != string::npos) {
00166 tmp.erase(loc,1);
00167 }
00168 tokens.push_back(tmp);
00169
00170
00171 lastPos = str.find_first_not_of(delimiters, pos);
00172
00173
00174 pos = str.find_first_of(delimiters, lastPos);
00175
00176 }
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00210 #endif // FUNC_H