shithub: orca

Download patch

ref: 97c05c3efced5687cf2f1900058cda582fe4d39a
parent: 1f2c2cfc784cc2e1d55d3cc197fc2101ab00118c
author: cancel <cancel@cancel.fm>
date: Mon Dec 17 23:28:17 EST 2018

Add start of more advanced file name handling

--- a/term_util.c
+++ b/term_util.c
@@ -2,6 +2,40 @@
 #include <form.h>
 #include <menu.h>
 
+// No overflow checks in most of these guys. Switch to use 'sds' if we ever
+// need anything more advanced.
+void heapstr_init(Heapstr* hs) {
+  enum { InitialCapacity = 16 };
+  hs->str = malloc(InitialCapacity);
+  hs->capacity = InitialCapacity;
+  hs->str[0] = 0;
+}
+void heapstr_init_cstr(Heapstr* hs, char const* cstr) {
+  Usz len = strlen(cstr);
+  hs->str = malloc(len + 1);
+  hs->capacity = len + 1;
+  memcpy(hs->str, cstr, len + 1);
+}
+void heapstr_deinit(Heapstr* hs) { free(hs->str); }
+void heapstr_reserve(Heapstr* hs, Usz capacity) {
+  if (hs->capacity < capacity) {
+    Usz new_cap = orca_round_up_power2(capacity);
+    hs->str = realloc(hs->str, new_cap);
+    hs->capacity = new_cap;
+  }
+}
+void heapstr_set_cstrlen(Heapstr* hs, char const* cstr, Usz len) {
+  heapstr_reserve(hs, len + 1);
+  memcpy(hs->str, cstr, len + 1);
+}
+void heapstr_set_cstr(Heapstr* hs, char const* cstr) {
+  Usz len = strlen(cstr);
+  heapstr_set_cstrlen(hs, cstr, len);
+}
+Usz heapstr_len(Heapstr const* hs) {
+  return strlen(hs->str);
+}
+
 void term_util_init_colors() {
   if (has_colors()) {
     // Enable color
--- a/term_util.h
+++ b/term_util.h
@@ -2,6 +2,20 @@
 #include "base.h"
 #include <ncurses.h>
 
+// Quick'n'dirty heap allocated string, zero-terminated, not 'binary safe' for
+// null chars.
+typedef struct {
+  char* str;
+  Usz capacity;
+} Heapstr;
+
+void heapstr_init(Heapstr* hs);
+void heapstr_init_cstr(Heapstr* hs, char const* cstr);
+void heapstr_deinit(Heapstr* hs);
+void heapstr_set_cstr(Heapstr* hs, char const* cstr);
+void heapstr_set_cstrlen(Heapstr* hs, char const* cstr, Usz size);
+Usz heapstr_len(Heapstr const* hs);
+
 #define CTRL_PLUS(c) ((c)&037)
 
 typedef enum {
--- a/tui_main.c
+++ b/tui_main.c
@@ -1742,6 +1742,8 @@
     }
   }
 
+  Heapstr file_name;
+
   if (input_file) {
     Field_load_error fle = field_load_file(input_file, &ged_state.field);
     if (fle != Field_load_error_ok) {
@@ -1767,13 +1769,15 @@
       }
       fprintf(stderr, "File load error: %s.\n", errstr);
       ged_deinit(&ged_state);
+      qnav_deinit();
       return 1;
     }
+    heapstr_init_cstr(&file_name, input_file);
   } else {
-    input_file = "unnamed";
+    heapstr_init_cstr(&file_name, "unnamed");
     field_init_fill(&ged_state.field, 25, 57, '.');
   }
-  ged_state.filename = input_file;
+  ged_state.filename = file_name.str;
   ged_set_midi_mode(&ged_state, &midi_mode);
 
   // Set up timer lib
@@ -1962,7 +1966,7 @@
                 try_save_with_msg(&ged_state);
                 break;
               case Main_menu_save_as:
-                push_save_as_form(ged_state.filename);
+                push_save_as_form(file_name.str);
                 break;
               }
             }
@@ -2129,5 +2133,6 @@
   }
   endwin();
   ged_deinit(&ged_state);
+  heapstr_deinit(&file_name);
   return 0;
 }