Skip to content

Commit

Permalink
Updated translation from letter algorithm
Browse files Browse the repository at this point in the history
- Deleted un-needed struct from mors.h
- Program now works on release mode.
  • Loading branch information
Ironozd committed Aug 15, 2023
1 parent d91abad commit 5ebbc1f
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ConvertMe.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
-.../.-/-.../.-
.... . .-.. .-.. --- / .-- --- .-. .-.. -..
4 changes: 4 additions & 0 deletions src/defs/definition_importer.cpp
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
9 changes: 7 additions & 2 deletions src/defs/definition_importer.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef H_MDEF_IMPORTER
#define H_MDEF_IMPORTER

#include <fstream>
#include <sstream>
#include <iostream>
Expand All @@ -8,12 +11,14 @@ class MDefImporter{
public:
static void ImportDefinitions() { Get().ImportDefinitionsImpl(); }

std::map<std::string, std::shared_ptr<MLetter>> getDefinitions() {return Definitions;}
static std::map<std::string, std::shared_ptr<MLetter>> getDefinitions() {return Get().Definitions;}
private:
static MDefImporter& Get(){
static MDefImporter importer;
return importer;
}
void ImportDefinitionsImpl();
std::map<std::string, std::shared_ptr<MLetter>> Definitions;
};
};

#endif
1 change: 1 addition & 0 deletions src/defs/defs.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
|/
A|.-
B|-...
C|-.-.
Expand Down
9 changes: 5 additions & 4 deletions src/mors_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ std::vector<std::shared_ptr<MLetter>> 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<std::shared_ptr<MChar>> chars;
for(auto CH : token){
chars.emplace_back(std::make_shared<MChar>(std::string(1, CH)));
Expand All @@ -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 <file_path>";
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;
}
1 change: 1 addition & 0 deletions src/mors_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <iostream>
#include <fstream>
#include "defs/definition_importer.h"
#include "translation/translate.h"


class FileToMors{
Expand Down
8 changes: 4 additions & 4 deletions src/structure/mors.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef H_MORS_IMPL
#define H_MORS_IMPL

#include <iostream>
#include <vector>
#include <memory>
Expand Down Expand Up @@ -38,7 +41,4 @@ class MLetter
std::vector<std::shared_ptr<MChar>> mChars;
};

struct MFile
{
std::vector<std::shared_ptr<MLetter>> mLetters;
};
#endif
18 changes: 17 additions & 1 deletion src/translation/translate.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
#include "translate.h"
#include "translate.h"

std::string translate::MLetterToString(const std::vector<std::shared_ptr<MLetter>>& 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;
}
18 changes: 16 additions & 2 deletions src/translation/translate.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#ifndef H_TRANSLATE
#define H_TRANSLATE

#include <vector>
#include <iostream>
#include <memory>
#include "../structure/mors.h"
#include "../defs/definition_importer.h"

class translate{

};
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<std::shared_ptr<MLetter>>& Letters);
};

#endif

0 comments on commit 5ebbc1f

Please sign in to comment.