55 lines
2.2 KiB
C++
55 lines
2.2 KiB
C++
#include <cstdio>
|
|
#include <cstring>
|
|
#if __BYTE_ORDER__ != 1234
|
|
#error "BAD ENDIAN"
|
|
#endif
|
|
static const char * oname = "table.cpp";
|
|
static const char * const morse_code [] = { /* nedefinované znaky nahrazeny mezerou */
|
|
" ", /* */ " ", /*!*/ ".-..-.", /*"*/ " ", /*#*/ " ", /*$*/
|
|
" ", /*%*/ " ", /*&*/ ".----.", /*'*/ "-.--.", /*(*/ "-.--.-", /*)*/
|
|
" ", /***/ ".-.-.", /*+*/ "--..--", /*,*/ "-....-", /*-*/ ".-.-.-", /*.*/ "-..-." , /*/*/
|
|
"-----", /*0*/ ".----", /*1*/ "..---", /*2*/ "...--", /*3*/ "....-", /*4*/
|
|
".....", /*5*/ "-....", /*6*/ "--...", /*7*/ "---..", /*8*/ "----.", /*9*/
|
|
"---...", /*:*/ "-.-.-.", /*;*/ " ", /*<*/ "-...-" , /*=*/ " ", /*>*/ "..--..", /*?*/
|
|
".--.-.", /*@*/
|
|
".-", /*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*/ " ", " ", " ", " ", "..--.-", /*_*/
|
|
};
|
|
union morse_byte {
|
|
struct {
|
|
unsigned char bits : 5; // Jednotlivé bity.
|
|
unsigned char mlen : 3; // A jejich délka - max 6.
|
|
}; // Pokud je 6 nebo 7, pak je nejnižší bit zároveň součástí bitů
|
|
unsigned char byte;
|
|
explicit constexpr morse_byte () noexcept : byte (0u) {}
|
|
};
|
|
static morse_byte compress (const unsigned n) {
|
|
const char * const ptr = morse_code [n];
|
|
const unsigned len = strlen (ptr);
|
|
morse_byte mb;
|
|
if (ptr [0] == ' ') return mb;
|
|
mb.mlen = len;
|
|
for (unsigned n=0; n<len; n++) {
|
|
if (ptr [n] == '-') mb.byte |= (1u << n);
|
|
}
|
|
return mb;
|
|
}
|
|
/* Generátor komprimované tabulky morse kódů */
|
|
int main () {
|
|
FILE * out = fopen(oname, "w");
|
|
if (!out) return 1;
|
|
unsigned n = 0u;
|
|
fprintf(out, "static const unsigned char compressed_table [] = {");
|
|
for (auto & e: morse_code) {
|
|
if ((n & 0xf) == 0u) fprintf(out, "\n");
|
|
fprintf(out, " 0x%02x,", compress(n).byte);
|
|
n += 1u;
|
|
}
|
|
fprintf(out, "\n};\n");
|
|
|
|
fclose (out);
|
|
return 0;
|
|
}
|