All Classes Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
lines.hpp
1 // The MIT License (MIT)
2 
3 // Copyright (c) 2012-2014 Danny Y., Rapptz
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of
6 // this software and associated documentation files (the "Software"), to deal in
7 // the Software without restriction, including without limitation the rights to
8 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 // the Software, and to permit persons to whom the Software is furnished to do so,
10 // subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #ifndef GEARS_IO_LINES_HPP
23 #define GEARS_IO_LINES_HPP
24 
25 #include <istream>
26 #include <string>
27 #include <iterator>
28 
29 namespace gears {
30 namespace io {
41 template<typename CharT = char, typename Traits = std::char_traits<CharT>>
42 struct line_iterator : std::iterator<std::input_iterator_tag, std::basic_string<CharT, Traits>> {
43 private:
44  std::basic_istream<CharT, Traits>* reader;
45  std::basic_string<CharT, Traits> value;
46  bool status;
47 public:
48  line_iterator() noexcept: reader(nullptr), status(false) {}
49  line_iterator(std::basic_istream<CharT, Traits>& out) noexcept: reader(&out) {
50  status = reader && *reader && std::getline(*reader, value);
51  }
52 
53  auto operator++() noexcept -> decltype(*this) {
54  status = reader && *reader && std::getline(*reader, value);
55  return *this;
56  }
57 
58  line_iterator operator++(int) noexcept {
59  auto copy = *this;
60  ++(*this);
61  return copy;
62  }
63 
64  auto operator*() noexcept -> decltype(value) {
65  return value;
66  }
67 
68  auto operator->() noexcept -> decltype(&value) {
69  return &value;
70  }
71 
72  bool operator==(const line_iterator& other) const noexcept {
73  return (status == other.status) && (!status || reader == other.reader);
74  }
75 
76  bool operator!=(const line_iterator& other) const noexcept {
77  return not (*this == other);
78  }
79 };
80 
90 template<typename CharT, typename Traits>
91 struct line_reader {
92 private:
93  std::basic_istream<CharT, Traits>& in;
94 public:
95  line_reader(std::basic_istream<CharT, Traits>& in) noexcept: in(in) {}
96 
97  line_iterator<CharT, Traits> begin() noexcept {
98  return { in };
99  }
100 
101  line_iterator<CharT, Traits> end() noexcept {
102  return { };
103  }
104 };
105 
131 template<typename CharT, typename Traits>
132 inline line_reader<CharT, Traits> lines(std::basic_istream<CharT, Traits>& in) {
133  return line_reader<CharT, Traits>(in);
134 }
135 } // io
136 } // gears
137 
138 #endif // GEARS_IO_LINES_HPP