1878 |
27 Mar 09 |
peter |
1 |
#ifndef _theplu_yat_utility_getline_iterator_ |
1878 |
27 Mar 09 |
peter |
2 |
#define _theplu_yat_utility_getline_iterator_ |
1878 |
27 Mar 09 |
peter |
3 |
|
1878 |
27 Mar 09 |
peter |
// $Id$ |
1878 |
27 Mar 09 |
peter |
5 |
|
1878 |
27 Mar 09 |
peter |
6 |
/* |
3210 |
05 May 14 |
peter |
Copyright (C) 2009, 2013 Peter Johansson |
1878 |
27 Mar 09 |
peter |
8 |
|
1878 |
27 Mar 09 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
1878 |
27 Mar 09 |
peter |
10 |
|
1878 |
27 Mar 09 |
peter |
The yat library is free software; you can redistribute it and/or |
1878 |
27 Mar 09 |
peter |
modify it under the terms of the GNU General Public License as |
1878 |
27 Mar 09 |
peter |
published by the Free Software Foundation; either version 3 of the |
1878 |
27 Mar 09 |
peter |
License, or (at your option) any later version. |
1878 |
27 Mar 09 |
peter |
15 |
|
1878 |
27 Mar 09 |
peter |
The yat library is distributed in the hope that it will be useful, |
1878 |
27 Mar 09 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1878 |
27 Mar 09 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1878 |
27 Mar 09 |
peter |
General Public License for more details. |
1878 |
27 Mar 09 |
peter |
20 |
|
1878 |
27 Mar 09 |
peter |
You should have received a copy of the GNU General Public License |
1878 |
27 Mar 09 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
1878 |
27 Mar 09 |
peter |
23 |
*/ |
1878 |
27 Mar 09 |
peter |
24 |
|
1878 |
27 Mar 09 |
peter |
25 |
#include <boost/iterator/iterator_facade.hpp> |
1878 |
27 Mar 09 |
peter |
26 |
|
1878 |
27 Mar 09 |
peter |
27 |
#include <iterator> |
1878 |
27 Mar 09 |
peter |
28 |
#include <string> |
1878 |
27 Mar 09 |
peter |
29 |
|
1878 |
27 Mar 09 |
peter |
30 |
namespace theplu { |
1878 |
27 Mar 09 |
peter |
31 |
namespace yat { |
1878 |
27 Mar 09 |
peter |
32 |
namespace utility { |
1878 |
27 Mar 09 |
peter |
33 |
|
1878 |
27 Mar 09 |
peter |
34 |
/** |
1878 |
27 Mar 09 |
peter |
\brief Read from std::istream with std::getline. |
1878 |
27 Mar 09 |
peter |
36 |
|
1879 |
27 Mar 09 |
peter |
A GetlineIterator is an \input_iterator similar to |
1878 |
27 Mar 09 |
peter |
std::istream_iterator that can be used to read from an \c |
1878 |
27 Mar 09 |
peter |
istream. The main difference is that GetlineIterator reads from |
1878 |
27 Mar 09 |
peter |
the istream using \c std::getline rather than \c operator>>. \c |
1878 |
27 Mar 09 |
peter |
value_type is std::string and \c operator* returns \c const \c |
1878 |
27 Mar 09 |
peter |
std::string&. When end of stream is reached iterator gets into a |
1878 |
27 Mar 09 |
peter |
special state \a end \a of \a stream. Two iterators are equal if |
1878 |
27 Mar 09 |
peter |
they are \a end \a of \a stream or if they are pointing to the |
1878 |
27 Mar 09 |
peter |
same stream (and not being \a end \a of \a stream). |
1878 |
27 Mar 09 |
peter |
46 |
|
1878 |
27 Mar 09 |
peter |
Here is an example copying lines in a file to a \c vector<std::string>: |
1878 |
27 Mar 09 |
peter |
\code |
1878 |
27 Mar 09 |
peter |
std::ifstream is("file.txt"); |
1878 |
27 Mar 09 |
peter |
std::vector<std::std::string> vec; |
1878 |
27 Mar 09 |
peter |
GetlineIterator begin(is); |
1878 |
27 Mar 09 |
peter |
GetlineIterator end; |
1878 |
27 Mar 09 |
peter |
std::copy(begin, end, std::back_inserter(vec)); |
1878 |
27 Mar 09 |
peter |
\endcode |
1881 |
28 Mar 09 |
peter |
55 |
|
1881 |
28 Mar 09 |
peter |
\since New in yat 0.6 |
1878 |
27 Mar 09 |
peter |
57 |
*/ |
1878 |
27 Mar 09 |
peter |
58 |
class GetlineIterator |
1878 |
27 Mar 09 |
peter |
59 |
: public boost::iterator_facade< |
1878 |
27 Mar 09 |
peter |
60 |
GetlineIterator, const std::string, std::input_iterator_tag |
1878 |
27 Mar 09 |
peter |
61 |
> |
1878 |
27 Mar 09 |
peter |
62 |
{ |
1878 |
27 Mar 09 |
peter |
63 |
public: |
1878 |
27 Mar 09 |
peter |
64 |
/** |
1878 |
27 Mar 09 |
peter |
\brief Constructor of end of stream iterator |
1878 |
27 Mar 09 |
peter |
66 |
*/ |
1878 |
27 Mar 09 |
peter |
67 |
GetlineIterator(void); |
1878 |
27 Mar 09 |
peter |
68 |
|
1878 |
27 Mar 09 |
peter |
69 |
/** |
1878 |
27 Mar 09 |
peter |
\brief Constructor |
3148 |
23 Dec 13 |
peter |
71 |
|
1878 |
27 Mar 09 |
peter |
\param is istream to extract string elements from. |
3148 |
23 Dec 13 |
peter |
\param delimiter the delimiting character. |
1878 |
27 Mar 09 |
peter |
74 |
*/ |
3148 |
23 Dec 13 |
peter |
75 |
explicit GetlineIterator(std::istream& is, char delimiter='\n'); |
1878 |
27 Mar 09 |
peter |
76 |
|
1878 |
27 Mar 09 |
peter |
77 |
private: |
1878 |
27 Mar 09 |
peter |
78 |
friend class boost::iterator_core_access; |
1878 |
27 Mar 09 |
peter |
79 |
|
1878 |
27 Mar 09 |
peter |
80 |
char delimiter_; |
1878 |
27 Mar 09 |
peter |
81 |
std::istream* istream_; |
1878 |
27 Mar 09 |
peter |
82 |
std::string line_; |
1878 |
27 Mar 09 |
peter |
83 |
|
3148 |
23 Dec 13 |
peter |
84 |
GetlineIterator::reference dereference(void) const; |
1878 |
27 Mar 09 |
peter |
85 |
bool equal(const GetlineIterator& other) const; |
3148 |
23 Dec 13 |
peter |
86 |
void increment(void); |
1878 |
27 Mar 09 |
peter |
// Using compiler generated copy |
1878 |
27 Mar 09 |
peter |
//GetlineIterator(const GetlineIterator&); |
1878 |
27 Mar 09 |
peter |
//GetlineIterator& operator=(const GetlineIterator&); |
1878 |
27 Mar 09 |
peter |
90 |
}; |
1878 |
27 Mar 09 |
peter |
91 |
|
1878 |
27 Mar 09 |
peter |
92 |
}}} // of namespace utility, yat, and theplu |
1878 |
27 Mar 09 |
peter |
93 |
|
1878 |
27 Mar 09 |
peter |
94 |
#endif |