Loading...
Searching...
No Matches
Streambuf.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// Streambuf.cpp
//------------------------------------------------------------------------------
// 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/99
//------------------------------------------------------------------------------
#include "assa/Streambuf.h"
#include "assa/MemDump.h"
using namespace ASSA;
void
io_ptrs::
dump () const
{
#ifdef LOG_PACKET
trace_with_mask("io_ptrs::dump",STRMBUF);
int len;
DL((STRMBUF,"---Ptr------:---Val---\n"));
DL((STRMBUF,"m_read_base.: 0x%x\n", (u_long)m_read_base));
DL((STRMBUF,"m_read_ptr..: 0x%x\n", (u_long)m_read_ptr ));
DL((STRMBUF,"m_read_end..: 0x%x\n", (u_long)m_read_end ));
if (m_read_ptr && (len = m_read_end - m_read_ptr) > 0) {
MemDump get_area (m_read_ptr, len);
DL((STRMBUF,"\n%s\n", get_area.getMemDump ()));
}
DL((STRMBUF,"m_write_base: 0x%x\n", (u_long)m_write_base));
DL((STRMBUF,"m_write_ptr.: 0x%x\n", (u_long)m_write_ptr ));
DL((STRMBUF,"m_write_end.: 0x%x\n", (u_long)m_write_end ));
if (m_write_base && (len = m_write_ptr - m_write_base) > 0) {
MemDump put_area (m_write_base, len);
DL((STRMBUF,"%s\n", put_area.getMemDump ()));
}
DL((STRMBUF,"m_buf_base..: 0x%x\n", (u_long)m_buf_base ));
DL((STRMBUF,"m_buf_end...: 0x%x\n", (u_long)m_buf_end ));
DL((STRMBUF,"------------:---------\n");
#endif
}
int
Streambuf::
snextc ()
{
trace_with_mask("Streambuf::snextc",STRMBUFTRACE);
if (m_read_ptr >= m_read_end && underflow () == EOF) {
return EOF;
}
return m_read_ptr++, sgetc ();
}
void
Streambuf::
setg (char* gbeg_, char* gnext_, char* gend_)
{
trace_with_mask("Streambuf::setg",STRMBUFTRACE);
m_read_base = gbeg_;
m_read_ptr = gnext_;
m_read_end = gend_;
}
void
Streambuf::
setb (char* b_, char* eb_, int del_)
{
trace_with_mask("Streambuf::setb",STRMBUFTRACE);
if (m_buf_base && !(m_flags & USER_BUF))
delete m_buf_base;
m_buf_base = b_;
m_buf_end = eb_;
if (del_)
m_flags &= ~ USER_BUF; // clear bit
else
m_flags |= USER_BUF; // set bit
dump ();
}
Streambuf*
Streambuf::
setbuf (char* p_, int len_)
{
trace_with_mask("Streambuf::setb",STRMBUFTRACE);
if (sync () == EOF) // Flush out all pending bytes before
return NULL; // resetting buffer. Also, first time around,
// calling sync() suppose to set put area
// pointers.
if (p_ == NULL || len_ == 0) {
DL((STRMBUF,"Unbuffered IO set.\n"));
unbuffered (1);
// We do it from doalloc instead - vlg
// setb (m_shortbuf, m_shortbuf+1, 0);
}
else {
DL((STRMBUF,"Buffered IO set.\n"));
unbuffered (0);
setb (p_, p_ + len_, 0);
}
setp (0, 0);
setg (0, 0, 0);
return this;
}
int
Streambuf::
xsgetn (char* data_, int len_)
{
trace_with_mask("Streambuf::xsgetn",STRMBUFTRACE);
/*
Get area is empty and nothing is on the socket.
*/
int count = m_read_end - m_read_ptr; // Bytes in Get area
if (count == 0 && underflow () == EOF) {
DL((STRMBUFTRACE,"returning %d. count: %d\n", EOF));
return EOF;
}
count = m_read_end - m_read_ptr; // Adjusted bytes in Get area
DL((STRMBUFTRACE,"Adjusted bytes in Get Area: %d\n",count));
if (count > len_) {
count = len_;
}
if (count <= 0) {
count = 0; // Peer closed connection
}
else if (count > 20) {
memcpy (data_, m_read_ptr, count);
m_read_ptr += count;
}
else {
char* s = data_;
char* p = m_read_ptr;
int i = count;
while (i-- > 0) {
*s++ = *p++;
}
m_read_ptr = p;
}
DL((STRMBUFTRACE,"Transferred %d bytes to user-space buffer\n", count));
return (count);
}
int
Streambuf::
uflow ()
{
trace_with_mask("Streambuf::uflow",STRMBUFTRACE);
if (underflow () == EOF)
return EOF;
dump ();
return *(unsigned char *) m_read_ptr++;
}
int
Streambuf::
xsputn (const char* data_, int len_)
{
trace_with_mask("Streambuf::xsputn",STRMBUFTRACE);
const char* s = data_;
int more = len_;
if (more <= 0) {
return 0;
}
for (;;) {
int count = m_write_end - m_write_ptr; // Space available
if (count > 0) {
if (count > more) // Enough buffer space
count = more;
if (count > 20) {
memcpy (m_write_ptr, s, count);
s += count;
m_write_ptr += count;
}
else if (count <= 0) {
count = 0;
}
else {
char* p = m_write_ptr;
int i;
for (i=count; --i >= 0;) {
*p++ = *s++;
}
m_write_ptr = p;
}
more -= count;
} // if (count>0)
if (more == 0 || overflow ((unsigned char) *s++) == EOF) {
break;
}
more--;
} // for (;;)
return (len_ - more);
}
int
Streambuf::doallocate ()
{
trace_with_mask("Streambuf::doallocate",STRMBUFTRACE);
char* buf;
buf = new char [1024];
if (buf == NULL) {
return EOF;
}
setb (buf, buf+1024, 1);
return 1;
}
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
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
u_long
unsigned long u_long
Definition Logger_Impl.h:41
MemDump.h
A Hex/Ascii memory dump of similar to od(1) UNIX utility.
Streambuf.h
Streambuf class is based on Standard C++ iostream streambuf class.
ASSA::MemDump
Definition MemDump.h:41
ASSA::MemDump::getMemDump
const char * getMemDump() const
Obtain a pointer to the dump image (null-terminated char string).
Definition MemDump.h:94
ASSA::Streambuf
Streambuf class.
Definition Streambuf.h:91
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::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::unbuffered
int unbuffered()
Definition Streambuf.h:408
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::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::setb
void setb(char *b_, char *eb_, int del_)
Establish the reserve area (buffer).
Definition Streambuf.cpp:80
ASSA::Streambuf::sync
virtual int sync()
This function synchronizes the streambuf with its actual stream of characters.
Definition Streambuf.h:523
ASSA::Streambuf::sgetc
int sgetc()
This function should probably have been called ``sgetc''.
Definition Streambuf.h:549
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::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_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::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::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
ASSA::STRMBUF
@ STRMBUF
Class Streambuf & friends messages.
Definition LogMask.h:45