Horizon
getlines.hpp
Go to the documentation of this file.
1 // Range v3 library
3 //
4 // Copyright Eric Niebler 2013-present
5 //
6 // Use, modification and distribution is subject to the
7 // Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
10 //
11 // Project home: https://github.com/ericniebler/range-v3
12 //
13 
14 #ifndef RANGES_V3_VIEW_GETLINES_HPP
15 #define RANGES_V3_VIEW_GETLINES_HPP
16 
17 #include <istream>
18 #include <string>
19 
20 #include <range/v3/range_fwd.hpp>
21 
23 #include <range/v3/utility/static_const.hpp>
24 #include <range/v3/view/facade.hpp>
25 
26 #include <range/v3/detail/prologue.hpp>
27 
28 namespace ranges
29 {
32  struct getlines_view : view_facade<getlines_view, unknown>
33  {
34  private:
35  friend range_access;
36  std::istream * sin_;
37  std::string str_;
38  char delim_;
39  struct cursor
40  {
41  private:
42  friend range_access;
43  using single_pass = std::true_type;
44  getlines_view * rng_ = nullptr;
45 
46  public:
47  cursor() = default;
48  explicit cursor(getlines_view * rng)
49  : rng_(rng)
50  {}
51  void next()
52  {
53  rng_->next();
54  }
55  std::string & read() const noexcept
56  {
57  return rng_->str_;
58  }
59  bool equal(default_sentinel_t) const
60  {
61  return !rng_->sin_;
62  }
63  bool equal(cursor that) const
64  {
65  return !rng_->sin_ == !that.rng_->sin_;
66  }
67  };
68  void next()
69  {
70  if(!std::getline(*sin_, str_, delim_))
71  sin_ = nullptr;
72  }
73  cursor begin_cursor()
74  {
75  return cursor{this};
76  }
77 
78  public:
79  getlines_view() = default;
80  getlines_view(std::istream & sin, char delim = '\n')
81  : sin_(&sin)
82  , str_{}
83  , delim_(delim)
84  {
85  this->next(); // prime the pump
86  }
87  std::string & cached() noexcept
88  {
89  return str_;
90  }
91  };
92 
94  using getlines_range RANGES_DEPRECATED(
95  "getlines_range has been renamed getlines_view") = getlines_view;
97 
98  struct getlines_fn
99  {
100  getlines_view operator()(std::istream & sin, char delim = '\n') const
101  {
102  return getlines_view{sin, delim};
103  }
104  };
105 
108 } // namespace ranges
109 
110 #include <range/v3/detail/epilogue.hpp>
111 
112 #endif
RANGES_INLINE_VARIABLE(detail::to_container_fn< detail::from_range< std::vector >>, to_vector) template< template< typename... > class ContT > auto to(RANGES_HIDDEN_DETAIL(detail
For initializing a container of the specified type with the elements of an Range.
Definition: conversion.hpp:399
Definition: default_sentinel.hpp:26
Definition: getlines.hpp:99
Definition: getlines.hpp:33
A utility for constructing a view from a (derived) type that implements begin and end cursors.
Definition: facade.hpp:66