-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataBase.cpp
183 lines (133 loc) · 4.09 KB
/
DataBase.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <bits/stdc++.h>
#include <stdlib.h>
#include "DataBase.h"
#include "FlashCard.h"
#include "Date.h"
using namespace std;
DataBase::DataBase() {
path = "./database/";
}
void DataBase::setPath (string addPath) {
path.append(addPath);
}
void DataBase::resetPath (void) {
path = "./database/";
}
string DataBase::getPath (void) {
return path;
}
bool DataBase::rm (string fileName) {
string cardPath = this->getPath();
string listPath = this->getPath();
string tmpPath = this->getPath();
char *card;
int day;
int month;
int year;
int wasRemoved;
FILE *list;
FILE *tmp;
cardPath.append(fileName);
listPath.append("cardList");
tmpPath.append("tmp");
wasRemoved = remove(cardPath.c_str());
if (wasRemoved == -1) return false;
list = fopen(listPath.c_str(), "a+");
tmp = fopen(tmpPath.c_str(), "w+");
while (fscanf(list, " %ms %d %d %d", &card, &day, &month, &year) != EOF) {
if (fileName == string(card)) continue;
fprintf(tmp, "%s %d %d %d\n", card, day, month, year);
}
free(card);
fclose(list);
fclose(tmp);
remove(cardPath.c_str());
remove(listPath.c_str());
rename(tmpPath.c_str(), listPath.c_str());
return true;
}
bool DataBase::add (FlashCard flashcard) {
string cardName = flashcard.getName();
string japanese = flashcard.getJapanese();
string furigana = flashcard.getFurigana();
string translation = flashcard.getTranslation();
int LN = flashcard.getLN();
float EF = flashcard.getEF();
string cardPath = this->getPath().append(cardName);
FILE *fileName;
fileName = fopen(cardPath.c_str(), "r");
if (fileName != NULL) {
fclose (fileName);
return false;
}
fileName = fopen(cardPath.c_str(), "w+");
fprintf(fileName, "%d\n%f\n%s\n%s\n%s\n", LN, EF, japanese.c_str(), furigana.c_str(), translation.c_str());
fclose(fileName);
appendToCardList(flashcard);
return true;
}
void DataBase::appendToCardList (FlashCard flashcard) {
string cardName = flashcard.getName();
Date date = flashcard.getDate();
int fcDay = date.getDay();
int fcMonth = date.getMonth();
int fcYear = date.getYear();
string cardListPath = this->getPath().append("cardList");
string tmpPath = this->getPath().append("tmp");
FILE *cardList = fopen(cardListPath.c_str(), "a+");
FILE *tmp = fopen(tmpPath.c_str(), "w+");
bool wasAdded = false;
bool isEmpty = true;
char *name;
int day, month, year;
while(fscanf(cardList, " %ms %d %d %d", &name, &day, &month, &year) != EOF) {
Date tmpDate(day, month, year);
if ((tmpDate == date or tmpDate > date) and wasAdded == false) {
wasAdded = true;
fprintf(tmp, "%s %d %d %d\n", cardName.c_str(), fcDay, fcMonth, fcYear);
}
fprintf(tmp, "%s %d %d %d\n", name, day, month, year);
isEmpty = false;
}
if (isEmpty == true or (isEmpty == false and wasAdded == false)) {
fprintf(tmp, "%s %d %d %d\n", cardName.c_str(), fcDay, fcMonth, fcYear);
}
fclose(cardList);
fclose(tmp);
remove(cardListPath.c_str());
rename(tmpPath.c_str(), cardListPath.c_str());
}
vector <FlashCard> DataBase::getTodaysCards (void) {
string pathToPaste = getPath();
string pathCardList = getPath().append("cardList");
string pathTmpCard;
Date today = getCurrentDate();
int day, month, year;
char *name;
int LN;
float EF;
char *japanese, *furigana, *translation;
FILE *cardList = fopen(pathCardList.c_str(), "a+");
FILE *tmpFile;
vector <FlashCard> flashCardVector;
while (fscanf(cardList, " %ms %d %d %d", &name, &day, &month, &year) != EOF) {
Date tmpDate(day, month, year);
if (tmpDate > today) break;
pathTmpCard = pathToPaste;
pathTmpCard.append(string(name));
tmpFile = fopen(pathTmpCard.c_str(), "r+");
fscanf(tmpFile, "%d %f %ms %ms %ms", &LN, &EF, &japanese, &furigana, &translation);
FlashCard flashcard(name, LN, EF, tmpDate, string(japanese), string(furigana), string(translation));
flashCardVector.push_back(flashcard);
fclose(tmpFile);
}
fclose(cardList);
srand(unsigned(time(NULL)));
random_shuffle(flashCardVector.begin(), flashCardVector.end());
return flashCardVector;
}
void DataBase::updateFlashCard (FlashCard flashcard) {
string name = flashcard.getName();
rm(name);
add(flashcard);
}