summaryrefslogtreecommitdiff
path: root/project2/fileRows.cpp
blob: f28c96dda9e3eaaa76d22d1dee3088776cf3c1c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "fileRows.h"
#include <stdexcept>

ElementLoaderImpl<_FileIterate> fileiterateLoader("fileiterate");
ElementLoaderImpl<_FileView> fileviewLoader("fileview");

_FileRows::_FileRows(const xmlpp::Element * p) :
	path(p->get_attribute_value("path")),
	fieldSep(p->get_attribute_value("fieldSep")[0]),
	quoteChar(p->get_attribute_value("quoteChar")[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()
{
}

void
_FileRows::loadComplete()
{
}

unsigned int
_FileRows::columnCount() const
{
	return columns.size();
}

const Glib::ustring &
_FileRows::getColumnName(unsigned int col) const
{
	return columns[col];
}

void
_FileRows::execute() const
{
	FileStarChannel c(doOpen());
	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;
					inQuotes = !inQuotes;
				}
				else {
					prevWasQuote = inQuotes;
					inQuotes = !inQuotes;
				}
			}
			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 && !values.empty()) {
			while (values.size() < columns.size()) {
				values.push_back(ValPtr(new Glib::ustring()));
				curCol++;
			}
			rowReady();
		}
		values.clear();
	}
}

const Glib::ustring &
_FileRows::getCurrentValue(unsigned int col) const
{
	return *values[col];
}

bool
_FileRows::isNull(unsigned int col) const
{
	return false;
}

const 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;
		}
	}
	throw PerRowValues::FieldDoesNotExist();
}

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++;
		}
	}
}

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"
template class _GenericIterate<_FileRows>;