aboutsummaryrefslogtreecommitdiff
path: root/TXTParser.cpp
blob: 08c122c4b74e412f8220cc37abe741f9aa7aa5cf (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
#include <sstream>

#include "TXTParser.h"
#include "Color.h"
#include "Exception.h"

using namespace std;

unsigned int TXTParser::heuristic() {
	bool grid = false;
	int columns = 0;
	int rows = 0;
	int penalty = 1;

	istringstream lines(this->get_file().read());
	string line;
	while (getline(lines, line)) {
		if (line.back() == '\r') line.pop_back();

		if (line.size() == 0) {
			grid = true;
			continue;
		}
		if (!grid) continue;

		rows++;
		if (columns == 0) columns = line.size();
		penalty += abs(columns - static_cast<int>(line.size()));
	}

	if (grid == false) return 0;

	if (columns == 1) penalty += 1000;
	return (rows + columns) / penalty;
}


static void parse_header_def(MuseumDeserializer & d, const std::string & line) {
	CanvasData data;

	istringstream defs(line);
	string def;
	while (getline(defs, def, ',')) {
		size_t equals = def.find("=");
		if (equals == string::npos)
			throw Exception("TXTParser: invalid def \"%s\"", def.c_str());

		string key = def.substr(0, equals);

		if (key == "rows") {
			data.rows = stoi(def.substr(equals + 1));
			continue;
		}

		if (key == "cols") {
			data.columns = stoi(def.substr(equals + 1));
			continue;
		}

		throw Exception("TXTParser: invalid key \"%s\"", key.c_str());
	}

	d.set_canvas(data);
}

static size_t header_idx(vector<string> header, string field) {
	auto iter = find(header.begin(), header.end(), field);
	if (iter == header.end())
		throw Exception("header table is missing \"%s\" column", field.c_str());
	return iter - header.begin();
}

static Color stocolor(const string & str) {
	istringstream ss(str);
	string red, green, blue;
	getline(ss, red, ',');
	getline(ss, green, ',');
	getline(ss, blue, ',');
	return {
		.red = static_cast<unsigned int>(stoi(red)),
		.green = static_cast<unsigned int>(stoi(green)),
		.blue = static_cast<unsigned int>(stoi(blue)),
	};
}

static void parse_header_table(MuseumDeserializer & d, const std::string & header, std::istringstream & rows) {
	vector<string> table_header;
	istringstream header_row(header);
	string column;
	while (getline(header_row, column, ','))
		table_header.push_back(column);

	size_t type_idx = header_idx(table_header, "letter");
	size_t color_idx = header_idx(table_header, "rgb");
	size_t weight_idx = header_idx(table_header, "weight");

	string row;
	while (getline(rows, row)) {
		if (row.back() == '\r') row.pop_back();
		if (row.size() == 0) break;

		unsigned int col_idx = 0;
		string col = "";
		string stack = "";
		vector<string> cols = {};

		for (char c : row) {
			if (c == stack.back()) {
				stack.pop_back();
				continue;
			}
			if (c == '[') {
				stack.push_back(']');
				continue;
			}
			if (stack.size() == 0 && c == ',') {
				cols.push_back(col);
				col.clear();
				continue;
			}
			col.push_back(c);
		}
		if (col.size() > 0) cols.push_back(col);

		if (cols.size() != 3)
			throw Exception("invalid header table row");

		d.add_type(
			cols[type_idx],
			stocolor(cols[color_idx]),
			stoi(cols[weight_idx])
		);
	}
}

static void parse_header(MuseumDeserializer & d, istringstream & input) {
	string line;
	while (getline(input, line)) {
		if (line.back() == '\r') line.pop_back();
		if (line.size() == 0) break;

		if (line.find("=") != string::npos) {
			parse_header_def(d, line);
		} else {
			parse_header_table(d, line, input);
			break;
		}
	}
}

static void parse_canvas(MuseumDeserializer & d, istringstream & input) {
	unsigned int x = 0, y = 0;
	char c;
	while (input.get(c)) {
		if (c == '\r') continue;
		if (c == '\n') {
			x = 0;
			y += 1;
			continue;
		}
		d.set_tile({ .x = x, .y = y, .type = string(1, c), });
		x += 1;
	}
}

void TXTParser::parse(MuseumDeserializer & d) {
	istringstream input(this->get_file().read());

	parse_header(d, input);
	parse_canvas(d, input);
}