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
//Matthew Ellison
// Created: 4-25-18
//Modified: 4-25-18
//Modified: 5-5-18
//This file contains the implementation of the Playfair class
//It is designed to encrypt and decrypt strings using the Playfair cipher
@@ -9,19 +9,34 @@
#include "../Headers/Playfair.hpp"
#include <string>
#include <cctype>
#include <iostream>
///The letter that needs replaced for the cipher to work
char Playfair::REPLACED = 'J';
///The letter that replaces REPLACED
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';
/**
* @brief Construct a new Playfair object
*
*/
Playfair::Playfair(){
reset();
}
/**
* @brief Destroy the Playfair object
*
*/
Playfair::~Playfair(){
}
/**
* @brief Uses keyword to set the characters in grid correctly
*
*/
void Playfair::createGrid(){
unsigned int row, column;
bool found = false;
@@ -77,12 +92,19 @@ void Playfair::createGrid(){
++current;
}
//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){
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{
//Step through every element in the grid and check for it
for(int rowCnt = 0;rowCnt < 5;++rowCnt){
@@ -101,6 +123,13 @@ bool Playfair::checkGrid(const char letter) const{
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){
////Start here
//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
///Turn this into a propper exception throw rather than this
std::cout << "Error in searchGrid()\nLetter: " << letter << "\n\n" << getGrid() << std::endl;
//TODO: Turn this into a propper exception throw rather than this
//std::cout << "Error in searchGrid()\nLetter: " << letter << "\n\n" << getGrid() << std::endl;
}
/**
* @brief Makes sure all variables are blank
*
*/
void Playfair::reset(){
inputString = outputString = keyword = "";
//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(){
outputString = "";
char letter1, letter2;
@@ -189,12 +227,24 @@ std::string Playfair::encode(){
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){
setKeyword(keyword);
setInputString(input);
return encode();
}
/**
* @brief Decodes inputString using the rules of the Playfair cipher and stores it in outputString
*
* @return outputString
*/
std::string Playfair::decode(){
outputString = "";
char letter1, letter2;
@@ -251,12 +301,24 @@ std::string Playfair::decode(){
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){
setKeyword(keyword);
setInputString(input);
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){
//Make sure the keyword is blank
keyword = "";
@@ -276,10 +338,20 @@ void Playfair::setKeyword(std::string key){
createGrid();
}
/**
* @brief Returns the current keyword
*
* @return The current keyword
*/
std::string Playfair::getKeyword() const{
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){
//Make sure inputString is empty
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{
return inputString;
}
/**
* @brief Returns the current outputString
*
* @return The encoded/decoded message
*/
std::string Playfair::getOutputString() const{
return outputString;
}
/**
* @brief Returns a representation of the current grid
*
* @return A string representation of the grid
*/
std::string Playfair::getGrid() const{
std::string temp;
for(int row = 0;row < 5;++row){
@@ -345,10 +432,20 @@ std::string Playfair::getGrid() const{
return temp;
}
/**
* @brief Returns REPLACED
*
* @return The letter that needs replaced for the cipher to work
*/
char Playfair::getReplaced(){
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){
//Make sure the letter is in the correct range
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(){
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){
//Make sure the letter is in the correct range
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(){
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){
//Make sure the letter is in the correct range
if(doubled >= 'A' && doubled <= 'Z'){