No more allocs in funcs

master
Tait Hoyem 2 years ago
parent fdecf79c9f
commit ed369e188b

@ -4,14 +4,18 @@
#include <unistd.h>
#include <string.h>
#ifndef MAX_LINE_SIZE
#define MAX_LINE_SIZE 4096
#endif
const long DEFAULT_DASH_PAUSE = 300000;
const long DEFAULT_DOT_PAUSE = 150000;
int main(int argc, char *argv[]){
char* inputString;
char* outputString;
// outputChar tracks the current printing character when slow printing is enabled
char outputChar;
char* input_string = malloc(sizeof(char) * MAX_LINE_SIZE);
char* output_string = malloc(sizeof(char) * MAX_LINE_SIZE);
// output_char tracks the current printing character when slow printing is enabled
char output_char;
long dash_delay = DEFAULT_DASH_PAUSE;
long dot_delay = DEFAULT_DOT_PAUSE;
@ -44,52 +48,54 @@ int main(int argc, char *argv[]){
if (convertFromMorse){
// for every line in the input, until the end of file
while ((getline(&inputString, &buflen, stdin))!=EOF){
int stringLen = strlen(inputString);
while ((getline(&input_string, &buflen, stdin))!=EOF){
int stringLen = strlen(input_string);
// This effectively strips the newline off the string
if (inputString[stringLen-1] == '\n'){
inputString[stringLen-1] = '\0';
if (input_string[stringLen-1] == '\n'){
input_string[stringLen-1] = '\0';
}
outputString = morse_to_string(inputString);
printf("%s\n", outputString);
morse_to_string(input_string, output_string);
printf("%s\n", output_string);
// force-flush the input buffer just in case the user is in interactive mode
fflush(stdin);
}
} else {
// for each line of input
while ((getline(&inputString, &buflen, stdin))!=EOF){
int stringLen = strlen(inputString);
while ((getline(&input_string, &buflen, stdin))!=EOF){
int stringLen = strlen(input_string);
// This effectively strips the newline off the string
if (inputString[stringLen-1] == '\n'){
inputString[stringLen-1] = '\0';
if (input_string[stringLen-1] == '\n'){
input_string[stringLen-1] = '\0';
}
outputString = string_to_morse(inputString);
string_to_morse(input_string, output_string);
if (isSlow){
for (int i = 0; i < strlen(outputString); i++){
outputChar = outputString[i];
if (outputChar == '-'){
for (int i = 0; i < strlen(output_string); i++){
output_char = output_string[i];
if (output_char == '-'){
usleep(dash_delay);
printf("%c", outputChar);
} else if (outputChar == '.'){
printf("%c", output_char);
} else if (output_char == '.'){
usleep(dot_delay);
printf("%c", outputChar);
printf("%c", output_char);
} else {
printf("%c", outputChar);
printf("%c", output_char);
}
fflush(stdout);
}
printf("\n");
} else {
printf("%s\n", outputString);
printf("%s\n", output_string);
// force flush the buffer incase user is in interactive mode
fflush(stdin);
}
}
}
free(output_string);
free(input_string);
return 0;
}

@ -38,72 +38,49 @@ static const char* MORSE_ERROR = "........";
// ... 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;
const char* char_to_morse(char letter) {
for (int i = 0; i < NUM_OF_SYMBOLS; i++) {
if (letter == SYMBOLS[i]) {
return MORSE[i];
}
}
return result_ptr;
return MORSE_ERROR;
}
char* string_to_morse(char* string){
int string_len = strlen(string);
void string_to_morse(char* string, char* result){
int string_len = strlen(string);
// 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);
// sets everything to null in the string, just in case there was data there previously.
strcpy(result, "");
// +1 for NULL terminator
for (int i = 0; i < string_len; i++){
strcat(result, char_to_morse(string[i]));
if (i != string_len-1){
strcat(result, " ");
}
}
return result;
}
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++){
if (strcmp(morse, MORSE[i]) == 0){
result = SYMBOLS[i];
found_symbol = true;
const char morse_to_char(const char* morse) {
for (int i = 0; i < NUM_OF_SYMBOLS; i++) {
if (strcmp(morse, MORSE[i]) == 0) {
return SYMBOLS[i];
}
}
if (!found_symbol){
result = SYMBOL_ERROR;
}
return result;
return SYMBOL_ERROR;
}
char* morse_to_string(const char* morse_to_cpy){
void morse_to_string(const char* morse_to_cpy, char* result) {
int morse_length = strlen(morse_to_cpy)+1;
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));
// copy nothing to the string to avoid extra data
//strcpy(result, "");
// split by the space character
char* morse_ptr = strtok(morse, " \n");
// until we reach the end of the string
while (morse_ptr != NULL){
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");
}
return result;
}

@ -1,6 +1,6 @@
#include <stdbool.h>
const char * char_to_morse(char letter);
char * string_to_morse(char* string);
const char* char_to_morse(char letter);
void string_to_morse(char* string, char* result);
const char morse_to_char(const char* morse);
char * morse_to_string( const char* morse);
void morse_to_string(const char* morse, char* result);

@ -1,23 +0,0 @@
#!/bin/bash
# REQUIRES: play
# TODO: write using play libraries in C. Not sure if possible, but would be nice.
# ... even if just for compatibility reasons.
FILENAME="$1"
FILE_CONTENTS="$(cat $FILENAME)"
while read -n1 character; do
if [ "$character" == "-" ]; then
play -q -n synth 0.15 sine 850 vol 0.5
elif [ "$character" == "." ]; then
play -q -n synth 0.05 sine 850 vol 0.5
elif [ "$chracter" == "" ]; then
sleep 0.2
elif [ "$character" == "/" ]; then
sleep 0.5
fi
done < <(echo -n "$FILE_CONTENTS")

@ -66,11 +66,9 @@ void assert_ctm(char input, char* output){
}
void assert_stm(char* input, char* output){
char* result;
//char* result = malloc(sizeof(char)*strlen(input)*8);
// fill with blanks in case of previous data
// strcpy(result, "");
result = string_to_morse(input);
char result[strlen(input) * 7];
strcpy(result, "");
string_to_morse(input, result);
printf(FUNCTION_CALL_SFORMAT, "string_to_morse", input);
assert_str_eq(result, output);
}
@ -82,8 +80,10 @@ void assert_mtc(char* input, char output){
}
void assert_mts(char* input, char* output){
char* result = morse_to_string(input);
printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input);
char result[strlen(input) * 7];
strcpy(result, "");
morse_to_string(input, result);
printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input);
assert_str_eq(result, output);
}

Loading…
Cancel
Save