file loading works right now, added file printing

master
bx 2022-04-02 13:20:59 +00:00
parent 7e684b606d
commit a9118be5b1
6 changed files with 42 additions and 21 deletions

8
b_insert.c 100644
View File

@ -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;
}

View File

@ -1,24 +1,21 @@
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);
}

11
c_print.c 100644
View File

@ -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;
}
}

View File

@ -2,3 +2,4 @@ void (*commands[256])(char *) = {
NULL,
['q'] = c_quit,
['O'] = c_open,
['P'] = c_print,

2
efn.c
View File

@ -15,3 +15,5 @@
#include "c_open.c"
#include "b_truncate.c"
#include "c_print.c"
#include "b_insert.c"

2
efn.h
View File

@ -8,3 +8,5 @@ void c_quit(char *);
void c_open(char *);
#include <string.h>
void b_truncate(Row *);
void c_print(char *);
Row *b_insert(Row *);