raspikeyer/src/morse.cpp

186 lines
No EOL
4.4 KiB
C++

// #include <algorithm>
#include <map>
#include <string>
// #include <regex>
#include "morse.h"
std::map<char, std::string> morseMap = {
{'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', "-----"},
{',', "--..--"},
{'.', ".-.-.-"},
{'?', "..--.."},
{':', "---..."},
{'&', ".-..."},
// Some WinKeyer compatible mappings …
{'\"', ".-..-."}, // " => <RR>
{'$', "...-..-"}, // $ => <SX>
{'\'', ".----."}, // ' => <WG>
{'(', "-.--."}, // ( => <KN>
{')', "-.--.-"}, // ) => <KK>
{'+', ".-.-."}, // + => <AR>
{'-', "-....-"}, // - => <DU>
{'/', "-..-."}, // / => <DN>
{':', "-.--."}, // : => <KN>
{';', ".-.-"}, // ; => <AA>
{'<', ".-.-."}, // < => <AR> [sic!]
{'=', "-...-"}, // = => <BT>
{'>', "...-.-"}, // > == <SK>
{'@', ".--.-."}, // @ => <AC>
{'[', ".-..."}, // [ => <AS>
{'\\', "-..-."}, // \ => <DN>
{']', "-.--."}, // ] => <KN> [sic!]
};
/*
void refurbishMessage(std::string &msg)
{
// Make the message all upper case
std::transform(msg.begin(), msg.end(), msg.begin(), ::toupper);
// Encode the special characters as we like it
msg = std::regex_replace(msg, std::regex("<BT>"), "=");
msg = std::regex_replace(msg, std::regex("<BK>"), "b");
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");
// Remove all other unknown characters
msg.erase(remove_if(msg.begin(), msg.end(),
[](const char &c) { return c != ' ' && morseMap.find(c) == morseMap.end(); }),
msg.end());
// Remove spaces, if there are too many of them
msg = std::regex_replace(msg, std::regex("(\\s+)"), " ");
}
*/
std::string messageToMorse(std::string &msg)
{
// refurbishMessage(msg);
std::string morseString {};
for (unsigned int i = 0; i < msg.length(); i++) {
char c = msg[i];
if (c == ' ') {
morseString.push_back('w');
continue;
}
// Ignore and continue with next char, if not found
auto search = morseMap.find(c);
if (search == morseMap.end()) {
continue;
}
for (unsigned int j = 0; j < morseMap[c].length(); j++) {
char m = morseMap[c][j];
if (j == 0 && i > 0 && msg[i - 1] != ' ') {
morseString.push_back('c');
}
morseString += m;
if (j < morseMap[c].length() - 1) {
morseString.push_back('i');
}
}
}
// Append word space if last char was not a blank
if (msg.back() != ' ') {
morseString.push_back('w');
}
return morseString;
}
std::string charToMorse(const char ch)
{
std::string morseString {};
if (ch == ' ') {
morseString.push_back('w');
return morseString;
}
for (unsigned int j = 0; j < morseMap[ch].length(); j++) {
char m = morseMap[ch][j];
morseString += m;
if (j < morseMap[ch].length() - 1) {
morseString.push_back('i');
}
}
morseString.push_back('c');
return morseString;
}
bool charToMorse(const char ch, char *morseSymbols)
{
size_t index {0};
if (ch != ' ' && !morseMap.contains(ch)) {
return false;
}
if (ch == ' ') {
morseSymbols[index++] = 'w';
} else {
for (unsigned int i = 0; i < morseMap[ch].length(); i++) {
char m = morseMap[ch][i];
morseSymbols[index++] = m;
if (i < morseMap[ch].length() - 1) {
morseSymbols[index++] = 'i';
}
}
morseSymbols[index++] = 'c';
}
morseSymbols[index] = '\0';
return true;
}