1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file glinebuffer.h
19 ///
20 
21 #ifndef G_LINE_BUFFER_H
22 #define G_LINE_BUFFER_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gexception.h"
27 #include <string>
28 
29 /// \namespace GNet
30 namespace GNet
31 {
32 	class LineBuffer ;
33 	class LineBufferIterator ;
34 }
35 
36 /// \class GNet::LineBuffer
37 /// A class which does line buffering. Raw data is added, and
38 /// newline-delimited lines are extracted.
39 ///
40 /// Usage:
41 /// \code
42 /// {
43 ///   GNet::LineBuffer buffer ;
44 ///   buffer.add("abc") ;
45 ///   buffer.add("def\nABC\nDE") ;
46 ///   buffer.add("F\n") ;
47 ///   while( buffer.more() )
48 ///     cout << buffer.line() << endl ;
49 /// }
50 /// \endcode
51 ///
52 class GNet::LineBuffer
53 {
54 public:
55 	G_EXCEPTION( Overflow , "line buffer overflow: maximum input line length exceeded" ) ;
56 
57 	explicit LineBuffer( const std::string & eol = std::string("\n") , bool do_throw_on_overflow = false ) ;
58 		///< Constructor.
59 
60 	void add( const std::string & segment ) ;
61 		///< Adds a data segment.
62 
63 	void add( const char * p , std::string::size_type n ) ;
64 		///< Adds a data segment.
65 
66 	bool more() const ;
67 		///< Returns true if there are complete
68 		///< line(s) to be extracted.
69 
70 	const std::string & current() const ;
71 		///< Returns the current line, without extracting
72 		///< it. The line terminator is not included.
73 		///<
74 		///< Precondition: more()
75 
76 	void discard() ;
77 		///< Discards the current line.
78 		///<
79 		///< Precondition: more()
80 
81 	std::string line() ;
82 		///< Extracts a line and returns it as a string.
83 		///< The line terminator is not included.
84 
85 private:
86 	friend class LineBufferIterator ;
87 	LineBuffer( const LineBuffer & ) ;
88 	void operator=( const LineBuffer & ) ;
89 	void fix( std::string::size_type ) ;
90 	void check( std::string::size_type ) ;
91 	void lock() ;
92 	void unlock( std::string::size_type ) ;
93 
94 private:
95 	static unsigned long m_limit ;
96 	std::string m_eol ;
97 	std::string::size_type m_eol_length ;
98 	std::string m_store ;
99 	std::string::size_type m_p ;
100 	bool m_current_valid ; // mutable
101 	std::string m_current ; // mutable
102 	bool m_throw ;
103 	bool m_locked ;
104 } ;
105 
106 /// \class GNet::LineBufferIterator
107 /// An iterator class for GNet::LineBuffer.
108 /// Use of this class is optional but it may provide
109 /// some performance improvement. You are not allowed to add()
110 /// more data to the underlying line buffer while iterating.
111 ///
112 class GNet::LineBufferIterator
113 {
114 public:
115 	explicit LineBufferIterator( LineBuffer & ) ;
116 		///< Constructor.
117 
118 	~LineBufferIterator() ;
119 		///< Destructor.
120 
121 	bool more() const ;
122 		///< Returns true if there is a line() to be had.
123 
124 	const std::string & line() ;
125 		///< Returns the current line and increments the iterator.
126 		///< Precondition: more()
127 
128 private:
129 	LineBufferIterator( const LineBufferIterator & ) ; // not implemented
130 	void operator=( const LineBufferIterator & ) ; // not implemented
131 
132 private:
133 	LineBuffer & m_b ;
134 	std::string::size_type m_n ;
135 	std::string::size_type m_store_length ;
136 } ;
137 
138 inline
LineBufferIterator(LineBuffer & b)139 GNet::LineBufferIterator::LineBufferIterator( LineBuffer & b ) :
140 	m_b(b) ,
141 	m_n(0U) ,
142 	m_store_length(b.m_store.length())
143 {
144 	m_b.lock() ;
145 }
146 
147 inline
~LineBufferIterator()148 GNet::LineBufferIterator::~LineBufferIterator()
149 {
150 	m_b.unlock( m_n ) ;
151 }
152 
153 #endif
154 
155