// SPDX-License-Identifier: WTFPL #include #include #include #include #include "lev.h" #include "table.h" extern char *optarg; static const struct morse_table *fetch_code(wchar_t c) { int i; for (i = 0; i < codetable_entry; i++) { if (wcschr(codetable[i].characters, c) != NULL) return &codetable[i]; } return NULL; } void decode(char *out, const char *in) { int i, n = strlen(in); for (i = 0; i < n; i++) out += sprintf(out, (in[i] == '-') ? "ooox" : "ox"); sprintf(out, "xx"); } int main(int argc, char *argv[]) { int ch, i, j, d, compact = 0, verbose = 0; const struct morse_table *t, *u; const char *p; char buf_t[256], buf_u[256]; wchar_t koch[256] = L"KMURESNAPTLWI.JZ=FOY,VG5/Q92H38B?47C1D60X"; setlocale(LC_CTYPE, "en_US.UTF-8"); /* UTF-8 locale required */ while ((ch = getopt(argc, argv, "s:vc")) != -1) { switch (ch) { case 'v': verbose = 1; break; case 'c': compact = 1; break; case 's': memset(koch, 0, sizeof(koch)); p = optarg; mbsrtowcs(koch, &p, sizeof(koch) / sizeof(wchar_t) - 1, NULL); break; } } printf(" "); for (j = 0; koch[j]; j++) { printf("%lc ", koch[j]); if (koch[j] < 0x100) printf(" "); // XXX } printf("\n"); if (compact) { printf(" "); for (i = 0; koch[i]; i++) { u = fetch_code(koch[i]); decode(buf_u, u->code); t = fetch_code(koch[(i == 0) ? i : (i - 1)]); decode(buf_t, t->code); d = verbose ? LevenshteinDistance(buf_u, buf_t) : LevenshteinDistance(u->code, t->code); printf("%2d ", d); } printf("\n"); } else { for (j = 0; koch[j]; j++) { u = fetch_code(koch[j]); decode(buf_u, u->code); printf("%lc ", koch[j]); for (i = 0; koch[i]; i++) { t = fetch_code(koch[i]); decode(buf_t, t->code); d = verbose ? LevenshteinDistance(buf_u, buf_t) : LevenshteinDistance(u->code, t->code); if (i <= j) printf("%2d ", d); } printf("\n"); } } return 0; }