21#ifndef G_NET_LINE_BUFFER_H
22#define G_NET_LINE_BUFFER_H
33 class LineBufferConfig ;
34 class LineBufferIterator ;
35 class LineBufferState ;
85 G_EXCEPTION( ErrorOverflow ,
"line buffer overflow" ) ;
93 void add(
const std::string &
data ) ;
100 void expect( std::size_t n ) ;
118 std::string
eol()
const ;
124 template <
typename Tfn>
125 void apply(
const char *
data , std::size_t data_size , Tfn sink_fn ,
bool fragments =
false ) ;
145 template <
typename Tsink,
typename Tmemfun>
146 void apply( Tsink sink_p , Tmemfun sink_memfun ,
const char *
data , std::size_t data_size ,
bool fragments =
false ) ;
160 template <
typename Tsink,
typename Tmemfun,
typename Tmemfun2>
161 void apply( Tsink sink_p , Tmemfun sink_memfun ,
const char *
data , std::size_t data_size , Tmemfun2 fragments_memfun ) ;
166 template <
typename Tfn>
167 void apply(
const std::string & , Tfn sink_fn ,
bool fragments =
false ) ;
171 bool more(
bool fragments =
false ) ;
180 const char *
data()
const ;
186 std::size_t
size()
const ;
234 const char * m_data{
nullptr} ;
235 std::size_t m_size{0U} ;
236 std::size_t m_eolsize{0U} ;
237 std::size_t m_linesize{0U} ;
240 std::size_t set( LineStore & , std::size_t pos , std::size_t
size , std::size_t
eolsize ) ;
244 Extension(
LineBuffer * ,
const char * , std::size_t ) ;
246 Extension(
const Extension & ) = delete ;
247 Extension( Extension && ) = delete ;
248 void operator=(
const Extension & ) = delete ;
249 void operator=( Extension && ) = delete ;
256 ~LineBuffer() = default ;
259 void operator=(
const LineBuffer & ) = delete ;
263 friend class LineBufferState ;
264 void check(
const Output & ) ;
265 void output( std::size_t
size , std::size_t
eolsize ,
bool =
false ) ;
267 bool trivial( std::size_t pos )
const ;
268 bool finite()
const ;
269 std::size_t insize()
const ;
270 std::string head()
const ;
273 friend struct Extension ;
277 std::size_t m_warn_limit ;
279 std::size_t m_expect ;
298 const char *
data()
const ;
301 std::size_t
size()
const ;
307 std::string
line()
const ;
328 std::size_t
warn = 0U , std::size_t
fmin = 0U , std::size_t initial_expect = 0U ) ;
339 const std::string &
eol()
const ;
342 std::size_t
warn()
const ;
345 std::size_t
fmin()
const ;
348 std::size_t
expect()
const ;
376 std::size_t m_expect ;
391 std::string
eol()
const ;
394 std::size_t
size()
const ;
400 std::string
head()
const ;
414GNet::LineBuffer::Extension::Extension(
LineBuffer * line_buffer ,
const char * data , std::size_t size ) :
415 m_line_buffer(line_buffer) ,
416 m_call_frame(line_buffer->m_call_stack)
418 m_line_buffer->extensionStart( data ,
size ) ;
422GNet::LineBuffer::Extension::~Extension()
424 if( m_call_frame.valid() )
425 m_line_buffer->extensionEnd() ;
429bool GNet::LineBuffer::Extension::valid()
const
431 return m_call_frame.valid() ;
439 return m_out.m_size ;
443std::size_t GNet::LineBuffer::insize()
const
449std::string GNet::LineBuffer::head()
const
451 return m_in.head( 16U ) ;
457 return m_out.m_eolsize ;
463 return m_out.m_linesize ;
472template <
typename Tfn>
475 Extension e(
this , data_in , size_in ) ;
476 while( e.valid() && more(with_fragments) )
478 if( !sink_fn( data() , size() , eolsize() , linesize() , c0() ) )
483template <
typename Tsink,
typename Tmemfun>
484void GNet::LineBuffer::apply( Tsink sink_p , Tmemfun memfun ,
const char * data_in , std::size_t size_in ,
bool with_fragments )
486 Extension e(
this , data_in , size_in ) ;
487 while( e.valid() && more(with_fragments) )
489 if( !(sink_p->*memfun)( data() , size() , eolsize() , linesize() , c0() ) )
494template <
typename Tsink,
typename Tmemfun,
typename Tmemfun2>
495void GNet::LineBuffer::apply( Tsink sink_p , Tmemfun memfun ,
const char * data_in , std::size_t size_in , Tmemfun2 fragments_memfun )
497 Extension e(
this , data_in , size_in ) ;
498 while( e.valid() && more( (sink_p->*fragments_memfun)() ) )
500 if( !(sink_p->*memfun)( data() , size() , eolsize() , linesize() , c0() ) )
509 return apply( data.data() , data.size() , sink , with_fragments ) ;
542 m_line_buffer(line_buffer)
549 return m_line_buffer.more() ;
555 return std::string( m_line_buffer.data() , m_line_buffer.size() ) ;
561 return m_line_buffer.data() ;
567 return m_line_buffer.size() ;
573 return m_line_buffer.eolsize() ;
580 m_transparent(line_buffer.transparent()) ,
581 m_eol(line_buffer.eol()) ,
582 m_size(line_buffer.insize()) ,
583 m_head(line_buffer.head())
596 return m_transparent ;
608 return m_size == 0U ;
A configuration structure for GNet::LineBuffer.
static LineBufferConfig crlf()
Convenience factory function.
std::size_t warn() const
Returns the warn-limit, as passed to the constructor.
std::size_t expect() const
Returns the initial expect value, as passed to the constructor.
std::size_t fmin() const
Returns the minimum fragment size, as passed to the constructor.
static LineBufferConfig transparent()
Convenience factory function.
static LineBufferConfig http()
Convenience factory function.
const std::string & eol() const
Returns the end-of-line string as passed to the constructor.
static LineBufferConfig autodetect()
Convenience factory function.
static LineBufferConfig newline()
Convenience factory function.
static LineBufferConfig pop()
Convenience factory function.
LineBufferConfig(const std::string &eol=std::string(1U,'\n'), std::size_t warn=0U, std::size_t fmin=0U, std::size_t initial_expect=0U)
Constructor.
static LineBufferConfig smtp()
Convenience factory function.
Syntactic sugar for calling GNet::LineBuffer iteration methods.
LineBufferIterator(LineBuffer &buffer)
Constructor.
std::string line() const
Returns the current line (of length size()).
const char * data() const
See LineBuffer::data().
std::size_t eolsize() const
See LineBuffer::eolsize().
bool more()
See LineBuffer::more().
std::size_t size() const
See LineBuffer::size().
Provides information abount the state of a line buffer.
std::string head() const
Returns the first bytes of buffered data up to a limit of sixteen bytes.
bool empty() const
Returns true iff size() is zero.
bool transparent() const
Returns LineBuffer::transparent().
std::size_t size() const
Returns the number of bytes currently buffered up.
LineBufferState(const LineBuffer &)
Constructor.
std::string eol() const
Returns LineBuffer::eol().
A class that does line buffering, supporting auto-detection of line endings and fixed-size block extr...
LineBuffer(const LineBufferConfig &)
Constructor.
void expect(std::size_t n)
Requests that the next 'n' bytes extracted be extracted in one contiguous block, without regard to li...
bool transparent() const
Returns true if the current expect() value is infinite.
std::size_t linesize() const
Returns the current size of all the line fragments making up the current line.
void clear()
Clears the internal data.
void extensionStart(const char *, std::size_t)
A pseudo-private method used by the implementation of the apply() method template.
bool more(bool fragments=false)
Returns true if there is more data() to be had.
std::size_t size() const
Returns the size of the current data(), excluding the line ending.
std::size_t eolsize() const
Returns the size of line-ending associated with the current data().
const char * data() const
Returns a pointer for the current line, expect()ed fixed-size block, or line fragment.
void apply(const char *data, std::size_t data_size, Tfn sink_fn, bool fragments=false)
Adds the data and passes complete lines to the sink function with line-data, line-size,...
std::string eol() const
Returns the end-of-line string as passed in to the constructor, or as auto-detected.
void extensionEnd()
A pseudo-private method used by the implementation of the apply() method template.
void add(const std::string &data)
Adds a data segment.
void apply(const std::string &, Tfn sink_fn, bool fragments=false)
Overload taking a string as its data input, used in testing.
char c0() const
Returns the first character of the current line.
LineBufferState state() const
Returns information about the current state of the line-buffer.
An object to represent a nested execution context.
A linked list of CallFrame pointers.