#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);
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')
}
/* 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;
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 */
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)
{
#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 */
}
}
+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