StringUtilities.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/Utilities/StringUtilities.h>
00004 
00005 #include <fesa-core/Exception/FesaException.h>
00006 
00007 #include <cstdio>
00008 
00009 
00010 namespace fesa
00011 {
00012 
00013 void StringUtilities::tokenize(const std::string& str, std::vector<std::string>& tokens,
00014                                const std::string& delimiters)
00015 {
00016     if ( delimiters.empty() )
00017         throw FesaException(__FILE__, __LINE__, FesaErrorEmptyDelimeter.c_str());
00018 
00019     if (str.empty())
00020         return;//nothing to do for an empty string
00021 
00022     size_t pos = 0;//the position where we currently are
00023     size_t found = 0;//the next found delimiter
00024     while (found != std::string::npos)//if we cannot find any additional delimiter
00025     {
00026         pos = str.find_first_not_of(delimiters, found);//we move pos to the next valid character
00027         if (pos == std::string::npos)//if there is nothing left to tokenize
00028             return;
00029         found = str.find_first_of(delimiters, pos);//find next delimiters
00030         tokens.push_back(str.substr(pos,found - pos));
00031     }
00032 }
00033 
00034 void StringUtilities::trimWhiteSpace(std::string& str)
00035 {
00036     size_t pos = str.find(" ");
00037     while (pos != std::string::npos)
00038     {
00039         str.erase(pos, 1);
00040         pos = str.find(" ");
00041     }
00042 }
00043 
00044 void StringUtilities::removeBrackets(std::string& str)
00045 {
00046     //In front of the bracket, only spaces are allowed
00047     uint32_t openBracket = static_cast<uint32_t>(str.find_first_not_of(' '));
00048     if (openBracket == std::string::npos || str[openBracket] != '{')
00049         throw FesaException(__FILE__, __LINE__, FesaErrorMalFormatedData.c_str(),str.c_str());
00050     str.erase(0, openBracket+1);
00051 
00052     uint32_t closeBracket = static_cast<uint32_t>(str.find_last_not_of(' '));
00053     if (closeBracket == std::string::npos || str[closeBracket] != '}')
00054         throw FesaException(__FILE__, __LINE__, FesaErrorMalFormatedData.c_str(),str.c_str());
00055     str.erase(closeBracket, str.size()-closeBracket);
00056 }
00057 
00058 void StringUtilities::addBrackets(std::string& str)
00059 {
00060     str = '{' + str + '}';
00061 }
00062 
00063 std::string StringUtilities::toString(signed char data)
00064 {
00065     std::ostringstream oss;
00066     oss << (int32_t) data;
00067     return oss.str();
00068 }
00069 
00070 std::string StringUtilities::toString(uint32_t data)
00071 {
00072     char tmp[32];
00073     sprintf(tmp, "%u", data);
00074     return tmp;
00075 }
00076 
00077 std::string StringUtilities::toString(int32_t data)
00078 {
00079     char tmp[32];
00080     sprintf(tmp, "%d", data);
00081     return tmp;
00082 }
00083 
00084 std::string StringUtilities::toString(int64_t data)
00085 {
00086     char tmp[32];
00087     sprintf(tmp, "%lld", static_cast<long long>(data));
00088     return tmp;
00089 }
00090 
00091 std::string StringUtilities::toString(double data)
00092 {
00093     char tmp[32];
00094     sprintf(tmp, "%f", data);
00095     return tmp;
00096 }
00097 
00098 std::string StringUtilities::toString(const void * ptr)
00099 {
00100     char tmp[32];
00101     sprintf(tmp, "%p", ptr);
00102     return tmp;
00103 }
00104 
00105 void StringUtilities::getElements(const std::string& str, std::vector<std::string>& elements)
00106 {
00107     uint32_t pos = 0;
00108     uint32_t openBrackets = 0;
00109     uint32_t i;
00110     uint32_t size = static_cast<uint32_t>(str.length());
00111     for (i = 0; i < size; ++i)
00112     {
00113         if (str.at(i) == ',' && openBrackets == 0)
00114         {// we found a new element
00115             elements.push_back(str.substr(pos, i - pos));
00116             pos = i + 1;
00117         }
00118         else if (str.at(i) == '{')
00119             openBrackets++;
00120         else if (str.at(i) == '}')
00121             openBrackets--;
00122 
00123     }
00124     // add the last element
00125     if (pos != i)
00126     {
00127         elements.push_back(str.substr(pos, i - pos));
00128     }
00129     if (openBrackets != 0)
00130     {
00131         throw FesaException(__FILE__, __LINE__, FesaErrorMissingBrackets.c_str());
00132     }
00133 }
00134 
00135 void StringUtilities::addElements(std::string& str,const std::vector<std::string>& elements)
00136 {
00137     //if there is already an entry, we just append
00138     if(!str.empty())
00139         str += ',';
00140 
00141     std::vector<std::string>::const_iterator iter;
00142     for(iter=elements.begin();iter!=elements.end();iter++)
00143     {
00144         str += (*iter).c_str();
00145 
00146         //dont add colon after last element
00147         if(iter+1!=elements.end())
00148             str += ',';
00149     }
00150 }
00151 
00152 } // fesa

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1