fix memory leaks

master
Tait Hoyem 2 years ago
parent 163d968572
commit 1da3f5014b

@ -12,8 +12,9 @@ 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* input_string = malloc(sizeof(char) * MAX_LINE_SIZE); int string_len;
char* output_string = malloc(sizeof(char) * MAX_LINE_SIZE); char* input_string = NULL;
char* output_string = (char*) malloc(sizeof(char) * MAX_LINE_SIZE);
// output_char tracks the current printing character when slow printing is enabled // output_char tracks the current printing character when slow printing is enabled
char output_char; char output_char;
@ -48,53 +49,50 @@ int main(int argc, char *argv[]){
if (convert_from_morse){ if (convert_from_morse){
// for every line in the input, until the end of file // for every line in the input, until the end of file
while ((getline(&input_string, &buflen, stdin))!=EOF){ while ((string_len = getline(&input_string, &buflen, stdin))!=EOF){
int stringLen = strlen(input_string); // This effectively strips the newline off the string
if (input_string[string_len-1] == '\n'){
// This effectively strips the newline off the string input_string[string_len-1] = '\0';
if (input_string[stringLen-1] == '\n'){ }
input_string[stringLen-1] = '\0';
}
morse_to_string(input_string, output_string); morse_to_string(input_string, output_string);
printf("%s\n", output_string); 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(&input_string, &buflen, stdin))!=EOF){ while ((string_len = getline(&input_string, &buflen, stdin))!=EOF){
int stringLen = strlen(input_string); // This effectively strips the newline off the string
if (input_string[string_len-1] == '\n'){
// This effectively strips the newline off the string input_string[string_len-1] = '\0';
if (input_string[stringLen-1] == '\n'){ }
input_string[stringLen-1] = '\0';
}
string_to_morse(input_string, output_string); string_to_morse(input_string, output_string);
if (is_slow){ if (is_slow){
for (int i = 0; i < strlen(output_string); i++){ for (int i = 0; i < strlen(output_string); i++){
output_char = output_string[i]; output_char = output_string[i];
if (output_char == '-'){ if (output_char == '-'){
usleep(dash_delay); usleep(dash_delay);
printf("%c", output_char); printf("%c", output_char);
} else if (output_char == '.'){ } else if (output_char == '.'){
usleep(dot_delay); usleep(dot_delay);
printf("%c", output_char); printf("%c", output_char);
} else { } else {
printf("%c", output_char); printf("%c", output_char);
} }
fflush(stdout); fflush(stdout);
} }
printf("\n"); printf("\n");
} else { } else {
printf("%s\n", output_string); 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(output_string);
free(input_string); free(input_string);
return 0; return 0;

Loading…
Cancel
Save