A toolkit for working with phylogenetic data.
v0.24.0
utils/formats/csv/input_iterator.hpp
Go to the documentation of this file.
1 #ifndef GENESIS_UTILS_FORMATS_CSV_INPUT_ITERATOR_H_
2 #define GENESIS_UTILS_FORMATS_CSV_INPUT_ITERATOR_H_
3 
4 /*
5  Genesis - A toolkit for working with phylogenetic data.
6  Copyright (C) 2014-2020 Lucas Czech and HITS gGmbH
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 
21  Contact:
22  Lucas Czech <lucas.czech@h-its.org>
23  Exelixis Lab, Heidelberg Institute for Theoretical Studies
24  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
25 */
26 
37 
38 #include <iosfwd>
39 #include <memory>
40 #include <string>
41 #include <vector>
42 
43 namespace genesis {
44 namespace utils {
45 
46 // =================================================================================================
47 // Csv Input Iterator
48 // =================================================================================================
49 
51 {
52 public:
53 
54  // ---------------------------------------------------------------------
55  // Typedefs and Enums
56  // ---------------------------------------------------------------------
57 
60  using pointer = value_type&;
62  using iterator_category = std::input_iterator_tag;
63 
64  // ---------------------------------------------------------------------
65  // Constructor and Rule of Five
66  // ---------------------------------------------------------------------
67 
72  : input_stream_( nullptr )
73  , reader_()
74  , line_()
75  {}
76 
80  explicit CsvInputIterator( std::shared_ptr<utils::BaseInputSource> source )
81  : input_stream_( std::make_shared<utils::InputStream>( source ))
82  , reader_()
83  , line_()
84  {
85  increment();
86  }
87 
92  CsvInputIterator( std::shared_ptr<utils::BaseInputSource> source, CsvReader const& settings )
93  : input_stream_( std::make_shared<utils::InputStream>( source ))
94  , reader_( settings )
95  , line_()
96  {
97  increment();
98  }
99 
100  ~CsvInputIterator() = default;
101 
102  CsvInputIterator( CsvInputIterator const& ) = default;
103  CsvInputIterator( CsvInputIterator&& ) = default;
104 
105  CsvInputIterator& operator= ( CsvInputIterator const& ) = default;
107 
108  // -------------------------------------------------------------------------
109  // Comparators
110  // -------------------------------------------------------------------------
111 
112  bool operator == ( self_type const& other ) const
113  {
114  return input_stream_ == other.input_stream_;
115  }
116 
117  bool operator != ( self_type const& other ) const
118  {
119  return !( *this == other );
120  }
121 
125  explicit operator bool() const
126  {
127  return good_;
128  }
129 
130  // -------------------------------------------------------------------------
131  // Accessors
132  // -------------------------------------------------------------------------
133 
134  value_type const& operator * () const
135  {
136  return line_;
137  }
138 
139  value_type const* operator -> () const
140  {
141  return &line_;
142  }
143 
144  value_type const& dereference() const
145  {
146  return line_;
147  }
148 
149  // -------------------------------------------------------------------------
150  // Iteration
151  // -------------------------------------------------------------------------
152 
160  {
161  return *this;
162  }
163 
170  {
171  return self_type();
172  }
173 
175  {
176  increment();
177  return *this;
178  }
179 
180  void increment()
181  {
182  // Check whether the input stream is good (not end-of-stream) and can be read from.
183  // If not, we reached its end, so we stop reading in the next iteration.
184  if( ! input_stream_ || ! *input_stream_ ) {
185  good_ = false;
186  return;
187  }
188 
189  line_ = reader_.parse_line( *input_stream_ );
190  }
191 
192  // -------------------------------------------------------------------------
193  // Data Members
194  // -------------------------------------------------------------------------
195 
196  private:
197 
198  std::shared_ptr<utils::InputStream> input_stream_;
199 
200  bool good_ = true;
201  CsvReader reader_;
202  CsvReader::Line line_;
203 };
204 
205 } // namespace utils
206 } // namespace genesis
207 
208 #endif // include guard
CsvInputIterator(std::shared_ptr< utils::BaseInputSource > source, CsvReader const &settings)
Create an instance that reads from an input source, using the settings of a given CsvReader...
CsvInputIterator()
Create a default instance, with no input.
bool operator==(self_type const &other) const
STL namespace.
Container namespace for all symbols of genesis in order to keep them separate when used as a library...
bool operator!=(self_type const &other) const
Provides some valuable additions to STD.
CsvInputIterator & operator=(CsvInputIterator const &)=default
CsvInputIterator(std::shared_ptr< utils::BaseInputSource > source)
Create an instance that reads from an input source, using a default CsvReader.
self_type & begin()
Beginning of the iterator.
std::vector< std::string > parse_line(utils::InputStream &input_stream) const
Parse one line of the CSV data and return it.
Read Comma/Character Separated Values (CSV) data and other delimiter-separated formats.
Stream interface for reading data from an InputSource, that keeps track of line and column counters...