-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataKeeper.cpp
113 lines (97 loc) · 4 KB
/
dataKeeper.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
/* 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 "dataKeeper.h"
/* constructor */
dataKeeper::dataKeeper()
: wordsAmount(0), wordsLearned(0), wordsForgotten(0), slotsIDs(0), nextTime(0) {
myLearnedWords = std::vector<std::string>( ALLOWED_AMOUNT_LINES );
myForgottenWords = std::vector<std::string>( ALLOWED_AMOUNT_LINES );
};
/* add new words to an existing list */
void dataKeeper::appendToWords(std::string & newWord) {
std::string wordItself = newWord.substr(0, newWord.find(DELIMITER));
std::string wordTranslation = newWord.substr(wordItself.length() + 1, newWord.find(DELIMITER));
myWords.insert(std::make_pair(wordItself, wordTranslation));
wordsAmount++;
}
/* define whether the word has been forgotten or has been learned */
void dataKeeper::setWordStatus(bool status, std::string & word) {
if (status) {
if (!inArray(myLearnedWords, (const std::string) word) &&
!inArray(myForgottenWords, (const std::string) word)) {
myLearnedWords[wordsLearned] += word;
++wordsLearned;
}
} else {
if (!inArray(myForgottenWords, (const std::string) word) &&
!inArray(myLearnedWords, (const std::string) word)) {
myForgottenWords[wordsForgotten] += word;
++wordsForgotten;
}
}
}
/* randomly return some word from an array */
std::pair<std::string, std::string> dataKeeper::randomlyGiveWords() {
std::pair<std::string, std::string> nextWord = *select_randomly(myWords.begin(), myWords.end());
myWords.erase(nextWord.first); /* remove it, in order to next time not use it again */
return nextWord;
}
/* return randomly picked amount of seconds till next window appearing */
int dataKeeper::getNextTime() {
time_t t;
srand((unsigned) time(&t));
int nextTime = (rand()%(NEXT_TIME_MAX-NEXT_TIME_MIN))+NEXT_TIME_MIN;
return nextTime;
}
template<typename Iter, typename RandomGenerator>
Iter dataKeeper::select_randomly(Iter start, Iter end, RandomGenerator& g) {
std::uniform_int_distribution<> dis(0, std::distance(start, end) - 1);
std::advance(start, dis(g));
return start;
}
template<typename Iter>
Iter dataKeeper::select_randomly(Iter start, Iter end) {
static std::random_device rd;
static std::mt19937 gen(rd());
return select_randomly(start, end, gen);
}
/* give a summary on learned and forgotten words */
void dataKeeper::giveResults(std::string & givenResults) {
givenResults = "[Words learned]\n";
for (auto it = myLearnedWords.begin(); it != myLearnedWords.end(); ++it)
{
std::string tmp = *it;
if(!tmp.empty()) givenResults = givenResults + "- " + tmp + "\n";
}
givenResults = givenResults + "\n[Words forgotten]\n";
for (auto it = myForgottenWords.begin(); it != myForgottenWords.end(); ++it)
{
std::string tmp = *it;
if(!tmp.empty()) givenResults = givenResults + "- " + tmp + "\n";
}
}
/* save results for the next program run */
int dataKeeper::saveCurrentResults(std::string & givenResults, const char * dir) {
std::ofstream resultsFile;
std::string filename = dir;
filename = filename + "/" + RESULTS_FILE_NAME;
resultsFile.open(filename, std::ios::out);
if(resultsFile.is_open()) {
resultsFile << givenResults;
resultsFile.close();
return 0;
}
printf("ERROR: cannot create/open the requested file, please try again.\n");
return 1;
}