StringUtilities.h

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #ifndef STRING_UTILITIES_H_
00004 #define STRING_UTILITIES_H_
00005 
00006 #include <string>
00007 #include <sstream>
00008 #include <vector>
00009 
00010 #include <stdint.h>
00011 
00012 namespace fesa
00013 {
00014 
00015 class StringUtilities
00016 {
00017   public:
00018 
00026     static void tokenize(const std::string& str, std::vector<std::string>& tokens,
00027                          const std::string& delimiter = " ");
00028 
00033     static void trimWhiteSpace(std::string& str);
00034 
00040     static void removeBrackets(std::string& str);
00041 
00045     static void addBrackets(std::string& str);
00046 
00056     static void getElements(const std::string& str, std::vector<std::string>& elements);
00057 
00064     static void addElements(std::string& str,const std::vector<std::string>& elements);
00065 
00066 
00072     static std::string toString(signed char data);
00073 
00079     static std::string toString(uint32_t data);
00080 
00086     static std::string toString(int32_t data);
00087 
00093     static std::string toString(int64_t data);
00094 
00100     static std::string toString(double data);
00101 
00107     static std::string toString(const void* ptr);
00108 
00115     static bool fromString(bool& value, const std::string& str);
00116 
00123     static bool fromString(signed char& value, const std::string& str);
00124 
00131     template<typename T> static bool fromString(T&, const std::string&);
00132 };
00133 
00134 //******************************************************************
00135 
00136 inline bool StringUtilities::fromString(bool& val, const std::string& str)
00137 {
00138     if (str == "true" || str == "1")
00139     {
00140         val = true;
00141         return true;
00142     }
00143     if (str == "false" || str == "0")
00144     {
00145         val = false;
00146         return true;
00147     }
00148     return false;
00149 }
00150 
00151 inline bool StringUtilities::fromString(signed char& val, const std::string& str)
00152 {
00153     std::istringstream iss(str);
00154     int32_t tmp;
00155     iss >> tmp;
00156     val = (signed char) tmp;
00157     return !(iss >> tmp).fail();
00158 }
00159 
00160 template<typename T>
00161 inline bool StringUtilities::fromString(T& val, const std::string& str)
00162 {
00163     std::istringstream iss(str);
00164     return !(iss >> val).fail();
00165 }
00166 
00167 } // fesa
00168 
00169 
00170 #endif // STRING_UTILITIES_H_

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1