Add reverse option: -x

master
Tait Hoyem 5 years ago
parent 67a67f7070
commit 682f71611b

@ -17,42 +17,65 @@ static const char UPPERCASE_LETTERS[NUM_OF_LETTERS] = {'A', 'B', 'C', 'D', 'E',
// C is fun? // C is fun?
static const char MORSE[NUM_OF_LETTERS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"}; static const char MORSE[NUM_OF_LETTERS][6] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----"};
char *multi_tok(char *input, char *delimiter) {
static char *string;
if (input != NULL)
string = input;
if (string == NULL)
return string;
char *end = strstr(string, delimiter);
if (end == NULL) {
char *temp = string;
string = NULL;
return temp;
}
char *temp = string;
*end = '\0';
string = end + strlen(delimiter);
return temp;
}
void print_single_morse(char charToMorse, bool isSlow, bool addLetter){ void print_single_morse(char charToMorse, bool isSlow, bool addLetter){
int sizeOfMorseCode; int sizeOfMorseCode;
bool isConvertable = false;
char convertedTo[6];
// Loop through every character in the list of letters // Loop through every character in the list of letters
for (int j = 0; j < NUM_OF_LETTERS; j++){ for (int j = 0; j < NUM_OF_LETTERS; j++){
// If the character in the input string matches the character the character from the list // If the character in the input string matches the character the character from the list
if (charToMorse == LOWERCASE_LETTERS[j] || charToMorse == UPPERCASE_LETTERS[j]){ if (charToMorse == LOWERCASE_LETTERS[j] || charToMorse == UPPERCASE_LETTERS[j]){
strcpy(convertedTo, MORSE[j]);
sizeOfMorseCode = strlen(MORSE[j]); sizeOfMorseCode = strlen(MORSE[j]);
// Print chracter in ascii in brackets before the morse code isConvertable = true;
if (addLetter){ }
printf("(%c)", charToMorse, sizeOfMorseCode); }
}
// if speed restraints are being added for effect if (addLetter){
if (isSlow){ printf("(%c)", charToMorse);
// Loop through each character in the morse code for that chracter }
for (int k = 0; k < sizeOfMorseCode; k++){ if (isConvertable){
printf("%c", MORSE[j][k]); if (isSlow){
// Force flush buffer so it is garunteed to be written to, with the delays in between // loop through all the characters in the morse string
fflush(stdout); for (int i = 0; i < strlen(convertedTo); i++){
// If the morse character is a dash printf("%c", convertedTo[i]);
if (MORSE[j][k] == '-'){ if (convertedTo[i] == '-'){
// ******* TODO: Replace with Arduino light code // usleep is a microseconds
// Long delay code usleep(DASH_PAUSE_LENGTH);
usleep(DASH_PAUSE_LENGTH); } else if (convertedTo[i] == '.'){
} else { usleep(DOT_PAUSE_LENGTH);
// ******* TODO: Replace with Arduino light code
// Short delay code
usleep(DOT_PAUSE_LENGTH);
}
} }
printf(" ");
// If not slow
} else {
printf("%s ", MORSE[j]);
} }
// if not slow
} else {
printf("%s ", convertedTo);
} }
// if not convertable
} else {
printf("%c", charToMorse);
} }
} }
@ -66,16 +89,31 @@ void print_morse(char* strToMorse, bool isSlow, bool addLetter){
} }
} }
void print_unmorsed(char* morseToNormalize){ void reverse_morse(char* morseToNormalize){
bool isConvertable = false;
char convertedChar;
for (int i = 0; i < NUM_OF_LETTERS; i++){
if (strcmp(MORSE[i], morseToNormalize) == 0){
isConvertable = true;
convertedChar = UPPERCASE_LETTERS[i];
}
}
if (isConvertable){
printf("%c", convertedChar);
} else {
printf("%s", morseToNormalize);
}
} }
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
bool addLetterBeforeMorse = false; bool addLetterBeforeMorse = false;
bool isSlow = false; bool isSlow = false;
bool convertFromMorse = false;
char inputString[MAX_INPUT_SIZE]; size_t buflen = 0;
ssize_t lengthOfInputString;
for (int argi = 0; argi < argc; argi++){ for (int argi = 0; argi < argc; argi++){
char* arg = argv[argi]; char* arg = argv[argi];
@ -83,17 +121,38 @@ int main(int argc, char *argv[]){
isSlow = true; isSlow = true;
} else if (strcmp(arg, "--verbose") == 0 || strcmp(arg, "-v") == 0){ } else if (strcmp(arg, "--verbose") == 0 || strcmp(arg, "-v") == 0){
addLetterBeforeMorse = true; addLetterBeforeMorse = true;
} } else if (strcmp(arg, "--reverse") == 0 || strcmp(arg, "-x") == 0){
convertFromMorse = true;
}
} }
while (fgets(inputString, MAX_INPUT_SIZE, stdin)!=0){ if (convertFromMorse){
print_morse(inputString, isSlow, addLetterBeforeMorse); char* inputString;
printf("\n"); char* wordToken;
fflush(stdout);
fflush(stdin); char* letterToken;
bool lastTokenWasEmpty = false;
while ((getline(&inputString, &buflen, stdin))!=EOF){
wordToken = multi_tok(inputString, " ");
while (wordToken){
letterToken = strtok(wordToken, " \n");
while (letterToken){
reverse_morse(letterToken);
letterToken = strtok(NULL, " \n");
}
wordToken = multi_tok(NULL, " ");
printf(" ");
}
fflush(stdin);
}
} else {
char* inputString;
while ((getline(&inputString, &buflen, stdin))!=EOF){
print_morse(inputString, isSlow, addLetterBeforeMorse);
printf("\n");
fflush(stdin);
}
} }
printf("\n");
return 0; return 0;
} }

Loading…
Cancel
Save