aboutsummaryrefslogtreecommitdiff
path: root/CSVParser.cpp
blob: 824d5f4d05ebede3f9835869227346d7fdfc0998 (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
#include <cstdlib>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>

#include "CSVParser.h"
#include "Exception.h"
#include "ParserFactory.h"

using namespace std;

CSVParser CSVParser::instance {};
CSVParser::CSVParser() {
	ParserFactory::register_strategy(this);
}

unsigned int CSVParser::heuristic(FileReader & f) {
	const string content = f.read();
	int global_columns = 0;
	int columns = 1;
	int rows = 0;
	int penalty = 1;
	for (char c : content) {
		if (c == ',') columns++;
		if (c == '\n') {
			rows++;
			if (global_columns == 0) global_columns = columns;
			penalty += abs(global_columns - columns);
			columns = 1;
		}
	}
	if (global_columns == 1) penalty += 1000;
	return (rows + global_columns) / penalty;
}

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

void CSVParser::parse(FileReader & f, MuseumDeserializer & d) {
	vector<vector<string>> table = {};

	istringstream rows(f.read());
	string row;
	while (getline(rows, row)) {
		// ignore windows line endings
		if (row.back() == '\r') row.pop_back();
		istringstream columns(row);
		string column;

		vector<string> table_row = {};
		while (getline(columns, column, ',')) {
			table_row.push_back(column);
		}
		table.push_back(table_row);
	}

	if (table.size() < 1)
		throw Exception("not enough data rows in CSV file");

	vector<string> table_header = table[0];
	table.erase(table.begin());

	size_t x_idx = header_idx(table_header, "x");
	size_t y_idx = header_idx(table_header, "y");
	size_t vx_idx = header_idx(table_header, "vx");
	size_t vy_idx = header_idx(table_header, "vy");

	for (vector<string> row : table) {
		d.add_artist({
			.x = stof(row[x_idx]),
			.y = stof(row[y_idx]),
			.vx = stof(row[vx_idx]),
			.vy = stof(row[vy_idx]),
		});
	}
}