QStringHandler.hh

Go to the documentation of this file.
00001 #ifndef _Q_STRING_HANDLER_HH_
00002 #define _Q_STRING_HANDLER_HH_
00003 #include <string>
00011 namespace QStringHandler {
00012     inline std::string &SwallowSpaces(std::string& s) {
00013         size_t pos=0;
00014         while(s[pos]==' ')
00015             pos++;
00016         return s.erase( 0, pos);
00017     }
00018 
00019     inline std::string &RSwallowSpaces(std::string& s) {
00020         size_t pos = s.length()-1;
00021         while( s[pos] == ' ' )
00022             pos--;
00023         return s.erase( pos+1, std::string::npos );
00024     }
00025 
00026     // convert a string to integer checking error
00027     // return TRUE on success, false otherwise
00028     inline bool StringToInteger(const std::string& s, int &i) {
00029         for( unsigned int k=0; k<s.size(); k++) {
00030             if (k==0 && (s[k]=='-' || s[k]=='+')) continue;
00031             if ( isdigit(s[k]) ) continue;
00032             return false;
00033         }
00034         i = atoi(s.c_str());
00035         return true;                    
00036     }
00037 
00038     // return TRUE on success, false otherwise
00039     inline bool StringToBool(const std::string& s, bool &b) {
00040         if(s=="true"){b=true;return true;}
00041         if(s=="false"){b=false;return true;}
00042 
00043         return false;                   
00044     }
00045 
00046     // convert a string to double
00047     // return TRUE on success, false otherwise
00048     inline int StringToDouble(const std::string& s, double &d) {
00049         const char *str = s.c_str();
00050         char *err;
00051         double val = ::strtod(str,&err);
00052         if ( err == str || s=="Info" || s=="info" ) {
00053             return false;
00054         }
00055         d=val;
00056         return true;                    
00057     }
00058 
00059 }
00060 #endif

Generated on Fri Mar 6 13:40:39 2009 for CUORE Software by  doxygen 1.5.1