|
@@ -3,14 +3,20 @@
|
|
// TODO passwords are stored and checked in plain text
|
|
// TODO passwords are stored and checked in plain text
|
|
// TODO read userStorage file location from config
|
|
// TODO read userStorage file location from config
|
|
|
|
|
|
-void UserManager::init() {
|
|
|
|
- std::ifstream ifile("userStorage.txt");
|
|
|
|
- if (!ifile) {
|
|
|
|
|
|
+// initialize static filename to empty string
|
|
|
|
+std::string UserManager::filename = "";
|
|
|
|
+
|
|
|
|
+void UserManager::init(const std::string &file) {
|
|
|
|
+ filename = file;
|
|
|
|
+ std::ifstream ifile(filename);
|
|
|
|
+ if (!ifile.is_open()) {
|
|
// create new file if userStorage does not exist
|
|
// create new file if userStorage does not exist
|
|
- std::ofstream file;
|
|
|
|
- file.open("userStorage.txt");
|
|
|
|
- file << "cat;tac\n";
|
|
|
|
- file.close();
|
|
|
|
|
|
+ std::ofstream ofile;
|
|
|
|
+ ofile.open(filename);
|
|
|
|
+ ofile << "user;pass\n";
|
|
|
|
+ ofile.close();
|
|
|
|
+ std::cout << "Created \"" << filename << "\" and added the default user"
|
|
|
|
+ << std::endl;
|
|
}
|
|
}
|
|
ifile.close();
|
|
ifile.close();
|
|
}
|
|
}
|
|
@@ -52,7 +58,7 @@ void UserManager::deleteUser(const std::string &name, const std::string &pw) {
|
|
|
|
|
|
// read content from file into given map
|
|
// read content from file into given map
|
|
void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
|
|
void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
|
|
- std::ifstream ifile("userStorage.txt");
|
|
|
|
|
|
+ std::ifstream ifile(filename);
|
|
std::string line;
|
|
std::string line;
|
|
while (getline(ifile, line)) {
|
|
while (getline(ifile, line)) {
|
|
std::stringstream ss(line);
|
|
std::stringstream ss(line);
|
|
@@ -69,7 +75,7 @@ void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
|
|
// write content from map to file
|
|
// write content from map to file
|
|
void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
|
|
void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
|
|
std::ofstream file;
|
|
std::ofstream file;
|
|
- file.open("userStorage.txt");
|
|
|
|
|
|
+ file.open(filename);
|
|
for (auto const &x : user_map) {
|
|
for (auto const &x : user_map) {
|
|
file << x.first << ";" << x.second << std::endl;
|
|
file << x.first << ";" << x.second << std::endl;
|
|
}
|
|
}
|