Main Page | Modules | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

func.h

Go to the documentation of this file.
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     // Skip delimiters at beginning.
00156     string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00157     // Find first "non-delimiter".
00158     string::size_type pos     = str.find_first_of(delimiters, lastPos);
00159 
00160     while (string::npos != pos || string::npos != lastPos)
00161     {
00162         // Found a token, add it to the vector.
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                 //cout << "Token: " << tokens[tokens.size()-1] << "\n";
00170         // Skip delimiters.  Note the "not_of"
00171         lastPos = str.find_first_not_of(delimiters, pos);
00172                 
00173         // Find next "non-delimiter"
00174         pos = str.find_first_of(delimiters, lastPos);
00175                 
00176     }
00177 }
00178 
00179 /*vector<string> tokenize(const string& str,const string& delimiters)
00180 {
00181         vector<string> tokens;
00182         
00183         // skip delimiters at beginning.
00184         string::size_type lastPos = str.find_first_not_of(delimiters, 0);
00185         
00186         // find first "non-delimiter".
00187         string::size_type pos = str.find_first_of(delimiters, lastPos);
00188 
00189         while (string::npos != pos || string::npos != lastPos)
00190         {
00191                 // found a token, add it to the vector.
00192                 tokens.push_back(str.substr(lastPos, pos - lastPos));
00193                 
00194                 // skip delimiters.  Note the "not_of"
00195                 lastPos = str.find_first_not_of(delimiters, pos);
00196                 
00197                 // find next "non-delimiter"
00198                 pos = str.find_first_of(delimiters, lastPos);
00199         }
00200 
00201         return tokens;
00202 }
00203 */
00204 
00210 #endif // FUNC_H

Generated on Tue Apr 17 09:39:11 2007 for PCG Library by  doxygen 1.3.9.1