string_helper.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2014 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 #pragma once
21 
22 #ifndef STRING_HELPER_H
23 #define STRING_HELPER_H
24 
25 #include <stdlib.h>
26 #include <vector>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 
31 
32 
33 namespace cpe
34 {
35 
36 
37 
39 
42  {
43 
44 
45  public:
46 
47 
53  template <typename T>
54  static T value_of(const std::string& in,bool *is_ok=0)
55  {
56  T obj;
57  std::istringstream is(in);
58  bool _is_ok=bool(is>>obj);
59  if(is_ok!=0)
60  *is_ok=_is_ok;
61  return obj;
62  }
63 
65  template <typename T>
66  static std::vector <T> value_of(const std::vector <std::string>& in,bool *is_ok=0)
67  {
68 
69  if(is_ok!=0)
70  *is_ok=true;
71 
72  int N=in.size();
73  std::vector <T> obj(N);
74  bool temp_is_ok=true;
75  for(int k=0;k<N;++k)
76  {
77  obj[k]=value_of<T> (in[k],&temp_is_ok);
78  if(is_ok!=0 && temp_is_ok==false)
79  *is_ok=false;
80  }
81  return obj;
82  }
83 
88  template <typename T>
89  static std::string to_string(const T& t)
90  {
91  std::ostringstream oss;
92  oss<<t;
93  return std::string(oss.str());
94  }
95 
102  static std::string zero_padding(const std::string& input,const int& zero_number=4);
103 
105  template <typename T>
106  static std::string to_string_padded(const T& x,const unsigned int& zero_number=4)
108 
110  static std::string to_lower(const std::string& input);
112  static std::string to_upper(const std::string& input);
113 
115  static std::vector <std::string> delete_empty(const std::vector <std::string>& in);
116 
117 
126  static std::pair<std::pair<int,int>,std::pair<std::string,std::string> > extract_number_part(const std::string& filename);
127 
132  static std::vector<std::string> load_filename_sequence(const std::string filename,const unsigned int& iteration=1);
133 
134 
135  private:
136  };
137 
138 
141  {
142 
143  public:
151  static std::vector <std::string> tokenize(const std::string& str,const std::string& delimiters=" ");
152  private:
153  };
154 
157  {
158  public:
159 
165  static file_helper copy(const std::string& input_filename,const std::string& output_filename);
166 
167  private:
168  };
169 
170 
171 }
172 
173 #endif
static std::vector< std::string > load_filename_sequence(const std::string filename, const unsigned int &iteration=1)
load file sequence
Definition: string_helper.cpp:169
static std::string to_string_padded(const T &x, const unsigned int &zero_number=4)
helper to write padded string from a number easily
Definition: string_helper.hpp:106
A helper class to manipulate string (token, convert, ...)
Definition: string_helper.hpp:41
static std::string to_lower(const std::string &input)
return the lower case of a string
Definition: string_helper.cpp:72
static T value_of(const std::string &in, bool *is_ok=0)
return the value of the given string
Definition: string_helper.hpp:54
helper class with files
Definition: string_helper.hpp:156
static std::string to_upper(const std::string &input)
return the upper case of a string
Definition: string_helper.cpp:79
Helper class to tokenize easily a string.
Definition: string_helper.hpp:140
static std::string zero_padding(const std::string &input, const int &zero_number=4)
fill the appropriate number of zero to have the same size
Definition: string_helper.cpp:64
static std::vector< T > value_of(const std::vector< std::string > &in, bool *is_ok=0)
return the value of the given vector of string
Definition: string_helper.hpp:66
static std::string to_string(const T &t)
get the string corresponding to the given value
Definition: string_helper.hpp:89
static std::vector< std::string > delete_empty(const std::vector< std::string > &in)
delete the empty space of a vector of string
Definition: string_helper.cpp:87
static std::vector< std::string > tokenize(const std::string &str, const std::string &delimiters=" ")
tokenize a given string
Definition: string_helper.cpp:31
static file_helper copy(const std::string &input_filename, const std::string &output_filename)
copy the content of a file into an other one
Definition: string_helper.cpp:98
static std::pair< std::pair< int, int >, std::pair< std::string, std::string > > extract_number_part(const std::string &filename)
extract the last number part of a string and give the number of zeros
Definition: string_helper.cpp:124