blob: 2673ba41f4962e629263c5c463bc5aae3342f1e6 (
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
|
#include "Table.h"
#include "CellDouble.h"
#include "CellInt.h"
#include "CellString.h"
#include "CellFormula.h"
#include <cstring>
#include <fstream>
#include <exception>
#include <stdexcept>
#include <iostream> // TODO: remove
// Rough implementation of the grammar
// S -> W"=A"W | W"=A"W | WDW | WD.DW
// A -> Aa | Ab | ... | AZ | A* | ... | a | b | ...
// D -> D0 | D1 | ... | D9 | 0 | 1 | ... | 9
// W -> W | epsilon
void Table::readFromFile(std::ifstream& inFile) {
}
#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();
}
|