diff --git a/01_text_adventure/log.c b/01_text_adventure/log.c index 1a043b1..2a4d85f 100644 --- a/01_text_adventure/log.c +++ b/01_text_adventure/log.c @@ -3,6 +3,7 @@ #include #include +#include void draw_log(Log* log) { for(int line_num = 0; line_num < log->line_count; line_num++) { @@ -23,11 +24,21 @@ Log *create_log(void) { return log; } +#define MAX(a, b) a > b ? a : b +#define MIN(a, b) a < b ? a : b +#define LINE_LENGTH 60 + void push_line_to_log(Log* log, char* line) { + int line_length = MIN(strlen(line), LINE_LENGTH); + log->lines = realloc(log->lines, (log->line_count + 1) * sizeof(char*)); + log->lines[log->line_count] = malloc(line_length + 1); + memcpy(log->lines[log->line_count], line, line_length); + log->lines[log->line_count][line_length] = '\0'; log->line_count++; - log->lines = realloc(log->lines, log->line_count * sizeof(char*)); - log->lines[log->line_count - 1] = malloc(strlen(line) + 1); - strcpy(log->lines[log->line_count - 1], line); + + if (strlen(line) > LINE_LENGTH) { + push_line_to_log(log, line + LINE_LENGTH); + } } void free_log(Log* log) {