-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
167 lines (144 loc) · 5.85 KB
/
main.cpp
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
/* Copyright: 2021 Donat Zenichev development <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This package is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
On Debian systems, the complete text of the GNU General
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". */
#include "main.h"
int main(int agrc, char **argv)
{
std::ifstream learningFile;
/* general required classes for the work */
mainMenu myMainMenu;
dataKeeper myData;
SDLgraphics myWindow;
/* work with words and buttons on cards */
int pressedBut = 0;
std::pair<std::string, std::string> pickedWord;
/* data to show */
std::string results;
/* collect given arguments */
int tmp = agrc;
while (--tmp > 0) {
std::string argument(argv[tmp]);
if (argument.empty()) continue;
if (myMainMenu.CLIappendToArgvList(argument)) {
printf("WARNING: Something went wrong while appending arguments, terminating..\n");
return 1;
}
}
/* re-order arguments list, to let it look originally in the program */
std::reverse(myMainMenu.argvList.begin(), myMainMenu.argvList.end());
/* parse arguments and define the interaction mode
0 - interaction only via cli arguments
1 - interaction via usual cli menu
2 - interaction via gui menu */
int interactionMode = myMainMenu.defineTheMode();
/* ret: 0 - start processing cards / 1 - normal clearing / -1 - something went wrong */
int ret = 0;
switch (interactionMode) {
/* interaction only via cli arguments */
case 0:
/* check whether user wants a help output */
if (myMainMenu.inArguments(myMainMenu.argvList, ARG_HELP_MENU)) {
myMainMenu.CLIprovideHelp();
ret = 1;
break;
}
/* if the source file with data not defined, terminate */
if (!myMainMenu.inArguments(myMainMenu.argvList, ARG_OPEN_FILE)) {
printf("WARNING: --cli-mode picked out, but no source file provided.\n");
ret = -1;
break;
}
ret = myMainMenu.CLIsetSourceFile(); /* get file to open */
myMainMenu.CLIsetWorkingDir(); /* set working directory */
myMainMenu.CLIdefineTimeRanges(); /* define borders for a random time step */
break;
/* interaction via usual cli menu */
case 1:
ret = myMainMenu.launchInteractionWindow(); /* start usual cli menu */
break;
/* interaction via gui menu */
case 2:
printf("WARNING: Sorry, but the interaction via GUI is not available at the moment.\n");
printf("WARNING: Try to use the program in CLI mode / CLI menu mode\n");
ret = -1;
break;
default:
printf("WARNING: Cannot determine the interaction mode, terminating..\n");
ret = -1;
break;
}
/* basic checks before start flipping cards */
switch (ret) {
case -1:
printf("ERROR: Something went wrong, terminating the program.\n");
return 1;
case 1:
printf("DEBUG: Normal program clearing.\n");
return 0;
}
printf("DEBUG: Working directory has been set to: %s \n", myMainMenu.workingDir);
if (strlen(myMainMenu.openFile) == 0 ) {
printf("ERROR: Something went wrong, the source file wasn't defined properly.\n");
printf("\tHave you given the proper path to a file? Try to launch the program again!\n");
return 1;
}
/* try to open the given file and save all words in the memory stored inside */
learningFile.open(myMainMenu.openFile, std::ios::in);
if(learningFile.is_open()) {
std::string line;
while(getline(learningFile, line)) myData.appendToWords(line);
} else {
printf("ERROR: cannot open the requested file, please try again.\n");
return 1;
}
/* start showing the words and do that until an amount of cycles is done */
for(int cyclesPassed = 0; cyclesPassed < myData.wordsAmount; cyclesPassed++)
{
myWindow.createNewWindow(0, 0, WINDOW_TYPE_QUIZ);
/* shows and destorys it */
pickedWord = myData.randomlyGiveWords();
pressedBut = myWindow.showWindow("The word to learn:", pickedWord.first.c_str());
switch(pressedBut) {
case 0: /* no */
myData.setWordStatus(false, pickedWord.first);
myWindow.createNewWindow(0, 0, WINDOW_TYPE_NO_ANSWER);
myWindow.showWindow("The translation was:", pickedWord.second.c_str());
break;
case 1: /* yes */
myData.setWordStatus(true, pickedWord.first);
break;
case 2: /* a termination of the program - normal clearing */
printf("DEBUG: Thanks for using the program, good bye!\n");
return 0;
default:
break;
}
/* check if we have to stop cycline already now */
if (myData.showStopperA >= myData.wordsAmount ||
myData.showStopperB >= myData.wordsAmount ||
myData.showStopperA + myData.showStopperB >= myData.wordsAmount ||
myData.showStopperA >= ALLOWED_AMOUNT_LINES ||
myData.showStopperB >= ALLOWED_AMOUNT_LINES ||
myData.showStopperA + myData.showStopperB >= ALLOWED_AMOUNT_LINES
) break; /* quit the cycle if all processed */
std::this_thread::sleep_for(std::chrono::seconds(myData.getNextTime())); /* now sleep */
}
/* show results */
myData.giveResults(results);
myWindow.createNewWindow(0, 0, WINDOW_TYPE_RESULT);
myWindow.showWindow("Given results:", results.c_str());
/* save data for the next program run */
if (myData.saveCurrentResults(results, myMainMenu.workingDir))
printf("WARNING: Not able to save results!\n");
return 0;
}