Added propper error handling to main function

This commit is contained in:
2019-03-07 12:07:53 -05:00
parent 2d6605127c
commit 22bcb3eafd
3 changed files with 90 additions and 48 deletions

View File

@@ -1,11 +1,11 @@
//Ciphers/helperFunctions.hpp
//Matthew Ellison
// Created: 4-29-18
//Modified: 5-8-18
// Created: 04-29-18
//Modified: 03-07-19
//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
/*
Copyright (C) 2018 Matthew Ellison
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -38,6 +38,34 @@
#include <vector>
//Error handling classes
//Thrown if there is a file flag that has no corresponding file name
class noFileName{
private:
std::string type;
public:
noFileName(std::string type): type(type) {}
std::string getType(){ return type; }
};
//Thrown if there is an invalid flag given
class invalidFlag{
private:
std::string flag;
public:
invalidFlag(std::string flag): flag(flag) {}
std::string getFlag(){ return flag; }
};
//Thrown if there is an invalid combination of flags given
class invalidCombo{
private:
std::string type;
public:
invalidCombo(std::string type): type(type) {}
std::string getType(){ return type; }
};
//Holds the location of the possible cipher flags for both the string and boolean arrays
enum CipherFlagLocation { CAESAR, PLAYFAIR, VIGENERE, ATBASH, MORSE, AUTOKEY, NUM_CIPHERS,
ENCODE, DECODE, INPUT_FILE, OUTPUT_FILE, SIZE };
std::string flagStrings[SIZE];
@@ -101,7 +129,7 @@ std::vector<std::string> flagsToStrings(int argc, char** argv, std::string& inpu
inputFileName = argv[cnt];
}
else{
///Throw an error
throw noFileName("Input");
}
}
//Get the output file name if necessary
@@ -111,7 +139,7 @@ std::vector<std::string> flagsToStrings(int argc, char** argv, std::string& inpu
outputFileName = argv[cnt];
}
else{
///Throw an error
throw noFileName("Output");
}
}
}
@@ -138,7 +166,40 @@ void setArgs(std::string arg, bool cipherFlags[]){
return;
}
}
///Throw an error if the current flag was not found
throw invalidFlag(arg);
}
/**
* @brief Checks that the flags given are valid
*
* @param flags All of the flags that the program can have
* @return true If valid options were chosen
* @return false If invalid options were chosen
*/
void checkFlags(bool flags[]){
int counter = 0;
//Run through each flag and counter how many are true
for(int cnt = CAESAR;cnt < NUM_CIPHERS;++cnt){
if(flags[cnt]){
++counter;
}
}
//Check if a valid number of ciphers were given. (Only valid answer is 1)
if(counter > 1){
throw invalidCombo("too many ciphers are given");
}
else if(counter < 1){
throw invalidCombo("no cipher is given");
}
//Check if either encode or decode is set, but not both
if(flags[CipherFlagLocation::ENCODE] && flags[CipherFlagLocation::DECODE]){
throw invalidCombo("both encode and decode are set");
}
else if(!flags[CipherFlagLocation::ENCODE] && !flags[CipherFlagLocation::DECODE]){
throw invalidCombo("neither encode or decode is set");
}
}
/**
@@ -160,36 +221,7 @@ void getFlags(int argc, char** argv, bool cipherFlags[], std::string& inputStrin
//Set all the arguments. If there is an invalid argument the function will throw an error
setArgs(args.at(argCnt), cipherFlags);
}
}
/**
* @brief Checks that the flags given are valid
*
* @param flags All of the flags that the program can have
* @return true If valid options were chosen
* @return false If invalid options were chosen
*/
bool checkFlags(bool flags[]){
int counter = 0;
bool correct = false;
//Run through each flag and counter how many are true
for(int cnt = CAESAR;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[CipherFlagLocation::ENCODE] && flags[CipherFlagLocation::DECODE]) || (!flags[CipherFlagLocation::ENCODE] && !flags[CipherFlagLocation::DECODE])){
correct = false;
}
return correct;
checkFlags(cipherFlags);
}
/**

View File

@@ -1,11 +1,11 @@
//Ciphers/main.cpp
//Matthew Ellison
// Created: 4-25-18
//Modified: 12-10-18
// Created: 04-25-18
//Modified: 03-07-19
//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
/*
Copyright (C) 2018 Matthew Ellison
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -57,12 +57,22 @@ int main(int argc, char** argv){
cipherFlags[cnt] = false;
}
//Check what flags were set
getFlags(argc, argv, cipherFlags, inputFileName, outputFileName);
//Make sure that only one of the ciphers was selected
if(!checkFlags(cipherFlags)){
std::cout << "The flags given were invalid" << std::endl;
return 0;
//Set the flags and test for errors
try{
getFlags(argc, argv, cipherFlags, inputFileName, outputFileName);
}
//Catch any errors that were given
catch(noFileName error){
std::cout << "The " << error.getType() << " file flag was set but no file name was given" << std::endl;
return 1;
}
catch(invalidFlag error){
std::cout << '-' << error.getFlag() << " is not a valid flag" << std::endl;
return 1;
}
catch(invalidCombo error){
std::cout << "An invalid combination of flags was given where " << error.getType() << std::endl;
return 1;
}
//Check if output file can be opened

View File

@@ -1,11 +1,11 @@
//Ciphers/testMain.hpp
//Matthew Ellison
// Created: 4-30-18
//Modified: 5-8-18
// Created: 04-30-18
//Modified: 03-07-19
//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
/*
Copyright (C) 2018 Matthew Ellison
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by