diff options
author | Pixel <pixel@nobis-crew.org> | 2011-10-10 19:36:55 -0700 |
---|---|---|
committer | Pixel <pixel@nobis-crew.org> | 2011-10-10 19:36:55 -0700 |
commit | 965148b43b5b859934b7af2e8447ba1026a43a19 (patch) | |
tree | 290ff60cd1ebc9a065109620b2e9a5ff4e5b3d17 /src/Input.cc | |
parent | cf9a801ebcb4df0a8b1ea75e58ca8ea8960ba13b (diff) |
Adding the basic "Handle" structure, and adding an early version of Input.
Renamed suspend() to yield().
Fixed a couple of bugs, and reorganized slightly some code.
Diffstat (limited to 'src/Input.cc')
-rw-r--r-- | src/Input.cc | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/Input.cc b/src/Input.cc new file mode 100644 index 0000000..633a1a7 --- /dev/null +++ b/src/Input.cc @@ -0,0 +1,60 @@ +#include <string.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include "eio.h" +#include "Input.h" +#include "Task.h" + +struct cbResults_t { + Balau::Events::Custom evt; + int result, errorno; +}; + +static int eioDone(eio_req * req) { + cbResults_t * cbResults = (cbResults_t *) req->data; + cbResults->result = req->result; + cbResults->errorno = req->errorno; + cbResults->evt.doSignal(); +} + +Balau::Input::Input(const char * fname) throw (GeneralException) : m_fd(-1) { + cbResults_t cbResults; + m_name.set("Input(%s)", fname); + eio_req * r = eio_open(fname, O_RDONLY, 0, 0, eioDone, &cbResults); + Assert(r != 0); + Task::yield(&cbResults.evt); + Assert(cbResults.evt.gotSignal()); + if (cbResults.result < 0) { + char str[4096]; + throw GeneralException(String("Unable to open file ") + m_name + " for reading: " + strerror_r(cbResults.errorno, str, sizeof(str)) + " (err#" + cbResults.errorno + ")"); + } else { + m_fd = cbResults.result; + } +} + +void Balau::Input::close() throw (GeneralException) { + if (m_fd < 0) + return; + cbResults_t cbResults; + eio_req * r = eio_close(m_fd, 0, eioDone, &cbResults); + Assert(r != 0); + m_fd = -1; + Task::yield(&cbResults.evt); + Assert(cbResults.evt.gotSignal()); + if (cbResults.result < 0) { + char str[4096]; + strerror_r(cbResults.errorno, str, sizeof(str)); + throw GeneralException(String("Unable to close file ") + m_name + ": " + str); + } else { + m_fd = cbResults.result; + } +} + +bool Balau::Input::isClosed() { + return m_fd < 0; +} + +const char * Balau::Input::getName() { + return m_name.to_charp(); +} |