]> git.mdlowis.com Git - projs/tide.git/commitdiff
refactored save function
authorMichael D. Lowis <mike.lowis@gentex.com>
Wed, 12 Sep 2018 14:06:03 +0000 (10:06 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Wed, 12 Sep 2018 14:06:03 +0000 (10:06 -0400)
inc/edit.h
lib/buf.c
tests/lib/buf.c
tide.c

index 8e99967c459d597d5dfb4cc24a616e0c49e4605e..5a87aa841569b355f3e97a5146e66ee6e3d91274 100644 (file)
@@ -35,9 +35,10 @@ typedef struct {
 } Buf;
 
 void buf_init(Buf* buf);
+void buf_setpath(Buf* buf, char* path);
 void buf_load(Buf* buf, char* path);
 void buf_reload(Buf* buf);
-int buf_save(Buf* buf);
+int buf_save(Buf* buf, char* path);
 size_t buf_end(Buf* buf);
 
 int buf_getrat(Buf* buf, size_t off);
index 0501605c3a51aeec7d714e9da46e53b93708aea2..6244533ea36b15424448c2590e911cbdd9843e1e 100644 (file)
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -37,6 +37,13 @@ void buf_init(Buf* buf) {
     assert(buf->bufstart);
 }
 
+void buf_setpath(Buf* buf, char* path) {
+    if (path) {
+        free(buf->path);
+        buf->path = strdup(path);
+    }
+}
+
 void buf_load(Buf* buf, char* path) {
     if (!path) return;
     /* process the file path and address */
@@ -73,7 +80,8 @@ void buf_reload(Buf* buf) {
     buf_load(buf, path);
 }
 
-int buf_save(Buf* buf) {
+int buf_save(Buf* buf, char* path) {
+    buf_setpath(buf, path);
     if (0 == buf_end(buf)) return buf->status;
     char* wptr;
     long fd, nwrite = 0, towrite = 0;
index 7e130d5bf27e0c12416c076a06d12560a2f3e1e3..30eb03d52fd314747387900e334a8cfcf055221d 100644 (file)
@@ -99,7 +99,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&TestBuf);
         buf_load(&TestBuf, "testdocs/lorem.txt");
         TestBuf.status = MODIFIED;
-        buf_save(&TestBuf);
+        buf_save(&TestBuf, NULL);
         CHECK(TestBuf.status != MODIFIED);
     }
 
@@ -107,7 +107,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&TestBuf);
         buf_load(&TestBuf, "testdocs/waf");
         TestBuf.status = MODIFIED;
-        buf_save(&TestBuf);
+        buf_save(&TestBuf, NULL);
         CHECK(TestBuf.status != MODIFIED);
     }
 
@@ -115,7 +115,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&TestBuf);
         buf_load(&TestBuf, "testdocs/lf.txt");
         TestBuf.status = MODIFIED;
-        buf_save(&TestBuf);
+        buf_save(&TestBuf, NULL);
         CHECK(TestBuf.status != MODIFIED);
     }
 
@@ -123,7 +123,7 @@ TEST_SUITE(BufferTests) {
         buf_init(&TestBuf);
         buf_load(&TestBuf, "testdocs/crlf.txt");
         TestBuf.status = MODIFIED;
-        buf_save(&TestBuf);
+        buf_save(&TestBuf, NULL);
         CHECK(TestBuf.status != MODIFIED);
     }
 
diff --git a/tide.c b/tide.c
index 6927760ec9a8be69cfc2f227587c68d88bfbae3f..bd699e22eaf4f5a4e133f52953875926d67e1e29 100644 (file)
--- a/tide.c
+++ b/tide.c
@@ -258,22 +258,15 @@ static void quit(char* arg) {
 }
 
 static void put(char* arg) {
-    View* view = win_view(EDIT);
-    if (!arg) arg = view->buffer.path;
-    if (!arg) return;
-    char* path = realpath(arg, NULL);
-    if (!path) path = strdup(arg);
-    free(view->buffer.path);
-    view->buffer.path = path;
-    if (buf_save(&(view->buffer)) == NORMAL) {
-        char* path = realpath(view->buffer.path, NULL);
-        if (path) {
-            free(view->buffer.path);
-            view->buffer.path = path;
-        }
+    Buf* buf = win_buf(EDIT);
+    if (buf_save(buf, arg) == NORMAL) {
+        /* convert saved path to absolute path */
+        char* path = realpath(buf->path, NULL);
+        buf_setpath(buf, path);
+        free(path);
     }
-    win_title(view->buffer.path);
-    win_prop_set("TIDE_FILE", "file", view->buffer.path);
+    win_title(buf->path);
+    win_prop_set("TIDE_FILE", "file", buf->path);
 }
 
 static void get(char* arg) {
@@ -489,7 +482,7 @@ int main(int argc, char** argv) {
     /* if we still have args left we're going to open it in this instance */
     if (*argv) {
         char* path = realpath(*argv, NULL);
-        if (!path) path = strdup(*argv); // if  file doesnt exist, use the original name
+        if (!path) path = *argv; /* if file doesnt exist, use the original name */
         view_init(win_view(EDIT), path);
         win_title(path);
         win_prop_set("TIDE_FILE", "file", path);