string_util.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00033 #ifndef __string_util__header__
00034 #define __string_util__header__
00035 
00036 #include "gpucalc/data.h"
00037 
00038 
00039 namespace gpucalc
00040 {
00041 
00042 
00043 
00044  typedef std::vector<std::string> StringList;
00045     typedef std::map<std::string, std::string> StringMap;
00046 
00047 
00048 
00049  namespace auxillary
00050  {
00054   class _GpuCalcExport StringUtil
00055   {
00056 
00057 
00058    OBJ_DISABLE_COPY(StringUtil)
00059 
00060     StringUtil();
00061     ~StringUtil();
00062    
00063    
00064    
00065    
00066    public:
00067 
00068 
00072     template <typename T> static std::string toString(const T & Value)
00073     {
00074      std::ostringstream s;
00075      s << Value;
00076      return s.str();
00077     }
00078 
00079 
00083     static std::string toString(const bool & Value)
00084     {
00085      if (Value)
00086      {
00087       return std::string("true");
00088      }
00089      else
00090      {
00091       return std::string("false");
00092      }
00093     }
00094 
00095 
00096     template <typename T> static void fromString(const std::string & str, T & Value)
00097     {
00098      std::istringstream s(str);
00099      //s.get(str.c_str(), str.length());
00100      s >> Value;
00101     }
00102 
00103 
00107     static std::string toString(const ValueType & Value)
00108     {
00109      switch (Value)
00110      {
00111       case FloatType:
00112        return "Single precision floating point type";
00113        break;
00114 
00115       case DoubleType:
00116        return "Double precision floating point type";
00117        break;
00118 
00119       case IntType:
00120        return "Signed integer type";
00121        break;
00122 
00123       case UnsignedIntType:
00124        return "Unsigned integer type";
00125        break;
00126 
00127       case CharType:
00128        return "Signed character type";
00129        break;
00130 
00131       case UnsignedCharType:
00132        return "Unsigned character type";
00133        break;
00134 
00135       default:
00136        return "Not correct type";
00137        break;
00138      }
00139     }
00140 
00141 
00145     static std::string toString(const Object & Value)
00146     {
00147      return "Object with class name: \"" + Value.getClassName() + "\". And with object name: \"" + Value.getObjectName() + "\"";
00148     }
00149 
00150 
00154     static std::string toLower(const std::string & Value)
00155     {
00156      std::string result(Value);
00157      std::transform(Value.begin(), Value.end(), result.begin(), tolower);
00158      return result;
00159     }
00160 
00161 
00165     static std::string toUpper(const std::string & Value)
00166     {
00167      std::string result(Value);
00168      std::transform(Value.begin(), Value.end(), result.begin(), toupper);
00169      return result;
00170     }
00171 
00172 
00178     static std::string getLine(const std::string & Source, size_t Line)
00179     {
00180      std::string::size_type i_old = 0;
00181      std::string::size_type i_new = Source.find("\n");
00182      unsigned int Counter = 0;
00183      while (i_new != std::string::npos)
00184      {
00185       if (Line == Counter)
00186       {
00187        return std::string(Source, i_old, i_new - i_old);
00188       }
00189       i_old = i_new + 1;
00190       i_new = Source.find("\n", i_old);
00191       ++Counter;
00192      }
00193      Except<ERR_ITEM_NOT_FOUND>(Object("StringUtil", "StringUtil"), "Line number is not correct",
00194       "StringUtil::getLineFromSource", __FILE__, __LINE__);
00195     }
00196 
00197 
00201     static StringList split(const std::string & String, const std::string & Delimiter = "\n")
00202     {
00203      StringList result;
00204      std::string::size_type i_old = 0;
00205      std::string::size_type i_new = String.find(Delimiter);
00206      while (i_new != std::string::npos)
00207      {
00208       std::string temp(String, i_old, i_new - i_old);
00209       result.push_back(temp);
00210       i_old = i_new + Delimiter.length();
00211       i_new = String.find(Delimiter, i_old);
00212      }
00213      std::string temp(String, i_old, i_new - i_old);
00214      result.push_back(temp);
00215 
00216      return result;
00217     }
00218 
00219 
00223     static std::string concatenate(const StringList & sList, const std::string & Delimiter = "\n")
00224     {
00225      std::ostringstream s;
00226      std::copy(sList.begin(), sList.end(), std::ostream_iterator<std::string>(s, Delimiter.c_str()));
00227      return s.str();
00228     }
00229 
00230 
00231                 static std::string getFixedString(const std::string & String, unsigned int FixedStrLength, const std::string & Prefix = "", const std::string & Suffix = "")
00232                 {
00233                     std::string result;
00234                     unsigned int length = String.length();
00235                     if (length < (FixedStrLength - 1))
00236                     {
00237                         result = String + std::string(FixedStrLength - length, ' ');
00238                     }
00239                     else
00240                     {
00241                         result = std::string(String, 0, FixedStrLength - 4) + "... ";
00242                     }
00243                     return Prefix + result + Suffix;
00244                 }
00245   };
00246  }
00247 }
00248 
00249 #endif

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6