From 6e58217deeaed4f14820a4744bef72b508937e30 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 5 Aug 2010 00:47:01 +0000 Subject: Add ProcRows as a direct extension of FileRows Fix glaring bug in FileRows (vector.clear()/.empty() mixup) --- project2/fileRows.cpp | 19 ++++++++++++------- project2/fileRows.h | 9 +++++---- project2/fileStarGlibIoChannel.cpp | 39 ++++++++------------------------------ project2/fileStarGlibIoChannel.h | 14 +++++--------- project2/iterate.cpp | 2 ++ project2/procRows.cpp | 27 ++++++++++++++++++++++++++ project2/procRows.h | 17 +++++++++++++++++ project2/view.cpp | 2 ++ 8 files changed, 78 insertions(+), 51 deletions(-) create mode 100644 project2/procRows.cpp create mode 100644 project2/procRows.h diff --git a/project2/fileRows.cpp b/project2/fileRows.cpp index c2a3a09..239ad6b 100644 --- a/project2/fileRows.cpp +++ b/project2/fileRows.cpp @@ -1,5 +1,4 @@ #include "fileRows.h" -#include "fileStarGlibIoChannel.h" #include _FileRows::_FileRows(const xmlpp::Element * p) : @@ -36,11 +35,7 @@ _FileRows::getColumnName(unsigned int col) const void _FileRows::execute() const { - FILE * f = fopen(path.c_str(), "r"); - if (!f) { - throw std::runtime_error("Could not open file"); - } - FileStarChannel c(f); + FileStarChannel c(doOpen()); c.set_encoding(encoding); c.set_line_term(newline); Glib::ustring line; @@ -94,7 +89,7 @@ _FileRows::execute() const } rowReady(); } - values.empty(); + values.clear(); } } @@ -131,6 +126,16 @@ _FileRows::addColumn(const Glib::ustring & rawtok) const } } +FileStarChannel +_FileRows::doOpen() const +{ + FILE * f = fopen(path.c_str(), "r"); + if (!f) { + throw std::runtime_error("Could not open file"); + } + return FileStarChannel(f, true, fclose); +} + #include "view.hpp" template class _GenericView<_FileRows>; #include "iterate.hpp" diff --git a/project2/fileRows.h b/project2/fileRows.h index 84c7a5b..3e8d3c6 100644 --- a/project2/fileRows.h +++ b/project2/fileRows.h @@ -1,5 +1,5 @@ -#ifndef FILEITERATE_H -#define FILEITERATE_H +#ifndef FILEROWS_H +#define FILEROWS_H #include #include @@ -7,8 +7,7 @@ #include "view.h" #include "iterate.h" #include "rowBase.h" - -class ApplicationEngine; +#include "fileStarGlibIoChannel.h" class _FileRows : public RowBase { public: @@ -25,10 +24,12 @@ class _FileRows : public RowBase { const Glib::ustring path; protected: + virtual FileStarChannel doOpen() const; void addColumn(const Glib::ustring & rawtok) const; typedef boost::shared_ptr ValPtr; typedef std::vector Values; mutable Values values; + private: gunichar fieldSep; gunichar quoteChar; diff --git a/project2/fileStarGlibIoChannel.cpp b/project2/fileStarGlibIoChannel.cpp index 72c379f..bbc6591 100644 --- a/project2/fileStarGlibIoChannel.cpp +++ b/project2/fileStarGlibIoChannel.cpp @@ -4,47 +4,24 @@ #include #include -FileStarChannel::FileStarChannel(FILE * f) : +FileStarChannel::FileStarChannel(FILE * f, bool seekable, int (*closer)(FILE*)) : Glib::IOChannel(), - file(f) + file(f, closer) { - gobj()->is_seekable = 1; - gobj()->is_readable = 1; - gobj()->is_writeable = 0; -} - -ProcessStarChannel::ProcessStarChannel(FILE * f) : - FileStarChannel(f) -{ - gobj()->is_seekable = 0; + gobj()->is_seekable = seekable ? 1 : 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 -ProcessStarChannel::close_vfunc() -{ - if (file) { - if (pclose(file)) { - file = NULL; - throw std::runtime_error(strerror(errno)); - } - file = NULL; + file.reset(); } return Glib::IO_STATUS_NORMAL; } @@ -64,7 +41,7 @@ FileStarChannel::get_flags_vfunc() Glib::IOStatus FileStarChannel::seek_vfunc(gint64 offset, Glib::SeekType type) { - if (fseek(file, offset, type)) { + if (fseek(file.get(), offset, type)) { return Glib::IO_STATUS_ERROR; } return Glib::IO_STATUS_NORMAL; @@ -73,12 +50,12 @@ FileStarChannel::seek_vfunc(gint64 offset, Glib::SeekType type) Glib::IOStatus FileStarChannel::read_vfunc(char* buf, gsize count, gsize& bytes_read) { - bytes_read = fread(buf, 1, count, file); + bytes_read = fread(buf, 1, count, file.get()); if (bytes_read == 0) { - if (feof(file)) { + if (feof(file.get())) { return Glib::IO_STATUS_EOF; } - if (ferror(file)) { + if (ferror(file.get())) { return Glib::IO_STATUS_ERROR; } return Glib::IO_STATUS_AGAIN; diff --git a/project2/fileStarGlibIoChannel.h b/project2/fileStarGlibIoChannel.h index 2980a3c..b11ffe7 100644 --- a/project2/fileStarGlibIoChannel.h +++ b/project2/fileStarGlibIoChannel.h @@ -1,11 +1,13 @@ #ifndef FILESTARGLIBIOCHANNEL_H #define FILESTARGLIBIOCHANNEL_H +#include #include +#include class FileStarChannel : public Glib::IOChannel { public: - FileStarChannel(FILE * f); + FileStarChannel(FILE * f, bool seekable, int (*closer)(FILE *) = fclose); virtual ~FileStarChannel(); virtual Glib::IOStatus close_vfunc(); @@ -14,14 +16,8 @@ class FileStarChannel : public Glib::IOChannel { virtual Glib::IOStatus seek_vfunc(gint64 offset, Glib::SeekType type); virtual Glib::IOStatus read_vfunc(char* buf, gsize count, gsize& bytes_read); protected: - FILE * file; -}; - -class ProcessStarChannel : public FileStarChannel { - public: - ProcessStarChannel(FILE * f); - - virtual Glib::IOStatus close_vfunc(); + typedef boost::shared_ptr FilePtr; + FilePtr file; }; #endif diff --git a/project2/iterate.cpp b/project2/iterate.cpp index 59f41ff..3fc0e48 100644 --- a/project2/iterate.cpp +++ b/project2/iterate.cpp @@ -4,6 +4,7 @@ #include "xmlObjectLoader.h" #include "sqlRows.h" #include "fileRows.h" +#include "procRows.h" #include "task.h" _Iterate::_Iterate(const xmlpp::Element * p) : @@ -25,6 +26,7 @@ _Iterate::AddLoaders(Loaders & l, NoOutputExecutes & iterates) { l.insert(LoadersVT("sqliterate", _LoaderBase::Make<_SqlIterate, _NoOutputExecute, unsigned int, _SourceObject, &_SourceObject::order>(&iterates))); l.insert(LoadersVT("fileiterate", _LoaderBase::Make<_FileIterate, _NoOutputExecute, unsigned int, _SourceObject, &_SourceObject::order>(&iterates))); + l.insert(LoadersVT("prociterate", _LoaderBase::Make<_ProcIterate, _NoOutputExecute, unsigned int, _SourceObject, &_SourceObject::order>(&iterates))); } void diff --git a/project2/procRows.cpp b/project2/procRows.cpp new file mode 100644 index 0000000..e83d05c --- /dev/null +++ b/project2/procRows.cpp @@ -0,0 +1,27 @@ +#include "procRows.h" +#include + +_ProcRows::_ProcRows(const xmlpp::Element * p) : + _FileRows(p) +{ +} + +_ProcRows::~_ProcRows() +{ +} + +FileStarChannel +_ProcRows::doOpen() const +{ + FILE * f = popen(path.c_str(), "re"); + if (!f) { + throw std::runtime_error("Could not open file"); + } + return FileStarChannel(f, false, pclose); +} + +#include "view.hpp" +template class _GenericView<_ProcRows>; +#include "iterate.hpp" +template class _GenericIterate<_ProcRows>; + diff --git a/project2/procRows.h b/project2/procRows.h new file mode 100644 index 0000000..aaac430 --- /dev/null +++ b/project2/procRows.h @@ -0,0 +1,17 @@ +#ifndef PROCROWS_H +#define PROCROWS_H + +#include "fileRows.h" + +class _ProcRows : public _FileRows { + public: + _ProcRows(const xmlpp::Element * p); + ~_ProcRows(); + + virtual FileStarChannel doOpen() const; +}; +typedef _GenericView<_ProcRows> _ProcView; +typedef _GenericIterate<_ProcRows> _ProcIterate; + +#endif + diff --git a/project2/view.cpp b/project2/view.cpp index f4f99b2..530d7b5 100644 --- a/project2/view.cpp +++ b/project2/view.cpp @@ -4,6 +4,7 @@ #include "rawView.h" #include "fileRows.h" #include "sqlRows.h" +#include "procRows.h" _View::_View(const xmlpp::Element * p) : _SourceObject(p), @@ -24,6 +25,7 @@ _View::AddLoaders(Loaders & l, Views & views) l.insert(LoadersVT("sqlview", _LoaderBase::Make<_SqlView, _View, std::string, _SourceObject, &_SourceObject::name>(&views))); l.insert(LoadersVT("rawview", _LoaderBase::Make<_RawView, _View, std::string, _SourceObject, &_SourceObject::name>(&views))); l.insert(LoadersVT("fileview", _LoaderBase::Make<_FileView, _View, std::string, _SourceObject, &_SourceObject::name>(&views))); + l.insert(LoadersVT("procview", _LoaderBase::Make<_ProcView, _View, std::string, _SourceObject, &_SourceObject::name>(&views))); } void -- cgit v1.2.3