Add the whole file to the buffer

This commit is contained in:
Aaron Fischer 2018-09-21 09:10:15 +02:00
parent c54779f0ff
commit 98c53586d1

29
kilo.c
View file

@ -45,7 +45,7 @@ struct editorConfig {
int screenrows; int screenrows;
int screencols; int screencols;
int numrows; int numrows;
erow row; erow *row;
struct termios orig_termios; 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 ***/ /*** file i/o ***/
void editorOpen(char *filename) { void editorOpen(char *filename) {
@ -177,16 +190,11 @@ void editorOpen(char *filename) {
char *line = NULL; char *line = NULL;
size_t linecap = 0; size_t linecap = 0;
ssize_t linelen; ssize_t linelen;
linelen = getline(&line, &linecap, fp); while ((linelen = getline(&line, &linecap, fp)) != -1) {
if (linelen != -1) {
while (linelen > 0 && (line[linelen - 1] == '\n' || while (linelen > 0 && (line[linelen - 1] == '\n' ||
line[linelen - 1] == '\r')) line[linelen - 1] == '\r'))
linelen--; linelen--;
E.row.size = linelen; editorAppendRow(line, linelen);
E.row.chars = malloc(linelen + 1);
memcpy(E.row.chars, line, linelen);
E.row.chars[linelen] = '\0';
E.numrows = 1;
} }
free(line); free(line);
fclose(fp); fclose(fp);
@ -234,9 +242,9 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1); abAppend(ab, "~", 1);
} }
} else { } else {
int len = E.row.size; int len = E.row[y].size;
if (len > E.screencols) len = E.screencols; 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 abAppend(ab, "\x1b[K", 3); // Erase one line
@ -335,6 +343,7 @@ void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
E.numrows = 0; E.numrows = 0;
E.row = NULL;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) if (getWindowSize(&E.screenrows, &E.screencols) == -1)
die("getWindowSize"); die("getWindowSize");