string_helper.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2013 Damien Rohmer
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
20 #ifndef STRING_HELPER_H_
21 #define STRING_HELPER_H_
22 
23 #include <stdlib.h>
24 #include <vector>
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28 
29 
30 
31 namespace cpe
32 {
33 
34 
35 
37 
40  {
41 
42 
43  public:
44 
45 
51  template <typename T>
52  static T value_of(const std::string& in,bool *is_ok=0)
53  {
54  T obj;
55  std::istringstream is(in);
56  bool _is_ok=bool(is>>obj);
57  if(is_ok!=0)
58  *is_ok=_is_ok;
59  return obj;
60  }
61 
63  template <typename T>
64  static std::vector <T> value_of(const std::vector <std::string>& in,bool *is_ok=0)
65  {
66 
67  if(is_ok!=0)
68  *is_ok=true;
69 
70  int N=in.size();
71  std::vector <T> obj(N);
72  bool temp_is_ok=true;
73  for(int k=0;k<N;++k)
74  {
75  obj[k]=value_of<T> (in[k],&temp_is_ok);
76  if(is_ok!=0 && temp_is_ok==false)
77  *is_ok=false;
78  }
79  return obj;
80  }
81 
86  template <typename T>
87  static std::string to_string(const T& t)
88  {
89  std::ostringstream oss;
90  oss<<t;
91  return std::string(oss.str());
92  }
93 
100  static std::string zero_padding(const std::string& input,const int& zero_number=4);
101 
103  template <typename T>
104  static std::string to_string_padded(const T& x,const unsigned int& zero_number=4)
106 
108  static std::string to_lower(const std::string& input);
110  static std::string to_upper(const std::string& input);
111 
113  static std::vector <std::string> delete_empty(const std::vector <std::string>& in);
114 
115 
124  static std::pair<std::pair<int,int>,std::pair<std::string,std::string> > extract_number_part(const std::string& filename);
125 
130  static std::vector<std::string> load_filename_sequence(const std::string filename,const unsigned int& iteration=1);
131 
132 
133  private:
134  };
135 
136 
139  {
140 
141  public:
149  static std::vector <std::string> tokenize(const std::string& str,const std::string& delimiters=" ");
150  private:
151  };
152 
155  {
156  public:
157 
163  static file_helper copy(const std::string& input_filename,const std::string& output_filename);
164 
165  private:
166  };
167 
168 
169 }
170 
171 #endif