Moved the test main function to a new file for neatness

This commit is contained in:
2018-04-30 12:19:44 -04:00
parent 59759f69bb
commit 1410d9a985

View File

@@ -19,7 +19,7 @@
#include <string> #include <string>
enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, NUM_CIPHERS, enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, ATBASH, NUM_CIPHERS,
ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE }; ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE };
std::string flagStrings[SIZE]; std::string flagStrings[SIZE];
//Error handling //Error handling
@@ -29,6 +29,9 @@ std::string runCaesar(std::ifstream& infile, bool encode);
std::string runPlayfair(std::ifstream& infile, bool encode); std::string runPlayfair(std::ifstream& infile, bool encode);
std::string runVigenere(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){ void getFlags(int argc, char** argv, bool cipherFlags[], std::string& inputFileString, std::string& outputFileString){
@@ -92,12 +95,34 @@ void setFlagStrings(){
flagStrings[CAESAR] = "-c"; flagStrings[CAESAR] = "-c";
flagStrings[PLAYFAIR] = "-p"; flagStrings[PLAYFAIR] = "-p";
flagStrings[VIGENERE] = "-v"; flagStrings[VIGENERE] = "-v";
flagStrings[ATBASH] = "-a";
flagStrings[ENCODE] = "-e"; flagStrings[ENCODE] = "-e";
flagStrings[DECODE] = "-d"; flagStrings[DECODE] = "-d";
flagStrings[INPUT_FILE] = "-i"; flagStrings[INPUT_FILE] = "-i";
flagStrings[OUTPUT_FILE] = "-o"; 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 runCaesar(std::ifstream& infile, bool encode){
std::string inputString, cipherString; std::string inputString, cipherString;
int offset; int offset;
@@ -193,4 +218,32 @@ std::string runVigenere(std::ifstream& infile, bool encode){
return cipherString; 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 #endif //HELPER_FUNCTIONS_HPP