-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathtt_text.c
1732 lines (1376 loc) · 42.2 KB
/
tt_text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// This file is part of Dire Wolf, an amateur radio packet TNC.
//
// Copyright (C) 2013, 2015 John Langner, WB2OSZ
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
/*------------------------------------------------------------------
*
* Module: tt-text.c
*
* Purpose: Translate between text and touch tone representation.
*
* Description: Letters can be represented by different touch tone
* keypad sequences.
*
* References: This is based upon APRStt (TM) documents but not 100%
* compliant due to ambiguities and inconsistencies in
* the specifications.
*
* http://www.aprs.org/aprstt.html
*
*---------------------------------------------------------------*/
/*
* There are two different encodings called:
*
* * Two-key
*
* Digits are represented by a single key press.
* Letters (or space) are represented by the corresponding
* key followed by A, B, C, or D depending on the position
* of the letter.
*
* * Multi-press
*
* Letters are represented by one or more key presses
* depending on their position.
* e.g. on 5/JKL key, J = 1 press, K = 2, etc.
* The digit is the number of letters plus 1.
* In this case, press 5 key four times to get digit 5.
* When two characters in a row use the same key,
* use the "A" key as a separator.
*
* Examples:
*
* Character Multipress Two Key Comments
* --------- ---------- ------- --------
* 0 00 0 Space is handled like a letter.
* 1 1 1 No letters on 1 button.
* 2 2222 2 3 letters -> 4 key presses
* 9 99999 9
* W 9 9A
* X 99 9B
* Y 999 9C
* Z 9999 9D
* space 0 0A 0A was used in an APRStt comment example.
*
*
* Note that letters can occur in callsigns and comments.
* Everywhere else they are simply digits.
*
*
* * New fixed length callsign format
*
*
* The "QIKcom-2" project adds a new format where callsigns are represented by
* a fixed length string of only digits. The first 6 digits are the buttons corresponding
* to the letters. The last 4 take a little calculation. Example:
*
* W B 4 A P R original.
* 9 2 4 2 7 7 corresponding button.
* 1 2 0 1 1 2 character position on key. 0 for the digit.
*
* Treat the last line as a base 4 number.
* Convert it to base 10 and we get 1558 for the last four digits.
*/
/*
* Everything is based on this table.
* Changing it will change everything.
* In other words, don't mess with it.
* The world will come crumbling down.
*/
static const char translate[10][4] = {
/* A B C D */
/* --- --- --- --- */
/* 0 */ { ' ', 0, 0, 0 },
/* 1 */ { 0, 0, 0, 0 },
/* 2 */ { 'A', 'B', 'C', 0 },
/* 3 */ { 'D', 'E', 'F', 0 },
/* 4 */ { 'G', 'H', 'I', 0 },
/* 5 */ { 'J', 'K', 'L', 0 },
/* 6 */ { 'M', 'N', 'O', 0 },
/* 7 */ { 'P', 'Q', 'R', 'S' },
/* 8 */ { 'T', 'U', 'V', 0 },
/* 9 */ { 'W', 'X', 'Y', 'Z' } };
/*
* This is for the new 10 character fixed length callsigns for APRStt 3.
* Notice that it uses an old keypad layout with Q & Z on the 1 button.
* The TH-D72A and all telephones that I could find all have
* four letters each on the 7 and 9 buttons.
* This inconsistency is sure to cause confusion but the 6+4 scheme won't
* be possible with more than 4 characters assigned to one button.
* 4**6-1 = 4096 which fits in 4 decimal digits.
* 5**6-1 = 15624 would not fit.
*
* The column is a two bit code packed into the last 4 digits.
*/
static const char call10encoding[10][4] = {
/* 0 1 2 3 */
/* --- --- --- --- */
/* 0 */ { '0', ' ', 0, 0 },
/* 1 */ { '1', 'Q', 'Z', 0 },
/* 2 */ { '2', 'A', 'B', 'C' },
/* 3 */ { '3', 'D', 'E', 'F' },
/* 4 */ { '4', 'G', 'H', 'I' },
/* 5 */ { '5', 'J', 'K', 'L' },
/* 6 */ { '6', 'M', 'N', 'O' },
/* 7 */ { '7', 'P', 'R', 'S' },
/* 8 */ { '8', 'T', 'U', 'V' },
/* 9 */ { '9', 'W', 'X', 'Y' } };
/*
* Special satellite 4 digit gridsquares to cover "99.99% of the world's population."
*/
static const char grid[10][10][3] =
{ { "AP", "BP", "AO", "BO", "CO", "DO", "EO", "FO", "GO", "OJ" }, // 0 - Canada
{ "CN", "DN", "EN", "FN", "GN", "CM", "DM", "EM", "FM", "OI" }, // 1 - USA
{ "DL", "EL", "FL", "DK", "EK", "FK", "EJ", "FJ", "GJ", "PI" }, // 2 - C. America
{ "FI", "GI", "HI", "FH", "GH", "HH", "FG", "GG", "FF", "GF" }, // 3 - S. America
{ "JP", "IO", "JO", "KO", "IN", "JN", "KN", "IM", "JM", "KM" }, // 4 - Europe
{ "LO", "MO", "NO", "OO", "PO", "QO", "RO", "LN", "MN", "NN" }, // 5 - Russia
{ "ON", "PN", "QN", "OM", "PM", "QM", "OL", "PL", "OK", "PK" }, // 6 - Japan, China
{ "LM", "MM", "NM", "LL", "ML", "NL", "LK", "MK", "NK", "LJ" }, // 7 - India
{ "PH", "QH", "OG", "PG", "QG", "OF", "PF", "QF", "RF", "RE" }, // 8 - Aus / NZ
{ "IL", "IK", "IJ", "JJ", "JI", "JH", "JG", "KG", "JF", "KF" } }; // 9 - Africa
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdarg.h>
#include "direwolf.h"
#include "textcolor.h"
#include "tt_text.h"
#if defined(ENC_MAIN) || defined(DEC_MAIN)
void text_color_set (dw_color_t c) { return; }
int dw_printf (const char *fmt, ...)
{
va_list args;
int len;
va_start (args, fmt);
len = vprintf (fmt, args);
va_end (args);
return (len);
}
#endif
/*------------------------------------------------------------------
*
* Name: tt_text_to_multipress
*
* Purpose: Convert text to the multi-press representation.
*
* Inputs: text - Input string.
* Should contain only digits, letters, or space.
* All other punctuation is treated as space.
*
* quiet - True to suppress error messages.
*
* Outputs: buttons - Sequence of buttons to press.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
int tt_text_to_multipress (const char *text, int quiet, char *buttons)
{
const char *t = text;
char *b = buttons;
char c;
int row, col;
int errors = 0;
int found;
int n;
*b = '\0';
while ((c = *t++) != '\0') {
if (isdigit(c)) {
/* Count number of other characters assigned to this button. */
/* Press that number plus one more. */
n = 1;
row = c - '0';
for (col=0; col<4; col++) {
if (translate[row][col] != 0) {
n++;
}
}
if (buttons[0] != '\0' && *(b-1) == row + '0') {
*b++ = 'A';
}
while (n--) {
*b++ = row + '0';
*b = '\0';
}
}
else {
if (isupper(c)) {
;
}
else if (islower(c)) {
c = toupper(c);
}
else if (c != ' ') {
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to multi-press: Only letters, digits, and space allowed.\n");
}
c = ' ';
}
/* Search for everything else in the translation table. */
/* Press number of times depending on column where found. */
found = 0;
for (row=0; row<10 && ! found; row++) {
for (col=0; col<4 && ! found; col++) {
if (c == translate[row][col]) {
/* Stick in 'A' if previous character used same button. */
if (buttons[0] != '\0' && *(b-1) == row + '0') {
*b++ = 'A';
}
n = col + 1;
while (n--) {
*b++ = row + '0';
*b = '\0';
found = 1;
}
}
}
}
if (! found) {
errors++;
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to multi-press: INTERNAL ERROR. Should not be here.\n");
}
}
}
return (errors);
} /* end tt_text_to_multipress */
/*------------------------------------------------------------------
*
* Name: tt_text_to_two_key
*
* Purpose: Convert text to the two-key representation.
*
* Inputs: text - Input string.
* Should contain only digits, letters, or space.
* All other punctuation is treated as space.
*
* quiet - True to suppress error messages.
*
* Outputs: buttons - Sequence of buttons to press.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
int tt_text_to_two_key (const char *text, int quiet, char *buttons)
{
const char *t = text;
char *b = buttons;
char c;
int row, col;
int errors = 0;
int found;
*b = '\0';
while ((c = *t++) != '\0') {
if (isdigit(c)) {
/* Digit is single key press. */
*b++ = c;
*b = '\0';
}
else {
if (isupper(c)) {
;
}
else if (islower(c)) {
c = toupper(c);
}
else if (c != ' ') {
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to two key: Only letters, digits, and space allowed.\n");
}
c = ' ';
}
/* Search for everything else in the translation table. */
found = 0;
for (row=0; row<10 && ! found; row++) {
for (col=0; col<4 && ! found; col++) {
if (c == translate[row][col]) {
*b++ = '0' + row;
*b++ = 'A' + col;
*b = '\0';
found = 1;
}
}
}
if (! found) {
errors++;
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to two-key: INTERNAL ERROR. Should not be here.\n");
}
}
}
return (errors);
} /* end tt_text_to_two_key */
/*------------------------------------------------------------------
*
* Name: tt_letter_to_two_digits
*
* Purpose: Convert one letter to 2 digit representation.
*
* Inputs: c - One letter.
*
* quiet - True to suppress error messages.
*
* Outputs: buttons - Sequence of two buttons to press.
* "00" for error because this is probably
* being used to build up a fixed length
* string where positions are signficant.
* Must be at least 3 bytes.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
// TODO: need to test this.
int tt_letter_to_two_digits (char c, int quiet, char buttons[3])
{
int row, col;
int errors = 0;
int found;
strlcpy(buttons, "", 3);
if (islower(c)) {
c = toupper(c);
}
if ( ! isupper(c)) {
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Letter to two digits: \"%c\" found where a letter is required.\n", c);
}
strlcpy (buttons, "00", 3);
return (errors);
}
/* Search in the translation table. */
found = 0;
for (row=0; row<10 && ! found; row++) {
for (col=0; col<4 && ! found; col++) {
if (c == translate[row][col]) {
buttons[0] = '0' + row;
buttons[1] = '1' + col;
buttons[2] = '\0';
found = 1;
}
}
}
if (! found) {
errors++;
text_color_set (DW_COLOR_ERROR);
dw_printf ("Letter to two digits: INTERNAL ERROR. Should not be here.\n");
strlcpy (buttons, "00", 3);
}
return (errors);
} /* end tt_letter_to_two_digits */
/*------------------------------------------------------------------
*
* Name: tt_text_to_call10
*
* Purpose: Convert text to the 10 character callsign format.
*
* Inputs: text - Input string.
* Should contain from 1 to 6 letters and digits.
*
* quiet - True to suppress error messages.
*
* Outputs: buttons - Sequence of buttons to press.
* Should be exactly 10 unless error.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
int tt_text_to_call10 (const char *text, int quiet, char *buttons)
{
const char *t;
char *b;
char c;
int packed; /* two bits per character */
int row, col;
int errors = 0;
int found;
char padded[8];
char stemp[8];
strcpy (buttons, "");
/* Quick validity check. */
if (strlen(text) < 1 || strlen(text) > 6) {
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to callsign 6+4: Callsign \"%s\" not between 1 and 6 characters.\n", text);
}
errors++;
return (errors);
}
for (t = text; *t != '\0'; t++) {
if (! isalnum(*t)) {
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to callsign 6+4: Callsign \"%s\" can contain only letters and digits.\n", text);
}
errors++;
return (errors);
}
}
/* Append spaces if less than 6 characters. */
strcpy (padded, text);
while (strlen(padded) < 6) {
strcat (padded, " ");
}
b = buttons;
packed = 0;
for (t = padded; *t != '\0'; t++) {
c = *t;
if (islower(c)) {
c = toupper(c);
}
/* Search in the translation table. */
found = 0;
for (row=0; row<10 && ! found; row++) {
for (col=0; col<4 && ! found; col++) {
if (c == call10encoding[row][col]) {
*b++ = '0' + row;
*b = '\0';
packed = packed * 4 + col; /* base 4 to binary */
found = 1;
}
}
}
if (! found) {
/* Earlier check should have caught any character not in translation table. */
errors++;
text_color_set (DW_COLOR_ERROR);
dw_printf ("Text to callsign 6+4: INTERNAL ERROR 0x%02x. Should not be here.\n", c);
}
}
/* Binary to decimal for the columns. */
snprintf (stemp, sizeof(stemp), "%04d", packed);
strcat (buttons, stemp);
return (errors);
} /* end tt_text_to_call10 */
/*------------------------------------------------------------------
*
* Name: tt_text_to_satsq
*
* Purpose: Convert Special Satellite Gridsquare to 4 digit DTMF representation.
*
* Inputs: text - Input string.
* Should be two letters (A thru R) and two digits.
*
* quiet - True to suppress error messages.
*
* Outputs: buttons - Sequence of buttons to press.
* Should be 4 digits unless error.
*
* Returns: Number of errors detected.
*
* Example: "FM19" is converted to "1819."
* "AA00" is converted to empty string and error return code.
*
*----------------------------------------------------------------*/
int tt_text_to_satsq (const char *text, int quiet, char *buttons, size_t buttonsize)
{
int row, col;
int errors = 0;
int found;
char uc[3];
strlcpy (buttons, "", buttonsize);
/* Quick validity check. */
if (strlen(text) < 1 || strlen(text) > 4) {
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Satellite Gridsquare to DTMF: Gridsquare \"%s\" must be 4 characters.\n", text);
}
errors++;
return (errors);
}
/* Changing to upper case makes things easier later. */
uc[0] = islower(text[0]) ? toupper(text[0]) : text[0];
uc[1] = islower(text[1]) ? toupper(text[1]) : text[1];
uc[2] = '\0';
if (uc[0] < 'A' || uc[0] > 'R' || uc[1] < 'A' || uc[1] > 'R') {
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Satellite Gridsquare to DTMF: First two characters \"%s\" must be letters in range of A to R.\n", text);
}
errors++;
return (errors);
}
if (! isdigit(text[2]) || ! isdigit(text[3])) {
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Satellite Gridsquare to DTMF: Last two characters \"%s\" must digits.\n", text);
}
errors++;
return (errors);
}
/* Search in the translation table. */
found = 0;
for (row=0; row<10 && ! found; row++) {
for (col=0; col<10 && ! found; col++) {
if (strcmp(uc,grid[row][col]) == 0) {
char btemp[8];
btemp[0] = row + '0';
btemp[1] = col + '0';
btemp[2] = text[2];
btemp[3] = text[3];
btemp[4] = '\0';
strlcpy (buttons, btemp, buttonsize);
found = 1;
}
}
}
if (! found) {
/* Sorry, Greenland, and half of Africa, and ... */
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Satellite Gridsquare to DTMF: Sorry, your location can't be converted to DTMF.\n");
}
}
return (errors);
} /* end tt_text_to_satsq */
/*------------------------------------------------------------------
*
* Name: tt_text_to_ascii2d
*
* Purpose: Convert text to the two digit per ascii character representation.
*
* Inputs: text - Input string.
* Any printable ASCII characters.
*
* quiet - True to suppress error messages.
*
* Outputs: buttons - Sequence of buttons to press.
*
* Returns: Number of errors detected.
*
* Description: The standard comment format uses the multipress
* encoding which allows only single case letters, digits,
* and the space character.
* This is a more flexible format that can handle all
* printable ASCII characters. We take the character code,
* subtract 32 and convert to two decimal digits. i.e.
* space = 00
* ! = 01
* " = 02
* ...
* ~ = 94
*
* This is mostly for internal use, so macros can generate
* comments with all characters.
*
*----------------------------------------------------------------*/
int tt_text_to_ascii2d (const char *text, int quiet, char *buttons)
{
const char *t = text;
char *b = buttons;
char c;
int errors = 0;
*b = '\0';
while ((c = *t++) != '\0') {
int n;
/* "isprint()" might depend on locale so use brute force. */
if (c < ' ' || c > '~') c = '?';
n = c - 32;
*b++ = (n / 10) + '0';
*b++ = (n % 10) + '0';
*b = '\0';
}
return (errors);
} /* end tt_text_to_ascii2d */
/*------------------------------------------------------------------
*
* Name: tt_multipress_to_text
*
* Purpose: Convert the multi-press representation to text.
*
* Inputs: buttons - Input string.
* Should contain only 0123456789A.
*
* quiet - True to suppress error messages.
*
* Outputs: text - Converted to letters, digits, space.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
int tt_multipress_to_text (const char *buttons, int quiet, char *text)
{
const char *b = buttons;
char *t = text;
char c;
int row, col;
int errors = 0;
int maxspan;
int n;
*t = '\0';
while ((c = *b++) != '\0') {
if (isdigit(c)) {
/* Determine max that can occur in a row. */
/* = number of other characters assigned to this button + 1. */
maxspan = 1;
row = c - '0';
for (col=0; col<4; col++) {
if (translate[row][col] != 0) {
maxspan++;
}
}
/* Count number of consecutive same digits. */
n = 1;
while (c == *b) {
b++;
n++;
}
if (n < maxspan) {
*t++ = translate[row][n-1];
*t = '\0';
}
else if (n == maxspan) {
*t++ = c;
*t = '\0';
}
else {
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Multi-press to text: Maximum of %d \"%c\" can occur in a row.\n", maxspan, c);
}
/* Treat like the maximum length. */
*t++ = c;
*t = '\0';
}
}
else if (c == 'A' || c == 'a') {
/* Separator should occur only if digit before and after are the same. */
if (b == buttons + 1 || *b == '\0' || *(b-2) != *b) {
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Multi-press to text: \"A\" can occur only between two same digits.\n");
}
}
}
else {
/* Completely unexpected character. */
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Multi-press to text: \"%c\" not allowed.\n", c);
}
}
}
return (errors);
} /* end tt_multipress_to_text */
/*------------------------------------------------------------------
*
* Name: tt_two_key_to_text
*
* Purpose: Convert the two key representation to text.
*
* Inputs: buttons - Input string.
* Should contain only 0123456789ABCD.
*
* quiet - True to suppress error messages.
*
* Outputs: text - Converted to letters, digits, space.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
int tt_two_key_to_text (const char *buttons, int quiet, char *text)
{
const char *b = buttons;
char *t = text;
char c;
int row, col;
int errors = 0;
*t = '\0';
while ((c = *b++) != '\0') {
if (isdigit(c)) {
/* Letter (or space) if followed by ABCD. */
row = c - '0';
col = -1;
if (*b >= 'A' && *b <= 'D') {
col = *b++ - 'A';
}
else if (*b >= 'a' && *b <= 'd') {
col = *b++ - 'a';
}
if (col >= 0) {
if (translate[row][col] != 0) {
*t++ = translate[row][col];
*t = '\0';
}
else {
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Two key to text: Invalid combination \"%c%c\".\n", c, col+'A');
}
}
}
else {
*t++ = c;
*t = '\0';
}
}
else if ((c >= 'A' && c <= 'D') || (c >= 'a' && c <= 'd')) {
/* ABCD not expected here. */
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Two-key to text: A, B, C, or D in unexpected location.\n");
}
}
else {
/* Completely unexpected character. */
errors++;
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Two-key to text: Invalid character \"%c\".\n", c);
}
}
}
return (errors);
} /* end tt_two_key_to_text */
/*------------------------------------------------------------------
*
* Name: tt_two_digits_to_letter
*
* Purpose: Convert the two digit representation to one letter.
*
* Inputs: buttons - Input string.
* Should contain exactly two digits.
*
* quiet - True to suppress error messages.
*
* textsiz - Size of result storage. Typically 2.
*
* Outputs: text - Converted to string which should contain one upper case letter.
* Empty string on error.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/
int tt_two_digits_to_letter (const char *buttons, int quiet, char *text, size_t textsiz)
{
char c1 = buttons[0];
char c2 = buttons[1];
int row, col;
int errors = 0;
char stemp2[2];
strlcpy (text, "", textsiz);
if (c1 >= '2' && c1 <= '9') {
if (c2 >= '1' && c2 <= '4') {
row = c1 - '0';
col = c2 - '1';
if (translate[row][col] != 0) {
stemp2[0] = translate[row][col];
stemp2[1] = '\0';
strlcpy (text, stemp2, textsiz);
}
else {
errors++;
strlcpy (text, "", textsiz);
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Two digits to letter: Invalid combination \"%c%c\".\n", c1, c2);
}
}
}
else {
errors++;
strlcpy (text, "", textsiz);
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Two digits to letter: Second character \"%c\" must be in range of 1 through 4.\n", c2);
}
}
}
else {
errors++;
strlcpy (text, "", textsiz);
if (! quiet) {
text_color_set (DW_COLOR_ERROR);
dw_printf ("Two digits to letter: First character \"%c\" must be in range of 2 through 9.\n", c1);
}
}
return (errors);
} /* end tt_two_digits_to_letter */
/*------------------------------------------------------------------
*
* Name: tt_call10_to_text
*
* Purpose: Convert the 10 digit callsign representation to text.
*
* Inputs: buttons - Input string.
* Should contain only ten digits.
*
* quiet - True to suppress error messages.
*
* Outputs: text - Converted to callsign with upper case letters and digits.
*
* Returns: Number of errors detected.
*
*----------------------------------------------------------------*/