-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliInitializator.cpp
More file actions
169 lines (151 loc) · 4.3 KB
/
Copy pathCliInitializator.cpp
File metadata and controls
169 lines (151 loc) · 4.3 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
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
#include<iostream>
#include<fstream>
#include "CliInitializator.h"
#include "Play.h"
using namespace std;
void CliInitializator::startCliModule() {
setProgramsVariables();
mainLoop();
}
void CliInitializator::setProgramsVariables() {
setlocale(LC_ALL, "");
loadCommands();
}
void CliInitializator::mainLoop() {
cout << "MIDI Parser 0.1.0\nMi³osz Nowaczyk 2020" << endl;
string input;
bool programRunning;
do {
cout << "\n";
getline(cin, input);
programRunning = analyzeInput(input);
} while (programRunning);
}
enum class registerdCommands {
help = 1,
exit = 2,
play = 3
};
bool CliInitializator::analyzeInput(string input) {
int* commandChoosen = new int{(int) checkCommandId(input.substr(0 ,input.find(" "))) };
string* commandArgument = new string(input.substr(input.find(" ") + 1));
switch ((int) *commandChoosen) {
case (int) registerdCommands::help:
printAllCommands();
break;
case (int) registerdCommands::exit:
return false;
break;
case (int) registerdCommands::play:
{
Play* play = new Play(commandArgument);
play->run();
delete(play);
}
break;
default:
if (input.substr(0, input.find(" ")).size() != 0) {
cout << "Undefined command \"" + input.substr(0, input.find(" ")) + "\", type help to list all commands." << endl;
}
break;
}
return true;
}
enum class loadingMode {
id = 0,
commandName = 1,
commandDescription = 2,
};
// Todo: change all variables to be allocated dynamicly
void CliInitializator::loadCommands() {
ifstream* commandsLoader = new ifstream("commands.conf", ios::in);
char singleCharacter;
string newWord = "";
CommandEntity* tmp = nullptr;
int currentMode = (int) loadingMode::id;
if (commandsLoader->is_open()) {
while (commandsLoader->get(singleCharacter)) {
if (singleCharacter != ',' && singleCharacter != '\n') {
newWord += singleCharacter;
}
else {
if (currentMode == (int) loadingMode::id) {
tmp = new CommandEntity();
tmp->setCommandId(stoi(newWord));
currentMode = (int)loadingMode::commandName;
}
else if (currentMode == (int) loadingMode::commandName) {
tmp->setCommandName(newWord);
currentMode = (int)loadingMode::commandDescription;
}
else if (currentMode == (int)loadingMode::commandDescription) {
tmp->setCommandDescription(newWord);
saveCommand(tmp);
currentMode = (int)loadingMode::id;
}
newWord = "";
}
}
tmp->setCommandDescription(newWord);
saveCommand(tmp);
commandsLoader->close();
delete(commandsLoader);
}
else {
cout << "Failed to open the commands.conf file!" << endl;
exit(1);
}
}
void CliInitializator::saveCommand(CommandEntity* newCommand) {
if (commands == nullptr) {
commands = newCommand;
}
else {
CommandEntity* tmp = commands;
while (commands->getNext() != nullptr) {
commands = commands->getNext();
}
commands->setNext(newCommand);
commands = tmp;
}
}
// Todo: chagne place of methode to CommandEntity
void CliInitializator::printAllCommands() {
CommandEntity* tmp = commands;
while (commands->getNext() != nullptr) {
cout << commands->getCommandName() + " - " + commands->getCommandDescription() << endl;
commands = commands->getNext();
}
cout << commands->getCommandName() + " - " + commands->getCommandDescription() << endl;
commands = tmp;
}
// Todo: chagne place of methode to CommandEntity
void CliInitializator::printSelectedCommand(string commandName) {
CommandEntity* tmp = commands;
while (commands->getNext() != nullptr) {
if (commandName == commands->getCommandName()) {
cout << commands->getCommandName() + " - " + commands->getCommandDescription() << endl;
}
commands = commands->getNext();
}
if (commandName == commands->getCommandName()) {
cout << commands->getCommandName() + " - " + commands->getCommandDescription() << endl;
}
commands = tmp;
}
// Todo: chagne place of methode to CommandEntity
int* CliInitializator::checkCommandId(string usersCommandName) {
int* chosenCommandId = new int{0};
CommandEntity* tmp = commands;
while (commands->getNext() != nullptr) {
if (usersCommandName == commands->getCommandName()) {
chosenCommandId = (int*) commands->getCommandId();
}
commands = commands->getNext();
if (usersCommandName == commands->getCommandName()) {
chosenCommandId = (int*) commands->getCommandId();
}
}
commands = tmp;
return chosenCommandId;
}