-
Notifications
You must be signed in to change notification settings - Fork 10
/
morse.c
316 lines (280 loc) · 6.64 KB
/
morse.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
#include "morse.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUM_LEN 10
char num[][5] = {
{'-','-','-','-','-'},//0
{'.','-','-','-','-'},//1
{'.','.','-','-','-'},//2
{'.','.','.','-','-'},//3
{'.','.','.','.','-'},//4
{'.','.','.','.','.'},//5
{'-','.','.','.','.'},//6
{'-','-','.','.','.'},//7
{'-','-','-','.','.'},//8
{'-','-','-','-','.'} //9
};
#define MARK_LEN 17
char mark[][8] = {
{'.', '-', '.', '-', '.', '-', '*', '.'},//. 0
{'-', '-', '-', '.', '.', '.', '*', ':'},//:
{'-', '-', '.', '.', '-', '-', '*', ','},//,
{'-', '.', '-', '.', '-', '.', '*', ';'},//;
{'.', '.', '-', '-', '.', '.', '*', '?'},//?
{'-', '.', '.', '.', '-', '*', '*', '='},//=
{'.', '-', '-', '-', '-', '.', '*', '\''},//'
{'-', '.', '.', '-', '.', '*', '*', '/'},///
{'-', '.', '-', '.', '-', '-', '*', '!'},//!
{'-', '.', '.', '.', '.', '-', '*', '-'},//-
{'.', '.', '-', '-', '.', '-', '*', '_'},//_
{'.', '-', '.', '.', '-', '.', '*', '"'},//"
{'-', '.', '-', '-', '.', '*', '*', '('},//(
{'-', '.', '-', '-', '.', '-', '*', ')'},//)
{'.', '.', '.', '-', '.', '.', '-', '$'},//$
{'.', '-', '.', '.', '.', '*', '*', '&'},//&
{'.', '-', '-', '.', '-', '.', '*', '@'} //@ 16
};
#define CHARACTER_LEN 26
char a2[][4] = {
{'.','-','*','*'},//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
};
Morse_t* new_morse() {
Morse_t *ret;
ret = (Morse_t*)malloc(sizeof(Morse_t));
memset(ret->c, 0, 9);
return ret;
}
/*
* MARK
*/
bool mark2morse(char n, Morse_t *morse) {
int a = 0;
for (; a < MARK_LEN; a++) {
if (mark[a][7] == n) {
morse->c[0] = mark[a][0];
morse->c[1] = mark[a][1];
morse->c[2] = mark[a][2];
morse->c[3] = mark[a][3];
morse->c[4] = mark[a][4];
morse->c[5] = mark[a][5];
morse->c[6] = mark[a][6];
return true;
}
}
return false;
}
bool morse2mark(Morse_t *morse, char *n) {
int a = 0;
for (; a < MARK_LEN; a++) {
if (mark[a][0] == morse->c[0] &&
mark[a][1] == morse->c[1] &&
mark[a][2] == morse->c[2] &&
mark[a][3] == morse->c[3] &&
mark[a][4] == morse->c[4] &&
mark[a][5] == morse->c[5] &&
mark[a][6] == morse->c[6] ) {
*n = mark[a][7];
return true;
}
}
return false;
}
/*
* NUMBER
*/
bool num2morse(char n, Morse_t *morse) {
int pos = n - 48;
if (pos <= 9 && pos >= 0) {
morse->c[0] = num[pos][0];
morse->c[1] = num[pos][1];
morse->c[2] = num[pos][2];
morse->c[3] = num[pos][3];
morse->c[4] = num[pos][4];
return true;
}
return false;
}
bool morse2num(Morse_t *morse, char *n) {
int i = 0;
for (; i < NUM_LEN; i++) {
if (num[i][0] == morse->c[0] &&
num[i][1] == morse->c[1] &&
num[i][2] == morse->c[2] &&
num[i][3] == morse->c[3] &&
num[i][4] == morse->c[4] ) {
*n = (char)(i + 48);
return true;
}
}
return false;
}
/*
* CHARACTER
*/
bool str2morse(char m , Morse_t *morse) {
int pos = m - 97;
if (pos >= 0 && pos <= 25) {
morse->c[0] = a2[pos][0];
morse->c[1] = a2[pos][1];
morse->c[2] = a2[pos][2];
morse->c[3] = a2[pos][3];
return true;
}
return false;
}
bool morse2str(Morse_t *morse, char *ch) {
int i = 0;
for (i = 0; i < CHARACTER_LEN; i++) {
if (a2[i][0] == morse->c[0] &&
a2[i][1] == morse->c[1] &&
a2[i][2] == morse->c[2] &&
a2[i][3] == morse->c[3]) {
*ch = (char)(i + 97);
return true;
}
}
return false;
}
void morse_str_to_str(char *morse ,char *string, int buf_len) {
Morse_t *temp = new_morse();
int a = 0;
int b = 0;
int c = 0;
int len = 0;
char ch = '*';
memset(temp->c, '*', 8);//fill the morse-array with '*'
len = strlen(morse);
for ( ; a < len; a ++) {
if (c > buf_len) {
printf("the string buffer is too little\n");
return;
}
if (morse[a] != SEPARATOR && morse[a] != FAKE_SPACE)
temp->c[b++] = morse[a];
else if (morse[a] == SEPARATOR && morse[a-1] != FAKE_SPACE) {//get one charactor
if (true == morse2str(temp, &ch) && b < 5) {
string[c++] = ch;
} else if (true == morse2mark(temp, &ch)) {
string[c++] = ch;
} else if (true == morse2num(temp, &ch)) {
string[c++] = ch;
} else {
printf("has morse that not be decoded !\n");
}
//clean
b = 0;
memset(temp->c, '*' ,8);
} else if (morse[a] == FAKE_SPACE) { //have a space
string[c++] = ' ';
}
}
}
void str_to_morse_str(char *string ,char *morse, int buf_len) {
int a = 0;
int b = 0;
int len = strlen(string);
Morse_t * temp = new_morse();
for (; a < len; a ++ ) {
if (buf_len < 8 || b >= buf_len) {
printf("morse buffer is too litte\n");
break;
}
if (string[a] != ' ') {
//if is a num
if (string[a] >= '0' && string[a] <= '9') {
if (true == num2morse(string[a], temp)) {
morse[b++] = temp->c[0];
morse[b++] = temp->c[1];
morse[b++] = temp->c[2];
morse[b++] = temp->c[3];
morse[b++] = temp->c[4];
} else {
printf("encode on mumber error \n");
return ;
}
}
//if is a character
else if (string[a] >= 97 && string[a] <= 122) {
if (true == str2morse(string[a], temp)) {
morse[b++] = temp->c[0];
if (temp->c[1] != '*')
morse[b++] = temp->c[1];
if (temp->c[2] != '*')
morse[b++] = temp->c[2];
if (temp->c[3] != '*')
morse[b++] = temp->c[3];
} else {
printf("encode on str error \n");
return ;
}
}
//if is a mark
else if (string[a] <= 127) {
if (true == mark2morse(string[a], temp)) {
morse[b++] = temp->c[0];
morse[b++] = temp->c[1];
morse[b++] = temp->c[2];
morse[b++] = temp->c[3];
morse[b++] = temp->c[4];
if (temp->c[5] != '*')
morse[b++] = temp->c[5];
if (temp->c[6] != '*')
morse[b++] = temp->c[6];
} else {
printf("encode on mark error \n");
return ;
}
} else {
printf("out of the morse character \n");
return ;
}
//clean
memset(temp->c, 0 , 8);
morse[b++] = SEPARATOR;
} else if (string[a] == ' ') { //have a space and add / to instead
morse[b++] = FAKE_SPACE;
morse[b++] = SEPARATOR;
}
}
}
void str2lowcase(char *str, char *out, int buf_len) {
int len = strlen(str);
int a = 0;
if (len >= buf_len) {
printf("buf is to low\n");
return;
}
for (;a < len; a++) {
if (str[a] >= 'A' && str[a] <= 'Z') {
out[a] = str[a] + 32;
} else {
out[a] = str[a];
}
}
}