]> git.mdlowis.com Git - projs/tide.git/commitdiff
refactored a bit to move gap buffer specific logic to its own file
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 14 Oct 2019 13:50:16 +0000 (09:50 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 14 Oct 2019 13:50:16 +0000 (09:50 -0400)
src/lib/buf.c
src/lib/gapbuf.c

index bd545483d250e824707683b2c22cb941a0353d5b..9abd8ba8770800e32a42c4bd08eef0a3e124ea2f 100644 (file)
@@ -8,6 +8,9 @@
 #include <sys/stat.h>
 #include "config.h"
 
+extern void gapbuf_init(Buf* buf);
+extern long gapbuf_save(Buf* buf, char* path);
+extern void gapbuf_load(Buf* buf, char* path);
 extern char gapbuf_getb(Buf* buf, size_t off);
 extern void gapbuf_putb(Buf* buf, char b, Sel* p_sel);
 extern void gapbuf_del(Buf* buf, size_t off, size_t len);
@@ -45,17 +48,6 @@ static Sel selget(Buf* buf)
     return (sel.end < sel.beg ? selswap(sel) : sel);
 }
 
-static size_t pagealign(size_t sz)
-{
-    size_t pgsize = sysconf(_SC_PAGE_SIZE);
-    size_t alignmask = (pgsize - 1);
-    if (sz & alignmask)
-    {
-        sz += pgsize - (sz & alignmask);
-    }
-    return sz;
-}
-
 static void putch(Buf* buf, char b, Sel* p_sel)
 {
     if (b != '\r')
@@ -87,12 +79,8 @@ void buf_init(Buf* buf)
     }
 
     /* reset the state to defaults */
+    gapbuf_init(buf);
     buf->status    = NORMAL;
-    buf->bufsize   = 8192;
-    buf->bufstart  = malloc(buf->bufsize);
-    buf->bufend    = buf->bufstart + buf->bufsize;
-    buf->gapstart  = buf->bufstart;
-    buf->gapend    = buf->bufend;
     buf->undo      = NULL;
     buf->redo      = NULL;
     buf->transid   = -1;
@@ -123,31 +111,10 @@ void buf_load(Buf* buf, char* path)
             path += 2;
         }
         buf->path = strdup(path);
-
-        /* load the contents from the file */
-        int fd, nread;
-        struct stat sb = {0};
-        if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0))
-        {
-            /* allocate the buffer in advance */
-            free(buf->bufstart);
-            buf->bufsize  = pagealign(sb.st_size);
-            buf->bufstart = malloc(buf->bufsize);
-            buf->bufend   = buf->bufstart + buf->bufsize;
-            buf->gapstart = buf->bufstart;
-            buf->gapend   = buf->bufend;
-            /* Read the file into the buffer */
-            while (sb.st_size && (nread = read(fd, buf->gapstart, sb.st_size)) > 0)
-                buf->gapstart += nread, sb.st_size -= nread;
-        }
-        if (fd > 0)
-        {
-            close(fd);
-        }
+        gapbuf_load(buf, buf->path);
 
         /* reset buffer state */
         buf->status  = NORMAL;
-        buf->modtime = (uint64_t)sb.st_mtime;
         buf_logclear(buf);
 
         /* use the EOL style of the first line to determine EOL style */
@@ -222,26 +189,8 @@ int buf_save(Buf* buf, char* path)
             trim_whitespace(buf);
         }
 
-        char* wptr;
-        long fd, nwrite = 0, towrite = 0;
-        if (buf->path && (fd = open(buf->path, O_WRONLY|O_CREAT|O_TRUNC, 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 */
-            buf->status = (nwrite >= 0 ? NORMAL : ERRORED);
-        }
-        else
-        {
-            buf->status = ERRORED;
-        }
+        long nwrite = gapbuf_save(buf, buf->path);
+        buf->status = (nwrite >= 0 ? NORMAL : ERRORED);
 
         if (buf->status == NORMAL)
         {
index 1609b03568e528a41420de00b91b40f1ae2ac134..50cbef81eadac3d2f6b1540e8c469bc467900c07 100644 (file)
@@ -8,6 +8,17 @@
 #include <sys/stat.h>
 #include "config.h"
 
+static size_t pagealign(size_t sz)
+{
+    size_t pgsize = sysconf(_SC_PAGE_SIZE);
+    size_t alignmask = (pgsize - 1);
+    if (sz & alignmask)
+    {
+        sz += pgsize - (sz & alignmask);
+    }
+    return sz;
+}
+
 static void resize(Buf* buf, size_t sz)
 {
     /* allocate the new buffer and gap */
@@ -60,6 +71,64 @@ static void syncgap(Buf* buf, size_t off)
     }
 }
 
+void gapbuf_init(Buf* buf)
+{
+    buf->bufsize   = 8192;
+    buf->bufstart  = malloc(buf->bufsize);
+    buf->bufend    = buf->bufstart + buf->bufsize;
+    buf->gapstart  = buf->bufstart;
+    buf->gapend    = buf->bufend;
+}
+
+long gapbuf_save(Buf* buf, char* path)
+{
+    char* wptr;
+    long fd, nwrite = 0, towrite = 0;
+    if (path && (fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 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 */
+        buf->status = (nwrite >= 0 ? NORMAL : ERRORED);
+    }
+    else
+    {
+        nwrite = -1;
+    }
+    return nwrite;
+}
+
+void gapbuf_load(Buf* buf, char* path)
+{
+    /* load the contents from the file */
+    int fd, nread;
+    struct stat sb = {0};
+    if (((fd = open(path, O_RDONLY, 0)) >= 0) && (fstat(fd, &sb) >= 0) && (sb.st_size > 0))
+    {
+        /* allocate the buffer in advance */
+        free(buf->bufstart);
+        buf->bufsize  = pagealign(sb.st_size);
+        buf->bufstart = malloc(buf->bufsize);
+        buf->bufend   = buf->bufstart + buf->bufsize;
+        buf->gapstart = buf->bufstart;
+        buf->gapend   = buf->bufend;
+        /* Read the file into the buffer */
+        while (sb.st_size && (nread = read(fd, buf->gapstart, sb.st_size)) > 0)
+            buf->gapstart += nread, sb.st_size -= nread;
+    }
+    if (fd > 0)
+    {
+        close(fd);
+    }
+}
+
 char gapbuf_getb(Buf* buf, size_t off)
 {
     int c = '\n'; // TODO: get rid of this hack