Browse Source

Tuck file opening under editor_load_from_file()

rexim 2 years ago
parent
commit
9d5cb8c217
3 changed files with 39 additions and 22 deletions
  1. 22 11
      src/editor.c
  2. 2 1
      src/editor.h
  3. 15 10
      src/main.c

+ 22 - 11
src/editor.c

@@ -49,24 +49,31 @@ void editor_save_to_file(const Editor *editor, const char *file_path)
     fclose(f);
     fclose(f);
 }
 }
 
 
-static size_t file_size(FILE *file)
+static Errno file_size(FILE *file, size_t *size)
 {
 {
     long saved = ftell(file);
     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);
     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;
     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) {
     if (e->data.capacity < data_size) {
         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);
     fread(e->data.items, data_size, 1, file);
-    assert(!ferror(file));
+    if (ferror(file)) return_defer(errno);
     e->data.count = data_size;
     e->data.count = data_size;
 
 
     editor_recompute_lines(e);
     editor_recompute_lines(e);
+
+defer:
+    if (file) fclose(file);
+    return result;
 }
 }
 
 
 size_t editor_cursor_row(const Editor *e)
 size_t editor_cursor_row(const Editor *e)

+ 2 - 1
src/editor.h

@@ -2,6 +2,7 @@
 #define EDITOR_H_
 #define EDITOR_H_
 
 
 #include <stdlib.h>
 #include <stdlib.h>
+#include "common.h"
 
 
 typedef struct {
 typedef struct {
     size_t begin;
     size_t begin;
@@ -28,7 +29,7 @@ typedef struct {
 } Editor;
 } Editor;
 
 
 void editor_save_to_file(const Editor *editor, const char *file_path);
 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_backspace(Editor *editor);
 void editor_delete(Editor *editor);
 void editor_delete(Editor *editor);

+ 15 - 10
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)
 int main(int argc, char **argv)
 {
 {
+    Errno err;
+
     editor_recompute_lines(&editor);
     editor_recompute_lines(&editor);
 
 
     FT_Library library = {0};
     FT_Library library = {0};
@@ -255,6 +257,9 @@ int main(int argc, char **argv)
         return 1;
         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;
     const char *file_path = NULL;
 
 
     if (argc > 1) {
     if (argc > 1) {
@@ -262,17 +267,17 @@ int main(int argc, char **argv)
     }
     }
 
 
     if (file_path) {
     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 = ".";
     const char *dir_path = ".";
-    Errno err = fb_open_dir(&fb, dir_path);
+    err = fb_open_dir(&fb, dir_path);
     if (err != 0) {
     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;
         return 1;
     }
     }
 
 
@@ -374,11 +379,11 @@ int main(int argc, char **argv)
                     case SDLK_RETURN: {
                     case SDLK_RETURN: {
                         if (fb.cursor < fb.files.count) {
                         if (fb.cursor < fb.files.count) {
                             file_path = fb.files.items[fb.cursor];
                             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;
                                 file_browser = false;
-                                fclose(file);
                             }
                             }
                         }
                         }
                     }
                     }