diff --git a/GDGameSave.hpp b/GDGameSave.hpp index 6d1a230..f5ba638 100644 --- a/GDGameSave.hpp +++ b/GDGameSave.hpp @@ -7,6 +7,15 @@ constexpr int CHUNK = 16384; constexpr int WINDOW_BITS = 15; +//Returns true if it was able to open the given file for reading +bool IsFile(const std::string& filename) +{ + std::ifstream file(filename); + const bool ret = file.good(); + file.close(); + return ret; +} + //Reads the contents of a given file into a string std::string FileToStr(const std::string& filename) { std::ifstream file(filename, std::ios::binary); @@ -18,11 +27,14 @@ std::string FileToStr(const std::string& filename) { } //Writes the contents of a string to the given file -void StrToFile(const std::string& filename, const std::string& data) { - std::ofstream ofile; - ofile.open(filename); +// returns false if it was unable to open the file +bool StrToFile(const std::string& filename, const std::string& data) { + std::ofstream ofile(filename, std::ios::binary); + if(!ofile.good()) + return false; ofile << data; ofile.close(); + return true; } //XORs each byte of a string reference diff --git a/Main.cpp b/Main.cpp index 66fa021..859d0b7 100755 --- a/Main.cpp +++ b/Main.cpp @@ -20,41 +20,80 @@ const std::string commands = R"(Commands: 3: Encrypt and save to GD folder 0: Quit)"; +enum class Command +{ + Decrypt, + Encrypt +}; + +bool ExecCommand(Command cmd, const std::string& in_filename, const std::string& out_filename) +{ + if(!IsFile(in_filename)) + { + std::cerr << "ERROR: Could not open file " << in_filename << " for reading" << std::endl; + return false; + } + switch(cmd) + { + case Command::Decrypt: + std::cout << "Decrypting " << in_filename << "..." << std::endl; + if(!StrToFile(out_filename, Decrypt(in_filename))) + { + std::cerr << "ERROR: Could not open file " << out_filename << " for writing" << std::endl; + return false; + } + break; + case Command::Encrypt: + std::cout << "Encrypting " << in_filename << "..." << std::endl; + if(!StrToFile(out_filename, Encrypt(in_filename))) + { + std::cerr << "ERROR: Could not open file " << out_filename << " for writing" << std::endl; + return false; + } + } + return true; +} + int main(int argc, const char** argv) { std::vector args; for(int i = 0; i < argc; i++) args.push_back(argv[i]); - bool quit = false; std::string cmd; - while(!quit) + while(true) { std::cout << commands << std::endl; std::cout << ">> "; std::cin >> cmd; if(cmd == "1") { - std::cout << "Decrypting..." << std::endl; - const std::string decrypted = Decrypt(gdLocalFolder + saves[0]); - StrToFile(saves[0] + ".xml", decrypted); + for(unsigned int i = 0; i < 2; i++) + { + if(!ExecCommand(Command::Decrypt, gdLocalFolder + saves[i], saves[i] + ".xml")) + continue; + } } else if(cmd == "2") { - std::cout << "Encrypting..." << std::endl; - const std::string encrypted = Encrypt(saves[0] + ".xml"); - StrToFile(saves[0], encrypted); + for(unsigned int i = 0; i < 2; i++) + { + if(!ExecCommand(Command::Encrypt, saves[i] + ".xml", saves[i])) + continue; + } } else if(cmd == "3") { - std::cout << "Encrypting and saving to GD folder..." << std::endl; - const std::string encrypted = Encrypt(saves[0] + ".xml"); - StrToFile(gdLocalFolder + saves[0], encrypted); + for(unsigned int i = 0; i < 2; i++) + { + if(!ExecCommand(Command::Encrypt, saves[i] + ".xml", gdLocalFolder + saves[i])) + continue; + } } else if(cmd == "0") { std::cout << "Quitting" << std::endl; - quit = true; + break; } else std::cerr << "Invalid command" << std::endl;