file loading works right now, added file printing
parent
7e684b606d
commit
a9118be5b1
|
@ -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;
|
||||
}
|
39
c_open.c
39
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,3 +2,4 @@ void (*commands[256])(char *) = {
|
|||
NULL,
|
||||
['q'] = c_quit,
|
||||
['O'] = c_open,
|
||||
['P'] = c_print,
|
||||
|
|
2
efn.c
2
efn.c
|
@ -15,3 +15,5 @@
|
|||
#include "c_open.c"
|
||||
|
||||
#include "b_truncate.c"
|
||||
#include "c_print.c"
|
||||
#include "b_insert.c"
|
||||
|
|
Loading…
Reference in New Issue