summaryrefslogtreecommitdiff
path: root/project2/fileRows.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'project2/fileRows.cpp')
-rw-r--r--project2/fileRows.cpp138
1 files changed, 138 insertions, 0 deletions
diff --git a/project2/fileRows.cpp b/project2/fileRows.cpp
new file mode 100644
index 0000000..57ff8e5
--- /dev/null
+++ b/project2/fileRows.cpp
@@ -0,0 +1,138 @@
+#include "fileRows.h"
+#include "fileStarGlibIoChannel.h"
+#include <stdexcept>
+
+_FileRows::_FileRows(const xmlpp::Element * p) :
+ path(p->get_attribute_value("path")),
+ fieldSep(p->get_attribute_value("fieldSeps")[0]),
+ quoteChar(p->get_attribute_value("quoteChars")[0]),
+ newline(p->get_attribute_value("newline")),
+ encoding(p->get_attribute_value("encoding"))
+{
+ BOOST_FOREACH(const xmlpp::Node * node, p->find("columns/column")) {
+ const xmlpp::Element * elem = dynamic_cast<const xmlpp::Element *>(node);
+ if (elem) {
+ columns.push_back(elem->get_child_text()->get_content());
+ }
+ }
+}
+
+_FileRows::~_FileRows()
+{
+}
+
+unsigned int
+_FileRows::columnCount() const
+{
+ return columns.size();
+}
+
+Glib::ustring
+_FileRows::getColumnName(unsigned int col) const
+{
+ return columns[col];
+}
+
+void
+_FileRows::execute() const
+{
+ FILE * f = fopen(path.c_str(), "r");
+ if (!f) {
+ throw std::runtime_error("Could not open file");
+ }
+ FileStrChannel c(f);
+ c.set_encoding(encoding);
+ c.set_line_term(newline);
+ Glib::ustring line;
+ while (c.read_line(line) == Glib::IO_STATUS_NORMAL) {
+ line.erase(line.length() - newline.length());
+ Columns::const_iterator curCol = columns.begin();
+ bool mkCols = columns.empty();
+ bool inQuotes = false;
+ bool prevWasQuote = false;
+ Glib::ustring tok;
+ BOOST_FOREACH(gunichar c, line) {
+ if (c == quoteChar) {
+ if (prevWasQuote) {
+ tok += c;
+ prevWasQuote = false;
+ }
+ else {
+ inQuotes = !inQuotes;
+ prevWasQuote = true;
+ }
+ }
+ else if ((!inQuotes) && (c == fieldSep)) {
+ prevWasQuote = false;
+ if (mkCols) {
+ addColumn(tok);
+ }
+ else {
+ values.push_back(ValPtr(new Glib::ustring(tok)));
+ curCol++;
+ }
+ tok.clear();
+ }
+ else {
+ prevWasQuote = false;
+ tok += c;
+ }
+ }
+ if (tok.length()) {
+ if (mkCols) {
+ addColumn(tok);
+ }
+ else {
+ values.push_back(ValPtr(new Glib::ustring(tok)));
+ curCol++;
+ }
+ }
+ if (!mkCols) {
+ while (curCol != columns.end()) {
+ values.push_back(ValPtr(new Glib::ustring()));
+ curCol++;
+ }
+ rowReady();
+ }
+ values.empty();
+ }
+}
+
+Glib::ustring
+_FileRows::getCurrentValue(unsigned int col) const
+{
+ return *values[col];
+}
+
+Glib::ustring
+_FileRows::getCurrentValue(const Glib::ustring & id) const
+{
+ Values::const_iterator v = values.begin();
+ for (Columns::const_iterator i = columns.begin(); i != columns.end(); i++, v++) {
+ if (*i == id) {
+ return **v;
+ }
+ }
+ return "";
+}
+
+void
+_FileRows::addColumn(const Glib::ustring & rawtok) const
+{
+ columns.push_back(rawtok);
+ Glib::ustring & tok(columns.back());
+ for (Glib::ustring::iterator i = tok.begin(); i != tok.end(); ) {
+ if (!isalnum(*i)) {
+ tok.erase(i);
+ }
+ else {
+ i++;
+ }
+ }
+}
+
+#include "view.hpp"
+template class _GenericView<_FileRows>;
+#include "iterate.hpp"
+template class _GenericIterate<_FileRows>;
+