summaryrefslogtreecommitdiff
path: root/project2/streams/streamNvpRows.cpp
blob: 281360f427ed67a4869fce9c9f1380bca943d44d (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <pch.hpp>
#include "variables.h"
#include "stream.h"
#include "definedColumns.h"
#include "rowProcessor.h"
#include "textReader.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/trim.hpp>

class RowProcessor;

/// Base class for Project2 components that create a row set based on the contents of a byte stream
class StreamNvpRows : public RowSet {
	public:
		class ParseState : public RowState {
			public:
				typedef boost::function<bool (size_t start)> Process;

				class Token {
					public:
						Token(const Glib::ustring cs, Process p) :
							chars(cs),
							process(p)
						{
						}
						bool operator<(const Token & other) const {
							return (other.chars.length() < this->chars.length() ||
									(other.chars.length() == this->chars.length() && this->chars < other.chars));
						}
						const Glib::ustring chars;
						const Process process;
						mutable size_t firstMatch;
				};

				ParseState(const StreamNvpRows * rows, ExecContext * e, const RowProcessorCallback & proc) :
					sr(rows),
					rp(proc),
					ec(e),
					inQuotes(false),
					inValue(false),
					prevWasQuote(false)
				{
					tokens.insert(Token(sr->newline, boost::bind(&StreamNvpRows::ParseState::newRecord, this, _1)));
					tokens.insert(Token(sr->assign, boost::bind(&StreamNvpRows::ParseState::newField, this, _1)));
					tokens.insert(Token(sr->fieldSep, boost::bind(&StreamNvpRows::ParseState::newValue, this, _1)));
					if (!sr->quoteChar.empty()) {
						tokens.insert(Token(sr->quoteChar, boost::bind(&StreamNvpRows::ParseState::quote, this, _1)));
					}
				}

				~ParseState()
				{
					if (!std::uncaught_exception()) {
						newRecord(tok.length());
					}
				}
				const Columns & getColumns() const {
					return columns;
				}

				void pushChar(gunichar c)
				{
					tok += c;
					if (inQuotes) {
						if (boost::algorithm::ends_with(tok, sr->quoteChar)) {
							quote(tok.length() - sr->quoteChar.length());
						}
						else {
							prevWasQuote = false;
						}
					}
					else {
						if (tok.length() >= tokens.begin()->chars.length()) {
							for (auto & t : tokens) {
								t.firstMatch = tok.rfind(t.chars);
								if (t.firstMatch < tok.length() - tokens.begin()->chars.length()) {
									t.firstMatch = -1;
								}
							}
							Tokens::const_iterator t  = std::min_element(tokens.begin(), tokens.end(),
									[](const Token & a, const Token & b) { return a.firstMatch < b.firstMatch; });
							if (t->firstMatch != (size_t)-1) {
								if (t->process(t->firstMatch)) {
									tok = tok.substr(t->firstMatch + t->chars.length());
								}
							}
						}
					}
				}

			private:
				bool newRecord(size_t start) {
					if (start > 0) {
						if (inValue) {
							newValue(start);
						}
						else {
							newField(start);
						}
					}
					if (!columns.empty()) {
						process(ec, rp);
					}
					fields.clear();
					columns.clear();
					return true;
				}

				bool newField(size_t start) {
					columns.insert(new Column(columns.size(),
								boost::algorithm::trim_copy_if(tok.substr(0, start), g_unichar_isspace)));
					fields.push_back(Null());
					inValue = true;
					return true;
				}

				bool newValue(size_t start) {
					fields.back() = VariableType::make(boost::algorithm::trim_copy_if(tok.substr(0, start), g_unichar_isspace));
					inValue = false;
					return true;
				}

				bool quote(size_t start) {
					if (prevWasQuote) {
						prevWasQuote = false;
						inQuotes = !inQuotes;
					}
					else {
						prevWasQuote = inQuotes;
						inQuotes = !inQuotes;
						tok.erase(start, sr->quoteChar.length());
					}
					return false;
				}

				const StreamNvpRows * sr;
				const RowProcessorCallback & rp;
				ExecContext * ec;
				bool inQuotes;
				bool inValue;
				bool prevWasQuote;
				Glib::ustring tok;
				mutable Columns columns;

				typedef std::set<Token> Tokens;
				Tokens tokens;

		};

		StreamNvpRows(ScriptNodePtr p) :
			SourceObject(p),
			RowSet(p),
			fieldSep(p->value("fieldSep", ",", NULL).as<Glib::ustring>()),
			quoteChar(p->value("quoteChar", "\"", NULL).as<Glib::ustring>()),
			keepBlankRows(p->value("keepBlankRows", false, NULL)),
			countBlankRows(p->value("countBlankRows", false, NULL)),
			newline(p->value("newline", "\n", NULL).as<Glib::ustring>()),
			assign(p->value("assign", "=", NULL).as<Glib::ustring>()),
			encoding(p->value("encoding", "utf-8", NULL).as<std::string>())
		{
			p->script->loader.addLoadTarget(p, Storer::into<StreamFactory>(&stream));
		}

		void execute(const Glib::ustring &, const RowProcessorCallback & rp, ExecContext * ec) const
		{
			ParseState ps(this, ec, rp);
			TextReader::CharSink cs = boost::bind(&StreamNvpRows::ParseState::pushChar, &ps, _1);
			TextReader t(encoding.c_str());
			stream->runStream(boost::bind(&TextReader::read, &t, _1, _2, cs), ec);
		}

	private:
		StreamPtr stream;
		const Glib::ustring fieldSep;
		const Glib::ustring quoteChar;
		const bool keepBlankRows;
		const bool countBlankRows;
		const Glib::ustring newline;
		const Glib::ustring assign;
		const std::string encoding;
};
NAMEDFACTORY("streamnvprows", StreamNvpRows, RowSetFactory);