-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommand.cpp
More file actions
76 lines (67 loc) · 2.61 KB
/
command.cpp
File metadata and controls
76 lines (67 loc) · 2.61 KB
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
//
// Created by tomanm10 on 04.12.2019.
//
#include <iostream>
#include "command.hpp"
#include "matrix.hpp"
#include "system_solver.hpp"
bool CommandInterpreter::USING_MULTIPLE_THREADS = false; // use one thread as default
Command CommandInterpreter::get_command(std::ostream &ostream, std::istream &istream) {
ostream << "Enter command to execute..." << std::endl;
istream >> std::ws; // clear the whitespaces that left in the stream
std::string input;
std::getline(istream, input);
return str_to_command(input);
}
void CommandInterpreter::print_help(std::ostream &ostream) {
ostream << "######################" << std::endl;
ostream << "AVAILABLE COMMANDS" << std::endl;
ostream << "######################" << std::endl;
for (auto &i : command_list) {
ostream << i.first << " - " << get_command_help(i.second) << std::endl;
}
ostream << "######################" << std::endl;
}
Command CommandInterpreter::str_to_command(const std::string &cmd) {
try {
return command_list.at(cmd);
} catch (std::out_of_range &e) {
return Command::UNKNOWN;
}
}
std::string CommandInterpreter::get_command_help(const Command &cmd) {
try {
return command_descriptions.at(cmd);
} catch (std::out_of_range &e) {
return "";
}
}
void CommandInterpreter::process_command(std::ostream &ostream, std::istream &istream, const Command &cmd) {
if (cmd == Command::QUIT) {
ostream << "Shutting down..." << std::endl;
} else if (cmd == Command::HELP) {
print_help(ostream);
} else if (cmd == Command::ONE_THREAD) {
CommandInterpreter::USING_MULTIPLE_THREADS = false;
ostream << "Using one thread for computation." << std::endl;
} else if (cmd == Command::MTP_THREAD) {
CommandInterpreter::USING_MULTIPLE_THREADS = true;
ostream << "Using multiple threads for computation." << std::endl;
} else if (cmd == Command::CMD_INPUT) {
try {
Matrix matrix = MatrixCreator::parse_from_cmd_line(ostream, istream);
SystemSolver::solve(ostream, matrix);
} catch (std::exception &e) {
ostream << "An exception occurred: " << e.what() << std::endl;
}
} else if (cmd == Command::TXT_INPUT) {
try {
Matrix matrix = MatrixCreator::parse_from_txt_file(ostream, istream);
SystemSolver::solve(ostream, matrix);
} catch (std::exception &e) {
ostream << "An exception occurred: " << e.what() << std::endl;
}
} else if (cmd == Command::UNKNOWN) {
ostream << "Unknown command entered" << std::endl;
}
}