Started over.

This commit is contained in:
crs
2001-10-06 14:13:28 +00:00
parent 27ead1f713
commit ff81f708e2
132 changed files with 7634 additions and 3960 deletions

107
io/CBufferedInputStream.cpp Normal file
View File

@@ -0,0 +1,107 @@
#include "CBufferedInputStream.h"
#include "CLock.h"
#include "CMutex.h"
#include "CThread.h"
#include "IJob.h"
#include "XIO.h"
#include <string.h>
#include <assert.h>
//
// CBufferedInputStream
//
CBufferedInputStream::CBufferedInputStream(CMutex* mutex, IJob* closeCB) :
m_mutex(mutex),
m_empty(mutex, true),
m_closeCB(closeCB),
m_closed(false),
m_hungup(false)
{
assert(m_mutex != NULL);
}
CBufferedInputStream::~CBufferedInputStream()
{
delete m_closeCB;
}
void CBufferedInputStream::write(
const void* data, UInt32 n) throw()
{
if (!m_hungup && n > 0) {
m_buffer.write(data, n);
m_empty = (m_buffer.getSize() == 0);
m_empty.broadcast();
}
}
void CBufferedInputStream::hangup() throw()
{
m_hungup = true;
m_empty.broadcast();
}
UInt32 CBufferedInputStream::readNoLock(
void* dst, UInt32 n) throw(XIO)
{
if (m_closed) {
throw XIOClosed();
}
// wait for data (or hangup)
while (!m_hungup && m_empty == true) {
m_empty.wait();
}
// read data
const UInt32 count = m_buffer.getSize();
if (n > count) {
n = count;
}
if (n > 0) {
if (dst != NULL) {
::memcpy(dst, m_buffer.peek(n), n);
}
m_buffer.pop(n);
}
// update empty state
if (m_buffer.getSize() == 0) {
m_empty = true;
m_empty.broadcast();
}
return n;
}
UInt32 CBufferedInputStream::getSizeNoLock() const throw()
{
return m_buffer.getSize();
}
void CBufferedInputStream::close() throw(XIO)
{
CLock lock(m_mutex);
if (m_closed) {
throw XIOClosed();
}
m_closed = true;
if (m_closeCB) {
m_closeCB->run();
}
}
UInt32 CBufferedInputStream::read(
void* dst, UInt32 n) throw(XIO)
{
CLock lock(m_mutex);
return readNoLock(dst, n);
}
UInt32 CBufferedInputStream::getSize() const throw()
{
CLock lock(m_mutex);
return getSizeNoLock();
}

51
io/CBufferedInputStream.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef CBUFFEREDINPUTSTREAM_H
#define CBUFFEREDINPUTSTREAM_H
#include "CStreamBuffer.h"
#include "CCondVar.h"
#include "IInputStream.h"
class CMutex;
class IJob;
class CBufferedInputStream : public IInputStream {
public:
CBufferedInputStream(CMutex*, IJob* adoptedCloseCB);
~CBufferedInputStream();
// the caller is expected to lock the mutex before calling
// methods unless otherwise noted.
// manipulators
// write() appends n bytes to the buffer
void write(const void*, UInt32 n) throw();
// causes read() to always return immediately. if there is no
// more data then it returns 0. further writes are discarded.
void hangup() throw();
// same as read() but caller must lock the mutex
UInt32 readNoLock(void*, UInt32 count) throw(XIO);
// accessors
// same as getSize() but caller must lock the mutex
UInt32 getSizeNoLock() const throw();
// IInputStream overrides
// these all lock the mutex for their duration
virtual void close() throw(XIO);
virtual UInt32 read(void*, UInt32 count) throw(XIO);
virtual UInt32 getSize() const throw();
private:
CMutex* m_mutex;
CCondVar<bool> m_empty;
IJob* m_closeCB;
CStreamBuffer m_buffer;
bool m_closed;
bool m_hungup;
};
#endif

View File

@@ -0,0 +1,79 @@
#include "CBufferedOutputStream.h"
#include "CLock.h"
#include "CMutex.h"
#include "CThread.h"
#include "IJob.h"
#include "XIO.h"
#include <assert.h>
//
// CBufferedOutputStream
//
CBufferedOutputStream::CBufferedOutputStream(CMutex* mutex, IJob* closeCB) :
m_mutex(mutex),
m_closeCB(closeCB),
m_closed(false)
{
assert(m_mutex != NULL);
}
CBufferedOutputStream::~CBufferedOutputStream()
{
delete m_closeCB;
}
const void* CBufferedOutputStream::peek(UInt32 n) throw()
{
return m_buffer.peek(n);
}
void CBufferedOutputStream::pop(UInt32 n) throw()
{
m_buffer.pop(n);
}
UInt32 CBufferedOutputStream::getSize() const throw()
{
return m_buffer.getSize();
}
void CBufferedOutputStream::close() throw(XIO)
{
CLock lock(m_mutex);
if (m_closed) {
throw XIOClosed();
}
m_closed = true;
if (m_closeCB) {
m_closeCB->run();
}
}
UInt32 CBufferedOutputStream::write(
const void* data, UInt32 n) throw(XIO)
{
CLock lock(m_mutex);
if (m_closed) {
throw XIOClosed();
}
m_buffer.write(data, n);
return n;
}
void CBufferedOutputStream::flush() throw(XIO)
{
// wait until all data is written
while (getSizeWithLock() > 0) {
CThread::sleep(0.05);
}
}
UInt32 CBufferedOutputStream::getSizeWithLock() const throw()
{
CLock lock(m_mutex);
return m_buffer.getSize();
}

View File

@@ -0,0 +1,46 @@
#ifndef CBUFFEREDOUTPUTSTREAM_H
#define CBUFFEREDOUTPUTSTREAM_H
#include "CStreamBuffer.h"
#include "IOutputStream.h"
class CMutex;
class IJob;
class CBufferedOutputStream : public IOutputStream {
public:
CBufferedOutputStream(CMutex*, IJob* adoptedCloseCB);
~CBufferedOutputStream();
// the caller is expected to lock the mutex before calling
// methods unless otherwise noted.
// manipulators
// peek() returns a buffer of n bytes (which must be <= getSize()).
// pop() discards the next n bytes.
const void* peek(UInt32 n) throw();
void pop(UInt32 n) throw();
// accessors
// return the number of bytes in the buffer
UInt32 getSize() const throw();
// IOutputStream overrides
// these all lock the mutex for their duration
virtual void close() throw(XIO);
virtual UInt32 write(const void*, UInt32 count) throw(XIO);
virtual void flush() throw(XIO);
private:
UInt32 getSizeWithLock() const throw();
private:
CMutex* m_mutex;
IJob* m_closeCB;
CStreamBuffer m_buffer;
bool m_closed;
};
#endif

25
io/CInputStreamFilter.cpp Normal file
View File

@@ -0,0 +1,25 @@
#include "CInputStreamFilter.h"
#include <assert.h>
//
// CInputStreamFilter
//
CInputStreamFilter::CInputStreamFilter(IInputStream* stream, bool adopted) :
m_stream(stream),
m_adopted(adopted)
{
assert(m_stream != NULL);
}
CInputStreamFilter::~CInputStreamFilter()
{
if (m_adopted) {
delete m_stream;
}
}
IInputStream* CInputStreamFilter::getStream() const throw()
{
return m_stream;
}

28
io/CInputStreamFilter.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef CINPUTSTREAMFILTER_H
#define CINPUTSTREAMFILTER_H
#include "IInputStream.h"
class CInputStreamFilter : public IInputStream {
public:
CInputStreamFilter(IInputStream*, bool adoptStream = true);
~CInputStreamFilter();
// manipulators
// accessors
// IInputStream overrides
virtual void close() throw(XIO) = 0;
virtual UInt32 read(void*, UInt32 maxCount) throw(XIO) = 0;
virtual UInt32 getSize() const throw() = 0;
protected:
IInputStream* getStream() const throw();
private:
IInputStream* m_stream;
bool m_adopted;
};
#endif

View File

@@ -0,0 +1,25 @@
#include "COutputStreamFilter.h"
#include <assert.h>
//
// COutputStreamFilter
//
COutputStreamFilter::COutputStreamFilter(IOutputStream* stream, bool adopted) :
m_stream(stream),
m_adopted(adopted)
{
assert(m_stream != NULL);
}
COutputStreamFilter::~COutputStreamFilter()
{
if (m_adopted) {
delete m_stream;
}
}
IOutputStream* COutputStreamFilter::getStream() const throw()
{
return m_stream;
}

28
io/COutputStreamFilter.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef COUTPUTSTREAMFILTER_H
#define COUTPUTSTREAMFILTER_H
#include "IOutputStream.h"
class COutputStreamFilter : public IOutputStream {
public:
COutputStreamFilter(IOutputStream*, bool adoptStream = true);
~COutputStreamFilter();
// manipulators
// accessors
// IOutputStream overrides
virtual void close() throw(XIO) = 0;
virtual UInt32 write(const void*, UInt32 count) throw(XIO) = 0;
virtual void flush() throw(XIO) = 0;
protected:
IOutputStream* getStream() const throw();
private:
IOutputStream* m_stream;
bool m_adopted;
};
#endif

107
io/CStreamBuffer.cpp Normal file
View File

@@ -0,0 +1,107 @@
#include "CStreamBuffer.h"
#include <assert.h>
//
// CStreamBuffer
//
const UInt32 CStreamBuffer::kChunkSize = 4096;
CStreamBuffer::CStreamBuffer() : m_size(0)
{
// do nothing
}
CStreamBuffer::~CStreamBuffer()
{
// do nothing
}
const void* CStreamBuffer::peek(UInt32 n) throw()
{
assert(n <= m_size);
// reserve space in first chunk
ChunkList::iterator head = m_chunks.begin();
head->reserve(n);
// consolidate chunks into the first chunk until it has n bytes
ChunkList::iterator scan = head;
++scan;
while (head->size() < n && scan != m_chunks.end()) {
head->insert(head->end(), scan->begin(), scan->end());
scan = m_chunks.erase(scan);
}
return reinterpret_cast<const void*>(head->begin());
}
void CStreamBuffer::pop(UInt32 n) throw()
{
m_size -= n;
// discard chunks until more than n bytes would've been discarded
ChunkList::iterator scan = m_chunks.begin();
while (scan->size() <= n && scan != m_chunks.end()) {
n -= scan->size();
scan = m_chunks.erase(scan);
}
// if there's anything left over then remove it from the head chunk.
// if there's no head chunk then we're already empty.
if (scan == m_chunks.end()) {
m_size = 0;
}
else if (n > 0) {
scan->erase(scan->begin(), scan->begin() + n);
}
}
void CStreamBuffer::write(
const void* vdata, UInt32 n) throw()
{
assert(vdata != NULL);
if (n == 0) {
return;
}
m_size += n;
// cast data to bytes
const UInt8* data = reinterpret_cast<const UInt8*>(vdata);
// point to last chunk if it has space, otherwise append an empty chunk
ChunkList::iterator scan = m_chunks.end();
if (scan != m_chunks.begin()) {
--scan;
if (scan->size() >= kChunkSize)
++scan;
}
if (scan == m_chunks.end()) {
scan = m_chunks.insert(scan);
}
// append data in chunks
while (n > 0) {
// choose number of bytes for next chunk
UInt32 count = kChunkSize - scan->size();
if (count > n)
count = n;
// transfer data
scan->insert(scan->end(), data, data + count);
n -= count;
data += count;
// append another empty chunk if we're not done yet
if (n > 0) {
scan = m_chunks.insert(scan);
}
}
}
UInt32 CStreamBuffer::getSize() const throw()
{
return m_size;
}

38
io/CStreamBuffer.h Normal file
View File

@@ -0,0 +1,38 @@
#ifndef CSTREAMBUFFER_H
#define CSTREAMBUFFER_H
#include "BasicTypes.h"
#include <list>
#include <vector>
class CStreamBuffer {
public:
CStreamBuffer();
~CStreamBuffer();
// manipulators
// peek() returns a buffer of n bytes (which must be <= getSize()).
// pop() discards the next n bytes.
const void* peek(UInt32 n) throw();
void pop(UInt32 n) throw();
// write() appends n bytes to the buffer
void write(const void*, UInt32 n) throw();
// accessors
// return the number of bytes in the buffer
UInt32 getSize() const throw();
private:
static const UInt32 kChunkSize;
typedef std::vector<UInt8> Chunk;
typedef std::list<Chunk> ChunkList;
ChunkList m_chunks;
UInt32 m_size;
};
#endif

29
io/IInputStream.h Normal file
View File

@@ -0,0 +1,29 @@
#ifndef IINPUTSTREAM_H
#define IINPUTSTREAM_H
#include "IInterface.h"
#include "BasicTypes.h"
#include "XIO.h"
class IInputStream : public IInterface {
public:
// manipulators
// close the stream
virtual void close() throw(XIO) = 0;
// read up to maxCount bytes into buffer, return number read.
// blocks if no data is currently available. if buffer is NULL
// then the data is discarded.
virtual UInt32 read(void* buffer, UInt32 maxCount) throw(XIO) = 0;
// accessors
// get a conservative estimate of the available bytes to read
// (i.e. a number not greater than the actual number of bytes).
// some streams may not be able to determine this and will always
// return zero.
virtual UInt32 getSize() const throw() = 0;
};
#endif

24
io/IOutputStream.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef IOUTPUTSTREAM_H
#define IOUTPUTSTREAM_H
#include "IInterface.h"
#include "BasicTypes.h"
#include "XIO.h"
class IOutputStream : public IInterface {
public:
// manipulators
// close the stream
virtual void close() throw(XIO) = 0;
// write count bytes to stream
virtual UInt32 write(const void*, UInt32 count) throw(XIO) = 0;
// flush the stream
virtual void flush() throw(XIO) = 0;
// accessors
};
#endif

29
io/Makefile Normal file
View File

@@ -0,0 +1,29 @@
DEPTH=..
include $(DEPTH)/Makecommon
#
# target file
#
TARGET = io
#
# source files
#
LCXXINCS = \
-I$(DEPTH)/base \
-I$(DEPTH)/mt \
$(NULL)
CXXFILES = \
XIO.cpp \
CInputStreamFilter.cpp \
COutputStreamFilter.cpp \
CStreamBuffer.cpp \
CBufferedInputStream.cpp \
CBufferedOutputStream.cpp \
$(NULL)
targets: $(LIBTARGET)
$(LIBTARGET): $(OBJECTS) $(DEPLIBS)
if test ! -d $(LIBDIR); then $(MKDIR) $(LIBDIR); fi
$(ARF) $(LIBTARGET) $(OBJECTS)

46
io/XIO.cpp Normal file
View File

@@ -0,0 +1,46 @@
#include "XIO.h"
//
// XIOErrno
//
XIOErrno::XIOErrno() : MXErrno()
{
// do nothing
}
XIOErrno::XIOErrno(int err) : MXErrno(err)
{
// do nothing
}
//
// XIOClose
//
CString XIOClose::getWhat() const throw()
{
return format("XIOClose", "close: %1", XIOErrno::getErrstr());
}
//
// XIOClosed
//
CString XIOClosed::getWhat() const throw()
{
return format("XIOClosed", "already closed");
}
//
// XIOEndOfStream
//
CString XIOEndOfStream::getWhat() const throw()
{
return format("XIOEndOfStream", "reached end of stream");
}

33
io/XIO.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef XIO_H
#define XIO_H
#include "XBase.h"
#include "BasicTypes.h"
class XIO : public XBase { };
class XIOErrno : public XIO, public MXErrno {
public:
XIOErrno();
XIOErrno(int);
};
class XIOClose: public XIOErrno {
protected:
// XBase overrides
virtual CString getWhat() const throw();
};
class XIOClosed : public XIO {
protected:
// XBase overrides
virtual CString getWhat() const throw();
};
class XIOEndOfStream : public XIO {
protected:
// XBase overrides
virtual CString getWhat() const throw();
};
#endif