-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
207 lines (164 loc) · 5.04 KB
/
main.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "morse.h"
void usage(void) {
fprintf(stderr,
"Usage: morse [OPTION] [STRING]\n"
"Converts standard input or argument to morse or text\n\n"
" -t morse to text\n"
" -m text to morse\n"
);
}
void die(const char *msg) {
fputs(msg, stderr);
abort();
}
typedef enum mode {
TO_MORSE,
TO_TEXT
} mode;
/*
* Demonstrates how to parse morse from a stream
*/
int stdin_morse_to_text(morse_parser *parser) {
char input_char = EOF;
morse_state result = MORSE_INVALID_SEQUENCE;
char value;
int return_value = EXIT_SUCCESS;
while ((input_char = getc(stdin)) != EOF) {
if (input_char == MORSE_DIT || input_char == MORSE_DAH) {
result = morse_push_symbol(parser, input_char);
if (result == MORSE_INVALID_SEQUENCE) {
fprintf(stderr, "invalid morse sequence\n");
return_value = EXIT_FAILURE;
break;
}
// If reading from something other than a stream, like an actual telegraph key or signal,
// then you'd use timing to determing character boundaries.
} else if (input_char == ' ' || input_char == '\n') {
result = morse_get_value(parser, &value);
if (result == MORSE_INVALID_SEQUENCE) {
fprintf(stderr, "invalid morse sequence\n");
return_value = EXIT_FAILURE;
break;
} else {
printf("%c", value);
morse_reset(parser);
}
} else {
fprintf(stderr, "invalid morse sequence\n");
return_value = EXIT_FAILURE;
break;
}
}
return return_value;
}
/*
* Demonstrates how to convert a morse string to text
*/
int morse_string_to_text(morse_parser *parser, char *input) {
morse_state result = MORSE_INVALID_SEQUENCE;
size_t buflen = 256;
char buf[buflen];
int written = 0;
int return_value = -1;
while (return_value == -1) {
result = morse_to_text(parser, input, strlen(input), buf, buflen, &written);
if (result == MORSE_INVALID_SEQUENCE) {
fprintf(stderr, "invalid morse sequence\n");
return_value = EXIT_FAILURE;
break;
} else if (result == MORSE_CONTINUE) {
printf("%.*s", written, buf);
} else if (result == MORSE_DONE) {
printf("%.*s", written, buf);
return_value = EXIT_SUCCESS;
break;
} else {
fprintf(stderr, "unknown error\n");
return_value = EXIT_FAILURE;
break;
}
}
return return_value;
}
/*
* Demonstrates converting text to a morse string
*/
int text_to_morse(morse_parser *parser, char *input) {
size_t buflen = 10;
char buf[buflen];
int written = 0;
int return_value = -1;
morse_state result = MORSE_INVALID_SEQUENCE;
while (return_value == -1) {
result = morse_from_text(parser, input, strlen(input), buf, buflen, &written);
if (result == MORSE_INVALID_SEQUENCE) {
fprintf(stderr, "invalid text: %s\nOnly a-z A-Z 0-9 supported.\n", input);
return_value = EXIT_FAILURE;
} else if (result == MORSE_CONTINUE) {
printf("%.*s", written, buf);
} else if (result == MORSE_DONE) {
printf("%.*s", written, buf);
break;
} else {
fprintf(stderr, "unknown error\n");
return_value = EXIT_FAILURE;
}
}
return return_value;
}
int main(int argc, char **argv) {
// Argument Parsing
mode mode;
char *input = NULL;
if (argc < 2) {
usage();
exit(EXIT_FAILURE);
} else if (strncmp(argv[1], "-t", 2) == 0) {
mode = TO_TEXT;
} else if (strncmp(argv[1], "-m", 2) == 0) {
mode = TO_MORSE;
} else {
usage();
exit(EXIT_FAILURE);
}
if (argc > 2) {
input = argv[2];
}
morse_parser parser;
morse_reset(&parser);
// String passed as argument
if (input != NULL) {
if (mode == TO_TEXT) {
return morse_string_to_text(&parser, input);
} else {
return text_to_morse(&parser, input);
}
// Read from stdin
} else {
if (mode == TO_TEXT) {
return stdin_morse_to_text(&parser);
} else {
char temp[2] = {0, 0};
bool firstchar = true;
while ((temp[0] = getc(stdin)) != EOF) {
if (!firstchar) {
if (temp[0] != '\n') {
printf(" ");
}
} else {
firstchar = false;
}
int result = text_to_morse(&parser, temp);
morse_reset(&parser);
if (result == EXIT_FAILURE) {
return result;
}
}
}
}
return EXIT_SUCCESS;
}