Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:09:55

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Eric Niebler 2013-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 
0014 #ifndef RANGES_V3_VIEW_GETLINES_HPP
0015 #define RANGES_V3_VIEW_GETLINES_HPP
0016 
0017 #include <istream>
0018 #include <string>
0019 
0020 #include <range/v3/range_fwd.hpp>
0021 
0022 #include <range/v3/iterator/default_sentinel.hpp>
0023 #include <range/v3/utility/static_const.hpp>
0024 #include <range/v3/view/facade.hpp>
0025 
0026 #include <range/v3/detail/prologue.hpp>
0027 
0028 namespace ranges
0029 {
0030     /// \addtogroup group-views
0031     /// @{
0032     struct getlines_view : view_facade<getlines_view, unknown>
0033     {
0034     private:
0035         friend range_access;
0036         std::istream * sin_;
0037         std::string str_;
0038         char delim_;
0039         struct cursor
0040         {
0041         private:
0042             friend range_access;
0043             using single_pass = std::true_type;
0044             getlines_view * rng_ = nullptr;
0045 
0046         public:
0047             cursor() = default;
0048             explicit cursor(getlines_view * rng)
0049               : rng_(rng)
0050             {}
0051             void next()
0052             {
0053                 rng_->next();
0054             }
0055             std::string & read() const noexcept
0056             {
0057                 return rng_->str_;
0058             }
0059             bool equal(default_sentinel_t) const
0060             {
0061                 return !rng_->sin_;
0062             }
0063             bool equal(cursor that) const
0064             {
0065                 return !rng_->sin_ == !that.rng_->sin_;
0066             }
0067         };
0068         void next()
0069         {
0070             if(!std::getline(*sin_, str_, delim_))
0071                 sin_ = nullptr;
0072         }
0073         cursor begin_cursor()
0074         {
0075             return cursor{this};
0076         }
0077 
0078     public:
0079         getlines_view() = default;
0080         getlines_view(std::istream & sin, char delim = '\n')
0081           : sin_(&sin)
0082           , str_{}
0083           , delim_(delim)
0084         {
0085             this->next(); // prime the pump
0086         }
0087         std::string & cached() noexcept
0088         {
0089             return str_;
0090         }
0091     };
0092 
0093     /// \cond
0094     using getlines_range RANGES_DEPRECATED(
0095         "getlines_range has been renamed getlines_view") = getlines_view;
0096     /// \endcond
0097 
0098     struct getlines_fn
0099     {
0100         getlines_view operator()(std::istream & sin, char delim = '\n') const
0101         {
0102             return getlines_view{sin, delim};
0103         }
0104     };
0105 
0106     RANGES_INLINE_VARIABLE(getlines_fn, getlines)
0107     /// @}
0108 } // namespace ranges
0109 
0110 #include <range/v3/detail/epilogue.hpp>
0111 
0112 #endif