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 void
c_open(char *file) { c_open(char *file) {
file += 2; FILE *f = fopen(file + 2, "r");
FILE *f = fopen(file, "r"); if(f == NULL) { puts("couldnt open file."); return; }
if (f == NULL) { b_truncate(buffer);
puts("couldnt open file."); Row *row = buffer;
return; while (1) {
} size_t red = 0;
b_truncate(buffer); for (int i = 0; i < ROW_SIZE; i++) {
Row *row = buffer; red = fread(&row->text[i], 1, 1, f);
row->n = NULL; if (red != 1) return;
while (1) { if (row->text[i] == '\n') {
size_t red = fread(&row->text, 1, ROW_SIZE, f); row->text[i] = 0;
if (red < ROW_SIZE) { break;
break; }
} else { }
Row *new_row = calloc(1, sizeof(Row)); row = b_insert(row);
new_row->p = row; }
row->n = new_row; fclose(f);
row = new_row;
}
}
} }

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, NULL,
['q'] = c_quit, ['q'] = c_quit,
['O'] = c_open, ['O'] = c_open,
['P'] = c_print,

2
efn.c
View File

@ -15,3 +15,5 @@
#include "c_open.c" #include "c_open.c"
#include "b_truncate.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 *); void c_open(char *);
#include <string.h> #include <string.h>
void b_truncate(Row *); void b_truncate(Row *);
void c_print(char *);
Row *b_insert(Row *);