libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
csv_iterator_impl.hpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright © 2020 by J.M.McGuiness, coder@hussar.me.uk & M.Waplington
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE {
20 
21 inline
22 csv_iterator::csv_iterator(char delim) noexcept(true)
23 : delim_(delim) {
24 }
25 
26 inline
27 csv_iterator::csv_iterator(std::string const& data, char delim) noexcept(false)
28 : data_(&data), delim_(delim) {
29  read();
30 }
31 
32 inline csv_iterator::reference
33 csv_iterator::operator*() const noexcept(true) {
34  return field_;
35 }
36 
37 inline csv_iterator::pointer
38 csv_iterator::operator->() const noexcept(true) {
39  return &field_;
40 }
41 
42 inline csv_iterator
43 csv_iterator::operator++() noexcept(false) {
44  read();
45  return *this;
46 }
47 
48 inline csv_iterator
49 csv_iterator::operator++(int) noexcept(false) {
50  csv_iterator tmp=*this;
51  read();
52  return tmp;
53 }
54 
55 inline bool
56 csv_iterator::operator==(csv_iterator const& i) const noexcept(true) {
57  return (data_==i.data_ && valid_==i.valid_ && pos_==i.pos_)
58  || (!valid_ && !i.valid_);
59 }
60 
61 inline bool
62 csv_iterator::operator!=(csv_iterator const& i) const noexcept(true) {
63  return !(*this==i);
64 }
65 
66 inline void
67 csv_iterator::read() noexcept(false) {
68  valid_=data_ && (pos_<data_->size());
69  if (valid_) {
70  const std::size_t last_pos=pos_;
71  pos_=data_->find(delim_, last_pos);
72  if (pos_==std::string::npos) {
73  pos_=data_->size();
74  }
75  assert(pos_>last_pos);
76  field_=data_->substr(last_pos, pos_-last_pos);
77  ++pos_;
78  }
79 }
80 
81 } }