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 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");