From 5ebbc1f14af27a54dea2e3e10dfda9f8f4a2b0dd Mon Sep 17 00:00:00 2001 From: Ironozd Date: Tue, 15 Aug 2023 13:19:10 +0300 Subject: [PATCH] Updated translation from letter algorithm - Deleted un-needed struct from mors.h - Program now works on release mode. --- ConvertMe.txt | 2 +- src/defs/definition_importer.cpp | 4 ++++ src/defs/definition_importer.h | 9 +++++++-- src/defs/defs.txt | 1 + src/mors_file.cpp | 9 +++++---- src/mors_file.h | 1 + src/structure/mors.h | 8 ++++---- src/translation/translate.cpp | 18 +++++++++++++++++- src/translation/translate.h | 18 ++++++++++++++++-- 9 files changed, 56 insertions(+), 14 deletions(-) diff --git a/ConvertMe.txt b/ConvertMe.txt index c535829..7a5f13c 100644 --- a/ConvertMe.txt +++ b/ConvertMe.txt @@ -1 +1 @@ --.../.-/-.../.- \ No newline at end of file +.... . .-.. .-.. --- / .-- --- .-. .-.. -.. \ No newline at end of file diff --git a/src/defs/definition_importer.cpp b/src/defs/definition_importer.cpp index 8fc3110..e208e48 100644 --- a/src/defs/definition_importer.cpp +++ b/src/defs/definition_importer.cpp @@ -1,7 +1,11 @@ #include "definition_importer.h" void MDefImporter::ImportDefinitionsImpl(){ + #ifdef MDEBUG std::ifstream defFile("D:\\GitRepos\\mors\\src\\defs\\defs.txt"); + #else + std::ifstream defFile("defs.txt"); + #endif if(!defFile.is_open()) { std::cout << "MDefImporter::ImportDefinition:: Failed to load definition.\n"; diff --git a/src/defs/definition_importer.h b/src/defs/definition_importer.h index 98c09f6..9c807fe 100644 --- a/src/defs/definition_importer.h +++ b/src/defs/definition_importer.h @@ -1,3 +1,6 @@ +#ifndef H_MDEF_IMPORTER +#define H_MDEF_IMPORTER + #include #include #include @@ -8,7 +11,7 @@ class MDefImporter{ public: static void ImportDefinitions() { Get().ImportDefinitionsImpl(); } - std::map> getDefinitions() {return Definitions;} + static std::map> getDefinitions() {return Get().Definitions;} private: static MDefImporter& Get(){ static MDefImporter importer; @@ -16,4 +19,6 @@ class MDefImporter{ } void ImportDefinitionsImpl(); std::map> Definitions; -}; \ No newline at end of file +}; + +#endif \ No newline at end of file diff --git a/src/defs/defs.txt b/src/defs/defs.txt index 968daae..0e50d9c 100644 --- a/src/defs/defs.txt +++ b/src/defs/defs.txt @@ -1,3 +1,4 @@ + |/ A|.- B|-... C|-.-. diff --git a/src/mors_file.cpp b/src/mors_file.cpp index 4ad6390..a0aea23 100644 --- a/src/mors_file.cpp +++ b/src/mors_file.cpp @@ -9,7 +9,7 @@ std::vector> FileToMors::ConvertFileToMors(std::string while(std::getline(mFile, line)){ std::istringstream iss(line); - while(std::getline(iss, token, '/')){ + while(std::getline(iss, token, ' ')){ std::vector> chars; for(auto CH : token){ chars.emplace_back(std::make_shared(std::string(1, CH))); @@ -27,16 +27,17 @@ int main(int argc, char* argv[]){ MDefImporter::ImportDefinitions(); std::string filePath = ""; #ifndef MDEBUG - if(argc == 0){ + if(argc == 1){ std::cout << "Please enter a file path!\nUsage: .\\mors_file "; return 1; } - filePath = argv[0]; + filePath = argv[1]; #else filePath = "D:\\GitRepos\\mors\\ConvertMe.txt"; #endif auto letters = FileToMors::ConvertFileToMors(filePath); std::cout << "Loaded " << letters.size() << " letters from file.\n"; - + std::cout << "-------------------[ RESULT ]-----------------------\n"; + std::cout << translate::MLetterToString(letters) << std::endl; return 0; } \ No newline at end of file diff --git a/src/mors_file.h b/src/mors_file.h index 94ef6b3..96850a7 100644 --- a/src/mors_file.h +++ b/src/mors_file.h @@ -2,6 +2,7 @@ #include #include #include "defs/definition_importer.h" +#include "translation/translate.h" class FileToMors{ diff --git a/src/structure/mors.h b/src/structure/mors.h index 8cfef10..978ad57 100644 --- a/src/structure/mors.h +++ b/src/structure/mors.h @@ -1,3 +1,6 @@ +#ifndef H_MORS_IMPL +#define H_MORS_IMPL + #include #include #include @@ -38,7 +41,4 @@ class MLetter std::vector> mChars; }; -struct MFile -{ - std::vector> mLetters; -}; +#endif \ No newline at end of file diff --git a/src/translation/translate.cpp b/src/translation/translate.cpp index 3b2fe13..02fa93d 100644 --- a/src/translation/translate.cpp +++ b/src/translation/translate.cpp @@ -1 +1,17 @@ -#include "translate.h" \ No newline at end of file +#include "translate.h" + +std::string translate::MLetterToString(const std::vector>& Letters){ + std::string result = ""; + for(auto letter : Letters){ + int i = 0; + for(auto def : MDefImporter::getDefinitions()){ + if(MLetter::isEqualTo(letter, def.second)){ + result.append(def.first); + }else{ + i++; + } + } + if(i == MDefImporter::getDefinitions().size()) result.append("*"); + } + return result; +} \ No newline at end of file diff --git a/src/translation/translate.h b/src/translation/translate.h index b2025f9..bc3e4d7 100644 --- a/src/translation/translate.h +++ b/src/translation/translate.h @@ -1,5 +1,19 @@ +#ifndef H_TRANSLATE +#define H_TRANSLATE +#include +#include +#include +#include "../structure/mors.h" +#include "../defs/definition_importer.h" class translate{ - -}; \ No newline at end of file +public: + /* + TO BE IMPLEMENTED / INEFFICIENT + Large amounts of morse code will take a long time and/or crash. + */ + static std::string MLetterToString(const std::vector>& Letters); +}; + +#endif \ No newline at end of file