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