You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.0 KiB

2 years ago
// add test to check mirroring
#include "morse.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <limits.h>
#include <stdlib.h>
#define NUM_OF_SYMBOLS 82
#define MAX_MORSE_LENGTH 9
static const char SYMBOLS[NUM_OF_SYMBOLS] = {
'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',
'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',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'.', ',', '?', '\'', '!', '/', '(', ')', '&', ':', ';', '=', '+', '-', '_', '"', '$', '@',
' '};
static const char SYMBOL_ERROR = '~';
static const char MORSE[NUM_OF_SYMBOLS][MAX_MORSE_LENGTH] = {
// A-Z
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
// a-z (same as above, but needed twice due to design choices)
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
// 0-9
5 years ago
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.",
// . , ? ' ! / ( ) & : ; = + - _ " $ @ EOF
".-.-.-", "--..--", "..--..", ".----.", "-.-.--", "-..-.", "-.--.", "-.--.-", ".-...", "---...", "-.-.-.", "-...-", ".-.-.", "-...-", "..--.-", ".-..-.", "...-..-", ".--.-.",
// space
"/"};
static const char* MORSE_ERROR = "........";
5 years ago
// This function returns a pointer to the morse code written above.
// The genuis of this function feels amazing because I've never used C before, but I see why
// ... it's used for performence!
//
// I do not copy ANY data. The pointer returned points to the above array!
const char* char_to_morse(char letter){
const char* result_ptr = malloc(sizeof(char)*MAX_MORSE_LENGTH);
bool result_set = false;
for (int i = 0; i < NUM_OF_SYMBOLS; i++){
if (letter == SYMBOLS[i]){
result_ptr = MORSE[i];
result_set = true;
}
}
if (!result_set){
result_ptr = MORSE_ERROR;
}
return result_ptr;
}
5 years ago
char* string_to_morse(char* string){
int string_len = strlen(string);
5 years ago
// worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces)
// +1 for NULL terminator
char* result = malloc(sizeof(char)*string_len*MAX_MORSE_LENGTH);
5 years ago
// sets everything to null in the string, just in case there was data there previously.
5 years ago
strcpy(result, "");
5 years ago
for (int i = 0; i < string_len; i++){
strcat(result, char_to_morse(string[i]));
5 years ago
if (i != string_len-1){
strcat(result, " ");
}
}
return result;
}
5 years ago
const char morse_to_char(const char* morse){
bool found_symbol = false;
char result;
for (int i = 0; i < NUM_OF_SYMBOLS && !found_symbol; i++){
5 years ago
if (strcmp(morse, MORSE[i]) == 0){
result = SYMBOLS[i];
found_symbol = true;
5 years ago
}
}
if (!found_symbol){
result = SYMBOL_ERROR;
}
return result;
5 years ago
}
char* morse_to_string(const char* morse_to_cpy){
int morse_length = strlen(morse_to_cpy)+1;
5 years ago
char morse[morse_length];
strcpy(morse, morse_to_cpy);
// allocate the amount of space the morse takes up for the text string
// this could be changed, but I don't see the point in optimizing the small things here.
char* result = malloc(sizeof(char)*strlen(morse));
5 years ago
// copy nothing to the string to avoid extra data
//strcpy(result, "");
5 years ago
// split by the space character
char* morse_ptr = strtok(morse, " \n");
5 years ago
// until we reach the end of the string
while (morse_ptr != NULL){
char char_to_add = morse_to_char(morse_ptr);
// give the address of the single character to concatinate to result
strcat(result, &char_to_add);
// reset the morse ptr for the next section between a space
morse_ptr = strtok(NULL, " \n");
5 years ago
}
return result;
5 years ago
}