From 76ab3f1ddc143d65e97ab301c742ad6553f1b560 Mon Sep 17 00:00:00 2001 From: Nicolas 'Pixel' Noble Date: Sun, 20 Jan 2013 19:09:14 -0800 Subject: Adding open() as an operation to Input and Output. --- includes/Handle.h | 1 + includes/Input.h | 3 ++- includes/Output.h | 3 ++- src/Input.cc | 10 ++++++---- src/Output.cc | 10 ++++++---- tests/test-Handles.cc | 15 ++++++++++++--- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/includes/Handle.h b/includes/Handle.h index e9e94b4..32a5975 100644 --- a/includes/Handle.h +++ b/includes/Handle.h @@ -15,6 +15,7 @@ class FileSystem { class ENoEnt : public GeneralException { public: ENoEnt(const char * name) : GeneralException(String("No such file or directory: `") + name + "'") { } + ENoEnt(const String & name) : ENoEnt(name.to_charp()) { } }; class IOBase; diff --git a/includes/Input.h b/includes/Input.h index e0ad329..51b17c7 100644 --- a/includes/Input.h +++ b/includes/Input.h @@ -6,7 +6,8 @@ namespace Balau { class Input : public SeekableHandle { public: - Input(const char * fname) throw (GeneralException); + Input(const char * fname); + void open() throw (GeneralException); virtual void close() throw (GeneralException); virtual ssize_t read(void * buf, size_t count) throw (GeneralException); virtual bool isClosed(); diff --git a/includes/Output.h b/includes/Output.h index d771227..fec3154 100644 --- a/includes/Output.h +++ b/includes/Output.h @@ -6,7 +6,8 @@ namespace Balau { class Output : public SeekableHandle { public: - Output(const char * fname, bool truncate = true) throw (GeneralException); + Output(const char * fname); + void open(bool truncate = true) throw (GeneralException); virtual void close() throw (GeneralException); virtual ssize_t write(const void * buf, size_t count) throw (GeneralException); virtual bool isClosed(); diff --git a/src/Input.cc b/src/Input.cc index 5525932..2a89fa9 100644 --- a/src/Input.cc +++ b/src/Input.cc @@ -68,18 +68,20 @@ class AsyncOpStat : public Balau::AsyncOperation { }; -Balau::Input::Input(const char * fname) throw (GeneralException) { +Balau::Input::Input(const char * fname) { m_name.set("Input(%s)", fname); m_fname = fname; +} - Printer::elog(E_INPUT, "Opening file %s", fname); +void Balau::Input::open() throw (GeneralException) { + Printer::elog(E_INPUT, "Opening file %s", m_fname.to_charp()); cbResults_t cbResults; - createAsyncOp(new AsyncOpOpen(fname, &cbResults)); + createAsyncOp(new AsyncOpOpen(m_fname.to_charp(), &cbResults)); Task::operationYield(&cbResults.evt); if (cbResults.result < 0) { if (cbResults.errorno == ENOENT) { - throw ENoEnt(fname); + throw ENoEnt(m_fname); } else { char str[4096]; throw GeneralException(String("Unable to open file ") + m_name + " for reading: " + strerror_r(cbResults.errorno, str, sizeof(str)) + " (err#" + cbResults.errorno + ")"); diff --git a/src/Output.cc b/src/Output.cc index 1688ec7..7a50b03 100644 --- a/src/Output.cc +++ b/src/Output.cc @@ -69,18 +69,20 @@ class AsyncOpStat : public Balau::AsyncOperation { }; -Balau::Output::Output(const char * fname, bool truncate) throw (GeneralException) { +Balau::Output::Output(const char * fname) { m_name.set("Output(%s)", fname); m_fname = fname; +} - Printer::elog(E_OUTPUT, "Opening file %s", fname); +void Balau::Output::open(bool truncate) throw (GeneralException) { + Printer::elog(E_OUTPUT, "Opening file %s", m_fname.to_charp()); cbResults_t cbResults; - createAsyncOp(new AsyncOpOpen(fname, truncate, &cbResults)); + createAsyncOp(new AsyncOpOpen(m_fname.to_charp(), truncate, &cbResults)); Task::operationYield(&cbResults.evt); if (cbResults.result < 0) { if (cbResults.errorno == ENOENT) { - throw ENoEnt(fname); + throw ENoEnt(m_fname); } else { char str[4096]; throw GeneralException(String("Unable to open file ") + m_name + " for reading: " + strerror_r(cbResults.errorno, str, sizeof(str)) + " (err#" + cbResults.errorno + ")"); diff --git a/tests/test-Handles.cc b/tests/test-Handles.cc index 847cb24..24e92aa 100644 --- a/tests/test-Handles.cc +++ b/tests/test-Handles.cc @@ -22,13 +22,15 @@ void MainTask::Do() { bool failed = false; try { - IO i(new Input("SomeInexistantFile.txt")); + IO i(new Input("SomeInexistantFile.txt")); + i->open(); } catch (ENoEnt e) { failed = true; } TAssert(failed); - IO i(new Input("tests/rtest.txt")); + IO i(new Input("tests/rtest.txt")); + i->open(); Printer::log(M_STATUS, "Opened file %s:", i->getName()); Printer::log(M_STATUS, " - size = %" PRIi64, i->getSize()); @@ -64,7 +66,8 @@ void MainTask::Do() { TAssert(r == (s - 5)); TAssert(memcmp(buf1, buf2, s) == 0); - IO o(new Output("tests/out.txt")); + IO o(new Output("tests/out.txt")); + o->open(); s = o->wtell(); TAssert(s == 0); s = o->getSize(); @@ -106,6 +109,7 @@ void MainTask::Do() { { IO o(new Output("tests/out.z")); + o->open(); IO z(new ZStream(o)); z->detach(); z->writeString("foobar\n"); @@ -113,6 +117,7 @@ void MainTask::Do() { { IO o(new Output("tests/out.gz")); + o->open(); IO z(new ZStream(o, Z_BEST_COMPRESSION, ZStream::GZIP)); z->detach(); z->writeString("foobar\n"); @@ -120,6 +125,7 @@ void MainTask::Do() { { IO o(new Output("tests/out.raw")); + o->open(); IO z(new ZStream(o, Z_BEST_COMPRESSION, ZStream::RAW)); z->detach(); z->writeString("foobar\n"); @@ -127,6 +133,7 @@ void MainTask::Do() { { IO i(new Input("tests/out.z")); + i->open(); IO z(new ZStream(i)); IO s(new BStream(z)); z->detach(); @@ -137,6 +144,7 @@ void MainTask::Do() { { IO i(new Input("tests/out.gz")); + i->open(); IO z(new ZStream(i, Z_BEST_COMPRESSION, ZStream::GZIP)); IO s(new BStream(z)); z->detach(); @@ -147,6 +155,7 @@ void MainTask::Do() { { IO i(new Input("tests/out.raw")); + i->open(); IO z(new ZStream(i, Z_BEST_COMPRESSION, ZStream::RAW)); IO s(new BStream(z)); z->detach(); -- cgit v1.2.3