PCMStringConverter.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMStringConverter.h"
00030 
00031 namespace ParCompMark
00032 {
00033 
00034   //
00035   // Class constants
00036   //
00037 
00038   const s32 StringConverter::DEFAULTPRECISION = -1;
00039   const s32 StringConverter::DEFAULTFIELDWIDTH = -1;
00040 
00041   //
00042   // Class methods
00043   //
00044 
00045    std::string StringConverter::toString(const u8 & value)
00046   {
00047         std::ostringstream osstr;
00048         osstr << value;
00049         return osstr.str();
00050   }
00051 
00052  /*----------------------------------------------------------------------*/
00053 
00054   std::string StringConverter::toString(const u16 & value)
00055   {
00056         std::ostringstream osstr;
00057         osstr << value;
00058         return osstr.str();
00059   }
00060 
00061  /*----------------------------------------------------------------------*/
00062 
00063   std::string StringConverter::toString(const u32 & value)
00064   {
00065         std::ostringstream osstr;
00066         osstr << value;
00067         return osstr.str();
00068   }
00069 
00070  /*----------------------------------------------------------------------*/
00071 
00072   std::string StringConverter::toString(const s8 & value)
00073   {
00074         std::ostringstream osstr;
00075         osstr << value;
00076         return osstr.str();
00077   }
00078 
00079  /*----------------------------------------------------------------------*/
00080 
00081   std::string StringConverter::toString(const s16 & value)
00082   {
00083         std::ostringstream osstr;
00084         osstr << value;
00085         return osstr.str();
00086   }
00087 
00088  /*----------------------------------------------------------------------*/
00089 
00090   std::string StringConverter::toString(const s32 & value)
00091   {
00092         std::ostringstream osstr;
00093         osstr << value;
00094         return osstr.str();
00095   }
00096 
00097  /*----------------------------------------------------------------------*/
00098 
00099   std::string StringConverter::toString(const Real & value, const s32 & fieldWidth,
00100                                         const s32 & precision)
00101   {
00102         std::ostringstream osstr;
00103         if(precision == DEFAULTPRECISION)
00104          osstr << value;
00105         else
00106         {
00107          osstr.precision(precision);
00108          osstr << std::fixed << value;
00109         }
00110         return _fitFieldWidth(osstr.str(), fieldWidth);
00111   }
00112 
00113  /*----------------------------------------------------------------------*/
00114 
00115   std::string StringConverter::toString(const bool value)
00116   {
00117         std::ostringstream osstr;
00118         osstr << value;
00119         return osstr.str();
00120   }
00121 
00122  /*----------------------------------------------------------------------*/
00123 
00124   std::string StringConverter::toString(const void *value)
00125   {
00126         std::ostringstream osstr;
00127         osstr << value;
00128         return osstr.str();
00129   }
00130 
00131  /*----------------------------------------------------------------------*/
00132 
00133   u32 StringConverter::toU32(const std::string value)
00134   {
00135         u32 result;
00136 
00137         std::istringstream isstr(value);
00138 
00139         isstr >> result;
00140         return result;
00141   }
00142 
00143  /*----------------------------------------------------------------------*/
00144 
00145   Real StringConverter::toReal(const std::string value)
00146   {
00147         Real result;
00148 
00149         std::istringstream isstr(value);
00150 
00151         isstr >> result;
00152         return result;
00153   }
00154 
00155  /*----------------------------------------------------------------------*/
00156 
00157   void *StringConverter::parsePointer(const std::string value)
00158   {
00159         std::istringstream isstr(value, std::istringstream::in);
00160         void *p;
00161 
00162         isstr >> p;
00163         return p;
00164   }
00165 
00166  /*----------------------------------------------------------------------*/
00167 
00168   void StringConverter::trim(std::string & str)
00169   {
00170         std::string::size_type pos = str.find_last_not_of(" \t");
00171         if(pos != std::string::npos)
00172         {
00173          str.erase(pos + 1);
00174          pos = str.find_first_not_of(" \t");
00175          if(pos != std::string::npos)
00176                 str.erase(0, pos);
00177         } else
00178          str.erase(str.begin(), str.end());
00179   }
00180 
00181  /*----------------------------------------------------------------------*/
00182 
00183   std::vector < std::string > StringConverter::tokenize(const std::string & str, const char *delimiters)
00184   {
00185         std::vector < std::string > tokens;
00186 
00187         std::string::size_type lastPos = 0, pos = 0;
00188         int count = 0;
00189 
00190         if(str.length() < 1)
00191          return tokens;
00192 
00193         // skip delimiters at beginning. 
00194         lastPos = str.find_first_not_of(delimiters, 0);
00195 
00196         if((str.substr(0, lastPos - pos).length()) > 0)
00197         {
00198          count = str.substr(0, lastPos - pos).length();
00199 
00200          for(int i = 0; i < count; i++)
00201                 tokens.push_back("");
00202 
00203          if(std::string::npos == lastPos)
00204                 tokens.push_back("");
00205         }
00206         // find first "non-delimiter".
00207         pos = str.find_first_of(delimiters, lastPos);
00208 
00209         while(std::string::npos != pos || std::string::npos != lastPos)
00210         {
00211          // found a token, add it to the vector.
00212          tokens.push_back(str.substr(lastPos, pos - lastPos));
00213 
00214          // skip delimiters. Note the "not_of"
00215          lastPos = str.find_first_not_of(delimiters, pos);
00216 
00217          if((std::string::npos != pos) && (str.substr(pos, lastPos - pos).length() > 1))
00218          {
00219                 count = str.substr(pos, lastPos - pos).length();
00220 
00221                 for(int i = 0; i < count; i++)
00222                  tokens.push_back("");
00223          }
00224 
00225          pos = str.find_first_of(delimiters, lastPos);
00226         }
00227 
00228         return tokens;
00229   }
00230 
00231  /*----------------------------------------------------------------------*/
00232 
00233   std::string StringConverter::_fitFieldWidth(const std::string & str, const s32 & fieldWidth)
00234   {
00235         s32 padding = fieldWidth - str.size();
00236 
00237         if(fieldWidth == DEFAULTFIELDWIDTH || padding <= 0)
00238          return str;
00239         char fitStr[padding + 1];
00240 
00241         memset(fitStr, ' ', padding);
00242         fitStr[padding] = '\0';
00243 
00244         return fitStr + str;
00245   }
00246 
00247  /*----------------------------------------------------------------------*/
00248 
00249 }