kbufferedio.cpp
/*
* This file is part of the KDE libraries
* Copyright (C) 2001 Thiago Macieira <thiago.macieira@kdemail.net>
*
* 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.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <string.h>
#include <qptrlist.h>
#include <qcstring.h>
#include "kbufferedio.h"
// constructor
KBufferedIO::KBufferedIO() :
inBufIndex(0), outBufIndex(0)
{
inBuf.setAutoDelete(true);
outBuf.setAutoDelete(true);
}
// destructor
KBufferedIO::~KBufferedIO()
{
}
// sets the buffer sizes
// this implementation doesn't support setting the buffer sizes
// if any parameter is different than -1 or -2, fail
bool KBufferedIO::setBufferSize(int rsize, int wsize /* = -2 */)
{
if (wsize != -2 && wsize != -1)
return false;
if (rsize != -2 && rsize != -1)
return false;
return true;
}
int KBufferedIO::bytesAvailable() const
{
return readBufferSize();
}
int KBufferedIO::bytesToWrite() const
{
return writeBufferSize();
}
// This function will scan the read buffer for a '\n'
bool KBufferedIO::canReadLine() const
{
if (bytesAvailable() == 0)
return false; // no new line in here
QByteArray* buf;
// scan each QByteArray for the occurrence of '\n'
QPtrList<QByteArray> &buflist = ((KBufferedIO*)this)->inBuf;
buf = buflist.first();
char *p = buf->data() + inBufIndex;
int n = buf->size() - inBufIndex;
while (buf != NULL)
{
while (n--)
if (*p++ == '\n')
return true;
buf = buflist.next();
if (buf != NULL)
{
p = buf->data();
n = buf->size();
}
}
return false; // no new line found
}
// unreads the current data
// that is, writes into the read buffer, at the beginning
int KBufferedIO::unreadBlock(const char *data, uint len)
{
return feedReadBuffer(len, data, true);
}
//
// protected member functions
//
unsigned KBufferedIO::consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard)
{
{
register unsigned u = readBufferSize();
if (nbytes > u)
nbytes = u; // we can't consume more than there is
}
QByteArray *buf;
unsigned copied = 0;
unsigned index = inBufIndex;
buf = inBuf.first();
while (nbytes && buf)
{
// should we copy it all?
unsigned to_copy = buf->size() - index;
if (to_copy > nbytes)
to_copy = nbytes;
if (destbuffer)
memcpy(destbuffer + copied, buf->data() + index, to_copy);
nbytes -= to_copy;
copied += to_copy;
if (buf->size() - index > to_copy)
{
index += to_copy;
break; // we aren't copying everything, that means that's
// all the user wants
}
else
{
index = 0;
if (discard)
{
inBuf.remove();
buf = inBuf.first();
}
else
buf = inBuf.next();
}
}
if (discard)
inBufIndex = index;
return copied;
}
void KBufferedIO::consumeWriteBuffer(unsigned nbytes)
{
QByteArray *buf = outBuf.first();
if (buf == NULL)
return; // nothing to consume
if (nbytes < buf->size() - outBufIndex)
// we want to consume less than there is in the first buffer
outBufIndex += nbytes;
else
{
nbytes -= buf->size() - outBufIndex;
outBufIndex = 0;
outBuf.remove();
while ((buf = outBuf.current()) != NULL)
if (buf->size() <= nbytes)
{
nbytes -= buf->size();
outBuf.remove();
}
else
{
outBufIndex = nbytes;
break;
}
}
}
unsigned KBufferedIO::feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning)
{
if (nbytes == 0)
return 0;
QByteArray *a = new QByteArray(nbytes);
a->duplicate(buffer, nbytes);
if (atBeginning)
inBuf.prepend(a);
else
inBuf.append(a);
return nbytes;
}
unsigned KBufferedIO::feedWriteBuffer(unsigned nbytes, const char *buffer)
{
if (nbytes == 0)
return 0;
QByteArray *a = new QByteArray(nbytes);
a->duplicate(buffer, nbytes);
outBuf.append(a);
return nbytes;
}
unsigned KBufferedIO::readBufferSize() const
{
unsigned count = 0;
QByteArray *buf = ((KBufferedIO*)this)->inBuf.first();
while (buf != NULL)
{
count += buf->size();
buf = ((KBufferedIO*)this)->inBuf.next();
}
return count - inBufIndex;
}
unsigned KBufferedIO::writeBufferSize() const
{
unsigned count = 0;
QByteArray *buf = ((KBufferedIO*)this)->outBuf.first();
while (buf != NULL)
{
count += buf->size();
buf = (const_cast<KBufferedIO*>(this))->outBuf.next();
}
return count - outBufIndex;
}
void KBufferedIO::virtual_hook( int id, void* data )
{ KAsyncIO::virtual_hook( id, data ); }
#include "kbufferedio.moc"
KBufferedIO::bytesToWrite
virtual int bytesToWrite() const
Returns the number of bytes yet to write, still in the write buffer.
Definition: kbufferedio.cpp:119
KBufferedIO::readBufferSize
virtual unsigned readBufferSize() const
Returns the number of bytes in the read buffer.
Definition: kbufferedio.cpp:270
KBufferedIO::consumeReadBuffer
virtual unsigned consumeReadBuffer(unsigned nbytes, char *destbuffer, bool discard=true)
Consumes data from the input buffer.
Definition: kbufferedio.cpp:164
KBufferedIO::feedReadBuffer
virtual unsigned feedReadBuffer(unsigned nbytes, const char *buffer, bool atBeginning=false)
Feeds data into the input buffer.
Definition: kbufferedio.cpp:243
KBufferedIO::~KBufferedIO
virtual ~KBufferedIO()
Destroys this class.
Definition: kbufferedio.cpp:97
KBufferedIO
This abstract class implements basic functionality for buffered input/output.
Definition: kbufferedio.h:56
KBufferedIO::outBuf
QPtrList< QByteArray > outBuf
For an explanation on how this buffer work, please refer to the comments at the top of kbufferedio...
Definition: kbufferedio.h:218
KBufferedIO::feedWriteBuffer
virtual unsigned feedWriteBuffer(unsigned nbytes, const char *buffer)
Feeds data into the output buffer.
Definition: kbufferedio.cpp:259
KBufferedIO::inBufIndex
unsigned inBufIndex
Offset into first input buffer.
Definition: kbufferedio.h:220
KBufferedIO::bytesAvailable
virtual int bytesAvailable() const
Returns the number of bytes available for reading in the read buffer.
Definition: kbufferedio.cpp:114
KBufferedIO::consumeWriteBuffer
virtual void consumeWriteBuffer(unsigned nbytes)
Consumes data from the output buffer.
Definition: kbufferedio.cpp:214
KBufferedIO::KBufferedIO
KBufferedIO()
Definition: kbufferedio.cpp:89
KBufferedIO::canReadLine
virtual bool canReadLine() const
Checks whether there is enough data in the buffer to read a line.
Definition: kbufferedio.cpp:125
KBufferedIO::unreadBlock
virtual int unreadBlock(const char *data, uint len)
Unreads some data.
Definition: kbufferedio.cpp:155
KBufferedIO::setBufferSize
virtual bool setBufferSize(int rsize, int wsize=-2)
Sets the internal buffer size to value.
Definition: kbufferedio.cpp:104
KBufferedIO::outBufIndex
unsigned outBufIndex
Offset into first output buffer.
Definition: kbufferedio.h:220
KBufferedIO::inBuf
QPtrList< QByteArray > inBuf
For an explanation on how this buffer work, please refer to the comments at the top of kbufferedio...
Definition: kbufferedio.h:212
KBufferedIO::writeBufferSize
virtual unsigned writeBufferSize() const
Returns the number of bytes in the write buffer.
Definition: kbufferedio.cpp:283

KDE Home | KDE Accessibility Home | Description of Access Keys