raspikeyer/src/morse.cpp

100 lines
3.5 KiB
C++
Raw Normal View History

2024-02-23 08:58:48 +01:00
#include <algorithm>
#include <map>
#include <regex>
#include "morse.h"
std::map<char, std::string> morseCode = {
{'A', ".-"}, {'B', "-..."}, {'C', "-.-."}, {'D', "-.."}, {'E', "."}, {'F', "..-."},
{'G', "--."}, {'H', "...."}, {'I', ".."}, {'J', ".---"}, {'K', "-.-"}, {'L', ".-.."},
{'M', "--"}, {'N', "-."}, {'O', "---"}, {'P', ".--."}, {'Q', "--.-"}, {'R', ".-."},
{'S', "..."}, {'T', "-"}, {'U', "..-"}, {'V', "...-"}, {'W', ".--"}, {'X', "-..-"},
{'Y', "-.--"}, {'Z', "--.."}, {'1', ".----"}, {'2', "..---"}, {'3', "...--"}, {'4', "....-"},
{'5', "....."}, {'6', "-...."}, {'7', "--..."}, {'8', "---.."}, {'9', "----."}, {'0', "-----"},
{',', "--..--"}, {'.', ".-.-.-"}, {'?', "..--.."}, {'/', "-..-."}, {'-', "-....-"}, {':', "---..."},
{'&', ".-..."}, {'\'', ".----."}, {'@', ".--.-."}, {')', "-.--.-"}, {'(', "-.--."}, {'\"', ".-..-."},
{'=', "-...-"}, // '=' == <BT>
{'k', "-.--."}, // k == <KN>
{'s', "...-.-"}, // s == <SK>
{'+', ".-.-."}, // + == <AR>
{'a', "-.-.-"}, // a == <KA>
};
2024-02-23 23:26:59 +01:00
void refurbishMessage(std::string &msg)
2024-02-23 08:58:48 +01:00
{
2024-02-23 23:26:59 +01:00
printf("The message is: %s\n", msg.c_str());
//std::string msgRefurb;
//msgRefurb.resize(msg.size());
2024-02-23 08:58:48 +01:00
// Make the message all upper case
2024-02-23 23:26:59 +01:00
//std::transform(msg.begin(), msg.end(), msgRefurb.begin(), [](unsigned char c) { return std::toupper(c); });
std::transform(msg.begin(), msg.end(), msg.begin(), ::toupper);
2024-02-23 08:58:48 +01:00
// Encode the special characters as we like it
2024-02-23 23:26:59 +01:00
//msg = std::regex_replace(msg, std::regex("<BT>"), "=");
//msg = std::regex_replace(msg, std::regex("<KN>"), "k");
//msg = std::regex_replace(msg, std::regex("<SK>"), "s");
//msg = std::regex_replace(msg, std::regex("<AR>"), "+");
//msg = std::regex_replace(msg, std::regex("<KA>"), "a");
//printf("Msg Origgg: %s\n", msg.c_str());
2024-02-23 08:58:48 +01:00
// Remove all other unknown characters
2024-02-23 23:26:59 +01:00
//msg.erase(remove_if(msg.begin(), msg.end(),
// [](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }),
// msg.end());
2024-02-23 08:58:48 +01:00
// Remove spaces, if there are too many of them
2024-02-23 23:26:59 +01:00
//msg = std::regex_replace(msg, std::regex("(\\s+)"), " ");
2024-02-23 08:58:48 +01:00
2024-02-23 23:26:59 +01:00
//printf("Msg Origgg2: %s\n", msg.c_str());
//return msgRefurb;
2024-02-23 14:29:36 +01:00
}
2024-02-23 23:26:59 +01:00
std::string messageToMorse(std::string &msg)
2024-02-23 14:29:36 +01:00
{
2024-02-23 23:26:59 +01:00
refurbishMessage(msg);
std::string morseString = "";
2024-02-23 14:29:36 +01:00
2024-02-23 23:26:59 +01:00
printf("Ref Mesg (%i): %s\n", msg.length(), msg.c_str());
for (unsigned int i = 0; i < msg.length(); i++) {
char c = msg[i];
2024-02-23 14:29:36 +01:00
if (c == ' ') {
morseString += 'w';
continue;
}
// Ignore and continue with next char, if not found
auto search = morseCode.find(c);
if (search == morseCode.end()) {
2024-02-23 23:26:59 +01:00
printf("Nanu (i=%i)? %c\n", i, c);
2024-02-23 14:29:36 +01:00
continue;
}
for (unsigned int j = 0; j < morseCode[c].length(); j++) {
2024-02-23 23:26:59 +01:00
char m = morseCode[c][j]; printf("M = %c\n", m);
if (j == 0 && i > 0 && msg[i - 1] != ' ') {
2024-02-23 14:29:36 +01:00
morseString += 'c';
}
morseString += m;
if (j < morseCode[c].length() - 1) {
morseString += 'i';
}
}
}
// Append word space if last char was not a blank
2024-02-23 23:26:59 +01:00
if (msg.back() != ' ') {
2024-02-23 14:29:36 +01:00
morseString += 'w';
}
2024-02-23 23:26:59 +01:00
printf("Sodele: %s\n", morseString.c_str());
printf("Ref Mesg 2 (%i): %s\n", msg.length(), msg.c_str());
2024-02-23 14:29:36 +01:00
return morseString;
2024-02-23 08:58:48 +01:00
}