-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cpp
More file actions
89 lines (78 loc) · 3.06 KB
/
FileManager.cpp
File metadata and controls
89 lines (78 loc) · 3.06 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
#include "header/FileManager.h"
void FileManager::create_new_account(const std::vector<std::string> &information) noexcept {
const std::string fileName = "Files/Accounts/" + information[0];
std::ofstream fout(fileName);
fout << information[0] << ' ' << information[1] << ' ' << information[2] << ' ' << information[3] << std::endl;
fout << "__HISTORY__" << std::endl;
fout.close();
}
void FileManager::insert_to_users_file(const std::string &username) const noexcept {
const std::string fileName = "Files/users";
std::ofstream fout(fileName, std::ios::out | std::ios::app);
fout << username << std::endl;
fout.close();
}
std::vector<std::string> FileManager::get_information_from_user_file(const std::string &username) const {
const std::string fileName = "Files/Accounts/" + username;
std::ifstream fin(fileName);
if (fin.fail())
throw std::runtime_error("File not found -> get_information_from_user_file");
std::string line, user_name, name, email, password;
std::getline(fin, line);
std::stringstream ss(line);
ss >> user_name >> password >> email >> name;
fin.close();
return {user_name, password, email, name};
}
bool FileManager::valid_login(const std::string &_username, const std::string &_password) const noexcept {
const std::string fileName = "Files/Accounts/" + _username;
std::string line, username, email, pass, name;
std::ifstream fin(fileName);
if (fin.fail())
return false;
getline(fin, line);
std::stringstream ss(line);
ss >> username >> pass >> email >> name;
if (pass == _password) {
return true;
}
return false;
}
void FileManager::load_all_file(std::vector<std::string> &container, const std::string &fileName) {
std::ifstream fin(fileName);
if (fin.fail())
throw std::invalid_argument("File not found -> load all file function");
std::string line;
while (getline(fin, line)) {
container.push_back(line);
}
fin.close();
}
void FileManager::insert_new_history_to_user(const std::string &book_name, int page, int total_num_of_pages,
std::string user) {
const std::string fileName = "Files/Accounts/" + user;
std::ofstream fout(fileName, std::ios::out | std::ios::app);
fout << book_name << " __page " << page << " /" << total_num_of_pages << " " << std::endl;
fout.close();
}
bool FileManager::book_exist(const std::string &book_name) const {
const std::string fileName = "Files/booksOnGoogle";
std::ifstream fin(fileName);
if (fin.fail())
throw std::invalid_argument("File not found -> book exist");
std::string line;
while (getline(fin, line)) {
if (line == book_name) {
fin.close();
return true;
}
}
fin.close();
return false;
}
void FileManager::add_book_to_site(const std::string &book_name) noexcept {
const std::string fileName = "Files/booksOnSite";
std::ofstream fout(fileName, std::ios::out | std::ios::app);
fout << book_name << std::endl;
fout.close();
}