raspikeyer/src/morse.cpp

156 lines
3.8 KiB
C++
Raw Normal View History

2024-02-28 13:27:09 +01:00
//#include <algorithm>
2024-02-23 08:58:48 +01:00
#include <map>
2024-02-28 13:27:09 +01:00
//#include <regex>
2024-02-23 08:58:48 +01:00
#include "morse.h"
2024-02-28 13:35:09 +01:00
std::map<char, std::string> morseMap = {
2024-02-28 13:19:33 +01:00
{'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!]
2024-02-23 08:58:48 +01:00
};
2024-02-28 13:27:09 +01:00
/*
2024-02-23 23:26:59 +01:00
void refurbishMessage(std::string &msg)
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(), msg.begin(), ::toupper);
2024-02-23 08:58:48 +01:00
// Encode the special characters as we like it
2024-02-25 21:28:42 +01:00
msg = std::regex_replace(msg, std::regex("<BT>"), "=");
2024-02-25 21:37:37 +01:00
msg = std::regex_replace(msg, std::regex("<BK>"), "b");
2024-02-25 21:28:42 +01:00
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");
2024-02-23 08:58:48 +01:00
// Remove all other unknown characters
2024-02-25 21:28:42 +01:00
msg.erase(remove_if(msg.begin(), msg.end(),
2024-02-28 13:35:09 +01:00
[](const char &c) { return c != ' ' && morseMap.find(c) == morseMap.end(); }),
2024-02-25 21:37:37 +01:00
msg.end());
2024-02-23 08:58:48 +01:00
// Remove spaces, if there are too many of them
2024-02-25 21:28:42 +01:00
msg = std::regex_replace(msg, std::regex("(\\s+)"), " ");
2024-02-23 14:29:36 +01:00
}
2024-02-28 13:27:09 +01:00
*/
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-28 13:27:09 +01:00
//refurbishMessage(msg);
2024-02-23 14:29:36 +01:00
2024-02-25 21:37:37 +01:00
std::string morseString {};
2024-02-23 23:26:59 +01:00
for (unsigned int i = 0; i < msg.length(); i++) {
char c = msg[i];
2024-02-23 14:29:36 +01:00
if (c == ' ') {
2024-02-25 21:28:42 +01:00
morseString.push_back('w');
2024-02-23 14:29:36 +01:00
continue;
}
// Ignore and continue with next char, if not found
2024-02-28 13:35:09 +01:00
auto search = morseMap.find(c);
if (search == morseMap.end()) {
2024-02-23 14:29:36 +01:00
continue;
}
2024-02-28 13:35:09 +01:00
for (unsigned int j = 0; j < morseMap[c].length(); j++) {
char m = morseMap[c][j];
2024-02-23 23:26:59 +01:00
if (j == 0 && i > 0 && msg[i - 1] != ' ') {
2024-02-25 21:28:42 +01:00
morseString.push_back('c');
2024-02-23 14:29:36 +01:00
}
morseString += m;
2024-02-28 13:35:09 +01:00
if (j < morseMap[c].length() - 1) {
2024-02-25 21:28:42 +01:00
morseString.push_back('i');
2024-02-23 14:29:36 +01:00
}
}
}
// Append word space if last char was not a blank
2024-02-23 23:26:59 +01:00
if (msg.back() != ' ') {
2024-02-26 15:57:13 +01:00
morseString.push_back('w');
2024-02-23 14:29:36 +01:00
}
return morseString;
2024-02-26 15:19:11 +01:00
}
2024-02-28 13:19:33 +01:00
std::string charToMorse(char ch)
{
2024-02-26 15:19:11 +01:00
std::string morseString {};
if (ch == ' ') {
2024-02-28 13:19:33 +01:00
morseString.push_back('w');
return morseString;
}
2024-02-26 15:19:11 +01:00
2024-02-28 13:35:09 +01:00
for (unsigned int j = 0; j < morseMap[ch].length(); j++) {
char m = morseMap[ch][j];
2024-02-26 15:19:11 +01:00
2024-02-28 13:19:33 +01:00
morseString += m;
2024-02-26 15:19:11 +01:00
2024-02-28 13:35:09 +01:00
if (j < morseMap[ch].length() - 1) {
2024-02-28 13:19:33 +01:00
morseString.push_back('i');
2024-02-26 15:19:11 +01:00
}
2024-02-28 13:19:33 +01:00
}
2024-02-26 15:19:11 +01:00
2024-02-26 15:57:13 +01:00
morseString.push_back('c');
2024-02-26 15:19:11 +01:00
return morseString;
2024-02-23 08:58:48 +01:00
}