From bc5bbba53f143dcd6fc6ffb94d7c0221e22b54af Mon Sep 17 00:00:00 2001
From: Marek Blok <Marek.Blok@pg.edu.pl>
Date: Fri, 21 Apr 2023 11:09:18 +0200
Subject: [PATCH] TMorseTable LoadFromFile fix

---
 CHANGELOG                      |   3 +++
 matlab/Polish.mct              | Bin 0 -> 419 bytes
 src/Main.cpp                   |  14 ++++++++++++++
 src/cpp/DSP_modules_misc.cpp   |  26 +++-----------------------
 src/include/DSP_lib.h          |   2 +-
 src/include/DSP_modules_misc.h |   4 +++-
 6 files changed, 24 insertions(+), 25 deletions(-)
 create mode 100644 matlab/Polish.mct

diff --git a/CHANGELOG b/CHANGELOG
index a72b97a..2d6b4e8 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -8,6 +8,9 @@ TODO::
 LAST DONE:
 
 CHANGES:
+- ver. 0.20.026 - <b>2022.04.14</b> Fixed: 
+  - fixed DSP::TMorseTable::LoadFromFile
+  - DSP::TMorseTable::LoadFromFile and DSP::u::MORSEkey::LoadFromFile now read file based on full path to *.mct file
 - ver. 0.20.025 - <b>2022.04.14</b> Changed: 
   - removed "using namespace std"
   - fixed several Linux compatibility issues
diff --git a/matlab/Polish.mct b/matlab/Polish.mct
new file mode 100644
index 0000000000000000000000000000000000000000..7534b590b36209835fcf6a93d2d5f8a9f19501c9
GIT binary patch
literal 419
zcmW;IyDr305P<P-#Vrv+L#Q?y#QnZhcK?fYTldSVG+U@7n~1DLgQz4l9zaB`(dcL-
zBqZV$C^Vb!1iqZ=mzg<dX3mMlYKZps3%mJ3Jexl#QAsitrQ;6?s(>VPND2ldf;ve<
zOeUdUrl3Klp<OaiE;Fzwv(O}S&?)m!EelW~S!fZD4arjIz%tYutw61;LYb^VqpXJo
z*nnoE&9Dbsp&vQuGPw<Z(g(ldb|rE1ZyAJFaad7?;ZR24NJimI5^ybJCY{>@_hR3o
z&mD(L>4h(8gB@$#aODM)r$)a2(OPTx6K>(e+A|!Bqu=BPe&i!8z^7gOt#f!cc>zye
OzYCv1l;Oo_0{#IBZ!hZr

literal 0
HcmV?d00001

diff --git a/src/Main.cpp b/src/Main.cpp
index 58c7c56..7c01e03 100644
--- a/src/Main.cpp
+++ b/src/Main.cpp
@@ -2,6 +2,8 @@
 
 #include <DSP_sockets.h>
 #include <DSP_lib.h>
+#include <DSP_modules_misc.h>
+
 //#include <DSP_AudioMixer.h>
 #include "Main.h"
 
@@ -2951,6 +2953,18 @@ int main(int argc, char*argv[])
   DSP::log.SetLogState(DSP::e::LogState::console | DSP::e::LogState::file);
   DSP::log.SetLogFileName("DSPElib_test_log.txt");
 
+  DSP::log << "test MORSEkey" << std::endl;
+  {
+    DSP::TMorseTable MorseTable;
+    MorseTable.LoadFromFile("../matlab/Polish.mct");
+
+    DSP::Clock_ptr MasterClock = DSP::Clock::CreateMasterClock();
+    DSP::u::MORSEkey morse_key(MasterClock);
+    morse_key.LoadCodeTable("../matlab/Polish.mct");
+
+    DSP::Clock::FreeClocks();
+  }
+
   #ifdef ALSA_support_H
     DSP::log << "test ALSA" << std::endl;
     test_ALSA();
diff --git a/src/cpp/DSP_modules_misc.cpp b/src/cpp/DSP_modules_misc.cpp
index b67ccfe..bcbd420 100644
--- a/src/cpp/DSP_modules_misc.cpp
+++ b/src/cpp/DSP_modules_misc.cpp
@@ -22,7 +22,6 @@
 int DSP::TMorseTable::TablesNo=0;
 DSP::TMorseTable *DSP::TMorseTable::FirstTable=NULL;
 DSP::TMorseTable *DSP::TMorseTable::Current=NULL;
-const std::string &DSP::TMorseTable::BaseDirectory="./";
 
 DSP::TMorseTable::TMorseTable(void)
 {
@@ -431,7 +430,6 @@ uint32_t DSP::TMorseTable::Char2Number(char znak)
 void DSP::TMorseTable::Save2File(const std::string &Name)
 {
   #if _DEMO_ == 0
-    std::string Dir_FileName;
     unsigned char pomB;
     uint16_t pom;
     unsigned long ind;
@@ -439,11 +437,7 @@ void DSP::TMorseTable::Save2File(const std::string &Name)
 
     FileName = Name;
 
-    Dir_FileName = BaseDirectory;
-    Dir_FileName += "config/";
-    Dir_FileName += FileName;
-
-    plik = fopen(Dir_FileName.c_str(), "wb");
+    plik = fopen(FileName.c_str(), "wb");
 
     pomB=FontsEditEntriesNo;
     fwrite(&pomB, sizeof(unsigned char), 1, plik);
@@ -476,7 +470,6 @@ void DSP::TMorseTable::Save2File(const std::string &Name)
 
     fclose(plik);
 
-    Dir_FileName = "";
   #endif // _DEMO_ == 0
 }
 
@@ -484,7 +477,6 @@ void DSP::TMorseTable::Save2File(const std::string &Name)
 
 bool DSP::TMorseTable::LoadFromFile(const std::string &Name)
 {
-  std::string Dir_FileName;
   uint8_t IleFONT;
   uint8_t ver;
   FILE *plik;
@@ -499,11 +491,7 @@ bool DSP::TMorseTable::LoadFromFile(const std::string &Name)
   else
     TableDescription = Name;
 
-  Dir_FileName = BaseDirectory;
-  Dir_FileName += "config/";
-  Dir_FileName += FileName;
-
-  plik = fopen(Dir_FileName.c_str(), "rb");
+  plik = fopen(FileName.c_str(), "rb");
   if (plik == NULL)
     return false;
 
@@ -570,7 +558,7 @@ bool DSP::TMorseTable::LoadFromFile(const std::string &Name)
   {
     if (ind<MaxMorseCodeEntriesNumber)
     {
-      fread(MorseCode+ind, sizeof(uint16_t), 1, plik);
+      fread(MorseCode+ind, sizeof(uint32_t), 1, plik);
 
       fread(CharCode+ind, sizeof(char), 1, plik);
       switch(ver)
@@ -591,7 +579,6 @@ bool DSP::TMorseTable::LoadFromFile(const std::string &Name)
 
   fclose(plik);
 
-  Dir_FileName = "";
   return true;
 }
 
@@ -680,7 +667,6 @@ void DSP::TMorseTable::NewTable(void)
 {
   //TSearchRec F;
   TMorseTable *pom;
-  std::string Dir_FileName;
   int ind_;
 
   pom=FirstTable;
@@ -705,10 +691,6 @@ void DSP::TMorseTable::NewTable(void)
   Current->FileName = Current->TableDescription;
   Current->FileName += ".mct";
 
-  Dir_FileName = Current->BaseDirectory;
-  Dir_FileName += "config/";
-  Dir_FileName += Current->FileName;
-
   /*
   while (FindFirst(Dir_FileName, faAnyFile, F)==0)
   {
@@ -744,8 +726,6 @@ void DSP::TMorseTable::NewTable(void)
   }
   else
     SaveCurrent();
-
-  Dir_FileName = "";
 }
 
 void DSP::TMorseTable::SaveCurrent(void)
diff --git a/src/include/DSP_lib.h b/src/include/DSP_lib.h
index f508d7f..ef40002 100644
--- a/src/include/DSP_lib.h
+++ b/src/include/DSP_lib.h
@@ -11,7 +11,7 @@
 
 #define DSP_VER_MAJOR 0
 #define DSP_VER_MINOR 20
-#define DSP_VER_BUILD 25 // !!! without zeroes before, else this will be treated as octal number
+#define DSP_VER_BUILD 26 // !!! without zeroes before, else this will be treated as octal number
 #define DSP_VER_YEAR  2023
 #define DSP_VER       DSP_VER_MAJOR.DSP_VER_MINOR.DSP_VER_BUILD
 
diff --git a/src/include/DSP_modules_misc.h b/src/include/DSP_modules_misc.h
index 553b423..ba9ccfe 100644
--- a/src/include/DSP_modules_misc.h
+++ b/src/include/DSP_modules_misc.h
@@ -33,7 +33,6 @@ class DSP::TMorseTable
   private:
     static int TablesNo;
     static TMorseTable *FirstTable;
-    static const std::string &BaseDirectory;
 
     TMorseTable *NextTable;
 
@@ -45,6 +44,7 @@ class DSP::TMorseTable
     static TMorseTable *Current;
 
   public:
+    //! loads coding table from file *.mct, where Name if filename with full path
     bool LoadFromFile(const std::string &Name);
 
     std::string FontName[FontsEditEntriesNo];
@@ -154,6 +154,8 @@ class DSP::u::MORSEkey : public DSP::Source // , public DSP::Randomization
         float dash2dot_ratio = 3.0, float space2dot_ratio = 7.0);
 
     bool LoadCodeTable(const std::string &filename);
+    //! \TODO add possibilitu to add user table without using file
+    void SetTable(const TMorseTable &new_table);
 
     //! Changes manually current key state
     /*!
-- 
GitLab