mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-06 18:33:58 -05:00
Added propper error handling
This commit is contained in:
@@ -14,6 +14,23 @@
|
||||
|
||||
class Playfair{
|
||||
private:
|
||||
//Classed to help with error handling
|
||||
class letterNotFound{
|
||||
private:
|
||||
char letter;
|
||||
public:
|
||||
letterNotFound(char letter): letter(letter) {}
|
||||
char getLetter(){ return letter; }
|
||||
};
|
||||
class invalidGrid{
|
||||
private:
|
||||
std::string type;
|
||||
unsigned int size;
|
||||
public:
|
||||
invalidGrid(std::string type, unsigned int size): type(type), size(size) {}
|
||||
std::string getType(){ return type; }
|
||||
int getSize(){ return size; }
|
||||
};
|
||||
static char REPLACED; //The letter that will need to be replaced in the grid and any input string or keyword
|
||||
static char REPLACER; //The letter that replaced REPLACED in any input string or keyword
|
||||
static char DOUBLED; //The letter that will be placed between double letters in the input string if necessary or to make the string length even
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
class Vigenere{
|
||||
protected:
|
||||
class emptyKeyword{}; //A class to help with error handling
|
||||
std::string inputString; //This is the string that you want to encode or decode
|
||||
std::string outputString; //This is the string that is output from encoding or decoding
|
||||
std::string keyword; //This is the keyword that is the resposible for determining the offsets that you change each character by
|
||||
|
||||
@@ -49,7 +49,7 @@ void Playfair::createGrid(){
|
||||
row = column = 0;
|
||||
|
||||
//Add any new leters from the keyword to the grid
|
||||
///If you reach row 5 then the entire grid has been filled
|
||||
//If you reach row 5 then the entire grid has been filled
|
||||
char current;
|
||||
for(unsigned int cnt = 0;(cnt < keyword.size()) && (row < 5);++cnt){
|
||||
current = keyword[cnt];
|
||||
@@ -76,7 +76,7 @@ void Playfair::createGrid(){
|
||||
//Until you reach the end of the grid you need to start with the first letter and try to add them to the grid
|
||||
current = 'A';
|
||||
while((row < 5) && (current <= 'Z')){
|
||||
////Start here
|
||||
//Start here
|
||||
//Make sure the is not the replaced letter
|
||||
if(current == REPLACED){
|
||||
++current;
|
||||
@@ -99,8 +99,11 @@ void Playfair::createGrid(){
|
||||
}
|
||||
//Put a check here that row == 5. If it doesn't then there is a problem
|
||||
//TODO: Make this a propper exception throw rather than this
|
||||
if(row != 5){
|
||||
//std::cout << "There is a problem with the grid!\n" << getGrid() << std::endl;;
|
||||
if(row < 5){
|
||||
throw invalidGrid("smaller", row);
|
||||
}
|
||||
else if(row > 5){
|
||||
throw invalidGrid("larger", row);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +140,7 @@ bool Playfair::checkGrid(const char letter) const{
|
||||
* @param col The column that the letter is found in
|
||||
*/
|
||||
void Playfair::searchGrid(char letter, int& row, int& col){
|
||||
////Start here
|
||||
//Start here
|
||||
//Check if letter needs to be replaced
|
||||
if(letter == REPLACED){
|
||||
letter = REPLACER;
|
||||
@@ -154,8 +157,7 @@ void Playfair::searchGrid(char letter, int& row, int& col){
|
||||
}
|
||||
|
||||
//If letter was not found you need to throw an error
|
||||
//TODO: Turn this into a propper exception throw rather than this
|
||||
//std::cout << "Error in searchGrid()\nLetter: " << letter << "\n\n" << getGrid() << std::endl;
|
||||
throw letterNotFound(letter);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,8 +190,17 @@ std::string Playfair::encode(){
|
||||
letter2 = inputString[cnt];
|
||||
|
||||
//Get the location in the grid of each letter
|
||||
try{
|
||||
searchGrid(letter1, row1, col1);
|
||||
searchGrid(letter2, row2, col2);
|
||||
}
|
||||
//Catch any error that might have occurred
|
||||
catch(letterNotFound error){
|
||||
std::string errorTemp = "There was an error int he grid\n";
|
||||
errorTemp += error.getLetter();
|
||||
errorTemp += " was not found";
|
||||
return errorTemp;
|
||||
}
|
||||
|
||||
//If the letters are in the same row, shift the column right by 1
|
||||
if(row1 == row2){
|
||||
@@ -241,7 +252,18 @@ std::string Playfair::encode(){
|
||||
* @return The encoded message
|
||||
*/
|
||||
std::string Playfair::encode(std::string keyword, std::string input){
|
||||
try{
|
||||
setKeyword(keyword);
|
||||
}
|
||||
//Try to catch any error that might have occurred
|
||||
catch(invalidGrid error){
|
||||
std::string errorString = "There was an error setting up the grid.\nThe grid is too ";
|
||||
errorString += error.getType();
|
||||
errorString += " with ";
|
||||
errorString += std::to_string(error.getSize());
|
||||
errorString += " rows";
|
||||
return errorString;
|
||||
}
|
||||
setInputString(input);
|
||||
return encode();
|
||||
}
|
||||
@@ -262,8 +284,17 @@ std::string Playfair::decode(){
|
||||
letter2 = inputString[cnt];
|
||||
|
||||
//Get the location in the grid of each letter
|
||||
try{
|
||||
searchGrid(letter1, row1, col1);
|
||||
searchGrid(letter2, row2, col2);
|
||||
}
|
||||
//Catch any error that might have occurred
|
||||
catch(letterNotFound error){
|
||||
std::string errorTemp = "There was an error int he grid\n";
|
||||
errorTemp += error.getLetter();
|
||||
errorTemp += " was not found";
|
||||
return errorTemp;
|
||||
}
|
||||
|
||||
//If the letters are in the same row, shift the column left by 1
|
||||
if(row1 == row2){
|
||||
@@ -315,7 +346,18 @@ std::string Playfair::decode(){
|
||||
* @return The decoded message
|
||||
*/
|
||||
std::string Playfair::decode(std::string keyword, std::string input){
|
||||
try{
|
||||
setKeyword(keyword);
|
||||
}
|
||||
//Try to catch any error that might have occurred
|
||||
catch(invalidGrid error){
|
||||
std::string errorString = "There was an error setting up the grid.\nThe grid is too ";
|
||||
errorString += error.getType();
|
||||
errorString += " with ";
|
||||
errorString += std::to_string(error.getSize());
|
||||
errorString += " rows";
|
||||
return errorString;
|
||||
}
|
||||
setInputString(input);
|
||||
return decode();
|
||||
}
|
||||
|
||||
@@ -88,6 +88,10 @@ void Vigenere::setKeyword(std::string key){
|
||||
//Make sure offset is empty before adding to it
|
||||
offset.clear();
|
||||
setOffset();
|
||||
//If after all the eliminating of unusable characters the keyword is empty throw an error
|
||||
if(keyword == ""){
|
||||
throw emptyKeyword();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,11 +161,13 @@ std::string Vigenere::encode(){
|
||||
*/
|
||||
std::string Vigenere::encode(std::string key, std::string input){
|
||||
reset();
|
||||
try{
|
||||
setKeyword(key);
|
||||
//Throw an error if there is no keyword
|
||||
//Would be better to throw an error here
|
||||
if(keyword == ""){
|
||||
return "Error! Empty keyword\n";
|
||||
}
|
||||
//Catch the emptyKeyword error if it was thrown
|
||||
catch(emptyKeyword){
|
||||
std::string errorString = "After all unusable characters were stripped away the keyword given was empty";
|
||||
return errorString;
|
||||
}
|
||||
setInputString(input);
|
||||
return encode();
|
||||
@@ -200,11 +206,13 @@ std::string Vigenere::decode(){
|
||||
*/
|
||||
std::string Vigenere::decode(std::string key, std::string input){
|
||||
reset();
|
||||
try{
|
||||
setKeyword(key);
|
||||
//Throw an error if there is no keyword
|
||||
//Would be better to throw an error here
|
||||
if(keyword == ""){
|
||||
return "Error! Empty keyword\n";
|
||||
}
|
||||
//Catch the emptyKeyword error if it was thrown
|
||||
catch(emptyKeyword){
|
||||
std::string errorString = "After all unusable characters were stripped away the keyword given was empty";
|
||||
return errorString;
|
||||
}
|
||||
setInputString(input);
|
||||
return decode();
|
||||
|
||||
Reference in New Issue
Block a user