diff --git a/helperFunctions.hpp b/helperFunctions.hpp index 8acd6ee..bdb5b0a 100644 --- a/helperFunctions.hpp +++ b/helperFunctions.hpp @@ -19,7 +19,7 @@ #include -enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, NUM_CIPHERS, +enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, ATBASH, NUM_CIPHERS, ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE }; std::string flagStrings[SIZE]; //Error handling @@ -29,6 +29,9 @@ std::string runCaesar(std::ifstream& infile, bool encode); std::string runPlayfair(std::ifstream& infile, bool encode); std::string runVigenere(std::ifstream& infile, bool encode); +//For the getCipher function +typedef std::string (*Fn)(std::ifstream&, bool); + void getFlags(int argc, char** argv, bool cipherFlags[], std::string& inputFileString, std::string& outputFileString){ @@ -92,12 +95,34 @@ void setFlagStrings(){ flagStrings[CAESAR] = "-c"; flagStrings[PLAYFAIR] = "-p"; flagStrings[VIGENERE] = "-v"; + flagStrings[ATBASH] = "-a"; flagStrings[ENCODE] = "-e"; flagStrings[DECODE] = "-d"; flagStrings[INPUT_FILE] = "-i"; flagStrings[OUTPUT_FILE] = "-o"; } +//Returns the correct function for the flags that are set +Fn getCipher(const bool cipherFlags[]){ + if(cipherFlags[CAESAR]){ + return runCaesar; + } + else if(cipherFlags[PLAYFAIR]){ + return runPlayfair; + } + else if(cipherFlags[VIGENERE]){ + return runVigenere; + } + else if(cipherFlags[ATBASH]){ + return runAtbash; + } + //If it didn't trip one of the flags, there was an error before this + else{ + std::cout << "There was an error selecting the appropriate function"; + exit(0); + } +} + std::string runCaesar(std::ifstream& infile, bool encode){ std::string inputString, cipherString; int offset; @@ -193,4 +218,32 @@ std::string runVigenere(std::ifstream& infile, bool encode){ return cipherString; } +std::string runAtbash(std::ifstream& infile, bool encode){ + std::string inputString, cipherString; + Atbash cipher; + //Check if the input file is open + if(infile.is_open()){ + std::getline(infile, inputString); + if(infile.fail()){ + failFlag = true; + cipherString = "Input file has an incorrect format\n"; + } + } + //Otherwise prompt for the appropriate strings + else{ + std::cout << "Enter the input string: "; + std::getline(std::cin, inputString); + } + + //Run the correct cipher + if(encode){ + cipherString = cipher.encode(inputString); + } + else{ + cipherString = cipher.decode(inputString); + } + + return cipherString; +} + #endif //HELPER_FUNCTIONS_HPP \ No newline at end of file