From: Michael D. Lowis Date: Tue, 6 Mar 2018 18:40:34 +0000 (-0500) Subject: Added logic to save file to disk X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=89a4092985cbd9c0ca977312b143ebe79c8f11f1;p=archive%2Fgapbuf.git Added logic to save file to disk --- diff --git a/buf.c b/buf.c index 5c45366..4959e86 100644 --- 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) diff --git a/testbuf.c b/testbuf.c index 6788c65..5d89e1e 100644 --- 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) {