glinebuffer.h
Go to the documentation of this file.
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 // ===
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 
30 namespace GNet
31 {
32  class LineBuffer ;
33  class LineBufferIterator ;
34 }
35 
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 ) ;
59 
60  void add( const std::string & segment ) ;
62 
63  void add( const char * p , std::string::size_type n ) ;
65 
66  bool more() const ;
69 
70  const std::string & current() const ;
75 
76  void discard() ;
80 
81  std::string line() ;
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 ;
100  bool m_current_valid ; // mutable
101  std::string m_current ; // mutable
102  bool m_throw ;
103  bool m_locked ;
104 } ;
105 
113 {
114 public:
115  explicit LineBufferIterator( LineBuffer & ) ;
117 
120 
121  bool more() const ;
123 
124  const std::string & line() ;
127 
128 private:
129  LineBufferIterator( const LineBufferIterator & ) ; // not implemented
130  void operator=( const LineBufferIterator & ) ; // not implemented
131 
132 private:
133  LineBuffer & m_b ;
135  std::string::size_type m_store_length ;
136 } ;
137 
138 inline
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
149 {
150  m_b.unlock( m_n ) ;
151 }
152 
153 #endif
154 
void add(const std::string &segment)
Adds a data segment.
Definition: glinebuffer.cpp:63
Network classes.
const std::string & current() const
Returns the current line, without extracting it.
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
bool more() const
Returns true if there is a line() to be had.
LineBuffer(const std::string &eol=std::string("\n"), bool do_throw_on_overflow=false)
Constructor.
Definition: glinebuffer.cpp:30
An iterator class for GNet::LineBuffer.
Definition: glinebuffer.h:112
LineBufferIterator(LineBuffer &)
Constructor.
Definition: glinebuffer.h:139
const std::string & line()
Returns the current line and increments the iterator.
~LineBufferIterator()
Destructor.
Definition: glinebuffer.h:148
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
A class which does line buffering.
Definition: glinebuffer.h:52
std::string line()
Extracts a line and returns it as a string.
bool more() const
Returns true if there are complete line(s) to be extracted.
Definition: glinebuffer.cpp:99
void discard()
Discards the current line.