From 33c23cfc10aae843b03d44971e3932248d4beb2b Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sun, 9 Jul 2017 21:44:30 -0500 Subject: [PATCH] Code cleanup and moving functions out of classes that do not belong --- Conversions.h | 25 +++++++++++++++++++++++++ Decryption.cpp | 31 +++++++++---------------------- Decryption.h | 3 +-- EncryptedMessaging.pro | 2 +- Encryption.cpp | 15 +++++---------- Encryption.h | 2 -- GenerateKeys.cpp | 7 +++---- KeyManagementWindow.cpp | 9 ++++----- KeyManagementWindow.h | 4 ++-- KeyRetrieval.cpp | 12 ++---------- KeyRetrieval.h | 2 +- 11 files changed, 53 insertions(+), 59 deletions(-) create mode 100644 Conversions.h diff --git a/Conversions.h b/Conversions.h new file mode 100644 index 0000000..378a211 --- /dev/null +++ b/Conversions.h @@ -0,0 +1,25 @@ +#ifndef CONVERSIONS_H_ +#define CONVERSIONS_H_ + +#include +#include + +class Conversions +{ +public: + Conversions() = default; + + std::string cstringToString(char[], const int); +private: +}; + +std::string Conversions::cstringToString(char tmp[], const int size) +{ + std::string tmpString{}; + std::stringstream cToS{}; + for (auto index =0; index!=size; ++ index) + cToS << tmp[index]; + cToS >> tmpString; + return tmpString; +} +#endif diff --git a/Decryption.cpp b/Decryption.cpp index 369f68f..75fa478 100644 --- a/Decryption.cpp +++ b/Decryption.cpp @@ -3,7 +3,7 @@ #include #include #include - +#include"Conversions.h" #include"Decryption.h" #include"KeyRetrieval.h" @@ -15,10 +15,8 @@ Decryption::Decryption(const std::string& message) } -void Decryption::setDecryptedMessage(const std::string& message) -{ this->decryptedMessage = message; } -void Decryption::setMessage(const std::string& message) -{ this->message = message; } +void Decryption::setDecryptedMessage(const std::string& message) { this->decryptedMessage = message; } +void Decryption::setMessage(const std::string& message) { this->message = message; } void Decryption::decryptMessage() { @@ -34,19 +32,17 @@ void Decryption::decryptMessage() for (auto indexOfChar = 0u; indexOfChar k{kr.keyStructure()}; std::vector c{kr.codeCharacterStructure()}; - for (auto index = 0; index!=c.size(); ++index) + for (auto index = 0u; index!=c.size(); ++index) decryptedCharacters[k[index]] = c[index]; } -std::string Decryption::cstringToString(char tmp[], const int& SIZE) -{ - std::string tmpString{}; - std::stringstream cToS{}; - for (auto index = 0; index!=SIZE; ++index) - cToS << tmp[index]; - cToS >> tmpString; - return tmpString; -} diff --git a/Decryption.h b/Decryption.h index 50afcf0..4220298 100644 --- a/Decryption.h +++ b/Decryption.h @@ -1,7 +1,6 @@ #ifndef DECRYPTION_H #define DECRYPTION_H -#include #include #include #include"Cryption.h" @@ -21,9 +20,9 @@ public: std::string getMessage() const override; private: void setupMap(); - std::string cstringToString(char[], const int&); std::string decryptedMessage; std::map decryptedCharacters; + const int keyLength{5}; }; #endif diff --git a/EncryptedMessaging.pro b/EncryptedMessaging.pro index b9b96f7..2b390b0 100644 --- a/EncryptedMessaging.pro +++ b/EncryptedMessaging.pro @@ -9,7 +9,7 @@ QT += widgets INCLUDEPATH += . # Input -HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h KeyRetrieval.h +HEADERS += Decryption.h Encryption.h GenerateKeys.h MessagingControls.h Cryption.h KeyManagementWindow.h KeyRetrieval.h Conversions.h SOURCES += Decryption.cpp \ Encryption.cpp \ Cryption.cpp \ diff --git a/Encryption.cpp b/Encryption.cpp index c16d15f..afe62fb 100644 --- a/Encryption.cpp +++ b/Encryption.cpp @@ -2,7 +2,6 @@ #include #include #include - #include"Encryption.h" #include"GenerateKeys.h" #include"KeyRetrieval.h" @@ -16,10 +15,8 @@ Encryption::Encryption(const std::string& message) } -void Encryption::setEncryptedMessage(const std::string& encryptedMessage) -{ this->encryptedMessage = encryptedMessage; } -void Encryption::setMessage(const std::string& message) -{ this->message = message; } +void Encryption::setEncryptedMessage(const std::string& encryptedMessage) { this->encryptedMessage = encryptedMessage; } +void Encryption::setMessage(const std::string& message) { this->message = message; } void Encryption::encryptMessage() { std::fstream ioEvent{}; @@ -41,11 +38,9 @@ void Encryption::setupMap() std::vector k{kr.keyStructure()}; std::vector c{kr.codeCharacterStructure()}; - for (auto index = 0; index!=k.size(); ++index) + for (auto index = 0u; index!=k.size(); ++index) encryptedCharacters[c[index]] = k[index]; } -std::string Encryption::getEncryptedMessage() const -{ return encryptedMessage; } -std::string Encryption::getMessage() const -{ return message; } +std::string Encryption::getEncryptedMessage() const { return encryptedMessage; } +std::string Encryption::getMessage() const { return message; } std::map Encryption::encryptedCharactersStructure() { return encryptedCharacters; } diff --git a/Encryption.h b/Encryption.h index a478480..21c0df4 100644 --- a/Encryption.h +++ b/Encryption.h @@ -1,7 +1,6 @@ #ifndef ENCRYPTION_H #define ENCRYPTION_H -#include #include #include #include"Cryption.h" @@ -25,5 +24,4 @@ private: std::string encryptedMessage; std::map encryptedCharacters; }; - #endif diff --git a/GenerateKeys.cpp b/GenerateKeys.cpp index 4a79ada..8c2f6f9 100644 --- a/GenerateKeys.cpp +++ b/GenerateKeys.cpp @@ -2,7 +2,6 @@ #include #include #include - #include"GenerateKeys.h" GenerateKeys::GenerateKeys() @@ -93,7 +92,7 @@ void GenerateKeys::generateKey(std::string& key, const int& SIZE) } void GenerateKeys::keyMove() { - for (auto index = 0; index!=allCharacters.size(); ++index) + for (auto index = 0u; index!=allCharacters.size(); ++index) { std::string tmpKey{}; generateKey(tmpKey, keySize); @@ -115,11 +114,11 @@ void GenerateKeys::print(const std::vector& ch) } bool GenerateKeys::isThereRepetition() { - for (auto index = 0; index!=allCharacters.size(); ++index) + for (auto index = 0u; index!=allCharacters.size(); ++index) { std::string chosenOne{keys[index]}; auto repetition{0}; - for (auto innerIndex = 0; innerIndex!=allCharacters.size(); ++innerIndex) + for (auto innerIndex = 0u; innerIndex!=allCharacters.size(); ++innerIndex) if (chosenOne == keys[innerIndex]) ++repetition; if (repetition > 1) diff --git a/KeyManagementWindow.cpp b/KeyManagementWindow.cpp index d564a9d..b0ad3c5 100644 --- a/KeyManagementWindow.cpp +++ b/KeyManagementWindow.cpp @@ -1,8 +1,3 @@ -#include"KeyManagementWindow.h" -#include"Encryption.h" -#include"GenerateKeys.h" -#include"KeyRetrieval.h" - #include #include #include @@ -12,6 +7,10 @@ #include #include #include +#include"KeyManagementWindow.h" +#include"Encryption.h" +#include"GenerateKeys.h" +#include"KeyRetrieval.h" KeyManagementWindow::KeyManagementWindow(QWidget* parent) : QDialog(parent) { diff --git a/KeyManagementWindow.h b/KeyManagementWindow.h index f1b3b73..607e4d6 100644 --- a/KeyManagementWindow.h +++ b/KeyManagementWindow.h @@ -35,8 +35,8 @@ private: unique_ptr comboBoxOfKeys; unique_ptr valueOfKey; unique_ptr generateNewDefaultKeys; - int windowWidth{400}; - int windowHeight{400}; + const int windowWidth{400}; + const int windowHeight{400}; }; #endif diff --git a/KeyRetrieval.cpp b/KeyRetrieval.cpp index 03bdc96..4c54f18 100644 --- a/KeyRetrieval.cpp +++ b/KeyRetrieval.cpp @@ -18,9 +18,7 @@ void KeyRetrieval::retrieveKey() { readKeys.getline(keyChar, '\n'); if (keyChar[0]!='\0') - { keys.push_back(keyChar); - } } } void KeyRetrieval::addToList() @@ -42,11 +40,5 @@ void KeyRetrieval::addRange(int from, const int to) codeCharacter.push_back(from); } -std::vector KeyRetrieval::keyStructure() -{ - return keys; -} -std::vector KeyRetrieval::codeCharacterStructure() -{ - return codeCharacter; -} +std::vector KeyRetrieval::keyStructure() { return keys; } +std::vector KeyRetrieval::codeCharacterStructure() { return codeCharacter; } diff --git a/KeyRetrieval.h b/KeyRetrieval.h index 8031f18..cfee765 100644 --- a/KeyRetrieval.h +++ b/KeyRetrieval.h @@ -22,6 +22,6 @@ private: std::vector keys; std::vector characters; std::vector codeCharacter; - int keySize{5}; + const int keySize{5}; }; #endif