Loading...
Searching...
No Matches
Streambuf.h
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Streambuf.h
//------------------------------------------------------------------------------
// Copyright (c) 1999 by Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Created: 12/02/1999
//------------------------------------------------------------------------------
#ifndef STREAM_BUF_H
#define STREAM_BUF_H
#include <stdio.h> // EOF
#include "assa/Assure.h" // trace() & Assert family
namespace ASSA {
class io_ptrs
{
public:
char* m_read_base;
char* m_read_ptr;
char* m_read_end;
char* m_write_base;
char* m_write_ptr;
char* m_write_end;
char* m_buf_base;
char* m_buf_end;
enum { USER_BUF = 1, UNBUFFERED = 2, EOF_SEEN = 4, ERR_SEEN = 8 };
int m_flags;
char m_shortbuf[1];
io_ptrs () : m_read_base (0), m_read_ptr (0), m_read_end (0),
m_write_base (0), m_write_ptr (0), m_write_end (0),
m_buf_base (0), m_buf_end (0), m_flags (0)
{
m_shortbuf [0] = 0;
}
void dump () const;
};
class Streambuf : public io_ptrs
{
public:
// Old comment:
//
// "Maximum frame size that can be transmitted unfragmented by TCP
// with MTU 1500 (1500-20-60). TCP frame can have options
// (up to 60 bytes) which, if ignored, might cause fragmentation.
// Also, the length of the IP packet must be evenly divisible by 8."
//
// On 100Mb networks, the reasonable buffer size seems to be 64K.
static const int MAXTCPFRAMESZ = 65536; // 64K
virtual ~Streambuf ();
Streambuf* pubsetbuf (char* s_, int n_);
int pubsync ();
int in_avail ();
int snextc ();
int sbumpc ();
int sgetc ();
int sgetn (char* b_, int len_);
int sputc (char c_);
int sputn (char* b_, int len_);
void unbuffered (int i_);
int unbuffered ();
protected:
Streambuf ();
Streambuf (const Streambuf&);
Streambuf& operator= (const Streambuf&);
char* base () const;
char* gptr () const;
char* egptr () const;
void setg (char* gbeg_, char* gnext_, char* gend_);
char* pbase () const;
char* pptr () const;
char* epptr () const;
void setp (char* pbeg_, char* pend_);
void pbump (int n_);
void setb (char* b_, char* eb_, int del_);
void init ();
protected:
virtual Streambuf* setbuf (char* p_, int len_);
virtual int sync ();
virtual int showmanyc ();
virtual int xsgetn (char* b_, int len_);
virtual int underflow ();
virtual int uflow ();
virtual int xsputn (const char* b_, int len_);
virtual int overflow (int c = EOF);
virtual int doallocate ();
};
inline Streambuf*
Streambuf::
pubsetbuf (char* s_, int n_)
{
trace_with_mask("Streambuf::pubsetbuf",STRMBUFTRACE);
return setbuf (s_, n_);
}
inline int
Streambuf::
pubsync ()
{
trace_with_mask("Streambuf::pubsync",STRMBUFTRACE);
return sync ();
}
inline int
Streambuf::
in_avail ()
{
trace_with_mask("Streambuf::in_avail",STRMBUFTRACE);
return m_read_end - m_read_ptr;
}
inline int
Streambuf::
unbuffered ()
{
trace_with_mask("Streambuf::unbuffered",STRMBUFTRACE);
return m_flags & UNBUFFERED ? 1 : 0;
}
inline void
Streambuf::
unbuffered (int i_)
{
trace_with_mask("Streambuf::unbuffered",STRMBUFTRACE);
if (i_)
m_flags |= UNBUFFERED; // set
else
m_flags &= ~ UNBUFFERED; // unset
}
inline
Streambuf::
Streambuf ()
{
trace_with_mask("Streambuf::Streambuf",STRMBUFTRACE);
init ();
}
inline void
Streambuf::
init ()
{
trace_with_mask("Streambuf::init", STRMBUFTRACE);
m_read_base = m_read_ptr = m_read_end = 0;
m_write_base = m_write_ptr = m_write_end = 0;
m_buf_base = m_buf_end = 0;
m_flags = 0;
m_shortbuf[0] = 0;
}
inline
Streambuf::
~Streambuf ()
{
trace_with_mask("Streambuf::~Streambuf",STRMBUFTRACE);
if (!(m_flags & USER_BUF)) {
delete [] m_buf_base;
m_buf_base = m_buf_end = 0;
}
}
inline char*
Streambuf::
base () const
{
trace_with_mask("Streambuf::base",STRMBUFTRACE);
return m_read_base;
}
inline char*
Streambuf::
gptr () const
{
trace_with_mask("Streambuf::gptr",STRMBUFTRACE);
return m_read_ptr;
}
inline char*
Streambuf::
egptr () const
{
trace_with_mask("Streambuf::egptr",STRMBUFTRACE);
return m_read_end;
}
inline char*
Streambuf::
pbase () const
{
trace_with_mask("Streambuf::pbase",STRMBUFTRACE);
return m_write_base;
}
inline char*
Streambuf::
pptr () const
{
trace_with_mask("Streambuf::pptr",STRMBUFTRACE);
return m_write_ptr;
}
inline char*
Streambuf::
epptr () const
{
trace_with_mask("Streambuf::epptr",STRMBUFTRACE);
return m_write_end;
}
inline void
Streambuf::
pbump (int n_)
{
trace_with_mask("Streambuf::pbump",STRMBUFTRACE);
m_write_ptr += n_;
}
inline int
Streambuf::
sync ()
{
trace_with_mask("Streambuf::sync",STRMBUFTRACE);
return 0;
}
inline int
Streambuf::
showmanyc ()
{
trace_with_mask("Streambuf::showmanyc",STRMBUFTRACE);
return -1;
}
inline int
Streambuf::
sbumpc ()
{
trace_with_mask("Streambuf::sbumpc",STRMBUFTRACE);
return (m_read_ptr >= m_read_end ? uflow ()
: *(unsigned char *) m_read_ptr++);
}
inline int
Streambuf::
sgetc ()
{
trace_with_mask("Streambuf::sgetc",STRMBUFTRACE);
return (m_read_ptr >= m_read_end && underflow () == EOF
? EOF : *(unsigned char*) m_read_ptr);
}
inline int
Streambuf::
sgetn (char* data_, int len_)
{
trace_with_mask("Streambuf::sgetn",STRMBUFTRACE);
return xsgetn (data_, len_);
}
inline int
Streambuf::
sputc (char c_)
{
trace_with_mask("Streambuf::sputc",STRMBUFTRACE);
return (m_write_ptr >= m_write_end
? overflow (c_)
: (unsigned char) (*m_write_ptr++ = c_));
}
inline int
Streambuf::
sputn (char* b_, int len_)
{
trace_with_mask("Streambuf::sputn",STRMBUFTRACE);
return xsputn (b_, len_);
}
inline void
Streambuf::
setp (char* pbeg_, char* pend_)
{
trace_with_mask("Streambuf::setp",STRMBUFTRACE);
m_write_base = m_write_ptr = pbeg_;
m_write_end = pend_;
}
inline int
Streambuf::
underflow ()
{
trace_with_mask("Streambuf::underflow",STRMBUFTRACE);
return (EOF);
}
inline int
Streambuf::
overflow (int /* c_ */)
{
trace_with_mask("Streambuf::overflow",STRMBUFTRACE);
return (EOF);
}
} // end namespace ASSA
#endif /* STREAM_BUF_H */
Assure.h
A collection of assert function wrappers.
trace_with_mask
#define trace_with_mask(s, m)
trace_with_mask() is used to trace function call chain in C++ program.
Definition Logger.h:437
ASSA::Streambuf
Streambuf class.
Definition Streambuf.h:91
ASSA::Streambuf::pptr
char * pptr() const
Returns a pointer to the beginning of the put area, and thus to the location of the next character th...
Definition Streambuf.h:499
ASSA::Streambuf::base
char * base() const
Returns the lowest possible value for gptr() - the beginning of the get area.
Definition Streambuf.h:464
ASSA::Streambuf::init
void init()
Definition Streambuf.h:438
ASSA::Streambuf::underflow
virtual int underflow()
This function is called to supply characters for input (from some source) when the get area is empty,...
Definition Streambuf.h:598
ASSA::Streambuf::pubsync
int pubsync()
Definition Streambuf.h:390
ASSA::Streambuf::setp
void setp(char *pbeg_, char *pend_)
Set put area pointers.
Definition Streambuf.h:588
ASSA::Streambuf::setbuf
virtual Streambuf * setbuf(char *p_, int len_)
Performs an operation that is defined separately for each class derived from Streambuf.
Definition Streambuf.cpp:100
ASSA::Streambuf::overflow
virtual int overflow(int c=EOF)
This function is called to consume characters (flush them to output), typically when the put area is ...
Definition Streambuf.h:607
ASSA::Streambuf::epptr
char * epptr() const
Returns a pointer just past the end of the put area, the maximum possible value for pptr().
Definition Streambuf.h:507
ASSA::Streambuf::sputc
int sputc(char c_)
This function stores c just after the put pointer, and advances the pointer one position,...
Definition Streambuf.h:568
ASSA::Streambuf::unbuffered
int unbuffered()
Definition Streambuf.h:408
ASSA::Streambuf::egptr
char * egptr() const
Returns a pointer just past the end of the get area, the maximum possible value for gptr().
Definition Streambuf.h:482
ASSA::Streambuf::MAXTCPFRAMESZ
static const int MAXTCPFRAMESZ
Size of the internal input/output buffer.
Definition Streambuf.h:106
ASSA::Streambuf::Streambuf
Streambuf()
The default constructor is protected for class Streambuf to asssure that only objects for classes der...
Definition Streambuf.h:429
ASSA::Streambuf::doallocate
virtual int doallocate()
This function is called by allocate when unbuffered() is zero and base() is zero.
Definition Streambuf.cpp:234
ASSA::Streambuf::xsgetn
virtual int xsgetn(char *b_, int len_)
The morphemes of showmanyc are "es-how-many-see", not "show-man-ic".
Definition Streambuf.cpp:128
ASSA::Streambuf::pbase
char * pbase() const
Returns a pointer to the beginning fo the space available for the put area, the lowest possible value...
Definition Streambuf.h:491
ASSA::Streambuf::uflow
virtual int uflow()
Reads the characters from the input sequence, if possible, and moves the stream position past it,...
Definition Streambuf.cpp:172
ASSA::Streambuf::operator=
Streambuf & operator=(const Streambuf &)
ASSA::Streambuf::setb
void setb(char *b_, char *eb_, int del_)
Establish the reserve area (buffer).
Definition Streambuf.cpp:80
ASSA::Streambuf::pbump
void pbump(int n_)
Advances the next pointer for the output sequence by n_.
Definition Streambuf.h:515
ASSA::Streambuf::sync
virtual int sync()
This function synchronizes the streambuf with its actual stream of characters.
Definition Streambuf.h:523
ASSA::Streambuf::in_avail
int in_avail()
This function returns the number of characters immediately available in the get area.
Definition Streambuf.h:399
ASSA::Streambuf::sgetc
int sgetc()
This function should probably have been called ``sgetc''.
Definition Streambuf.h:549
ASSA::Streambuf::Streambuf
Streambuf(const Streambuf &)
ASSA::Streambuf::gptr
char * gptr() const
Returns a pointer to the beginning of the get area, and thus to the next character to be fetched (if ...
Definition Streambuf.h:473
ASSA::Streambuf::pubsetbuf
Streambuf * pubsetbuf(char *s_, int n_)
Set buffer.
Definition Streambuf.h:381
ASSA::Streambuf::sputn
int sputn(char *b_, int len_)
From the location pointed to by ptr, stores exactly len characters after the put pointer,...
Definition Streambuf.h:579
ASSA::Streambuf::setg
void setg(char *gbeg_, char *gnext_, char *gend_)
Set get area pointers.
Definition Streambuf.cpp:69
ASSA::Streambuf::xsputn
virtual int xsputn(const char *b_, int len_)
Writes up to len_ characters to the output sequence as if by repeated calls to sputc (c).
Definition Streambuf.cpp:184
ASSA::Streambuf::sgetn
int sgetn(char *b_, int len_)
This function gets the next len_ characters following the get pointer, copying them to the char array...
Definition Streambuf.h:559
ASSA::Streambuf::~Streambuf
virtual ~Streambuf()
Definition Streambuf.h:452
ASSA::Streambuf::snextc
int snextc()
This function moves the get pointer forward one position, then returns the character after the get ...
Definition Streambuf.cpp:57
ASSA::io_ptrs::m_write_base
char * m_write_base
Definition Streambuf.h:37
ASSA::io_ptrs::m_shortbuf
char m_shortbuf[1]
Definition Streambuf.h:47
ASSA::io_ptrs::m_write_ptr
char * m_write_ptr
Definition Streambuf.h:38
ASSA::io_ptrs::m_write_end
char * m_write_end
Definition Streambuf.h:39
ASSA::io_ptrs::m_read_ptr
char * m_read_ptr
Definition Streambuf.h:34
ASSA::io_ptrs::m_read_end
char * m_read_end
Definition Streambuf.h:35
ASSA::io_ptrs::m_buf_base
char * m_buf_base
Definition Streambuf.h:41
ASSA::io_ptrs::io_ptrs
io_ptrs()
Definition Streambuf.h:49
ASSA::io_ptrs::m_read_base
char * m_read_base
Definition Streambuf.h:33
ASSA::io_ptrs::m_buf_end
char * m_buf_end
Definition Streambuf.h:42
ASSA::io_ptrs::USER_BUF
@ USER_BUF
Definition Streambuf.h:44
ASSA::io_ptrs::UNBUFFERED
@ UNBUFFERED
Definition Streambuf.h:44
ASSA::io_ptrs::EOF_SEEN
@ EOF_SEEN
Definition Streambuf.h:44
ASSA::io_ptrs::ERR_SEEN
@ ERR_SEEN
Definition Streambuf.h:44
ASSA::io_ptrs::dump
void dump() const
Definition Streambuf.cpp:23
ASSA::io_ptrs::m_flags
int m_flags
Definition Streambuf.h:46
ASSA
Definition Acceptor.h:40
ASSA::STRMBUFTRACE
@ STRMBUFTRACE
Extended Streambuf & friends messages.
Definition LogMask.h:46