summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrandomdan <randomdan@localhost>2010-08-05 00:47:01 +0000
committerrandomdan <randomdan@localhost>2010-08-05 00:47:01 +0000
commit6e58217deeaed4f14820a4744bef72b508937e30 (patch)
tree9b02ae03161e2a7463dc48ca149e03982ba6cbda
parentAccept and run checks before presentation (diff)
downloadproject2-6e58217deeaed4f14820a4744bef72b508937e30.tar.bz2
project2-6e58217deeaed4f14820a4744bef72b508937e30.tar.xz
project2-6e58217deeaed4f14820a4744bef72b508937e30.zip
Add ProcRows as a direct extension of FileRows
Fix glaring bug in FileRows (vector.clear()/.empty() mixup)
-rw-r--r--project2/fileRows.cpp19
-rw-r--r--project2/fileRows.h9
-rw-r--r--project2/fileStarGlibIoChannel.cpp39
-rw-r--r--project2/fileStarGlibIoChannel.h14
-rw-r--r--project2/iterate.cpp2
-rw-r--r--project2/procRows.cpp27
-rw-r--r--project2/procRows.h17
-rw-r--r--project2/view.cpp2
8 files changed, 78 insertions, 51 deletions
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 <stdexcept>
_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 <libxml++/nodes/element.h>
#include <boost/shared_ptr.hpp>
@@ -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<Glib::ustring> ValPtr;
typedef std::vector<ValPtr> 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 <string.h>
#include <stdexcept>
-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 <stdio.h>
#include <glibmm/iochannel.h>
+#include <boost/shared_ptr.hpp>
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<FILE> 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 <stdexcept>
+
+_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