mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-06 18:33:58 -05:00
Updated for Viginere and moving flag functions to new file
This commit is contained in:
196
helperFunctions.hpp
Normal file
196
helperFunctions.hpp
Normal file
@@ -0,0 +1,196 @@
|
||||
//Ciphers/helperFunctions.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 4-29-18
|
||||
//Modified: 4-29-18
|
||||
//This file contains the driver function and the test functions for the Cipher program
|
||||
//This program will use some simple ciphers that are no longer widely used but still fun to play with
|
||||
|
||||
|
||||
|
||||
#ifndef HELPER_FUNCTIONS_HPP
|
||||
#define HELPER_FUNCTIONS_HPP
|
||||
|
||||
|
||||
#include "Headers/Caesar.hpp"
|
||||
#include "Headers/Playfair.hpp"
|
||||
#include "Headers/Vigenere.hpp"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
|
||||
|
||||
enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, NUM_CIPHERS,
|
||||
ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE };
|
||||
std::string flagStrings[SIZE];
|
||||
//Error handling
|
||||
bool failFlag = false; //Set if something fails
|
||||
//The functions that will run the ciphers
|
||||
std::string runCaesar(std::ifstream& infile, bool encode);
|
||||
std::string runPlayfair(std::ifstream& infile, bool encode);
|
||||
std::string runVigenere(std::ifstream& infile, bool encode);
|
||||
|
||||
|
||||
|
||||
void getFlags(int argc, char** argv, bool cipherFlags[], std::string& inputFileString, std::string& outputFileString){
|
||||
//Cycle through every argument
|
||||
bool valid = false;
|
||||
for(int cnt = 1;cnt < argc;++cnt){
|
||||
//Step through every element in the flagStrings and compare the flags. If the flags do not match or you go out of bounds in argv throw an error
|
||||
for(int stringcnt = 0;stringcnt < SIZE;++stringcnt){
|
||||
if(argv[cnt] == flagStrings[stringcnt]){
|
||||
valid = cipherFlags[stringcnt] = true;
|
||||
//If the flag showed an input file get the file name if possible
|
||||
if(stringcnt == INPUT_FILE){
|
||||
//Make sure there is enough room in argv for the file name
|
||||
if((cnt + 1) < argc){
|
||||
inputFileString = argv[++cnt];
|
||||
}
|
||||
}
|
||||
//If the flag showed an output file get the file name if possible
|
||||
else if(stringcnt == OUTPUT_FILE){
|
||||
//Make sure there is enough room in argv for the file name
|
||||
if((cnt + 1) < argc){
|
||||
outputFileString = argv[++cnt];
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
//If the flag was not valid throw an error
|
||||
if(!valid){
|
||||
std::cout << argv[cnt] << " is an invalid flag" << std::endl;
|
||||
return;
|
||||
}
|
||||
valid = false;
|
||||
}
|
||||
}
|
||||
|
||||
bool checkFlags(bool flags[]){
|
||||
int counter = 0;
|
||||
bool correct = false;
|
||||
//Run through each flag and counter how many are true
|
||||
for(int cnt = 0;cnt < NUM_CIPHERS;++cnt){
|
||||
if(flags[cnt]){
|
||||
++counter;
|
||||
}
|
||||
}
|
||||
|
||||
//Check if a valid number of flags were true
|
||||
if(counter == 1){
|
||||
correct = true;
|
||||
}
|
||||
|
||||
//Check if either encode or decode is set, but not both
|
||||
if((flags[ENCODE] && flags[DECODE]) || (!flags[ENCODE] && !flags[DECODE])){
|
||||
correct = false;
|
||||
}
|
||||
|
||||
return correct;
|
||||
}
|
||||
|
||||
void setFlagStrings(){
|
||||
flagStrings[CAESAR] = "-c";
|
||||
flagStrings[PLAYFAIR] = "-p";
|
||||
flagStrings[VIGENERE] = "-v";
|
||||
flagStrings[ENCODE] = "-e";
|
||||
flagStrings[DECODE] = "-d";
|
||||
flagStrings[INPUT_FILE] = "-i";
|
||||
flagStrings[OUTPUT_FILE] = "-o";
|
||||
}
|
||||
|
||||
std::string runCaesar(std::ifstream& infile, bool encode){
|
||||
std::string inputString, cipherString;
|
||||
int offset;
|
||||
Caesar cipher;
|
||||
//Check if the input file is open
|
||||
if(infile.is_open()){
|
||||
infile >> offset;
|
||||
infile.ignore(10000, '\n'); //Get rid of the \n that is right after the offset
|
||||
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 cipher offset: ";
|
||||
std::cin >> offset;
|
||||
std::cout << "Enter the input string: ";
|
||||
std::getline(std::cin, inputString);
|
||||
}
|
||||
|
||||
//Run the correct cipher
|
||||
if(encode){
|
||||
cipherString = cipher.encode(offset, inputString);
|
||||
}
|
||||
else{
|
||||
cipherString = cipher.decode(offset, inputString);
|
||||
}
|
||||
|
||||
return cipherString;
|
||||
}
|
||||
|
||||
std::string runPlayfair(std::ifstream& infile, bool encode){
|
||||
std::string keyword, inputString, cipherString;
|
||||
Playfair cipher;
|
||||
//Check if the input file is open
|
||||
if(infile.is_open()){
|
||||
std::getline(infile, keyword);
|
||||
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 keyword: ";
|
||||
std::getline(std::cin, keyword);
|
||||
std::cout << "Enter the input string: ";
|
||||
std::getline(std::cin, inputString);
|
||||
}
|
||||
|
||||
//Run the correct cipher
|
||||
if(encode){
|
||||
cipherString = cipher.encode(keyword, inputString);
|
||||
}
|
||||
else{
|
||||
cipherString = cipher.decode(keyword, inputString);
|
||||
}
|
||||
|
||||
return cipherString;
|
||||
}
|
||||
|
||||
std::string runVigenere(std::ifstream& infile, bool encode){
|
||||
std::string keyword, inputString, cipherString;
|
||||
Vigenere cipher;
|
||||
//Check if the input file is open
|
||||
if(infile.is_open()){
|
||||
std::getline(infile, keyword);
|
||||
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 keyword: ";
|
||||
std::getline(std::cin, keyword);
|
||||
std::cout << "Enter the input string: ";
|
||||
std::getline(std::cin, inputString);
|
||||
}
|
||||
|
||||
//Run the correct cipher
|
||||
if(encode){
|
||||
cipherString = cipher.encode(keyword, inputString);
|
||||
}
|
||||
else{
|
||||
cipherString = cipher.decode(keyword, inputString);
|
||||
}
|
||||
|
||||
return cipherString;
|
||||
}
|
||||
|
||||
#endif //HELPER_FUNCTIONS_HPP
|
||||
Reference in New Issue
Block a user