From 22bcb3eafd68bbf55814c6c422920f5189cf6f7b Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Thu, 7 Mar 2019 12:07:53 -0500 Subject: [PATCH] Added propper error handling to main function --- helperFunctions.hpp | 104 +++++++++++++++++++++++++++++--------------- main.cpp | 28 ++++++++---- testHandler.hpp | 6 +-- 3 files changed, 90 insertions(+), 48 deletions(-) diff --git a/helperFunctions.hpp b/helperFunctions.hpp index 64fdfc4..a80ec2b 100644 --- a/helperFunctions.hpp +++ b/helperFunctions.hpp @@ -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 +//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 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 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); } /** diff --git a/main.cpp b/main.cpp index fb2ccf9..020283e 100644 --- a/main.cpp +++ b/main.cpp @@ -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 diff --git a/testHandler.hpp b/testHandler.hpp index f767785..f2d939a 100644 --- a/testHandler.hpp +++ b/testHandler.hpp @@ -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