A library for working with phylogenetic and population genetic data.
v0.32.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
genesis::utils::InputStream
Stream interface for reading data from an InputSource, that keeps track of line and column counters.
Definition: input_stream.hpp:88
genesis::utils::CsvInputIterator::operator!=
bool operator!=(self_type const &other) const
Definition: utils/formats/csv/input_iterator.hpp:117
genesis::utils::CsvReader::parse_line
std::vector< std::string > parse_line(utils::InputStream &input_stream) const
Parse one line of the CSV data and return it.
Definition: utils/formats/csv/reader.cpp:160
genesis::utils::CsvInputIterator::operator->
value_type const * operator->() const
Definition: utils/formats/csv/input_iterator.hpp:139
genesis::utils::CsvInputIterator::end
self_type end()
End of the iterator.
Definition: utils/formats/csv/input_iterator.hpp:169
genesis::utils::CsvInputIterator::reference
value_type & reference
Definition: utils/formats/csv/input_iterator.hpp:61
genesis::utils::CsvInputIterator::CsvInputIterator
CsvInputIterator()
Create a default instance, with no input.
Definition: utils/formats/csv/input_iterator.hpp:71
genesis::utils::CsvInputIterator::dereference
value_type const & dereference() const
Definition: utils/formats/csv/input_iterator.hpp:144
genesis::utils::CsvInputIterator::self_type
CsvInputIterator self_type
Definition: utils/formats/csv/input_iterator.hpp:58
genesis::utils::CsvInputIterator::begin
self_type & begin()
Beginning of the iterator.
Definition: utils/formats/csv/input_iterator.hpp:159
std.hpp
Provides some valuable additions to STD.
genesis::utils::CsvInputIterator::operator==
bool operator==(self_type const &other) const
Definition: utils/formats/csv/input_iterator.hpp:112
genesis::utils::CsvInputIterator::pointer
value_type * pointer
Definition: utils/formats/csv/input_iterator.hpp:60
reader.hpp
genesis::utils::CsvInputIterator::operator*
value_type const & operator*() const
Definition: utils/formats/csv/input_iterator.hpp:134
genesis::utils::CsvInputIterator::operator=
CsvInputIterator & operator=(CsvInputIterator const &)=default
input_stream.hpp
genesis::utils::CsvInputIterator::~CsvInputIterator
~CsvInputIterator()=default
genesis::utils::CsvInputIterator
Definition: utils/formats/csv/input_iterator.hpp:50
genesis::utils::CsvInputIterator::increment
void increment()
Definition: utils/formats/csv/input_iterator.hpp:180
genesis
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Definition: placement/formats/edge_color.cpp:42
genesis::utils::CsvInputIterator::CsvInputIterator
CsvInputIterator(std::shared_ptr< utils::BaseInputSource > source)
Create an instance that reads from an input source, using a default CsvReader.
Definition: utils/formats/csv/input_iterator.hpp:80
genesis::utils::CsvInputIterator::operator++
self_type & operator++()
Definition: utils/formats/csv/input_iterator.hpp:174
genesis::utils::CsvReader::Line
std::vector< Field > Line
Definition: utils/formats/csv/reader.hpp:79
genesis::utils::CsvReader
Read Comma/Character Separated Values (CSV) data and other delimiter-separated formats.
Definition: utils/formats/csv/reader.hpp:70
genesis::utils::CsvInputIterator::CsvInputIterator
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.
Definition: utils/formats/csv/input_iterator.hpp:92
genesis::utils::CsvInputIterator::iterator_category
std::input_iterator_tag iterator_category
Definition: utils/formats/csv/input_iterator.hpp:62
genesis::utils::CsvInputIterator::value_type
CsvReader::Line value_type
Definition: utils/formats/csv/input_iterator.hpp:59