blob: 3272f0d8d497be214374c5e88558ab8edb77730d (
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
|
#include "UserInterface.h"
#include "../Generic/List/List.hpp"
#include "../Generic/String/String.h"
#include "../Generic/ConsoleInterface/Toolbox.hpp"
#include <stdexcept>
/* Private (static) */
Table* UserInterface::table = nullptr;
String fileName = String();
void UserInterface::com_open(const char* params) {
if (table != nullptr) {
printLine("A file has already been opened!");
return;
}
try {
table = new Table(params);
fileName = params;
}
catch (std::logic_error err) {
printLine(err.what());
}
}
void UserInterface::com_close(const char* params) {
if (table == nullptr) {
printLine("No file has been opened!");
return;
}
delete table;
}
void UserInterface::com_save(const char* params) {
com_saveas(fileName.get_cstr());
}
void UserInterface::com_saveas(const char* params) {
if (table == nullptr) {
printLine("No file has been opened!");
return;
}
}
void UserInterface::com_help(const char* params) {
std::cout << "The following commands are supported:" << std::endl
<< "open <file> opens <file>" << std::endl
<< "close closes currently opened file" << std::endl
<< "save saves the currently open file" << std::endl
<< "saveas <file> saves the currently open file in <file>" << std::endl
<< "help prints this information" << std::endl
<< "exit exists the program" << std::endl;
}
void UserInterface::com_exit(const char* params) {
}
void UserInterface::com_print(const char* params) {
List<String> cells = table->getAllCells();
printTable(cells, table->get_cols());
}
void UserInterface::com_edit(const char* params) {
while (*params == ' ') params++;
unsigned row = atoi(++params);
while (*params != 'C') params++;
unsigned col = atoi(++params);
while (*params != ' ') params++;
table->putCell(row, col, params);
}
/* Public */
UserInterface::UserInterface(Table* inTable) : menu() {
if (table == nullptr)
table = inTable;
menu.addCommand(Command("help", com_help));
menu.addCommand(Command("print", com_print));
menu.addCommand(Command("edit", com_edit));
}
void UserInterface::run() {
menu.navigate();
}
|