string_helper.hpp

Go to the documentation of this file.
00001 /*
00002 **    TP ETI5 CPE Lyon
00003 **    Copyright (C) 2010 Damien Rohmer
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 3 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, see <http://www.gnu.org/licenses/>.
00017 */
00018 
00019 
00020 #ifndef _STRING_HELPER_H_
00021 #define _STRING_HELPER_H_
00022 
00023 #include <stdlib.h>
00024 #include <vector>
00025 #include <iostream>
00026 #include <sstream>
00027 #include <string>
00028 
00029 
00030 
00031 namespace cpe
00032 {
00033 
00034 
00035 
00037 
00039   class string_converter
00040   {
00041 
00042 
00043   public:
00044 
00045 
00051       template <typename T>
00052               static T value_of(const std::string& in,bool *is_ok=0)
00053       {
00054           T obj;
00055           std::istringstream is(in);
00056           bool _is_ok=bool(is>>obj);
00057           if(is_ok!=0)
00058               *is_ok=_is_ok;
00059           return obj;
00060       }
00061 
00063       template <typename T>
00064               static std::vector <T> value_of(const std::vector <std::string>& in,bool *is_ok=0)
00065       {
00066 
00067           if(is_ok!=0)
00068               *is_ok=true;
00069 
00070           int N=in.size();
00071           std::vector <T> obj(N);
00072           bool temp_is_ok=true;
00073           for(int k=0;k<N;++k)
00074           {
00075               obj[k]=value_of<T> (in[k],&temp_is_ok);
00076               if(is_ok!=0 && temp_is_ok==false)
00077                   *is_ok=false;
00078           }
00079           return obj;
00080       }
00081 
00086     template <typename T>
00087     static std::string to_string(const T& t)
00088     {
00089       std::ostringstream oss;
00090       oss<<t;
00091       return std::string(oss.str());
00092     }
00093 
00100     static std::string zero_padding(const std::string& input,const int& zero_number=4);
00101 
00103     template <typename T>
00104             static std::string to_string_padded(const T& x,const unsigned int& zero_number=4)
00105     {return string_converter::zero_padding(string_converter::to_string(x),zero_number);}
00106 
00108     static std::string to_lower(const std::string& input);
00110     static std::string to_upper(const std::string& input);
00111 
00113     static std::vector <std::string> delete_empty(const std::vector <std::string>& in);
00114 
00115 
00124     static std::pair<std::pair<int,int>,std::pair<std::string,std::string> > extract_number_part(const std::string& filename);
00125 
00130     static std::vector<std::string> load_filename_sequence(const std::string filename,const unsigned int& iteration=1);
00131 
00132 
00133   private:
00134   };
00135 
00136 
00138   class string_tokenizer
00139   {
00140 
00141   public:
00149       static std::vector <std::string> tokenize(const std::string& str,const std::string& delimiters=" ");
00150   private:
00151   };
00152 
00154   class file_helper
00155   {
00156       public:
00157 
00163       static file_helper copy(const std::string& input_filename,const std::string& output_filename);
00164 
00165       private:
00166   };
00167 
00168 
00169 }
00170 
00171 #endif
Generated on Mon Aug 22 15:53:12 2011 by  doxygen 1.6.3