blob: 57ff8e54c18fe182bdcb2d024346113101a7541d (
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
|
#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>;
|