Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Files.h"
- #include <fstream>
- #include <filesystem>
- #include <string>
- namespace fs = std::experimental::filesystem::v1;
- void Files::update()
- {
- std::fstream VarFile;
- printError("Saving data to files..\n");
- if (var->current != "DELETED_DATABASE") { // FOR MERGING
- std::ofstream{ tempValuesFile };
- VarFile.open(tempValuesFile);
- if (VarFile.good()) {
- if (VarFile << var->people << ";") {}
- else std::cerr << ERROR_FILE_INPUT;
- if (VarFile << reservedPeople << ";") {}
- else std::cerr << ERROR_FILE_INPUT;
- if (var->valuesList.size() > 0) {
- if (VarFile << var->valuesList.size() << ";\n") {}
- else std::cerr << ERROR_FILE_INPUT;
- for (const auto &i : var->valuesList) {
- if (VarFile << i << "$") {}
- else std::cerr << ERROR_FILE_INPUT;
- }
- }
- else
- VarFile << 0 << ";\n";
- if (fs::exists(currentVal))
- fs::remove(currentVal);
- }
- else
- printError("Error: File " + var->current + "_Values.txt" + " could not be opened\n");
- VarFile.close();
- fs::rename(tempValuesFile, currentVal);
- std::ofstream{ tempMembersFile };
- VarFile.open(tempMembersFile);
- if (VarFile.good()) {
- for (size_t i = 0; i < var->people && var->people > 0; ++i) {
- if (VarFile << i + 1 << "." << var->memberList[i][0]) {}
- else std::cerr << ERROR_FILE_INPUT;
- auto v_size = var->valuesList.size();
- for (size_t j = 1; j <= v_size && v_size > 0; ++j) {
- if (var->memberList[i].size() > j) {
- if (var->memberList[i][j].empty())
- var->memberList[i][j] = "void";
- }
- else
- var->memberList[i].push_back("void");
- if (VarFile << "$" << var->memberList[i][j]) {}
- else std::cerr << ERROR_FILE_INPUT;
- }
- if (VarFile << ";\n") {}
- else std::cout << ERROR_FILE_INPUT;
- }
- if (fs::exists(currentMem))
- fs::remove(currentMem);
- }
- else
- printError("Error: File" + var->current + "_Members.txt" + " could not be opened\n");
- VarFile.close();
- fs::rename(tempMembersFile, currentMem);
- }
- }
- void Files::open()
- {
- std::string pplstr, valstr;
- // VALUES LOADING
- std::fstream VarFile;
- VarFile.open(tempPPLFile, std::ios::in | std::ios::out);
- if (VarFile.good()) { // AFTER MERGING
- getline(VarFile, valstr, ';');
- var->people = std::stoi(valstr);
- }
- VarFile.close();
- fs::remove(tempPPLFile);
- const int standardSize = 10;
- VarFile.open(currentVal, std::ios::in | std::ios::out);
- if (VarFile.good()) {
- getline(VarFile, valstr, ';');
- if (!valstr.empty() && var->people == 0)
- var->people = catchStoi(valstr);
- getline(VarFile, valstr, ';');
- if (!valstr.empty())
- reservedPeople = catchStoi(valstr);
- if (reservedPeople == 0)
- reservedPeople = standardSize;
- var->memberList.reserve(reservedPeople);
- getline(VarFile, valstr, ';'); // skip values size (only for merging)
- while (getline(VarFile, valstr, '$')) {
- valstr.erase(std::remove(begin(valstr), end(valstr), '\n'), end(valstr));
- if (!valstr.empty())
- var->valuesList.push_back(valstr);
- }
- printError("File " + currentVal + " loaded.\n");
- }
- else
- reservedPeople = standardSize;
- VarFile.close();
- // MEMBER LOADING
- currentMem = var->current_path + R"(/)" + var->current + R"(/)" + var->current + "_Members.txt";
- VarFile.open(currentMem, std::ios::in | std::ios::out);
- if (VarFile.good()) {
- if (var->people > 0) {
- int i = 0;
- while (getline(VarFile, pplstr, ';') && !pplstr.empty()) {
- pplstr.erase(std::remove(begin(pplstr), end(pplstr), '\n'), end(pplstr));
- if (!pplstr.empty()) {
- std::istringstream iss(pplstr);
- std::string part;
- getline(iss, part, '.');
- var->memberList.resize(var->memberList.size() + 1);
- size_t j = 0;
- while (getline(iss, part, '$')) {
- if (part.empty())
- part = "void";
- var->memberList[i].push_back(part);
- ++j;
- }
- if (j < var->valuesList.size()) {
- auto v_size = var->valuesList.size();
- for (; j <= v_size;) {
- ++j;
- var->memberList[i].push_back("void");
- }
- }
- }
- ++i;
- }
- }
- printError("File " + currentMem + " loaded.\n");
- }
- else
- var->people = 0;
- VarFile.close();
- }
- bool Files::clear()
- {
- bool finished = false;
- std::fstream VarFile;
- VarFile.open(currentMem, std::ios::trunc | std::ios::in | std::ios::out);
- if (!VarFile.good())
- printError("Error occurred while clearing file: File could not be opened.\n");
- else
- finished = true;
- VarFile.close();
- VarFile.open(currentVal, std::ios::trunc | std::ios::in | std::ios::out);
- if (!VarFile.good()) {
- printError("Error occurred while clearing file: File could not be opened.\n");
- finished = false;
- }
- else {
- if (VarFile << "0;") {}
- else std::cerr << ERROR_FILE_INPUT;
- if (VarFile << "10;") {}
- else std::cerr << ERROR_FILE_INPUT;
- if (VarFile << "0;\n") {}
- else std::cerr << ERROR_FILE_INPUT;
- finished = true;
- }
- VarFile.close();
- return finished;
- }
- void Files::update_config() {
- printError("Updating main config file..");
- std::ofstream{ "TEMPConfig.config" };
- std::fstream ConfigFile;
- ConfigFile.open("TEMPConfig.config");
- if (ConfigFile.good()) {
- if (ConfigFile << "#If you edit the filepath remember to move files to the new location, otherwise they wont be loaded\n\n") {}
- else std::cerr << "(C) Error while trying to access config file - restart the program and configure again\n";
- if (ConfigFile << "Filepath: " << var->current_path << ";\n") {}
- else std::cerr << "(1) Error while trying to access config file - restart the program and configure again\n";
- if (ConfigFile << "\n#Do not edit this list on your own - each name is associated with files! Standard format: name1$name2$\n\n") {}
- else std::cerr << "(C) Error while trying to access config file - restart the program and configure again\n";
- std::string data_list;
- for (auto data1 : var->databaseList)
- data_list += data1 + "$";
- if (ConfigFile << "Databases: " << data_list << ";\n") {}
- else std::cerr << "(2) Error while trying to access config file - restart the program and configure again\n";
- }
- else
- printError("Main .config file could not be edited!\n");
- ConfigFile.close();
- if (fs::exists(mainCONFIG))
- fs::remove(mainCONFIG);
- fs::rename("TEMPConfig.config", mainCONFIG);
- }
- void Files::update_log() {
- if (var->Log_save && var->current != "DELETED_DATABASE") {
- printError("Generating log file..\n");
- const std::string LOG_FILE = var->current_path + R"(\)" + var->current + R"(\)" + var->current + "_latest.log";
- var->logstream << "Program properly closed.";
- if (fs::exists(LOG_FILE))
- fs::remove(LOG_FILE);
- if (std::ofstream{ LOG_FILE }) {
- std::fstream ConfigFile;
- ConfigFile.open(LOG_FILE);
- if (ConfigFile.good()) {
- std::string log_row;
- bool done = true;
- while (getline(var->logstream, log_row)) {
- log_row.erase(std::remove(begin(log_row), end(log_row), '\n'), end(log_row));
- if (ConfigFile << log_row << "\n") {}
- else done = false;
- }
- if (!done)
- std::cerr << "Updating log file failed.\n";
- }
- else
- std::cerr << "Log file could not be edited.\n";
- }
- else
- std::cerr << "Log file could not be created.\n";
- }
- }
- void Files::readConfig(std::string &file_content, const std::string &SearchFor, std::fstream &VarFile) {
- bool found = false;
- while (!found && getline(VarFile, file_content, '\n')) {
- if (!file_content.empty() && file_content != "\n") {
- if (file_content[0] != '#') {
- std::istringstream row(file_content);
- getline(row, file_content, ':');
- if (file_content == SearchFor) {
- getline(row, file_content, ';');
- file_content.erase(std::remove(begin(file_content), end(file_content), '\n'), end(file_content));
- file_content.erase(std::remove(begin(file_content), end(file_content), ' '), end(file_content));
- found = true;
- }
- }
- }
- }
- }
- void Files::load_path() {
- if (fs::exists(mainCONFIG)) {
- std::string filepath;
- std::fstream VarFile;
- VarFile.open(mainCONFIG);
- readConfig(filepath, "Filepath", VarFile);
- var->current_path = filepath;
- printError("Filepath loaded.\n");
- }
- else
- printError("Loading path failed - config file not found.\n");
- }
- void Files::load_config() {
- printError("Loading " + var->current + " config file..\n");
- std::string data_config = var->current_path + R"(/)" + var->current + R"(/)" + var->current + ".config";
- if (fs::exists(data_config)) {
- std::string file_content;
- std::fstream ConfigFile;
- ConfigFile.open(data_config);
- readConfig(file_content, "MySQL", ConfigFile);
- if (file_content == "true")
- var->MySQL = true;
- else
- var->MySQL = false;
- readConfig(file_content, "Files", ConfigFile);
- if (file_content == "true")
- var->SaveFile = true;
- else
- var->SaveFile = false;
- readConfig(file_content, "Log", ConfigFile);
- if (file_content == "true")
- var->Log_save = true;
- else
- var->Log_save = false;
- ConfigFile.close();
- }
- else {
- std::ofstream{ data_config };
- std::fstream ConfigFile;
- ConfigFile.open(data_config);
- if (ConfigFile.good()) {
- if (ConfigFile << "#Use '#' at the beginning of each comment line\n") {}
- else std::cerr << ERROR_CONFIG;
- if (ConfigFile << "#Use ';' at the end of each line, except comments\n\n") {}
- else std::cerr << ERROR_CONFIG;
- if (ConfigFile << "#Do not change type of database on your own - create a new database instead\n\n") {}
- else std::cerr << ERROR_CONFIG;
- std::string file_content;
- if (var->MySQL) file_content = "true"; else file_content = "false";
- if (ConfigFile << "MySQL: " << file_content << ";\n") {}
- else std::cerr << ERROR_CONFIG;
- if (var->SaveFile) file_content = "true"; else file_content = "false";
- if (ConfigFile << "Files: " << file_content << ";\n") {}
- if (ConfigFile << "\n#Generate the log from latest program run\n\n") {}
- else std::cerr << ERROR_CONFIG;
- if (ConfigFile << "Log: true;\n") {}
- else std::cerr << ERROR_CONFIG;
- if (ConfigFile << "\n#Filesystem settings\n") {}
- else std::cerr << ERROR_CONFIG;
- if (ConfigFile << "#NameFormat - members' names will look like this: John instead of jOhn\n\n") {}
- else std::cerr << ERROR_CONFIG;
- if (ConfigFile << "NameFormat: true;\n") {}
- else std::cerr << ERROR_CONFIG;
- }
- else
- printError("\nConfig file for " + var->current + " could not be created. Data wont be saved.\n");
- ConfigFile.close();
- }
- if (!var->MySQL && !var->SaveFile) {
- printError("\n\nWARNING: DATA SAVING HAS BEEN DISABLED. CREATE NEW DATABASE.\n\n\n");
- }
- }
- void Files::set_path(const std::string &path)
- {
- if (fs::exists(path)) {
- printError("File path has been changed to " + path + ".\n");
- var->current_path += path + R"(/FileDatabase)";
- fs::create_directory(var->current_path);
- }
- else
- printError("Invalid filepath - filepath not changed.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement