Added new documentation

This commit is contained in:
2018-05-05 17:13:51 -04:00
parent d1b1dc5e77
commit 411abe4547

View File

@@ -1,7 +1,7 @@
//Ciphers/Headers/Playfair.cpp //Ciphers/Headers/Playfair.cpp
//Matthew Ellison //Matthew Ellison
// Created: 4-25-18 // Created: 4-25-18
//Modified: 4-25-18 //Modified: 5-5-18
//This file contains the implementation of the Playfair class //This file contains the implementation of the Playfair class
//It is designed to encrypt and decrypt strings using the Playfair cipher //It is designed to encrypt and decrypt strings using the Playfair cipher
@@ -9,19 +9,34 @@
#include "../Headers/Playfair.hpp" #include "../Headers/Playfair.hpp"
#include <string> #include <string>
#include <cctype> #include <cctype>
#include <iostream>
///The letter that needs replaced for the cipher to work
char Playfair::REPLACED = 'J'; char Playfair::REPLACED = 'J';
///The letter that replaces REPLACED
char Playfair::REPLACER = 'I'; char Playfair::REPLACER = 'I';
///The letter that is added to the end to make the message of even length and placed between 2 ajoining letters that are the same
char Playfair::DOUBLED = 'X'; char Playfair::DOUBLED = 'X';
/**
* @brief Construct a new Playfair object
*
*/
Playfair::Playfair(){ Playfair::Playfair(){
reset(); reset();
} }
/**
* @brief Destroy the Playfair object
*
*/
Playfair::~Playfair(){ Playfair::~Playfair(){
} }
/**
* @brief Uses keyword to set the characters in grid correctly
*
*/
void Playfair::createGrid(){ void Playfair::createGrid(){
unsigned int row, column; unsigned int row, column;
bool found = false; bool found = false;
@@ -77,12 +92,19 @@ void Playfair::createGrid(){
++current; ++current;
} }
//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
///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;; //std::cout << "There is a problem with the grid!\n" << getGrid() << std::endl;;
} }
} }
/**
* @brief Checks if a letter is in the grid
*
* @param letter The letter you want to check for
* @return true If the letter is found
* @return false If the letter is not found
*/
bool Playfair::checkGrid(const char letter) const{ bool Playfair::checkGrid(const char letter) const{
//Step through every element in the grid and check for it //Step through every element in the grid and check for it
for(int rowCnt = 0;rowCnt < 5;++rowCnt){ for(int rowCnt = 0;rowCnt < 5;++rowCnt){
@@ -101,6 +123,13 @@ bool Playfair::checkGrid(const char letter) const{
return false; return false;
} }
/**
* @brief Searches the grid for a letter
*
* @param letter The letter you wish to find
* @param row The row 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
@@ -119,10 +148,14 @@ 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
///Turn this into a propper exception throw rather than this //TODO: Turn this into a propper exception throw rather than this
std::cout << "Error in searchGrid()\nLetter: " << letter << "\n\n" << getGrid() << std::endl; //std::cout << "Error in searchGrid()\nLetter: " << letter << "\n\n" << getGrid() << std::endl;
} }
/**
* @brief Makes sure all variables are blank
*
*/
void Playfair::reset(){ void Playfair::reset(){
inputString = outputString = keyword = ""; inputString = outputString = keyword = "";
//Clear the grid //Clear the grid
@@ -133,6 +166,11 @@ void Playfair::reset(){
} }
} }
/**
* @brief Encodes inputString using the rules of the Playfair cipher and stores it in outputString
*
* @return outputString
*/
std::string Playfair::encode(){ std::string Playfair::encode(){
outputString = ""; outputString = "";
char letter1, letter2; char letter1, letter2;
@@ -189,12 +227,24 @@ std::string Playfair::encode(){
return outputString; return outputString;
} }
/**
* @brief Uses keyword to set the grid, encodes input using the rules of the Playfair cipher, and returns the new message
*
* @param keyword The keyword used to create the grid
* @param input The message that needs encoded
* @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); setKeyword(keyword);
setInputString(input); setInputString(input);
return encode(); return encode();
} }
/**
* @brief Decodes inputString using the rules of the Playfair cipher and stores it in outputString
*
* @return outputString
*/
std::string Playfair::decode(){ std::string Playfair::decode(){
outputString = ""; outputString = "";
char letter1, letter2; char letter1, letter2;
@@ -251,12 +301,24 @@ std::string Playfair::decode(){
return outputString; return outputString;
} }
/**
* @brief Uses keyword to set the grid, decodes input using the rules of the Playfair cipher, and returns the new message
*
* @param keyword The keyword used to create the grid
* @param input The message that needs decoded
* @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); setKeyword(keyword);
setInputString(input); setInputString(input);
return decode(); return decode();
} }
/**
* @brief Removes all invalid characters and sets the keyword
*
* @param key The string that is used for the keyword
*/
void Playfair::setKeyword(std::string key){ void Playfair::setKeyword(std::string key){
//Make sure the keyword is blank //Make sure the keyword is blank
keyword = ""; keyword = "";
@@ -276,10 +338,20 @@ void Playfair::setKeyword(std::string key){
createGrid(); createGrid();
} }
/**
* @brief Returns the current keyword
*
* @return The current keyword
*/
std::string Playfair::getKeyword() const{ std::string Playfair::getKeyword() const{
return keyword; return keyword;
} }
/**
* @brief Removes all invalid characters and sets inputString
*
* @param input The message that needs to be encoded
*/
void Playfair::setInputString(std::string input){ void Playfair::setInputString(std::string input){
//Make sure inputString is empty //Make sure inputString is empty
inputString = ""; inputString = "";
@@ -326,14 +398,29 @@ void Playfair::setInputString(std::string input){
} }
} }
/**
* @brief Returns the current inputString
*
* @return The current message that needs encoded/decoded
*/
std::string Playfair::getInputString() const{ std::string Playfair::getInputString() const{
return inputString; return inputString;
} }
/**
* @brief Returns the current outputString
*
* @return The encoded/decoded message
*/
std::string Playfair::getOutputString() const{ std::string Playfair::getOutputString() const{
return outputString; return outputString;
} }
/**
* @brief Returns a representation of the current grid
*
* @return A string representation of the grid
*/
std::string Playfair::getGrid() const{ std::string Playfair::getGrid() const{
std::string temp; std::string temp;
for(int row = 0;row < 5;++row){ for(int row = 0;row < 5;++row){
@@ -345,10 +432,20 @@ std::string Playfair::getGrid() const{
return temp; return temp;
} }
/**
* @brief Returns REPLACED
*
* @return The letter that needs replaced for the cipher to work
*/
char Playfair::getReplaced(){ char Playfair::getReplaced(){
return REPLACED; return REPLACED;
} }
/**
* @brief Makes sure replaced is a valid character and sets REPLACED
*
* @param replaced The letter to replace REPLACED
*/
void Playfair::setReplaced(const char replaced){ void Playfair::setReplaced(const char replaced){
//Make sure the letter is in the correct range //Make sure the letter is in the correct range
if(replaced >= 'A' && replaced <= 'Z'){ if(replaced >= 'A' && replaced <= 'Z'){
@@ -362,10 +459,20 @@ void Playfair::setReplaced(const char replaced){
} }
} }
/**
* @brief Returned REPLACER
*
* @return The letter that replaces REPLACED
*/
char Playfair::getReplacer(){ char Playfair::getReplacer(){
return REPLACER; return REPLACER;
} }
/**
* @brief Makes sure replacer is a valid character and sets REPLACER
*
* @param replacer The letter to replace REPLACER
*/
void Playfair::setReplacer(const char replacer){ void Playfair::setReplacer(const char replacer){
//Make sure the letter is in the correct range //Make sure the letter is in the correct range
if(replacer >= 'A' && replacer <='Z'){ if(replacer >= 'A' && replacer <='Z'){
@@ -379,10 +486,20 @@ void Playfair::setReplacer(const char replacer){
} }
} }
/**
* @brief Return DOUBLED
*
* @return The letter that is added to the end to make the message of even length and placed between 2 ajoining letters that are the same
*/
char Playfair::getDoubled(){ char Playfair::getDoubled(){
return DOUBLED; return DOUBLED;
} }
/**
* @brief Makes sure doubled is a valid character and sets DOUBLED
*
* @param doubled The letter to replace DOUBLED
*/
void Playfair::setDoubled(const char doubled){ void Playfair::setDoubled(const char doubled){
//Make sure the letter is in the correct range //Make sure the letter is in the correct range
if(doubled >= 'A' && doubled <= 'Z'){ if(doubled >= 'A' && doubled <= 'Z'){