#pragma once #include #include namespace Balau { class FileSystem { public: static int mkdir(const char * path) throw (GeneralException); }; class ENoEnt : public GeneralException { public: ENoEnt(const char * name) : GeneralException(String("No such file or directory: `") + name + "'") { } }; class IOBase; template class IO; namespace Events { class BaseEvent; }; class Handle { public: virtual ~Handle() { Assert(m_refCount == 0); } virtual void close() throw (GeneralException) = 0; virtual bool isClosed() = 0; virtual bool isEOF() = 0; virtual bool canSeek(); virtual bool canRead(); virtual bool canWrite(); virtual const char * getName() = 0; virtual ssize_t read(void * buf, size_t count) throw (GeneralException); virtual ssize_t write(const void * buf, size_t count) throw (GeneralException); void writeString(const char * str, ssize_t len = -1) { if (len < 0) len = strlen(str); forceWrite(str, len); } void writeString(const String & str) { forceWrite(str.to_charp(), str.strlen()); } void seek(off_t offset, int whence = SEEK_SET) { rseek(offset, whence); } virtual void rseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); virtual void wseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); off_t tell() { return rtell(); } virtual off_t rtell() throw (GeneralException); virtual off_t wtell() throw (GeneralException); virtual off_t getSize(); virtual time_t getMTime(); ssize_t forceRead(void * buf, size_t count, Events::BaseEvent * evt = NULL) throw (GeneralException); ssize_t forceWrite(const void * buf, size_t count, Events::BaseEvent * evt = NULL) throw (GeneralException); protected: Handle() : m_refCount(0) { } private: void addRef() { m_refCount++; } void delRef() { if (--m_refCount == 0) { if (!isClosed()) close(); delete this; } } friend class IOBase; template friend class IO; int m_refCount; }; class IOBase { public: IOBase() : m_h(NULL) { } ~IOBase() { if (m_h) m_h->delRef(); } protected: void setHandle(Handle * h) { m_h = h; if (m_h) m_h->addRef(); } Handle * m_h; template friend class IO; }; template class IO : public IOBase { public: IO() { } IO(T * h) { setHandle(h); } IO(const IO & io) { if (io.m_h) setHandle(io.m_h); } template IO(const IO & io) { if (io.m_h) setHandle(io.m_h); } IO & operator=(const IO & io) { if (m_h) m_h->delRef(); setHandle(io.m_h); return *this; } T * operator->() { Assert(m_h); T * r = dynamic_cast(m_h); Assert(r); return r; } bool isNull() { return dynamic_cast(m_h); } }; class SeekableHandle : public Handle { public: SeekableHandle() : m_wOffset(0), m_rOffset(0) { } virtual bool canSeek(); virtual void rseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); virtual void wseek(off_t offset, int whence = SEEK_SET) throw (GeneralException); virtual off_t rtell() throw (GeneralException); virtual off_t wtell() throw (GeneralException); virtual bool isEOF(); protected: off_t getWOffset() { return m_wOffset; } off_t getROffset() { return m_rOffset; } private: off_t m_wOffset, m_rOffset; }; class ReadOnly : public Handle { public: ReadOnly(IO & io) : m_io(io) { Assert(m_io->canRead()); } 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(off_t offset, int whence = SEEK_SET) throw (GeneralException) { m_io->rseek(offset, whence); } virtual void wseek(off_t offset, int whence = SEEK_SET) throw (GeneralException) { throw GeneralException("Can't write"); } virtual off_t rtell() throw (GeneralException) { return m_io->rtell(); } virtual off_t wtell() throw (GeneralException) { throw GeneralException("Can't write"); } virtual off_t getSize() { return m_io->getSize(); } virtual time_t getMTime() { return m_io->getMTime(); } private: IO m_io; }; class WriteOnly : public Handle { public: WriteOnly(IO & io) : m_io(io) { Assert(m_io->canWrite()); } 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(off_t offset, int whence = SEEK_SET) throw (GeneralException) { throw GeneralException("Can't read"); } virtual void wseek(off_t offset, int whence = SEEK_SET) throw (GeneralException) { return m_io->wseek(offset, whence); } virtual off_t rtell() throw (GeneralException) { throw GeneralException("Can't read"); } virtual off_t wtell() throw (GeneralException) { return m_io->wtell(); } virtual off_t getSize() { return m_io->getSize(); } virtual time_t getMTime() { return m_io->getMTime(); } private: IO m_io; }; };