ReadStream.h
Go to the documentation of this file.
1 #ifndef _utl_ReadStream_h_
2 #define _utl_ReadStream_h_
3 
4 #include <utl/ShadowPtrPolicy.h>
5 #include <boost/lexical_cast.hpp>
6 #include <boost/algorithm/string/trim.hpp>
7 #include <vector>
8 #include <iostream>
9 #include <string>
10 
11 
12 namespace utl {
13 
14  /*
15  \brief Read simple structured data from stream sources
16 
17  The only requirement for reading a colon-structured data is that
18  is is described by some type T with the operator>> implemented.
19 
20  Example:
21  File contains columns: int time, double pressure, bool flag
22 
23  The structure is described by the following class:
24  \code
25  struct Data {
26  int fTime;
27  double fPressure;
28  bool fFlag;
29  };
30 
31  inline
32  std::istream&
33  operator>>(std::istream& is, Data& d)
34  {
35  return is >> d.fTime >> std::ws >> d.fPressure >> std::ws >> d.fFlag;
36  }
37  \endcode
38 
39  Having that, the whole file is read as
40  \code
41  ifstream file("foo.txt");
42  vector<Data> v;
43  ReadStream(file).GetLines(v);
44  \endcode
45 
46  By default, the GetLines() method does whitespace and comment stripping before
47  the string is passed to the boost::lexical_cast (which uses operator>>). If not
48  desired, this white-space compression and comment stripping can be switched off
49  by adding 'false' to the GetLines() call.
50 
51  The following types of input is stripped:
52  <ul>
53  <li> empty lines or lines with only white-space characters (this is done even
54  when 'false' is passed)
55  <li> shell-like comments: all the rest of the line after '#' is discarded
56  <li> C-like comments: all characters between inclusive '&47;*' and '*&47;' are removed
57  <li> C++-like comments: all characters after and including '//' are discarded
58  </ul>
59 
60  If your file contain more columns which you don't want to parse, issue
61  is.setstate(ios_base::eofbit) after you've read all the needed columns.
62  boost::lexical_cast namely checks for the state of the stream after operator>>
63  is called and requires the EOF state to be set.
64 
65  \author Darko Veberic
66  */
67 
68  class ReadStream {
69 
70  public:
71  ReadStream() = default;
72 
73  ReadStream(std::istream& is) : fStream(&is) { }
74 
75  void SetStream(std::istream& is) { fStream = &is; }
76 
77  std::istream& GetStream() const
79 
80  void Clear() { fStream = nullptr; }
81 
82  template<typename T> bool Get(T& t)
84 
85  template<typename T>
86  bool
87  GetLine(T& t, const bool trim = false)
88  {
90  std::string line;
91  if (!std::getline(*fStream, line))
92  return false;
93  if (trim)
94  boost::algorithm::trim(line);
95  t = boost::lexical_cast<T>(line);
96  return true;
97  }
98 
99  template<typename T>
100  void
101  GetLines(std::vector<T>& v, const bool filterComments = true, const bool trim = false)
102  {
104  std::string line;
105  while (std::getline(*fStream, line)) {
106  if (filterComments)
107  line = FilterComments(line);
108  if (trim)
109  boost::algorithm::trim(line);
110  if (!line.empty())
111  v.push_back(boost::lexical_cast<T>(line));
112  }
113  }
114 
115  explicit operator bool() const { return fStream && *fStream; }
116 
117  private:
118  ReadStream(const ReadStream&);
120 
121  static std::string FilterComments(const std::string& line);
122 
123  std::istream* fStream = nullptr;
124 
125  };
126 
127 }
128 
129 
130 #endif
std::istream * fStream
Definition: ReadStream.h:123
bool is(const double a, const double b)
Definition: testlib.cc:113
std::istream & GetStream() const
Definition: ReadStream.h:77
static std::string FilterComments(const std::string &line)
Definition: ReadStream.cc:10
static void Examine(T *const ptr)
ReadStream & operator=(const ReadStream &)
bool Get(T &t)
Definition: ReadStream.h:82
ReadStream(std::istream &is)
Definition: ReadStream.h:73
ReadStream()=default
void SetStream(std::istream &is)
Definition: ReadStream.h:75
bool GetLine(T &t, const bool trim=false)
Definition: ReadStream.h:87
void GetLines(std::vector< T > &v, const bool filterComments=true, const bool trim=false)
Definition: ReadStream.h:101

, generated on Tue Sep 26 2023.