From c699089e1d1ce5c163b810539a2c43c21737b4e7 Mon Sep 17 00:00:00 2001 From: SuperNovaa41 Date: Wed, 15 Jan 2025 16:43:55 -0500 Subject: [PATCH] implements file reading --- src/Makefile | 11 +++---- src/controls.c | 8 ++--- src/draw.c | 30 ++++++++++++------- src/file.c | 26 ++++++++++++++++ src/{headers => include}/__defines.h | 0 src/{headers => include}/controls.h | 0 src/{headers => include}/draw.h | 1 + src/include/file.h | 6 ++++ src/{headers => include}/term.h | 24 +++++++++++++-- src/main.c | 19 +++++++++--- src/term.c | 45 +++++++++++++++++++++++----- src/test.txt | 5 ++++ 12 files changed, 141 insertions(+), 34 deletions(-) create mode 100644 src/file.c rename src/{headers => include}/__defines.h (100%) rename src/{headers => include}/controls.h (100%) rename src/{headers => include}/draw.h (95%) create mode 100644 src/include/file.h rename src/{headers => include}/term.h (80%) create mode 100644 src/test.txt diff --git a/src/Makefile b/src/Makefile index b93660a..61377a1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,17 +1,18 @@ TARGET=editor CC=gcc -OBJ = main.o term.o draw.o controls.o +OBJ = main.o term.o draw.o controls.o file.o $(TARGET): $(OBJ) mkdir -p ../build $(CC) -o $(TARGET) $(OBJ) -g mv $(TARGET) ../build/ -main.o: headers/term.h headers/draw.h headers/controls.h -term.o: headers/term.h -draw.o: headers/term.h headers/draw.h -controls.o: headers/controls.h headers/__defines.h headers/draw.h headers/term.h +main.o: include/term.h include/draw.h include/controls.h +term.o: include/term.h +draw.o: include/term.h include/draw.h +controls.o: include/controls.h include/__defines.h include/draw.h include/term.h +file.o: include/file.h include/term.h diff --git a/src/controls.c b/src/controls.c index 2b5c7e4..a9e1997 100644 --- a/src/controls.c +++ b/src/controls.c @@ -1,9 +1,9 @@ #include -#include "headers/__defines.h" -#include "headers/controls.h" -#include "headers/draw.h" -#include "headers/term.h" +#include "include/__defines.h" +#include "include/controls.h" +#include "include/draw.h" +#include "include/term.h" extern editor_t editor; extern screen_buffer_t screen_buffer; diff --git a/src/draw.c b/src/draw.c index 22768ec..f6b98c5 100644 --- a/src/draw.c +++ b/src/draw.c @@ -2,8 +2,8 @@ #include #include -#include "headers/draw.h" -#include "headers/term.h" +#include "include/draw.h" +#include "include/term.h" #define MAX_LEN 256 @@ -27,34 +27,44 @@ void refresh_screen(void) cur_y = editor.cy; // hide the cursor to prevent flickering - screen_buffer_append(&screen_buffer, HIDE_CURSOR_STR, HIDE_CURSOR_STR_LEN); + screen_buffer_append(HIDE_CURSOR_STR, HIDE_CURSOR_STR_LEN); - screen_buffer_append(&screen_buffer, MOVE_CURSOR_HOME_STR, MOVE_CURSOR_HOME_STR_LEN); + screen_buffer_append(MOVE_CURSOR_HOME_STR, MOVE_CURSOR_HOME_STR_LEN); - screen_buffer_append(&screen_buffer, CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN); + screen_buffer_append(CLEAR_SCREEN_STR, CLEAR_SCREEN_STR_LEN); + + draw_file_buffer(); draw_editor_rows(); draw_cursor_pos(); move_cursor_pos(cur_x, cur_y); - screen_buffer_append(&screen_buffer, SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN); + screen_buffer_append(SHOW_CURSOR_STR, SHOW_CURSOR_STR_LEN); write(STDOUT_FILENO, screen_buffer.text, screen_buffer.len); } +void draw_file_buffer(void) +{ + size_t i; + for (i = 0; i < editor.num_rows; i++) { + screen_buffer_append(editor.rows[i].line, editor.rows[i].len); + } +} + void draw_editor_rows(void) { size_t i; - for (i = 0; i < editor.rows - 1; i++) - screen_buffer_append(&screen_buffer, "~\r\n", 3); + for (i = editor.num_rows; i < editor.screen_rows - 1; i++) + screen_buffer_append("~\r\n", 3); } void draw_cursor_pos(void) { char* pos = malloc(sizeof(char) * MAX_LEN); snprintf(pos, MAX_LEN, "%d,%d", editor.cx, editor.cy); - screen_buffer_append(&screen_buffer, pos, strlen(pos)); + screen_buffer_append(pos, strlen(pos)); free(pos); } @@ -63,7 +73,7 @@ void move_cursor_pos(int x, int y) // TODO: add check for boundaries char* out = malloc(sizeof(char) * MAX_LEN); snprintf(out, MAX_LEN, "\e[%d;%dH", y, x); - screen_buffer_append(&screen_buffer, out, strlen(out)); + screen_buffer_append(out, strlen(out)); free(out); } diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..8219c01 --- /dev/null +++ b/src/file.c @@ -0,0 +1,26 @@ +#include +#include + +#include "include/file.h" +#include "include/term.h" + +#define MAX_LINE_LEN 1024 + +void read_file(const char* filename) +{ + ssize_t linelen; + size_t n; + char* line = NULL; + + FILE* fp = fopen(filename, "r"); + if (NULL == fp) { + fprintf(stderr, "failed to open file for reading.\n"); + exit(EXIT_FAILURE); + } + + while (-1 != (linelen = getline(&line, &n, fp))) { + editor_add_row(line, linelen); + } + + fclose(fp); +} diff --git a/src/headers/__defines.h b/src/include/__defines.h similarity index 100% rename from src/headers/__defines.h rename to src/include/__defines.h diff --git a/src/headers/controls.h b/src/include/controls.h similarity index 100% rename from src/headers/controls.h rename to src/include/controls.h diff --git a/src/headers/draw.h b/src/include/draw.h similarity index 95% rename from src/headers/draw.h rename to src/include/draw.h index 52168d0..632d096 100644 --- a/src/headers/draw.h +++ b/src/include/draw.h @@ -18,6 +18,7 @@ void refresh_screen(void); +void draw_file_buffer(void); void draw_editor_rows(void); void draw_cursor_pos(void); diff --git a/src/include/file.h b/src/include/file.h new file mode 100644 index 0000000..fa1fac9 --- /dev/null +++ b/src/include/file.h @@ -0,0 +1,6 @@ +#ifndef FILE_H +#define FILE_H + +void read_file(const char* filename); + +#endif diff --git a/src/headers/term.h b/src/include/term.h similarity index 80% rename from src/headers/term.h rename to src/include/term.h index 8360177..1e500a9 100644 --- a/src/headers/term.h +++ b/src/include/term.h @@ -9,10 +9,19 @@ typedef enum { INSERT_MODE, } control_type_t; +typedef struct { + char* line; + size_t len; +} row_t; + typedef struct { struct termios term_settings; control_type_t control_type; - size_t cols, rows; + + row_t* rows; + size_t num_rows; + + size_t screen_cols, screen_rows; int cx, cy; // cursor pos } editor_t; @@ -67,7 +76,6 @@ void kill_application(void); /** * # screen_buffer_append * - * - screen_buffer_t* buf : The screen buffer * - const char* in : The input text * - size_t len : Length of the input text * @@ -78,7 +86,7 @@ void kill_application(void); * Returns -1 on failure, 0 on success * */ -int screen_buffer_append(screen_buffer_t* buf, const char* in, size_t len); +int screen_buffer_append(const char* in, size_t len); /** * # screen_buffer_free @@ -90,4 +98,14 @@ int screen_buffer_append(screen_buffer_t* buf, const char* in, size_t len); */ void screen_buffer_free(screen_buffer_t* buf); +/** + * # free_editor_row + * + * Frees all of the strings in each row + * and the row array + */ +void free_editor_row(void); + +void editor_add_row(const char* line, size_t len); + #endif diff --git a/src/main.c b/src/main.c index 7e8002c..6632ca0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,19 +1,30 @@ +#include +#include #include -#include "headers/controls.h" -#include "headers/draw.h" -#include "headers/term.h" +#include "include/controls.h" +#include "include/draw.h" +#include "include/file.h" +#include "include/term.h" editor_t editor; screen_buffer_t screen_buffer; -int main(void) +int main(int argc, char** argv) { char c; char* in; + if (argc > 2) { + fprintf(stderr, "Usage: %s \n || Usage: %s", argv[0], argv[0]); + return EXIT_FAILURE; + } + setup_terminal(); + if (argc == 2) + read_file(argv[1]); + refresh_screen(); // want to draw first since we're going to be waiting on the read while (1) { diff --git a/src/term.c b/src/term.c index 5275fb4..8794a18 100644 --- a/src/term.c +++ b/src/term.c @@ -6,7 +6,7 @@ #include -#include "headers/term.h" +#include "include/term.h" /** BEGIN extern section **/ @@ -109,11 +109,14 @@ void setup_terminal(void) editor.control_type = COMMAND_MODE; + editor.num_rows = 0; + editor.rows = NULL; + editor.cx = 1; editor.cy = 1; - editor.cols = win.ws_col; - editor.rows = win.ws_row; + editor.screen_cols = win.ws_col; + editor.screen_rows = win.ws_row; } void kill_application(void) @@ -125,19 +128,21 @@ void kill_application(void) reset_input_mode(); screen_buffer_free(&screen_buffer); + + free_editor_row(); } -int screen_buffer_append(screen_buffer_t *buf, const char *in, size_t len) +int screen_buffer_append(const char *in, size_t len) { // Need to realloc to allow for the input to fit into the buffer - char* new = realloc(buf->text, buf->len + len); + char* new = realloc(screen_buffer.text, screen_buffer.len + len); if (NULL == new) return -1; - memcpy(&new[buf->len], in, len); - buf->text = new; - buf->len += len; + memcpy(&new[screen_buffer.len], in, len); + screen_buffer.text = new; + screen_buffer.len += len; return 0; } @@ -146,3 +151,27 @@ void screen_buffer_free(screen_buffer_t *buf) { free(buf->text); } + +void free_editor_row(void) +{ + size_t i; + for (i = 0; i < editor.num_rows; i++) + free(editor.rows[i].line); + free(editor.rows); +} + +void editor_add_row(const char* line, size_t len) +{ + // Need to add space for another row + editor.rows = realloc(editor.rows, sizeof(row_t) * (editor.num_rows + 1)); + + // Set len and malloc space for the incoming line +1 for \0 + size_t idx = editor.num_rows; + editor.rows[idx].len = len; + editor.rows[idx].line = malloc(sizeof(char) * (len + 1)); + + memcpy(editor.rows[idx].line, line, len); + editor.rows[idx].line[len] = '\0'; // need to null terminate it + + editor.num_rows++; +} diff --git a/src/test.txt b/src/test.txt new file mode 100644 index 0000000..200f057 --- /dev/null +++ b/src/test.txt @@ -0,0 +1,5 @@ +hell othis +is a test of the +fiel reading +capabiliteis of my +edito