]> git.mdlowis.com Git - archive/gapbuf.git/commitdiff
Added logic to save file to disk
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Mar 2018 18:40:34 +0000 (13:40 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 6 Mar 2018 18:40:34 +0000 (13:40 -0500)
buf.c
testbuf.c

diff --git a/buf.c b/buf.c
index 5c453663b7f97e5b935d5c99b0f7065faaf5d93a..4959e86a4d9c3ef6b3a9cdc661ad02fd6aa74164 100644 (file)
--- a/buf.c
+++ b/buf.c
@@ -97,6 +97,24 @@ void buf_reload(Buf* buf) {
 }
 
 void buf_save(Buf* buf) {
+    char* wptr;
+    long fd, nwrite, towrite;
+    if (buf->path && (fd = open(buf->path, O_WRONLY|O_CREAT, 0644)) >= 0) {
+        /* write the chunk before the gap */
+        wptr = buf->bufstart, towrite = (buf->gapstart - buf->bufstart);
+        while (towrite && ((nwrite = write(fd, wptr, towrite)) > 0))
+            wptr += nwrite, towrite -= nwrite;
+        /* write the chunk after the gap */
+        wptr = buf->gapend, towrite = (buf->bufend - buf->gapend);
+        while (towrite && ((nwrite = write(fd, wptr, towrite)) > 0))
+            wptr += nwrite, towrite -= nwrite;
+        close(fd);
+        /* report success or failure */
+        if (nwrite >= 0)
+            buf->modified = false;
+        else
+            buf->errfn("Failed to write file");
+    }
 }
 
 Rune buf_getc(Buf* buf, Sel* sel)
index 6788c65424b629cf047c2b3594cdf633a4dd05c7..5d89e1e00fde3a31afc2f184b4aa582c5117d182 100644 (file)
--- a/testbuf.c
+++ b/testbuf.c
@@ -190,12 +190,11 @@ TEST_SUITE(BufferTests) {
         CHECK(!strcmp(TestBuf.path, "testdocs/lorem.txt"));
     }
 
-#if 0
     /* Saving
      *************************************************************************/
     TEST(buf_save should save a UTF-8 file to disk) {
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, "testdocs/lorem.txt");
+        buf_load(&TestBuf, NULL, "testdocs/lorem.txt");
         TestBuf.modified = true;
         buf_save(&TestBuf);
         CHECK(TestBuf.modified == false);
@@ -203,7 +202,7 @@ TEST_SUITE(BufferTests) {
 
     TEST(buf_save should save a non UTF-8 file to disk) {
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, "testdocs/waf");
+        buf_load(&TestBuf, NULL, "testdocs/waf");
         TestBuf.modified = true;
         buf_save(&TestBuf);
         CHECK(TestBuf.modified == false);
@@ -211,7 +210,7 @@ TEST_SUITE(BufferTests) {
 
     TEST(buf_save should save a file to disk with unix line endings) {
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, "testdocs/lf.txt");
+        buf_load(&TestBuf, NULL, "testdocs/lf.txt");
         TestBuf.modified = true;
         buf_save(&TestBuf);
         CHECK(TestBuf.modified == false);
@@ -219,15 +218,16 @@ TEST_SUITE(BufferTests) {
 
     TEST(buf_save should save a file to disk with dos line endings) {
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, "testdocs/crlf.txt");
+        buf_load(&TestBuf, NULL, "testdocs/crlf.txt");
         TestBuf.modified = true;
         buf_save(&TestBuf);
         CHECK(TestBuf.modified == false);
     }
 
+#if 0
     TEST(buf_save should make sure unix file ends witn newline) {
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, "testdocs/lf.txt");
+        buf_load(&TestBuf, NULL, "testdocs/lf.txt");
         TestBuf.modified = true;
         size_t end = buf_last(&TestBuf);
         buf_delete(&TestBuf, end-1, end);
@@ -237,9 +237,9 @@ TEST_SUITE(BufferTests) {
         CHECK(TestBuf.modified == false);
     }
 
-    TEST(buf_save should make sure dos file ends witn newline) {
+    TEST(buf_save should make sure dos file ends with newline) {
         buf_init(&TestBuf, NULL);
-        buf_load(&TestBuf, "testdocs/crlf.txt");
+        buf_load(&TestBuf, NULL, "testdocs/crlf.txt");
         TestBuf.modified = true;
         size_t end = buf_last(&TestBuf);
         buf_delete(&TestBuf, end-1, end);
@@ -249,6 +249,7 @@ TEST_SUITE(BufferTests) {
         CHECK(TestBuf.modified == false);
     }
 
+
     /* Insertions
      *************************************************************************/
     TEST(buf_putc should insert at 0 in empty buf) {