Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Models / Table.cpp
blob: 4bdf13e1ce8e53ad28a0e24154762c6dd3b1bfad (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
#include "Table.h"
#include "CellDouble.h"
#include "CellInt.h"
#include "CellString.h"
#include "CellFormula.h"
#include <cstring>

Table::Table(unsigned rows, unsigned cols) {
	cells = List<List<Cell*>>(rows);
	for (unsigned i = 0; i < rows; i++)
		cells[i] = List<Cell*>(cols);
}


bool containsNumber(const char*& str) {
	while (*str == ' ') str++;
	return ((*str == '+' || *str == '-') && (*(str + 1) >= '0' && *(str + 1) <= '9')) ||
		   (*str >= '0' && *str <= '9');
}

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

	if (containsNumber(rawValue)) {
		double intParse = atoi(rawValue);
		double doubleParse = atof(rawValue);

		newCell = (doubleParse - intParse > 0.00001 && doubleParse > 0.00001)
					? (Cell*)new CellDouble(doubleParse) : (Cell*)new CellInt(intParse);
	}
	else {
		newCell = (*rawValue == '=') ? (Cell*)new CellFormula(rawValue) : (Cell*)new CellString(rawValue);
	}

	cells[row][col] = newCell;
}

String Table::getAllCells() const {
	//TODO
	return String();
}