From 67b6a78b347ab7ba269a52d772e79d12444f6e96 Mon Sep 17 00:00:00 2001 From: Pixel Date: Mon, 10 Oct 2011 23:09:53 -0700 Subject: Adding a few more features to Input, and actually creating a slightly better ClassName system, when using gcc and libstdc++. --- includes/Exceptions.h | 16 ++++++++++++++++ includes/Handle.h | 1 + includes/Input.h | 1 + src/Handle.cc | 10 ++++++---- src/Input.cc | 4 ++++ tests/rtest.txt | 1 + tests/test-Handles.cc | 16 +++++++++++++++- 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 tests/rtest.txt diff --git a/includes/Exceptions.h b/includes/Exceptions.h index 39f3239..b6730c1 100644 --- a/includes/Exceptions.h +++ b/includes/Exceptions.h @@ -1,5 +1,6 @@ #pragma once +#include #include namespace Balau { @@ -21,6 +22,21 @@ class GeneralException { static inline void AssertHelper(const String & msg) throw(GeneralException) { throw GeneralException(msg); } +class ClassName { + public: + template ClassName(T * ptr); + ~ClassName() { free(m_demangled); } + const char * c_str() const { return m_demangled; } + private: + char * m_demangled; +}; + +template +ClassName::ClassName(T * ptr) { + int status; + m_demangled = abi::__cxa_demangle(typeid(*ptr).name(), 0, 0, &status); +} + }; #define Assert(c) if (!(c)) { \ diff --git a/includes/Handle.h b/includes/Handle.h index f2b78ad..489ab75 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -53,6 +53,7 @@ class IO { 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); diff --git a/includes/Input.h b/includes/Input.h index 7f801f6..71e6845 100644 --- a/includes/Input.h +++ b/includes/Input.h @@ -9,6 +9,7 @@ class Input : public SeekableHandle { Input(const char * fname) throw (GeneralException); virtual void close() throw (GeneralException); virtual bool isClosed(); + virtual bool canRead(); virtual const char * getName(); virtual off_t getSize(); virtual time_t getMTime(); diff --git a/src/Handle.cc b/src/Handle.cc index dc4e257..e00bcf2 100644 --- a/src/Handle.cc +++ b/src/Handle.cc @@ -55,7 +55,7 @@ time_t Balau::Handle::getMTime() { return -1; } ssize_t Balau::Handle::read(void * buf, size_t count) throw (GeneralException) { if (canRead()) - throw GeneralException(String("Handle ") + getName() + " can read, but read() not implemented (missing in class " + typeid(*this).name() + ")"); + throw GeneralException(String("Handle ") + getName() + " can read, but read() not implemented (missing in class " + ClassName(this).c_str() + ")"); else throw GeneralException("Handle can't read"); return -1; @@ -63,7 +63,7 @@ ssize_t Balau::Handle::read(void * buf, size_t count) throw (GeneralException) { ssize_t Balau::Handle::write(const void * buf, size_t count) throw (GeneralException) { if (canWrite()) - throw GeneralException(String("Handle ") + getName() + " can write, but write() not implemented (missing in class " + typeid(this).name() + ")"); + throw GeneralException(String("Handle ") + getName() + " can write, but write() not implemented (missing in class " + ClassName(this).c_str() + ")"); else throw GeneralException("Handle can't write"); return -1; @@ -71,7 +71,7 @@ ssize_t Balau::Handle::write(const void * buf, size_t count) throw (GeneralExcep void Balau::Handle::rseek(off_t offset, int whence) throw (GeneralException) { if (canSeek()) - throw GeneralException(String("Handle ") + getName() + " can seek, but rseek() not implemented (missing in class " + typeid(this).name() + ")"); + throw GeneralException(String("Handle ") + getName() + " can seek, but rseek() not implemented (missing in class " + ClassName(this).c_str() + ")"); else throw GeneralException("Handle can't seek"); } @@ -82,7 +82,7 @@ void Balau::Handle::wseek(off_t offset, int whence) throw (GeneralException) { off_t Balau::Handle::rtell() throw (GeneralException) { if (canSeek()) - throw GeneralException(String("Handle ") + getName() + " can seek, but rtell() not implemented (missing in class " + typeid(this).name() + ")"); + throw GeneralException(String("Handle ") + getName() + " can seek, but rtell() not implemented (missing in class " + ClassName(this).c_str() + ")"); else throw GeneralException("Handle can't seek"); } @@ -143,11 +143,13 @@ off_t Balau::SeekableHandle::rtell() throw (GeneralException) { Assert(canRead() || canWrite()); if (!canRead()) return wtell(); + return m_rOffset; } off_t Balau::SeekableHandle::wtell() throw (GeneralException) { Assert(canRead() || canWrite()); if (!canWrite()) return rtell(); + return m_wOffset; } diff --git a/src/Input.cc b/src/Input.cc index ea078dc..09b2903 100644 --- a/src/Input.cc +++ b/src/Input.cc @@ -93,3 +93,7 @@ off_t Balau::Input::getSize() { time_t Balau::Input::getMTime() { return m_mtime; } + +bool Balau::Input::canRead() { + return true; +} diff --git a/tests/rtest.txt b/tests/rtest.txt new file mode 100644 index 0000000..7340463 --- /dev/null +++ b/tests/rtest.txt @@ -0,0 +1 @@ +Read test diff --git a/tests/test-Handles.cc b/tests/test-Handles.cc index 68d8baa..8c3c190 100644 --- a/tests/test-Handles.cc +++ b/tests/test-Handles.cc @@ -16,9 +16,10 @@ void MainTask::Do() { failed = true; } Assert(failed); - IO i(new Input("Makefile")); + IO i(new Input("tests/rtest.txt")); Printer::log(M_STATUS, "Opened file %s:", i->getName()); Printer::log(M_STATUS, " - size = %lli", i->getSize()); + char mtimestr[32]; time_t mtime = i->getMTime(); ctime_r(&mtime, mtimestr); @@ -26,5 +27,18 @@ void MainTask::Do() { if (nl) *nl = 0; Printer::log(M_STATUS, " - mtime = %i (%s)", mtime, mtimestr); + + off_t s = i->rtell(); + Assert(s == 0); + + i->rseek(0, SEEK_END); + s = i->rtell(); + Assert(s == i->getSize()); + + i->rseek(0, SEEK_SET); + char * buf = (char *) malloc(i->getSize()); + ssize_t r = i->read(buf, s + 15); + Printer::log(M_STATUS, "Read %i bytes", r); + Printer::log(M_STATUS, "Test::Handles passed."); } -- cgit v1.2.3