}
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)
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);
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);
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);
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);
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);
CHECK(TestBuf.modified == false);
}
+
/* Insertions
*************************************************************************/
TEST(buf_putc should insert at 0 in empty buf) {