tOpen file with multiple lines - ve - a minimal text editor (work in progress) (HTM) git clone git://src.adamsgaard.dk/ve (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit fd944c194e43e14db7bf4fdaf5c5fbc726683c5e (DIR) parent 980ae0e805b66f9a92d2c36a920a1b6380e227a4 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 6 Aug 2019 12:16:15 +0200 Open file with multiple lines Diffstat: M byote.h | 2 +- M io.c | 47 +++++++++++++++++++++++++------ M io.h | 2 +- M main.c | 9 ++++++--- M output.c | 4 ++-- M terminal.c | 1 + 6 files changed, 50 insertions(+), 15 deletions(-) --- (DIR) diff --git a/byote.h b/byote.h t@@ -16,7 +16,7 @@ struct editor_config { int cursor_x, cursor_y; int screen_rows, screen_cols; int num_rows; - eRow row; + eRow *row; struct termios orig_termios; int status_height; int mode; /* 0: normal, 1: insert, 2: visual */ (DIR) diff --git a/io.c b/io.c t@@ -1,17 +1,48 @@ +/* add feature test macro for getline compatibility */ +#define _DEFAULT_SOURCE +#define _BSD_SOURCE +#define _GNU_SOURCE + #include <stdlib.h> +#include <stdio.h> #include <sys/types.h> #include <string.h> #include "byote.h" +#include "terminal.h" + +void editor_append_row(char *s, size_t len) +{ + int i; + + E.row = realloc(E.row, sizeof(eRow) * (E.num_rows + 1)); + i = E.num_rows; + E.row[i].size = len; + E.row[i].chars = malloc(len + 1); + memcpy(E.row[i].chars, s, len); + E.row[i].chars[len] = '\0'; + ++E.num_rows; +} void -file_open() +file_open(char *filename) { - char *line = "Hello, world!"; - ssize_t linelen = 13; + FILE *fp; + char *line; + size_t linecap; + ssize_t linelen; + + fp = fopen(filename, "r"); + if (!fp) + die("fopen in file_open"); - E.row.size = linelen; - E.row.chars = malloc(linelen + 1); - memcpy(E.row.chars, line, linelen); - E.row.chars[linelen] = '\0'; - E.num_rows = 1; + line = NULL; + linecap = 0; + while ((linelen = getline(&line, &linecap, fp)) != -1) { + while (linelen > 0 && (line[linelen - 1] == '\n' || + line[linelen - 1] == '\r')) + linelen--; + editor_append_row(line, linelen); + } + free(line); + fclose(fp); } (DIR) diff --git a/io.h b/io.h t@@ -1,6 +1,6 @@ #ifndef IO_H_ #define IO_H_ -void file_open(); +void file_open(char *filename); #endif (DIR) diff --git a/main.c b/main.c t@@ -4,12 +4,15 @@ #include "io.h" int -/* main(int argc, char* argv[]) */ -main() +main(int argc, char* argv[]) { enable_raw_mode(); init_editor(); - file_open(); + + if (argc >= 2) { + file_open(argv[1]); + } + while (1) { editor_refresh_screen(); editor_process_keypress(); (DIR) diff --git a/output.c b/output.c t@@ -77,10 +77,10 @@ editor_draw_rows(struct abuf *ab) for (y = 0; y < E.screen_rows; ++y) { if (y < E.num_rows) { - len = E.row.size; + len = E.row[y].size; if (len > E.screen_cols) len = E.screen_cols; - ab_append(ab, E.row.chars, len); + ab_append(ab, E.row[y].chars, len); } else { if (y == E.screen_rows-1) (DIR) diff --git a/terminal.c b/terminal.c t@@ -112,6 +112,7 @@ init_editor() { E.cursor_y = 0; E.mode = 0; E.num_rows = 0; + E.row = NULL; if (get_window_size(&E.screen_rows, &E.screen_cols) == -1) die("get_window_size"); }