commit 4433e11fc68154711c9135a4375818e56a6fdc15
parent 9d5cb8c21705502ac6de08dec7c35df4df2ead93
Author: rexim <reximkut@gmail.com>
Date: Tue, 10 Jan 2023 20:25:43 +0700
Move file_path inside of Editor structure
Diffstat:
5 files changed, 91 insertions(+), 35 deletions(-)
diff --git a/src/common.c b/src/common.c
@@ -56,6 +56,22 @@ defer:
return result;
}
+Errno write_entire_file(const char *file_path, const char *buf, size_t buf_size)
+{
+ Errno result = 0;
+ FILE *f = NULL;
+
+ f = fopen(file_path, "wb");
+ if (f == NULL) return_defer(errno);
+
+ fwrite(buf, 1, buf_size, f);
+ if (ferror(f)) return_defer(errno);
+
+defer:
+ if (f) fclose(f);
+ return result;
+}
+
char *read_entire_file(const char *file_path)
{
#define SLURP_FILE_PANIC \
diff --git a/src/common.h b/src/common.h
@@ -7,36 +7,67 @@ typedef int Errno;
#define return_defer(value) do { result = (value); goto defer; } while (0)
-#define UNIMPLEMENTED(...) \
- do { \
+#define UNIMPLEMENTED(...) \
+ do { \
printf("%s:%d: UNIMPLEMENTED: %s \n", __FILE__, __LINE__, __VA_ARGS__); \
- exit(1); \
+ exit(1); \
} while(0)
#define UNUSED(x) (void)(x)
#define DA_INIT_CAP 256
-#define da_append(da, item) \
- do { \
- if ((da)->count >= (da)->capacity) { \
- (da)->capacity = (da)->capacity == 0 ? DA_INIT_CAP : (da)->capacity*2; \
+#define da_append(da, item) \
+ do { \
+ if ((da)->count >= (da)->capacity) { \
+ (da)->capacity = (da)->capacity == 0 ? DA_INIT_CAP : (da)->capacity*2; \
(da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \
- assert((da)->items != NULL && "Buy more RAM lol"); \
- } \
- \
- (da)->items[(da)->count++] = (item); \
+ assert((da)->items != NULL && "Buy more RAM lol"); \
+ } \
+ \
+ (da)->items[(da)->count++] = (item); \
+ } while (0)
+
+#define da_append_many(da, new_items, new_items_count) \
+ do { \
+ if ((da)->count + new_items_count > (da)->capacity) { \
+ if ((da)->capacity == 0) { \
+ (da)->capacity = DA_INIT_CAP; \
+ } \
+ while ((da)->count + new_items_count > (da)->capacity) { \
+ (da)->capacity *= 2; \
+ } \
+ (da)->items = realloc((da)->items, (da)->capacity*sizeof(*(da)->items)); \
+ assert((da)->items != NULL && "Buy more RAM lol"); \
+ } \
+ memcpy((da)->items + (da)->count, new_items, new_items_count*sizeof(*(da)->items)); \
+ (da)->count += new_items_count; \
} while (0)
char *temp_strdup(const char *s);
void temp_reset(void);
typedef struct {
+ char *items;
+ size_t count;
+ size_t capacity;
+} String_Builder;
+
+#define sb_append_buf da_append_many
+#define sb_append_cstr(sb, cstr) \
+ do { \
+ size_t n = strlen(cstr); \
+ da_append_many(sb, cstr, n); \
+ } while (0)
+#define sb_append_null(sb) da_append_many(sb, "", 1)
+
+typedef struct {
const char **items;
size_t count;
size_t capacity;
} Files;
char *read_entire_file(const char *file_path);
+Errno write_entire_file(const char *file_path, const char *buf, size_t buf_size);
Errno read_entire_dir(const char *dir_path, Files *files);
#endif // COMMON_H_
diff --git a/src/editor.c b/src/editor.c
@@ -36,17 +36,20 @@ void editor_delete(Editor *e)
editor_recompute_lines(e);
}
-void editor_save_to_file(const Editor *editor, const char *file_path)
+Errno editor_save_as(Editor *editor, const char *file_path)
{
- FILE *f = fopen(file_path, "w");
- if (f == NULL) {
- fprintf(stdout, "ERROR: could not open file `%s`: %s\n",
- file_path, strerror(errno));
- exit(1);
- }
+ Errno err = write_entire_file(file_path, editor->data.items, editor->data.count);
+ if (err != 0) return err;
+ editor->file_path.count = 0;
+ sb_append_cstr(&editor->file_path, file_path);
+ sb_append_null(&editor->file_path);
+ return 0;
+}
- fwrite(editor->data.items, 1, editor->data.count, f);
- fclose(f);
+Errno editor_save(const Editor *editor)
+{
+ assert(editor->file_path.count > 0);
+ return write_entire_file(editor->file_path.items, editor->data.items, editor->data.count);
}
static Errno file_size(FILE *file, size_t *size)
@@ -88,6 +91,11 @@ Errno editor_load_from_file(Editor *e, const char *file_path)
editor_recompute_lines(e);
defer:
+ if (result == 0) {
+ e->file_path.count = 0;
+ sb_append_cstr(&e->file_path, file_path);
+ sb_append_null(&e->file_path);
+ }
if (file) fclose(file);
return result;
}
diff --git a/src/editor.h b/src/editor.h
@@ -21,14 +21,15 @@ typedef struct {
size_t capacity;
} Lines;
-
typedef struct {
Data data;
Lines lines;
+ String_Builder file_path;
size_t cursor;
} Editor;
-void editor_save_to_file(const Editor *editor, const char *file_path);
+Errno editor_save_as(Editor *editor, const char *file_path);
+Errno editor_save(const Editor *editor);
Errno editor_load_from_file(Editor *editor, const char *file_path);
void editor_backspace(Editor *editor);
diff --git a/src/main.c b/src/main.c
@@ -220,7 +220,7 @@ void render_editor(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer
}
// TODO: display errors reported via flash_error right in the text editor window somehow
-#define flash_error(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
+#define flash_error(...) fprintf(stderr, __VA_ARGS__)
int main(int argc, char **argv)
{
@@ -257,16 +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) {
- file_path = argv[1];
- }
-
- if (file_path) {
+ const char *file_path = argv[1];
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));
@@ -378,10 +371,11 @@ int main(int argc, char **argv)
case SDLK_RETURN: {
if (fb.cursor < fb.files.count) {
- file_path = fb.files.items[fb.cursor];
+ // TODO: go into folders
+ const char *file_path = fb.files.items[fb.cursor];
err = editor_load_from_file(&editor, file_path);
if (err != 0) {
- flash_error("Could not open file %s: %s", file_path, strerror(errno));
+ flash_error("Could not open file %s: %s", file_path, strerror(err));
} else {
file_browser = false;
}
@@ -398,8 +392,14 @@ int main(int argc, char **argv)
break;
case SDLK_F2: {
- if (file_path) {
- editor_save_to_file(&editor, file_path);
+ if (editor.file_path.count > 0) {
+ err = editor_save(&editor);
+ if (err != 0) {
+ flash_error("Could not save file currently edited file: %s", strerror(err));
+ }
+ } else {
+ // TODO: as the user for the path to save to in this situation
+ flash_error("No where to save the text");
}
}
break;