From ec0f82bb6ea911baee6b9654f799f037282bf5b9 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sun, 10 Aug 2014 02:58:35 -0700 Subject: Introducing notion of filters. --- includes/BStream.h | 19 +++-------- includes/Buffer.h | 14 ++++---- includes/Handle.h | 94 +++++++++++++++++++++++++++++------------------------- includes/ZHandle.h | 24 ++++++-------- 4 files changed, 74 insertions(+), 77 deletions(-) (limited to 'includes') diff --git a/includes/BStream.h b/includes/BStream.h index f39765d..0e7d5f6 100644 --- a/includes/BStream.h +++ b/includes/BStream.h @@ -4,31 +4,22 @@ namespace Balau { -class BStream : public Handle { +class BStream : public Filter { public: - BStream(const IO & h); - virtual void close() throw (GeneralException); - virtual bool isClosed(); - virtual bool isEOF(); - virtual bool canRead(); - virtual bool canWrite() { return m_h->canWrite(); } - virtual const char * getName(); + BStream(IO h); + virtual void close() throw (GeneralException) override; + virtual bool isEOF() override { return (m_availBytes == 0) && Filter::isEOF(); } + virtual const char * getName() override { return m_name.to_charp(); } virtual ssize_t read(void * buf, size_t count) throw (GeneralException); - virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) { return m_h->write(buf, count); } - virtual off64_t getSize(); int peekNextByte(); String readString(bool putNL = false); bool isEmpty() { return m_availBytes == 0; } - void detach() { m_detached = true; } private: - IO m_h; uint8_t * m_buffer = NULL; size_t m_availBytes = 0; size_t m_cursor = 0; String m_name; bool m_passThru = false; - bool m_detached = false; - bool m_closed = false; }; }; diff --git a/includes/Buffer.h b/includes/Buffer.h index 6b5318f..4983b98 100644 --- a/includes/Buffer.h +++ b/includes/Buffer.h @@ -12,12 +12,14 @@ class Buffer : public SeekableHandle { virtual void close() throw (GeneralException) override; virtual ssize_t read(void * buf, size_t count) throw (GeneralException) override; virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) override; - virtual bool isClosed() override; - virtual bool isEOF() override; - virtual bool canRead() override; - virtual bool canWrite() override; - virtual const char * getName() override; - virtual off64_t getSize() override; + virtual bool isClosed() override { return false; } + virtual bool isEOF() override { return rtell() == m_bufSize; } + virtual bool canRead() override { return true; } + virtual bool canWrite() override { return !m_fromConst; } + virtual bool canEAgainOnRead() override { return false; } + virtual bool canEAgainOnWrite() override { return false; } + virtual const char * getName() override { return "Buffer"; } + virtual off64_t getSize() override { return m_bufSize; } const uint8_t * getBuffer() { return m_buffer + rtell(); } void reset(); void clear(); diff --git a/includes/Handle.h b/includes/Handle.h index 6a087a7..1cbcabf 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -51,17 +51,19 @@ class Handle { virtual const char * getName() = 0; // normal API - virtual bool canSeek(); - virtual bool canRead(); - virtual bool canWrite(); + virtual bool canSeek() { return false; } + virtual bool canRead() { return false; } + virtual bool canWrite() { return false; } + virtual bool canEAgainOnRead() { return true; } + virtual bool canEAgainOnWrite() { return true; } virtual ssize_t read(void * buf, size_t count) throw (GeneralException) WARN_UNUSED_RESULT; virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) WARN_UNUSED_RESULT; virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException); virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException); virtual off64_t rtell() throw (GeneralException); virtual off64_t wtell() throw (GeneralException); - virtual off64_t getSize(); - virtual time_t getMTime(); + virtual off64_t getSize() { return -1; } + virtual time_t getMTime() { return -1; } virtual bool isPendingComplete() { return true; } enum Endianness { @@ -208,48 +210,54 @@ class SeekableHandle : public Handle { off64_t m_wOffset, m_rOffset; }; -class ReadOnly : public Handle { - public: - ReadOnly(IO & io) : m_io(io) { AAssert(m_io->canRead(), "You need to use ReadOnly with a Handle that can at least read"); } - virtual void close() throw (GeneralException) { m_io->close(); } - virtual bool isClosed() { return m_io->isClosed(); } - virtual bool isEOF() { return m_io->isEOF(); } - virtual bool canSeek() { return m_io->canSeek(); } - virtual bool canRead() { return true; } - virtual bool canWrite() { return false; } - virtual const char * getName() { return m_io->getName(); } - virtual ssize_t read(void * buf, size_t count) throw (GeneralException) { return m_io->read(buf, count); } - virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) { throw GeneralException("Can't write"); } - virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) { m_io->rseek(offset, whence); } - virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) { throw GeneralException("Can't write"); } - virtual off64_t rtell() throw (GeneralException) { return m_io->rtell(); } - virtual off64_t wtell() throw (GeneralException) { throw GeneralException("Can't write"); } - virtual off64_t getSize() { return m_io->getSize(); } - virtual time_t getMTime() { return m_io->getMTime(); } - private: +class Filter : public Handle { +public: + virtual void close() throw (GeneralException) override { if (!m_detached) m_io->close(); m_closed = true; } + virtual bool isClosed() override { return m_closed || m_io->isClosed(); } + virtual bool isEOF() override { return m_closed || m_io->isEOF(); } + virtual bool canSeek() override { return m_io->canSeek(); } + virtual bool canRead() override { return m_io->canRead(); } + virtual bool canWrite() override { return m_io->canWrite(); } + virtual bool canEAgainOnRead() { return m_io->canEAgainOnRead(); } + virtual bool canEAgainOnWrite() { return m_io->canEAgainOnWrite(); } + void detach() { m_detached = true; } + bool isDetached() { return m_detached; } + virtual const char * getName() override { return m_io->getName(); } + virtual ssize_t read(void * buf, size_t count) throw (GeneralException) override { return m_io->read(buf, count); } + virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) override { return m_io->write(buf, count); } + virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) override { m_io->rseek(offset, whence); } + virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) override { m_io->wseek(offset, whence); } + virtual off64_t rtell() throw (GeneralException) override { return m_io->rtell(); } + virtual off64_t wtell() throw (GeneralException) override { return m_io->wtell(); } + virtual off64_t getSize() override { return m_io->getSize(); } + virtual time_t getMTime() override { return m_io->getMTime(); } + virtual bool isPendingComplete() { return m_io->isPendingComplete(); } +protected: + Filter(IO & io) : m_io(io) { } + IO getIO() { return m_io; } +private: + bool m_detached = false, m_closed = false; IO m_io; }; -class WriteOnly : public Handle { +class ReadOnly : public Filter { public: - WriteOnly(IO & io) : m_io(io) { AAssert(m_io->canWrite(), "You need to use WriteOnly with a Handle that can at least write"); } - virtual void close() throw (GeneralException) { m_io->close(); } - virtual bool isClosed() { return m_io->isClosed(); } - virtual bool isEOF() { return m_io->isEOF(); } - virtual bool canSeek() { return m_io->canSeek(); } - virtual bool canRead() { return false; } - virtual bool canWrite() { return true; } - virtual const char * getName() { return m_io->getName(); } - virtual ssize_t read(void * buf, size_t count) throw (GeneralException) { throw GeneralException("Can't read"); } - virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) { return m_io->write(buf, count); } - virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) { throw GeneralException("Can't read"); } - virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) { return m_io->wseek(offset, whence); } - virtual off64_t rtell() throw (GeneralException) { throw GeneralException("Can't read"); } - virtual off64_t wtell() throw (GeneralException) { return m_io->wtell(); } - virtual off64_t getSize() { return m_io->getSize(); } - virtual time_t getMTime() { return m_io->getMTime(); } - private: - IO m_io; + ReadOnly(IO & io) : Filter(io) { AAssert(io->canRead(), "You need to use ReadOnly with a Handle that can at least read"); } + virtual bool canRead() override { return true; } + virtual bool canWrite() override { return false; } + virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) override { throw GeneralException("Can't write"); } + virtual void wseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) override { throw GeneralException("Can't write"); } + virtual off64_t wtell() throw (GeneralException) override { throw GeneralException("Can't write"); } +}; + +class WriteOnly : public Filter { + public: + WriteOnly(IO & io) : Filter(io) { AAssert(io->canWrite(), "You need to use WriteOnly with a Handle that can at least write"); } + virtual bool canRead() override { return false; } + virtual bool canWrite() override { return true; } + virtual ssize_t read(void * buf, size_t count) throw (GeneralException) override { throw GeneralException("Can't read"); } + virtual void rseek(off64_t offset, int whence = SEEK_SET) throw (GeneralException) override { throw GeneralException("Can't read"); } + virtual off64_t rtell() throw (GeneralException) override { throw GeneralException("Can't read"); } }; }; diff --git a/includes/ZHandle.h b/includes/ZHandle.h index dfb73fb..30db407 100644 --- a/includes/ZHandle.h +++ b/includes/ZHandle.h @@ -6,30 +6,26 @@ namespace Balau { -class ZStream : public Handle { +class ZStream : public Filter { public: typedef enum { ZLIB, GZIP, RAW, } header_t; - ZStream(const IO & h, int level = Z_BEST_COMPRESSION, header_t header = ZLIB); - virtual void close() throw (GeneralException); - virtual bool isClosed(); - virtual bool isEOF(); - virtual bool canRead(); - virtual bool canWrite(); - virtual const char * getName(); - virtual ssize_t read(void * buf, size_t count) throw (GeneralException); - virtual ssize_t write(const void * buf, size_t count) throw (GeneralException); - void detach() { m_detached = true; } + ZStream(IO h, int level = Z_BEST_COMPRESSION, header_t header = ZLIB); + virtual void close() throw (GeneralException) override; + virtual ssize_t read(void * buf, size_t count) throw (GeneralException) override; + virtual ssize_t write(const void * buf, size_t count) throw (GeneralException) override; + virtual bool isEOF() override { return m_phase == CLOSING || m_eof || Filter::isEOF(); } + virtual bool isClosed() override { return m_phase == CLOSING || Filter::isClosed(); } + virtual const char * getName() override { return m_name.to_charp(); } void flush() { doFlush(false); } void setUseAsyncOp(bool useAsyncOp) { m_useAsyncOp = useAsyncOp; } - virtual bool isPendingComplete(); + virtual bool isPendingComplete() override; void finish() { doFlush(true); } void doFlush(bool finish); private: - IO m_h; z_stream m_zin, m_zout; String m_name; uint8_t * m_buf = NULL; @@ -50,7 +46,7 @@ class ZStream : public Handle { size_t m_total, m_count, m_compressed; AsyncOperation * m_op = NULL; ssize_t m_status = IDLE; - bool m_detached = false, m_closed = false, m_eof = false, m_useAsyncOp = true; + bool m_useAsyncOp = true, m_eof = false; }; }; -- cgit v1.2.3