summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2011-10-10 23:09:53 -0700
committerPixel <pixel@nobis-crew.org>2011-10-10 23:31:07 -0700
commit67b6a78b347ab7ba269a52d772e79d12444f6e96 (patch)
tree5a105f61add7a5eaa8bd63a7182eecde84285b0e
parent8ae349b49e16064e4d84b6cfd256e3ca7fb0cd60 (diff)
Adding a few more features to Input, and actually creating a slightly better ClassName system, when using gcc and libstdc++.
-rw-r--r--includes/Exceptions.h16
-rw-r--r--includes/Handle.h1
-rw-r--r--includes/Input.h1
-rw-r--r--src/Handle.cc10
-rw-r--r--src/Input.cc4
-rw-r--r--tests/rtest.txt1
-rw-r--r--tests/test-Handles.cc16
7 files changed, 44 insertions, 5 deletions
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 <cxxabi.h>
#include <BString.h>
namespace Balau {
@@ -21,6 +22,21 @@ class GeneralException {
static inline void AssertHelper(const String & msg) throw(GeneralException) { throw GeneralException(msg); }
+class ClassName {
+ public:
+ template<typename T> ClassName(T * ptr);
+ ~ClassName() { free(m_demangled); }
+ const char * c_str() const { return m_demangled; }
+ private:
+ char * m_demangled;
+};
+
+template<typename T>
+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.");
}