source: trunk/yat/utility/GetlineIterator.h @ 1878

Last change on this file since 1878 was 1878, checked in by Peter, 15 years ago

Adding a GetlineIterator?. closes #507

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.7 KB
Line 
1#ifndef _theplu_yat_utility_getline_iterator_
2#define _theplu_yat_utility_getline_iterator_
3
4// $Id: GetlineIterator.h 1878 2009-03-27 11:52:28Z peter $
5
6/*
7  Copyright (C) 2009 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include <boost/iterator/iterator_facade.hpp>
26
27#include <iterator>
28#include <string>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     \brief Read from std::istream with std::getline.
36
37     An GetlineIterator is an \input_iterator similar to
38     std::istream_iterator that can be used to read from an \c
39     istream. The main difference is that GetlineIterator reads from
40     the istream using \c std::getline rather than \c operator>>. \c
41     value_type is std::string and \c operator* returns \c const \c
42     std::string&. When end of stream is reached iterator gets into a
43     special state \a end \a of \a stream. Two iterators are equal if
44     they are \a end \a of \a stream or if they are pointing to the
45     same stream (and not being \a end \a of \a stream).
46
47     Here is an example copying lines in a file to a \c vector<std::string>:
48     \code
49     std::ifstream is("file.txt");
50     std::vector<std::std::string> vec;
51     GetlineIterator begin(is);
52     GetlineIterator end;
53     std::copy(begin, end, std::back_inserter(vec));
54     \endcode
55  */
56  class GetlineIterator
57    : public boost::iterator_facade<
58    GetlineIterator, const std::string, std::input_iterator_tag
59    >
60  {
61  public:
62    /**
63       \brief Constructor of end of stream iterator
64    */
65    GetlineIterator(void);
66
67    /**
68       \brief Constructor
69       
70       \param is istream to extract string elements from.
71       \param delimiter the delimiting character.
72    */
73    GetlineIterator(std::istream& is, char delimiter='\n');
74
75  private:
76    friend class boost::iterator_core_access;
77
78    char delimiter_;
79    std::istream* istream_;
80    std::string line_;
81
82    GetlineIterator::reference dereference(void) const; 
83
84    bool equal(const GetlineIterator& other) const;
85
86    void increment(void); 
87
88    // Using compiler generated copy
89    //GetlineIterator(const GetlineIterator&);
90    //GetlineIterator& operator=(const GetlineIterator&);
91  };
92
93}}} // of namespace utility, yat, and theplu
94
95#endif
Note: See TracBrowser for help on using the repository browser.