Advertisement
Guest User

Untitled

a guest
Aug 29th, 2018
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.48 KB | None | 0 0
  1. #include "Files.h"
  2. #include <fstream>
  3. #include <filesystem>
  4. #include <string>
  5.  
  6. namespace fs = std::experimental::filesystem::v1;
  7.  
  8. void Files::update()
  9. {
  10.     std::fstream VarFile;
  11.     printError("Saving data to files..\n");
  12.     if (current != "DELETED_DATABASE") {  // FOR MERGING
  13.         std::ofstream{ tempValuesFile };
  14.         VarFile.open(tempValuesFile);
  15.         if (VarFile.good()) {
  16.             if (VarFile << people << ";") {}
  17.             else std::cerr << ERROR_FILE_INPUT;
  18.             if (VarFile << reservedPeople << ";") {}
  19.             else  std::cerr << ERROR_FILE_INPUT;
  20.             if (valuesList.size() > 0) {
  21.                 if (VarFile << valuesList.size() << ";\n") {}
  22.                 else std::cerr << ERROR_FILE_INPUT;
  23.                 for (const auto &i : valuesList) {
  24.                     if (VarFile << i << "$") {}
  25.                     else std::cerr << ERROR_FILE_INPUT;
  26.                 }
  27.             }
  28.             else
  29.                 VarFile << 0 << ";\n";
  30.             if (fs::exists(currentVal))
  31.                 fs::remove(currentVal);
  32.         }
  33.         else
  34.             printError("Error: File " + current + "_Values.txt" + " could not be opened\n");
  35.         VarFile.close();
  36.         fs::rename(tempValuesFile, currentVal);
  37.         std::ofstream{ tempMembersFile };
  38.         VarFile.open(tempMembersFile);
  39.         if (VarFile.good()) {
  40.             for (size_t i = 0; i < people && people > 0; ++i) {
  41.                 if (VarFile << i + 1 << "." << memberList[i][0]) {}
  42.                 else std::cerr << ERROR_FILE_INPUT;
  43.                 auto v_size = valuesList.size();
  44.                 for (size_t j = 1; j <= v_size && v_size > 0; ++j) {
  45.                     if (memberList[i].size() > j) {
  46.                         if (memberList[i][j].empty())
  47.                             memberList[i][j] = "void";
  48.                     }
  49.                     else
  50.                         memberList[i].push_back("void");
  51.                     if (VarFile << "$" << memberList[i][j]) {}
  52.                     else std::cerr << ERROR_FILE_INPUT;
  53.                 }
  54.                 if (VarFile << ";\n") {}
  55.                 else std::cout << ERROR_FILE_INPUT;
  56.             }
  57.             if (fs::exists(currentMem))
  58.                 fs::remove(currentMem);
  59.         }
  60.         else
  61.             printError("Error: File" + current + "_Members.txt" + " could not be opened\n");
  62.         VarFile.close();
  63.         fs::rename(tempMembersFile, currentMem);
  64.     }
  65. }
  66. void Files::open()
  67. {
  68.     std::string pplstr, valstr;
  69.     // VALUES LOADING
  70.     std::fstream VarFile;
  71.     VarFile.open(tempPPLFile, std::ios::in | std::ios::out);
  72.     if (VarFile.good()) {  // AFTER MERGING
  73.         getline(VarFile, valstr, ';');
  74.         people = std::stoi(valstr);
  75.     }
  76.     VarFile.close();
  77.     fs::remove(tempPPLFile);
  78.     const int standardSize = 10;
  79.     VarFile.open(currentVal, std::ios::in | std::ios::out);
  80.     if (VarFile.good()) {
  81.         getline(VarFile, valstr, ';');
  82.         if (!valstr.empty() && people == 0)
  83.             people = catchStoi(valstr);
  84.         getline(VarFile, valstr, ';');
  85.         if (!valstr.empty())
  86.             reservedPeople = catchStoi(valstr);
  87.         if (reservedPeople == 0)
  88.             reservedPeople = standardSize;
  89.         memberList.reserve(reservedPeople);
  90.         getline(VarFile, valstr, ';'); // skip values size (only for merging)
  91.         while (getline(VarFile, valstr, '$')) {
  92.             valstr.erase(std::remove(begin(valstr), end(valstr), '\n'), end(valstr));
  93.             if (!valstr.empty())
  94.                 valuesList.push_back(valstr);
  95.         }
  96.         printError("File " + currentVal + " loaded.\n");
  97.     }
  98.     else
  99.         reservedPeople = standardSize;
  100.     VarFile.close();
  101.     // MEMBER LOADING
  102.     currentMem = current_path + R"(/)" + current + R"(/)" + current + "_Members.txt";
  103.     VarFile.open(currentMem, std::ios::in | std::ios::out);
  104.     if (VarFile.good()) {
  105.         if (people > 0) {
  106.             int i = 0;
  107.             while (getline(VarFile, pplstr, ';') && !pplstr.empty()) {
  108.                 pplstr.erase(std::remove(begin(pplstr), end(pplstr), '\n'), end(pplstr));
  109.                 if (!pplstr.empty()) {
  110.                     std::istringstream iss(pplstr);
  111.                     std::string part;
  112.                     getline(iss, part, '.');
  113.                     memberList.resize(memberList.size() + 1);
  114.                     size_t j = 0;
  115.                     while (getline(iss, part, '$')) {
  116.                         if (part.empty())
  117.                             part = "void";
  118.                         memberList[i].push_back(part);
  119.                         ++j;
  120.                     }
  121.                     if (j < valuesList.size()) {
  122.                         auto v_size = valuesList.size();
  123.                         for (; j <= v_size;) {
  124.                             ++j;
  125.                             memberList[i].push_back("void");
  126.                         }
  127.                     }
  128.                 }
  129.                 ++i;
  130.             }
  131.         }
  132.         printError("File " + currentMem + " loaded.\n");
  133.     }
  134.     else
  135.         people = 0;
  136.     VarFile.close();
  137. }
  138. bool Files::clear()
  139. {
  140.     bool finished = false;
  141.     std::fstream VarFile;
  142.     VarFile.open(currentMem, std::ios::trunc | std::ios::in | std::ios::out);
  143.     if (!VarFile.good())
  144.         printError("Error occurred while clearing file: File could not be opened.\n");
  145.     else
  146.         finished = true;
  147.     VarFile.close();
  148.     VarFile.open(currentVal, std::ios::trunc | std::ios::in | std::ios::out);
  149.     if (!VarFile.good()) {
  150.         printError("Error occurred while clearing file: File could not be opened.\n");
  151.         finished = false;
  152.     }
  153.     else {
  154.         if (VarFile << "0;") {}
  155.         else std::cerr << ERROR_FILE_INPUT;
  156.         if (VarFile << "10;") {}
  157.         else std::cerr << ERROR_FILE_INPUT;
  158.         if (VarFile << "0;\n") {}
  159.         else std::cerr << ERROR_FILE_INPUT;
  160.         finished = true;
  161.     }
  162.     VarFile.close();
  163.     return finished;
  164. }
  165. void Files::update_config() {
  166.     printError("Updating main config file..");
  167.     std::ofstream{ "TEMPConfig.config" };
  168.     std::fstream ConfigFile;
  169.     ConfigFile.open("TEMPConfig.config");
  170.     if (ConfigFile.good()) {
  171.         if (ConfigFile << "#If you edit the filepath remember to move files to the new location, otherwise they wont be loaded\n\n") {}
  172.         else std::cerr << "(C) Error while trying to access config file - restart the program and configure again\n";
  173.         if (ConfigFile << "Filepath: " << current_path << ";\n") {}
  174.         else std::cerr << "(1) Error while trying to access config file - restart the program and configure again\n";
  175.         if (ConfigFile << "\n#Do not edit this list on your own - each name is associated with files! Standard format: name1$name2$\n\n") {}
  176.         else std::cerr << "(C) Error while trying to access config file - restart the program and configure again\n";
  177.         std::string data_list;
  178.         for (auto data1 : databaseList)
  179.             data_list += data1 + "$";
  180.         if (ConfigFile << "Databases: " << data_list << ";\n") {}
  181.         else std::cerr << "(2) Error while trying to access config file - restart the program and configure again\n";
  182.     }
  183.     else
  184.         printError("Main .config file could not be edited!\n");
  185.     ConfigFile.close();
  186.     if (fs::exists(mainCONFIG))
  187.         fs::remove(mainCONFIG);
  188.     fs::rename("TEMPConfig.config", mainCONFIG);
  189. }
  190. void Files::update_log() {
  191.     if (Log_save && current != "DELETED_DATABASE") {
  192.         printError("Generating log file..\n");
  193.         const std::string LOG_FILE = current_path + R"(\)" + current + R"(\)" + current + "_latest.log";
  194.         logstream << "Program properly closed.";
  195.         if (fs::exists(LOG_FILE))
  196.             fs::remove(LOG_FILE);
  197.         if (std::ofstream{ LOG_FILE }) {
  198.             std::fstream ConfigFile;
  199.             ConfigFile.open(LOG_FILE);
  200.             if (ConfigFile.good()) {
  201.                 std::string log_row;
  202.                 bool done = true;
  203.                 while (getline(logstream, log_row)) {
  204.                     log_row.erase(std::remove(begin(log_row), end(log_row), '\n'), end(log_row));
  205.                     if (ConfigFile << log_row << "\n") {}
  206.                     else done = false;
  207.                 }
  208.                 if (!done)
  209.                     std::cerr << "Updating log file failed.\n";
  210.             }
  211.             else
  212.                 std::cerr << "Log file could not be edited.\n";
  213.         }
  214.         else
  215.             std::cerr << "Log file could not be created.\n";
  216.     }
  217. }
  218. void Files::readConfig(std::string &file_content, const std::string &SearchFor, std::fstream &VarFile) {
  219.     bool found = false;
  220.     while (!found && getline(VarFile, file_content, '\n')) {
  221.         if (!file_content.empty() && file_content != "\n") {
  222.             if (file_content[0] != '#') {
  223.                 std::istringstream row(file_content);
  224.                 getline(row, file_content, ':');
  225.                 if (file_content == SearchFor) {
  226.                     getline(row, file_content, ';');
  227.                     file_content.erase(std::remove(begin(file_content), end(file_content), '\n'), end(file_content));
  228.                     file_content.erase(std::remove(begin(file_content), end(file_content), ' '), end(file_content));
  229.                     found = true;
  230.                 }
  231.             }
  232.         }
  233.     }
  234. }
  235. void Files::load_path() {
  236.     if (fs::exists(mainCONFIG)) {
  237.         std::string filepath;
  238.         std::fstream VarFile;
  239.         VarFile.open(mainCONFIG);
  240.         readConfig(filepath, "Filepath", VarFile);
  241.         current_path = filepath;
  242.         printError("Filepath loaded.\n");
  243.     }
  244.     else
  245.         printError("Loading path failed - config file not found.\n");
  246. }
  247. void Files::load_config() {
  248.     printError("Loading " + current + " config file..\n");
  249.     std::string data_config = current_path + R"(/)" + current + R"(/)" + current + ".config";
  250.     if (fs::exists(data_config)) {
  251.         std::string file_content;
  252.         std::fstream ConfigFile;
  253.         ConfigFile.open(data_config);
  254.         readConfig(file_content, "MySQL", ConfigFile);
  255.         if (file_content == "true")
  256.             MySQL = true;
  257.         else
  258.             MySQL = false;
  259.         readConfig(file_content, "Files", ConfigFile);
  260.         if (file_content == "true")
  261.             SaveFile = true;
  262.         else
  263.             SaveFile = false;
  264.         readConfig(file_content, "Log", ConfigFile);
  265.         if (file_content == "true")
  266.             Log_save = true;
  267.         else
  268.             Log_save = false;
  269.         ConfigFile.close();
  270.     }
  271.     else {
  272.         std::ofstream{ data_config };
  273.         std::fstream ConfigFile;
  274.         ConfigFile.open(data_config);
  275.         if (ConfigFile.good()) {
  276.             if (ConfigFile << "#Use '#' at the beginning of each comment line\n") {}
  277.             else std::cerr << ERROR_CONFIG;
  278.             if (ConfigFile << "#Use ';' at the end of each line, except comments\n\n") {}
  279.             else std::cerr << ERROR_CONFIG;
  280.             if (ConfigFile << "#Do not change type of database on your own - create a new database instead\n\n") {}
  281.             else std::cerr << ERROR_CONFIG;
  282.             std::string file_content;
  283.             if (MySQL) file_content = "true"; else file_content = "false";
  284.             if (ConfigFile << "MySQL: " << file_content << ";\n") {}
  285.             else std::cerr << ERROR_CONFIG;
  286.             if (SaveFile) file_content = "true"; else file_content = "false";
  287.             if (ConfigFile << "Files: " << file_content << ";\n") {}
  288.             if (ConfigFile << "\n#Generate the log from latest program run\n\n") {}
  289.             else std::cerr << ERROR_CONFIG;
  290.             if (ConfigFile << "Log: true;\n") {}
  291.             else std::cerr << ERROR_CONFIG;
  292.             if (ConfigFile << "\n#Filesystem settings\n") {}
  293.             else std::cerr << ERROR_CONFIG;
  294.             if (ConfigFile << "#NameFormat - members' names will look like this: John instead of jOhn\n\n") {}
  295.             else std::cerr << ERROR_CONFIG;
  296.             if (ConfigFile << "NameFormat: true;\n") {}
  297.             else std::cerr << ERROR_CONFIG;
  298.         }
  299.         else
  300.             printError("\nConfig file for " + current + " could not be created. Data wont be saved.\n");
  301.         ConfigFile.close();
  302.     }
  303.     if (!MySQL && !SaveFile) {
  304.         printError("\n\nWARNING: DATA SAVING HAS BEEN DISABLED. CREATE NEW DATABASE.\n\n\n");
  305.     }
  306. }
  307. void Files::set_path(const std::string &path)
  308. {
  309.     if (fs::exists(path)) {
  310.         printError("File path has been changed to " + path + ".\n");
  311.         current_path += path + R"(/FileDatabase)";
  312.         fs::create_directory(current_path);
  313.     }
  314.     else
  315.         printError("Invalid filepath - filepath not changed.\n");
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement