From 98c53586d17802ff5362a5ab99161bbb7c9e3183 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 21 Sep 2018 09:10:15 +0200 Subject: [PATCH] Add the whole file to the buffer --- kilo.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/kilo.c b/kilo.c index 35da4b9..60bfd7a 100644 --- a/kilo.c +++ b/kilo.c @@ -45,7 +45,7 @@ struct editorConfig { int screenrows; int screencols; int numrows; - erow row; + erow *row; struct termios orig_termios; }; @@ -168,6 +168,19 @@ int getWindowSize(int *rows, int *cols) { } } +/*** row operations ***/ + +void editorAppendRow(char *s, size_t len) { + E.row = realloc(E.row, sizeof(erow) * (E.numrows +1)); + + int at = E.numrows; + E.row[at].size = len; + E.row[at].chars = malloc(len + 1); + memcpy(E.row[at].chars, s, len); + E.row[at].chars[len] = '\0'; + E.numrows++; +} + /*** file i/o ***/ void editorOpen(char *filename) { @@ -177,16 +190,11 @@ void editorOpen(char *filename) { char *line = NULL; size_t linecap = 0; ssize_t linelen; - linelen = getline(&line, &linecap, fp); - if (linelen != -1) { + while ((linelen = getline(&line, &linecap, fp)) != -1) { while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r')) linelen--; - E.row.size = linelen; - E.row.chars = malloc(linelen + 1); - memcpy(E.row.chars, line, linelen); - E.row.chars[linelen] = '\0'; - E.numrows = 1; + editorAppendRow(line, linelen); } free(line); fclose(fp); @@ -234,9 +242,9 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { - int len = E.row.size; + int len = E.row[y].size; if (len > E.screencols) len = E.screencols; - abAppend(ab, E.row.chars, len); + abAppend(ab, E.row[y].chars, len); } abAppend(ab, "\x1b[K", 3); // Erase one line @@ -335,6 +343,7 @@ void initEditor() { E.cx = 0; E.cy = 0; E.numrows = 0; + E.row = NULL; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");