-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserData.cpp
108 lines (92 loc) · 3.54 KB
/
UserData.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
#include <vcl.h>
#pragma hdrstop
#include "UserData.h"
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
using namespace std;
namespace fs = std::filesystem;
extern vector<userBook> userBooks;
//Main.cpp
string returnStr(AnsiString output);
//UserControls.cpp
void clearAllInputs(TEdit *BookNameEdit1, TEdit *BookAuthorEdit1, TEdit *CustomBookGenre, TComboBox *BookGenreComboBox, TEdit *BookLengthEdit1);
void updateDisplays(TComboBox *genreComboBox, TComboBox *booksComboBox, TStringGrid *historyGrid, TMemo *statMemo, TLabel *label);
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
string getNext(string currLine, int *index) {
*index += + 2;
string result = "";
while(currLine[*index] != ']') {
result = result + currLine[*index];
(*index)++;
}
return result;
}
void rewriteFileData() {
ofstream userDataFile(mainFolder + "\\" + userDataPath);
for (int i = 0; i < userBooks.size(); i++) {
string tempString;
tempString = "[" + userBooks[i].bookName + "][" + to_string(userBooks[i].bookmark) + "][" + userBooks[i].bookAuthor + "][" + userBooks[i].genre + "][" + userBooks[i].startedReading + "][" + userBooks[i].finishedReading + "][" + to_string(userBooks[i].currPage) + "][" + to_string(userBooks[i].bookLength) + "]";
userDataFile << tempString << endl;
}
userDataFile.close();
}
void distributeString(string currLine) {
int index = -1;
string newBookName = getNext(currLine, &index);
string newBookmarkPage = getNext(currLine, &index);
string newBookAuthor = getNext(currLine, &index);
string newBookGenre = getNext(currLine, &index);
string newStartedReading = getNext(currLine, &index);
string newFinishedReading = getNext(currLine, &index);
string newCurrPage = getNext(currLine, &index);
string newBookLength = getNext(currLine, &index);
userBook newBook;
newBook.bookName = newBookName;
newBook.bookmark = stoi(newBookmarkPage);
newBook.bookAuthor = newBookAuthor;
newBook.genre = newBookGenre;
newBook.startedReading = newStartedReading;
newBook.currPage = stoi(newCurrPage);
newBook.bookLength = stoi(newBookLength);
newBook.finishedReading = newFinishedReading;
userBooks.push_back(newBook);
}
void getUserData() {
ifstream userDataFile(mainFolder + "\\" + userDataPath);
string currLine;
while (getline(userDataFile, currLine)) {
distributeString(currLine);
}
userDataFile.close();
}
void __fastcall TMainForm::AddNewButton1Click(TObject *Sender)
{
userBook newBook;
newBook.bookName = returnStr(BookNameEdit1->Text);
newBook.bookAuthor = returnStr(BookAuthorEdit1->Text);
if (CustomBookGenre->Text != "") {
newBook.genre = returnStr(CustomBookGenre->Text);
}
else {
newBook.genre = returnStr(BookGenreComboBox->Text);
}
long int t = static_cast<long int> (time(0));
string startedReadingTime = to_string(t);
newBook.startedReading = startedReadingTime;
newBook.finishedReading = "0";
newBook.bookmark = 0;
newBook.currPage = 0;
newBook.bookLength = stoi(returnStr(BookLenghtEdit1->Text));
userBooks.push_back(newBook);
rewriteFileData();
clearAllInputs(BookNameEdit1, BookAuthorEdit1, CustomBookGenre, BookGenreComboBox, BookLenghtEdit1);
updateDisplays(BookGenreComboBox, BookList, HistoryGrid, ReadStatMemo, ReportLabel5);
}