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
192
193
|
#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"S | W"A"S | WPDS | WPD.DS | epsilon
// A -> Aa | Ab | ... | AZ | A0 | A1 | ... | A* | ... | a | b | ... | epsilon
// D -> D0 | D1 | ... | D9 | 0 | 1 | ... | 9
// P -> + | - | epsilon
// W -> W | epsilon
//
// over the input file stream
void W(std::ifstream& in) {
while (in.peek() == ' ') in.get();
}
void P(std::ifstream& in, List<char>& buffer) {
if (in.peek() == '-' || in.peek() == '+')
buffer.add(in.get());
}
void D(std::ifstream& in, List<char>& buffer) {
while (in.peek() >= '0' && in.peek() <= '9')
buffer.add(in.get());
}
void A(std::ifstream& in, List<char>& buffer) {
while ((in.peek() >= ' ' && in.peek() <= '~') && in.peek() != '"')
buffer.add(in.get());
}
void throwException(std::ifstream& inFile, unsigned row) {
row++;
throw std::logic_error((
(((((String("Error: Invalid character '") += (char)inFile.peek())
+= "' at row ") += row) += "\" and column ") += ((int)inFile.tellg() / row + 1))
).get_cstr());
}
void Table::readFromFile(std::ifstream& inFile) {
unsigned row = 0;
cells.add(List<Cell*>());
while (inFile.peek() != EOF) {
if (inFile.peek() == '\n') {
row++;
inFile.get();
cells.add(List<Cell*>());
continue;
}
List<char> buffer;
W(inFile);
if (inFile.peek() == '"') {
inFile.get();
A(inFile, buffer);
buffer.add('\0');
if (inFile.peek() != '"') throwException(inFile, row);
inFile.get();
W(inFile);
if (inFile.peek() != ',') throwException(inFile, row);
inFile.get();
cells[row].add((buffer[0] == '=')
? (Cell*)new CellFormula(buffer.raw_data(), &cells)
: (Cell*)new CellString(buffer.raw_data()));
}
else if (inFile.peek() == ',') {
inFile.get();
cells[row].add((Cell*)new CellString(""));
}
else {
P(inFile, buffer);
D(inFile, buffer);
if (inFile.peek() == '.') {
buffer.add(inFile.get());
D(inFile, buffer);
buffer.add('\0');
W(inFile);
if (inFile.peek() != ',') throwException(inFile, row);
inFile.get();
cells[row].add((Cell*)new CellDouble(buffer.raw_data()));
}
else {
buffer.add('\0');
W(inFile);
if (inFile.peek() != ',') throwException(inFile, row);
inFile.get();
cells[row].add((Cell*)new CellInt(buffer.raw_data()));
}
}
}
}
#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();
}
#define abs(a) ((a < 0) ? -a : a)
#define FLOATING_POINT_PRECISION 0.0001f
void Table::putCell(unsigned row, unsigned col, const char* rawValue) {
Cell* newCell;
while (*rawValue == ' ') rawValue++;
if (*rawValue == '"') {
rawValue++;
newCell = (*rawValue == '=')
? (Cell*)new CellFormula(rawValue, &cells)
: (Cell*)new CellString(rawValue);
}
else if ((*rawValue == '+' || *rawValue == '-') || (*rawValue >= '0' && *rawValue <= '9')) {
double doubleParse = atof(rawValue);
unsigned intPart = abs(doubleParse);
newCell = (abs(doubleParse) - intPart > FLOATING_POINT_PRECISION)
? (Cell*)new CellDouble(doubleParse)
: (Cell*)new CellInt(intPart);
}
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_count(); j++) {
std::cout << i << ' ' << j << std::endl;
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();
}
|