Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models / Table.cpp
blob: 5e7f2a9e5020a7090bad177eb7402e0c88e328e8 (plain) (blame)
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
184
185
186
187
188
189
190
191
#include "Table.h"
#include "CellDouble.h"
#include "CellInt.h"
#include "CellString.h"
#include "CellFormula.h"
#include <cstring>
#include <fstream>
#include <exception>
#include <stdexcept>

#define FLOATING_POINT_PRECISION 0.00001f

unsigned countOfCommas(std::ifstream& file) {
	unsigned currPos = file.tellg();
	unsigned commaCount = 0;
	while (file.peek() != '\n' && file.peek() != EOF) {
		if (file.get() == ',') commaCount++;
	}
	file.seekg(currPos, std::ios::beg);
	return commaCount;
}

#define abs(a) ((a < 0) ? -a : a)
#define isDigit(a) (a >= '0' && a <= '9')
#define fileLocationExceptionMsg(mainMessage, lines, columns) (((String(mainMessage) += (unsigned)(lines)) += " and col ") += (unsigned)(columns)).get_cstr()

bool containsNumber(const char*& str) {
	while (*str == ' ') str++;
	return ((*str == '+' || *str == '-') && (*(str + 1) >= '0' && *(str + 1) <= '9')) ||
		   (*str >= '0' && *str <= '9'); // + or -, followed by digit, or it just a digit
}
#include <iostream>
void Table::readFromFile(std::ifstream& inFile) {
	unsigned cols = countOfCommas(inFile) + 1;

	unsigned colInd = 0;
	cells.add(List<Cell*>(cols));
	for (unsigned i = 0; i < cols; i++)
		cells[cells.get_count() - 1][i] = nullptr;
	while (inFile.peek() != EOF) {
		while (inFile.peek() == ' ') inFile.get();

		// Entering a new cell on the current row
		if (inFile.peek() == ',') {
			colInd++;
			inFile.get();
		}
		// Entering a new row
		else if (inFile.peek() == '\n') {
			cells.add(List<Cell*>(cols));
			for (unsigned i = 0; i < cols; i++)
				cells[cells.get_count() - 1][i] = nullptr;
			colInd = 0;
			inFile.get();
		}
		// Parsing number
		else if (inFile.peek() == '-' || inFile.peek() == '+' || isDigit(inFile.peek())) {
			int whole = 0;
			unsigned exponent = 1;

			if (inFile.peek() == '-' || inFile.peek() == '+') {
				inFile.get();
				if (inFile.peek() == '-') whole *= -1;
			}

			if (!isDigit(inFile.peek()))
				throw std::logic_error(fileLocationExceptionMsg("Error: Expected digit but got something else at row ", cells.get_count(), inFile.tellg() % cols + 1));

			// Parsing digits of whole number/whole part of floating point number
			while (isDigit(inFile.peek())) {
				(whole *= 10) += inFile.get() - '0';
			}
			// Parsing digits of potential floating point number
			if (inFile.peek() == '.') {
				inFile.get();
				while (isDigit(inFile.peek())) {
					(whole *= 10) += inFile.get() - '0';
					exponent *= 10;
				}
			}

			if (inFile.peek() != ' ' && inFile.peek() != ',' && inFile.peek() != '\n')
				throw std::logic_error(fileLocationExceptionMsg("Error: Expected space, comma or newline but got something else at row ", cells.get_count(), inFile.tellg() % cols + 1));

			if (exponent == 1)
				cells[cells.get_count() - 1][colInd] = (Cell*)new CellInt(whole);
			else
				cells[cells.get_count() - 1][colInd] = (Cell*)new CellDouble((double)whole / exponent);
		}
		// Parsing string or formula
		else if (inFile.peek() == '"') {
			String res;
			inFile.get();
			char buffer;
			while (inFile.peek() != '"') {
				if (inFile.peek() == '\n')
					throw std::logic_error(fileLocationExceptionMsg("Error: Could not find a matching end quote for string at row ", cells.get_count(), inFile.tellg() % cols + 1));
				if (inFile.peek() == '\\') {
					inFile.get();
				}
				buffer = inFile.get();
				res += (const char*)&buffer;
			}
			inFile.get();
			
			if (*res.get_cstr() == '=')
				cells[cells.get_count() - 1][colInd] = (Cell*)new CellFormula(res.get_cstr(), &cells);
			else
				cells[cells.get_count() - 1][colInd] = (Cell*)new CellString(res.get_cstr());
		}
		else
			throw std::logic_error(fileLocationExceptionMsg("Error: Could not determine type of value at row ", cells.get_count(), inFile.tellg() % cols + 1));
	}
}
#include <iostream>
Table::Table(const char* filePath) {
	std::ifstream inFile(filePath);
	if (!inFile.is_open())
		throw std::logic_error("Could not open file!");
	cells = List<List<Cell*>>();
	readFromFile(inFile);
	inFile.close();
}

Table::~Table() {
	for (unsigned i = 0; i < cells.get_length(); i++) {
		for (unsigned j = 0; j < cells.get_length(); j++) {
			delete cells[i][j];
		}
	}
}

unsigned Table::get_rows() const {
	return cells.get_count();
}

unsigned Table::get_cols() const {
	return cells[0].get_length();
}

void Table::putCell(unsigned row, unsigned col, const char* rawValue) {
	Cell* newCell;

	if (containsNumber(rawValue)) {
		double doubleParse = atof(rawValue);
		unsigned intPart = abs(doubleParse);

		newCell = (abs(doubleParse) - intPart > FLOATING_POINT_PRECISION)
					? (Cell*)new CellDouble(doubleParse) : (Cell*)new CellInt(intPart);
	}
	else if (*rawValue == '"') {
		rawValue++;
		newCell = (*rawValue == '=')
					? (Cell*)new CellFormula(rawValue, &cells) : (Cell*)new CellString(rawValue);
	}
	else
		throw std::logic_error("Error: Could not determine data type.");

	
	if (cells[row][col] != nullptr)
		delete cells[row][col];
	cells[row][col] = newCell;
}

List<String> Table::getAllCells() const {
	List<String> toRet;
	for (unsigned i = 0; i < cells.get_count(); i++) {
		for (unsigned j = 0; j < cells[i].get_length(); j++) {
			std::cout << i << ' ' << j << std::endl;
			if (cells[i][j] == nullptr) toRet.add("");
			else toRet.add(cells[i][j]->getValueForPrint());
		}
	}
	return toRet;
}

void Table::saveToFile(const char* filePath) const {
	std::ofstream outFile(filePath);
	if (!outFile.is_open())
		throw std::logic_error("Could not open file!");

	for (unsigned i = 0; i < cells.get_count(); i++) {
		for (unsigned j = 0; j < cells.get_count(); j++) {
			if (cells[i][j] != nullptr)
				cells[i][j]->writeToFile(outFile);
			outFile << ',';
		}
		outFile << '\n';
	}
	outFile.close();
}