From 64be24496e66ed26ed4d5af554a7fcef061d6e2d Mon Sep 17 00:00:00 2001 From: bx Date: Sat, 2 Apr 2022 14:58:16 +0000 Subject: [PATCH] can now save to file --- c_save.c | 17 +++++++++++++++++ commands.c | 1 + efn.c | 1 + efn.h | 1 + 4 files changed, 20 insertions(+) create mode 100644 c_save.c diff --git a/c_save.c b/c_save.c new file mode 100644 index 0000000..3f978cd --- /dev/null +++ b/c_save.c @@ -0,0 +1,17 @@ +void +c_save(char *file) { + FILE *f = fopen(file + 2, "w"); + if(f == NULL) { puts("couldnt open file."); return; } + Row *row = buffer; + while (1) { + for(int i = 0; i < ROW_SIZE; i++) { + if (row->text[i] == '\0' || row->text[i] == '\n') + break; + fwrite(&row->text[i], 1, 1, f); + } + fwrite("\n", 1, 1, f); + if (row->n == NULL) break; + row = row->n; + } +} + diff --git a/commands.c b/commands.c index b766b39..968b80a 100644 --- a/commands.c +++ b/commands.c @@ -6,3 +6,4 @@ NULL, ['w'] = c_writeline, ['q'] = NULL, ['Q'] = c_quit, +['S'] = c_save, diff --git a/efn.c b/efn.c index 07c18d9..d499431 100644 --- a/efn.c +++ b/efn.c @@ -20,3 +20,4 @@ #include "c_writeline.c" #include "b_getline.c" +#include "c_save.c" diff --git a/efn.h b/efn.h index a00a2e6..e118c15 100644 --- a/efn.h +++ b/efn.h @@ -12,3 +12,4 @@ void c_print(char *); Row *b_insert(Row *); Row *b_getline(Row *, int); void c_writeline(char *); +void c_save(char*);