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>
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string refurbishMessage(const std::string &msg)
|
|
|
|
{
|
2024-02-23 09:09:40 +01:00
|
|
|
std::string msgRefurb;
|
|
|
|
msgRefurb.resize(msg.length());
|
2024-02-23 08:58:48 +01:00
|
|
|
|
|
|
|
// Make the message all upper case
|
2024-02-23 09:09:40 +01:00
|
|
|
std::transform(msg.cbegin(), msg.cend(), msgRefurb.begin(), [](unsigned char c) { return std::toupper(c); });
|
2024-02-23 08:58:48 +01:00
|
|
|
|
|
|
|
// Encode the special characters as we like it
|
2024-02-23 09:09:40 +01:00
|
|
|
msgRefurb = std::regex_replace(msgRefurb, std::regex("<BT>"), "=");
|
|
|
|
msgRefurb = std::regex_replace(msgRefurb, std::regex("<KN>"), "k");
|
|
|
|
msgRefurb = std::regex_replace(msgRefurb, std::regex("<SK>"), "s");
|
|
|
|
msgRefurb = std::regex_replace(msgRefurb, std::regex("<AR>"), "+");
|
|
|
|
msgRefurb = std::regex_replace(msgRefurb, std::regex("<KA>"), "a");
|
2024-02-23 08:58:48 +01:00
|
|
|
|
|
|
|
// Remove all other unknown characters
|
2024-02-23 09:09:40 +01:00
|
|
|
msgRefurb.erase(remove_if(msgRefurb.begin(), msgRefurb.end(),
|
2024-02-23 08:58:48 +01:00
|
|
|
[](const char &c) { return c != ' ' && morseCode.find(c) == morseCode.end(); }),
|
2024-02-23 09:09:40 +01:00
|
|
|
msgRefurb.end());
|
2024-02-23 08:58:48 +01:00
|
|
|
|
|
|
|
// Remove spaces, if there are too many of them
|
2024-02-23 09:09:40 +01:00
|
|
|
msgRefurb = std::regex_replace(msgRefurb, std::regex("(\\s+)"), " ");
|
2024-02-23 08:58:48 +01:00
|
|
|
|
2024-02-23 09:09:40 +01:00
|
|
|
return msgRefurb;
|
2024-02-23 08:58:48 +01:00
|
|
|
}
|