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{
|
class Playfair{
|
||||||
private:
|
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 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 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
|
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{
|
class Vigenere{
|
||||||
protected:
|
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 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 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
|
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;
|
row = column = 0;
|
||||||
|
|
||||||
//Add any new leters from the keyword to the grid
|
//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;
|
char current;
|
||||||
for(unsigned int cnt = 0;(cnt < keyword.size()) && (row < 5);++cnt){
|
for(unsigned int cnt = 0;(cnt < keyword.size()) && (row < 5);++cnt){
|
||||||
current = keyword[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
|
//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';
|
current = 'A';
|
||||||
while((row < 5) && (current <= 'Z')){
|
while((row < 5) && (current <= 'Z')){
|
||||||
////Start here
|
//Start here
|
||||||
//Make sure the is not the replaced letter
|
//Make sure the is not the replaced letter
|
||||||
if(current == REPLACED){
|
if(current == REPLACED){
|
||||||
++current;
|
++current;
|
||||||
@@ -99,8 +99,11 @@ void Playfair::createGrid(){
|
|||||||
}
|
}
|
||||||
//Put a check here that row == 5. If it doesn't then there is a problem
|
//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
|
//TODO: Make this a propper exception throw rather than this
|
||||||
if(row != 5){
|
if(row < 5){
|
||||||
//std::cout << "There is a problem with the grid!\n" << getGrid() << std::endl;;
|
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
|
* @param col The column that the letter is found in
|
||||||
*/
|
*/
|
||||||
void Playfair::searchGrid(char letter, int& row, int& col){
|
void Playfair::searchGrid(char letter, int& row, int& col){
|
||||||
////Start here
|
//Start here
|
||||||
//Check if letter needs to be replaced
|
//Check if letter needs to be replaced
|
||||||
if(letter == REPLACED){
|
if(letter == REPLACED){
|
||||||
letter = REPLACER;
|
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
|
//If letter was not found you need to throw an error
|
||||||
//TODO: Turn this into a propper exception throw rather than this
|
throw letterNotFound(letter);
|
||||||
//std::cout << "Error in searchGrid()\nLetter: " << letter << "\n\n" << getGrid() << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -188,8 +190,17 @@ std::string Playfair::encode(){
|
|||||||
letter2 = inputString[cnt];
|
letter2 = inputString[cnt];
|
||||||
|
|
||||||
//Get the location in the grid of each letter
|
//Get the location in the grid of each letter
|
||||||
searchGrid(letter1, row1, col1);
|
try{
|
||||||
searchGrid(letter2, row2, col2);
|
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 the letters are in the same row, shift the column right by 1
|
||||||
if(row1 == row2){
|
if(row1 == row2){
|
||||||
@@ -241,7 +252,18 @@ std::string Playfair::encode(){
|
|||||||
* @return The encoded message
|
* @return The encoded message
|
||||||
*/
|
*/
|
||||||
std::string Playfair::encode(std::string keyword, std::string input){
|
std::string Playfair::encode(std::string keyword, std::string input){
|
||||||
setKeyword(keyword);
|
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);
|
setInputString(input);
|
||||||
return encode();
|
return encode();
|
||||||
}
|
}
|
||||||
@@ -262,8 +284,17 @@ std::string Playfair::decode(){
|
|||||||
letter2 = inputString[cnt];
|
letter2 = inputString[cnt];
|
||||||
|
|
||||||
//Get the location in the grid of each letter
|
//Get the location in the grid of each letter
|
||||||
searchGrid(letter1, row1, col1);
|
try{
|
||||||
searchGrid(letter2, row2, col2);
|
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 the letters are in the same row, shift the column left by 1
|
||||||
if(row1 == row2){
|
if(row1 == row2){
|
||||||
@@ -315,7 +346,18 @@ std::string Playfair::decode(){
|
|||||||
* @return The decoded message
|
* @return The decoded message
|
||||||
*/
|
*/
|
||||||
std::string Playfair::decode(std::string keyword, std::string input){
|
std::string Playfair::decode(std::string keyword, std::string input){
|
||||||
setKeyword(keyword);
|
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);
|
setInputString(input);
|
||||||
return decode();
|
return decode();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,6 +88,10 @@ void Vigenere::setKeyword(std::string key){
|
|||||||
//Make sure offset is empty before adding to it
|
//Make sure offset is empty before adding to it
|
||||||
offset.clear();
|
offset.clear();
|
||||||
setOffset();
|
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){
|
std::string Vigenere::encode(std::string key, std::string input){
|
||||||
reset();
|
reset();
|
||||||
setKeyword(key);
|
try{
|
||||||
//Throw an error if there is no keyword
|
setKeyword(key);
|
||||||
//Would be better to throw an error here
|
}
|
||||||
if(keyword == ""){
|
//Catch the emptyKeyword error if it was thrown
|
||||||
return "Error! Empty keyword\n";
|
catch(emptyKeyword){
|
||||||
|
std::string errorString = "After all unusable characters were stripped away the keyword given was empty";
|
||||||
|
return errorString;
|
||||||
}
|
}
|
||||||
setInputString(input);
|
setInputString(input);
|
||||||
return encode();
|
return encode();
|
||||||
@@ -200,11 +206,13 @@ std::string Vigenere::decode(){
|
|||||||
*/
|
*/
|
||||||
std::string Vigenere::decode(std::string key, std::string input){
|
std::string Vigenere::decode(std::string key, std::string input){
|
||||||
reset();
|
reset();
|
||||||
setKeyword(key);
|
try{
|
||||||
//Throw an error if there is no keyword
|
setKeyword(key);
|
||||||
//Would be better to throw an error here
|
}
|
||||||
if(keyword == ""){
|
//Catch the emptyKeyword error if it was thrown
|
||||||
return "Error! Empty keyword\n";
|
catch(emptyKeyword){
|
||||||
|
std::string errorString = "After all unusable characters were stripped away the keyword given was empty";
|
||||||
|
return errorString;
|
||||||
}
|
}
|
||||||
setInputString(input);
|
setInputString(input);
|
||||||
return decode();
|
return decode();
|
||||||
|
|||||||
Reference in New Issue
Block a user