From afd5f7e35ef47db08a49a718c1080745f6cd385f Mon Sep 17 00:00:00 2001 From: randomdan Date: Wed, 28 Jul 2010 23:17:07 +0000 Subject: Fix typo in class name Add file channel suitable for processes --- project2/fileRows.cpp | 2 +- project2/fileStarGlibIoChannel.cpp | 43 +++++++++++++++++++++++++++++++------- project2/fileStarGlibIoChannel.h | 15 +++++++++---- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/project2/fileRows.cpp b/project2/fileRows.cpp index 57ff8e5..c2a3a09 100644 --- a/project2/fileRows.cpp +++ b/project2/fileRows.cpp @@ -40,7 +40,7 @@ _FileRows::execute() const if (!f) { throw std::runtime_error("Could not open file"); } - FileStrChannel c(f); + FileStarChannel c(f); c.set_encoding(encoding); c.set_line_term(newline); Glib::ustring line; diff --git a/project2/fileStarGlibIoChannel.cpp b/project2/fileStarGlibIoChannel.cpp index 551763d..72c379f 100644 --- a/project2/fileStarGlibIoChannel.cpp +++ b/project2/fileStarGlibIoChannel.cpp @@ -1,7 +1,10 @@ #include "fileStarGlibIoChannel.h" #include +#include +#include +#include -FileStrChannel::FileStrChannel(FILE * f) : +FileStarChannel::FileStarChannel(FILE * f) : Glib::IOChannel(), file(f) { @@ -10,33 +13,56 @@ FileStrChannel::FileStrChannel(FILE * f) : gobj()->is_writeable = 0; } -FileStrChannel::~FileStrChannel() +ProcessStarChannel::ProcessStarChannel(FILE * f) : + FileStarChannel(f) +{ + gobj()->is_seekable = 0; + gobj()->is_readable = 1; + gobj()->is_writeable = 0; +} + +FileStarChannel::~FileStarChannel() +{ + close_vfunc(); +} + +Glib::IOStatus +FileStarChannel::close_vfunc() { if (file) { fclose(file); + file = NULL; } + return Glib::IO_STATUS_NORMAL; } -Glib::IOStatus FileStrChannel::close_vfunc() +Glib::IOStatus +ProcessStarChannel::close_vfunc() { if (file) { - fclose(file); + if (pclose(file)) { + file = NULL; + throw std::runtime_error(strerror(errno)); + } file = NULL; } return Glib::IO_STATUS_NORMAL; } -Glib::IOStatus FileStrChannel::set_flags_vfunc(Glib::IOFlags flags) +Glib::IOStatus +FileStarChannel::set_flags_vfunc(Glib::IOFlags flags) { return Glib::IO_STATUS_NORMAL; } -Glib::IOFlags FileStrChannel::get_flags_vfunc() +Glib::IOFlags +FileStarChannel::get_flags_vfunc() { return Glib::IO_FLAG_IS_SEEKABLE | Glib::IO_FLAG_IS_READABLE; } -Glib::IOStatus FileStrChannel::seek_vfunc(gint64 offset, Glib::SeekType type) +Glib::IOStatus +FileStarChannel::seek_vfunc(gint64 offset, Glib::SeekType type) { if (fseek(file, offset, type)) { return Glib::IO_STATUS_ERROR; @@ -44,7 +70,8 @@ Glib::IOStatus FileStrChannel::seek_vfunc(gint64 offset, Glib::SeekType type) return Glib::IO_STATUS_NORMAL; } -Glib::IOStatus FileStrChannel::read_vfunc(char* buf, gsize count, gsize& bytes_read) +Glib::IOStatus +FileStarChannel::read_vfunc(char* buf, gsize count, gsize& bytes_read) { bytes_read = fread(buf, 1, count, file); if (bytes_read == 0) { diff --git a/project2/fileStarGlibIoChannel.h b/project2/fileStarGlibIoChannel.h index 69f09ac..2980a3c 100644 --- a/project2/fileStarGlibIoChannel.h +++ b/project2/fileStarGlibIoChannel.h @@ -3,19 +3,26 @@ #include -class FileStrChannel : public Glib::IOChannel { +class FileStarChannel : public Glib::IOChannel { public: - FileStrChannel(FILE * f); - virtual ~FileStrChannel(); + FileStarChannel(FILE * f); + virtual ~FileStarChannel(); virtual Glib::IOStatus close_vfunc(); virtual Glib::IOStatus set_flags_vfunc(Glib::IOFlags flags); virtual Glib::IOFlags get_flags_vfunc(); virtual Glib::IOStatus seek_vfunc(gint64 offset, Glib::SeekType type); virtual Glib::IOStatus read_vfunc(char* buf, gsize count, gsize& bytes_read); - private: + protected: FILE * file; }; +class ProcessStarChannel : public FileStarChannel { + public: + ProcessStarChannel(FILE * f); + + virtual Glib::IOStatus close_vfunc(); +}; + #endif -- cgit v1.2.3