No more allocs in funcs

master
Tait Hoyem 2 years ago
parent fdecf79c9f
commit ed369e188b

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

@ -38,72 +38,49 @@ static const char* MORSE_ERROR = "........";
// ... it's used for performence! // ... it's used for performence!
// //
// I do not copy ANY data. The pointer returned points to the above array! // I do not copy ANY data. The pointer returned points to the above array!
const char* char_to_morse(char letter){ const char* char_to_morse(char letter) {
const char* result_ptr = malloc(sizeof(char)*MAX_MORSE_LENGTH); for (int i = 0; i < NUM_OF_SYMBOLS; i++) {
bool result_set = false; if (letter == SYMBOLS[i]) {
for (int i = 0; i < NUM_OF_SYMBOLS; i++){ return MORSE[i];
if (letter == SYMBOLS[i]){ }
result_ptr = MORSE[i];
result_set = true;
}
}
if (!result_set){
result_ptr = MORSE_ERROR;
} }
return result_ptr; return MORSE_ERROR;
} }
char* string_to_morse(char* string){ void string_to_morse(char* string, char* result){
int string_len = strlen(string); int string_len = strlen(string);
// worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces) // worse possible case is 8 times the length (assuming all numbers/punctuation, and adding spaces)
// +1 for NULL terminator // +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, "");
for (int i = 0; i < string_len; i++){ for (int i = 0; i < string_len; i++){
strcat(result, char_to_morse(string[i])); strcat(result, char_to_morse(string[i]));
if (i != string_len-1){ if (i != string_len-1){
strcat(result, " "); strcat(result, " ");
} }
} }
return result;
} }
const char morse_to_char(const char* morse){ const char morse_to_char(const char* morse) {
bool found_symbol = false; for (int i = 0; i < NUM_OF_SYMBOLS; i++) {
char result; if (strcmp(morse, MORSE[i]) == 0) {
for (int i = 0; i < NUM_OF_SYMBOLS && !found_symbol; i++){ return SYMBOLS[i];
if (strcmp(morse, MORSE[i]) == 0){
result = SYMBOLS[i];
found_symbol = true;
} }
} }
if (!found_symbol){ return SYMBOL_ERROR;
result = SYMBOL_ERROR;
}
return result;
} }
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; int morse_length = strlen(morse_to_cpy)+1;
char morse[morse_length]; char morse[morse_length];
strcpy(morse, morse_to_cpy); 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 // split by the space character
char* morse_ptr = strtok(morse, " \n"); char* morse_ptr = strtok(morse, " \n");
// until we reach the end of the string // 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); char char_to_add = morse_to_char(morse_ptr);
// give the address of the single character to concatinate to result // give the address of the single character to concatinate to result
strcat(result, &char_to_add); strcat(result, &char_to_add);
// reset the morse ptr for the next section between a space // reset the morse ptr for the next section between a space
morse_ptr = strtok(NULL, " \n"); morse_ptr = strtok(NULL, " \n");
} }
return result;
} }

@ -1,6 +1,6 @@
#include <stdbool.h> #include <stdbool.h>
const char * char_to_morse(char letter); const char* char_to_morse(char letter);
char * string_to_morse(char* string); void string_to_morse(char* string, char* result);
const char morse_to_char(const char* morse); 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){ void assert_stm(char* input, char* output){
char* result; char result[strlen(input) * 7];
//char* result = malloc(sizeof(char)*strlen(input)*8); strcpy(result, "");
// fill with blanks in case of previous data string_to_morse(input, result);
// strcpy(result, "");
result = string_to_morse(input);
printf(FUNCTION_CALL_SFORMAT, "string_to_morse", input); printf(FUNCTION_CALL_SFORMAT, "string_to_morse", input);
assert_str_eq(result, output); assert_str_eq(result, output);
} }
@ -82,8 +80,10 @@ void assert_mtc(char* input, char output){
} }
void assert_mts(char* input, char* output){ void assert_mts(char* input, char* output){
char* result = morse_to_string(input); char result[strlen(input) * 7];
printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input); strcpy(result, "");
morse_to_string(input, result);
printf(FUNCTION_CALL_SFORMAT, "morse_to_string", input);
assert_str_eq(result, output); assert_str_eq(result, output);
} }

Loading…
Cancel
Save