From 6147c3bd9a0f46de27a7f6b50a03a7708129207a Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Mon, 14 Oct 2019 09:50:16 -0400 Subject: [PATCH] refactored a bit to move gap buffer specific logic to its own file --- src/lib/buf.c | 65 +++++---------------------------------------- src/lib/gapbuf.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 58 deletions(-) diff --git a/src/lib/buf.c b/src/lib/buf.c index bd54548..9abd8ba 100644 --- a/src/lib/buf.c +++ b/src/lib/buf.c @@ -8,6 +8,9 @@ #include #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) { diff --git a/src/lib/gapbuf.c b/src/lib/gapbuf.c index 1609b03..50cbef8 100644 --- a/src/lib/gapbuf.c +++ b/src/lib/gapbuf.c @@ -8,6 +8,17 @@ #include #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 -- 2.52.0