diff --git a/b_insert.c b/b_insert.c new file mode 100644 index 0000000..5f3e1f2 --- /dev/null +++ b/b_insert.c @@ -0,0 +1,8 @@ +Row * +b_insert(Row *r) { + Row *new = calloc(1, sizeof(Row)); + new->n = r->n; + new->p = r; + r->n = new; + return new; +} diff --git a/c_open.c b/c_open.c index 8735817..82bbf40 100644 --- a/c_open.c +++ b/c_open.c @@ -1,24 +1,21 @@ -void +void c_open(char *file) { - file += 2; - FILE *f = fopen(file, "r"); - if (f == NULL) { - puts("couldnt open file."); - return; - } - b_truncate(buffer); - Row *row = buffer; - row->n = NULL; - while (1) { - size_t red = fread(&row->text, 1, ROW_SIZE, f); - if (red < ROW_SIZE) { - break; - } else { - Row *new_row = calloc(1, sizeof(Row)); - new_row->p = row; - row->n = new_row; - row = new_row; - } - } + FILE *f = fopen(file + 2, "r"); + if(f == NULL) { puts("couldnt open file."); return; } + b_truncate(buffer); + Row *row = buffer; + while (1) { + size_t red = 0; + for (int i = 0; i < ROW_SIZE; i++) { + red = fread(&row->text[i], 1, 1, f); + if (red != 1) return; + if (row->text[i] == '\n') { + row->text[i] = 0; + break; + } + } + row = b_insert(row); + } + fclose(f); } diff --git a/c_print.c b/c_print.c new file mode 100644 index 0000000..4c2a728 --- /dev/null +++ b/c_print.c @@ -0,0 +1,11 @@ +void +c_print(char *c) { + Row *row = buffer; + int line = 1; + for (;; line++) { + printf("%4.4i| %s\n", line, row->text); + if (row->n == NULL) break; + row = row -> n; + } +} + diff --git a/commands.c b/commands.c index 9a46b02..a972245 100644 --- a/commands.c +++ b/commands.c @@ -2,3 +2,4 @@ void (*commands[256])(char *) = { NULL, ['q'] = c_quit, ['O'] = c_open, +['P'] = c_print, diff --git a/efn.c b/efn.c index 1750d8e..868dab7 100644 --- a/efn.c +++ b/efn.c @@ -15,3 +15,5 @@ #include "c_open.c" #include "b_truncate.c" +#include "c_print.c" +#include "b_insert.c" diff --git a/efn.h b/efn.h index 695cb27..e86cc1e 100644 --- a/efn.h +++ b/efn.h @@ -8,3 +8,5 @@ void c_quit(char *); void c_open(char *); #include void b_truncate(Row *); +void c_print(char *); +Row *b_insert(Row *);