commit 9d5cb8c21705502ac6de08dec7c35df4df2ead93
parent 0c582f90a9878b05a537e8bf13922ecb3a6a9063
Author: rexim <reximkut@gmail.com>
Date: Tue, 10 Jan 2023 14:21:34 +0700
Tuck file opening under editor_load_from_file()
Diffstat:
3 files changed, 39 insertions(+), 22 deletions(-)
diff --git a/src/editor.c b/src/editor.c
@@ -49,24 +49,31 @@ void editor_save_to_file(const Editor *editor, const char *file_path)
fclose(f);
}
-static size_t file_size(FILE *file)
+static Errno file_size(FILE *file, size_t *size)
{
long saved = ftell(file);
- assert(saved >= 0);
- int err = fseek(file, 0, SEEK_END);
- assert(err == 0);
+ if (saved < 0) return errno;
+ if (fseek(file, 0, SEEK_END) < 0) return errno;
long result = ftell(file);
- assert(result >= 0);
- err = fseek(file, saved, SEEK_SET);
- assert(err == 0);
- return result;
+ if (result < 0) return errno;
+ if (fseek(file, saved, SEEK_SET) < 0) return errno;
+ *size = (size_t) result;
+ return 0;
}
-void editor_load_from_file(Editor *e, FILE *file)
+Errno editor_load_from_file(Editor *e, const char *file_path)
{
+ Errno result = 0;
+ FILE *file = NULL;
+
e->data.count = 0;
- size_t data_size = file_size(file);
+ file = fopen(file_path, "rb");
+ if (file == NULL) return_defer(errno);
+
+ size_t data_size;
+ Errno err = file_size(file, &data_size);
+ if (err != 0) return_defer(err);
if (e->data.capacity < data_size) {
e->data.capacity = data_size;
@@ -75,10 +82,14 @@ void editor_load_from_file(Editor *e, FILE *file)
}
fread(e->data.items, data_size, 1, file);
- assert(!ferror(file));
+ if (ferror(file)) return_defer(errno);
e->data.count = data_size;
editor_recompute_lines(e);
+
+defer:
+ if (file) fclose(file);
+ return result;
}
size_t editor_cursor_row(const Editor *e)
diff --git a/src/editor.h b/src/editor.h
@@ -2,6 +2,7 @@
#define EDITOR_H_
#include <stdlib.h>
+#include "common.h"
typedef struct {
size_t begin;
@@ -28,7 +29,7 @@ typedef struct {
} Editor;
void editor_save_to_file(const Editor *editor, const char *file_path);
-void editor_load_from_file(Editor *editor, FILE *file);
+Errno editor_load_from_file(Editor *editor, const char *file_path);
void editor_backspace(Editor *editor);
void editor_delete(Editor *editor);
diff --git a/src/main.c b/src/main.c
@@ -224,6 +224,8 @@ void render_editor(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer
int main(int argc, char **argv)
{
+ Errno err;
+
editor_recompute_lines(&editor);
FT_Library library = {0};
@@ -255,6 +257,9 @@ int main(int argc, char **argv)
return 1;
}
+ // TODO: move file_path inside of Editor
+ // Editor must own the file path it's currently editing. It's also important to transafer the ownership of
+ // the file name from File_Browser to Editor properly.
const char *file_path = NULL;
if (argc > 1) {
@@ -262,17 +267,17 @@ int main(int argc, char **argv)
}
if (file_path) {
- FILE *file = fopen(file_path, "r");
- if (file != NULL) {
- editor_load_from_file(&editor, file);
- fclose(file);
+ err = editor_load_from_file(&editor, file_path);
+ if (err != 0) {
+ fprintf(stderr, "ERROR: Could ont read file %s: %s\n", file_path, strerror(err));
+ return 1;
}
}
const char *dir_path = ".";
- Errno err = fb_open_dir(&fb, dir_path);
+ err = fb_open_dir(&fb, dir_path);
if (err != 0) {
- fprintf(stderr, "ERROR: Could not read directory %s: %s\n", dir_path, strerror(errno));
+ fprintf(stderr, "ERROR: Could not read directory %s: %s\n", dir_path, strerror(err));
return 1;
}
@@ -374,11 +379,11 @@ int main(int argc, char **argv)
case SDLK_RETURN: {
if (fb.cursor < fb.files.count) {
file_path = fb.files.items[fb.cursor];
- FILE *file = fopen(file_path, "r");
- if (file != NULL) {
- editor_load_from_file(&editor, file);
+ err = editor_load_from_file(&editor, file_path);
+ if (err != 0) {
+ flash_error("Could not open file %s: %s", file_path, strerror(errno));
+ } else {
file_browser = false;
- fclose(file);
}
}
}