From: Michael D. Lowis Date: Wed, 12 Sep 2018 14:06:03 +0000 (-0400) Subject: refactored save function X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=63124e12326f7435be713c28db468d052b3c6135;p=projs%2Ftide.git refactored save function --- diff --git a/inc/edit.h b/inc/edit.h index 8e99967..5a87aa8 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -35,9 +35,10 @@ typedef struct { } Buf; void buf_init(Buf* buf); +void buf_setpath(Buf* buf, char* path); void buf_load(Buf* buf, char* path); void buf_reload(Buf* buf); -int buf_save(Buf* buf); +int buf_save(Buf* buf, char* path); size_t buf_end(Buf* buf); int buf_getrat(Buf* buf, size_t off); diff --git a/lib/buf.c b/lib/buf.c index 0501605..6244533 100644 --- a/lib/buf.c +++ b/lib/buf.c @@ -37,6 +37,13 @@ void buf_init(Buf* buf) { assert(buf->bufstart); } +void buf_setpath(Buf* buf, char* path) { + if (path) { + free(buf->path); + buf->path = strdup(path); + } +} + void buf_load(Buf* buf, char* path) { if (!path) return; /* process the file path and address */ @@ -73,7 +80,8 @@ void buf_reload(Buf* buf) { buf_load(buf, path); } -int buf_save(Buf* buf) { +int buf_save(Buf* buf, char* path) { + buf_setpath(buf, path); if (0 == buf_end(buf)) return buf->status; char* wptr; long fd, nwrite = 0, towrite = 0; diff --git a/tests/lib/buf.c b/tests/lib/buf.c index 7e130d5..30eb03d 100644 --- a/tests/lib/buf.c +++ b/tests/lib/buf.c @@ -99,7 +99,7 @@ TEST_SUITE(BufferTests) { buf_init(&TestBuf); buf_load(&TestBuf, "testdocs/lorem.txt"); TestBuf.status = MODIFIED; - buf_save(&TestBuf); + buf_save(&TestBuf, NULL); CHECK(TestBuf.status != MODIFIED); } @@ -107,7 +107,7 @@ TEST_SUITE(BufferTests) { buf_init(&TestBuf); buf_load(&TestBuf, "testdocs/waf"); TestBuf.status = MODIFIED; - buf_save(&TestBuf); + buf_save(&TestBuf, NULL); CHECK(TestBuf.status != MODIFIED); } @@ -115,7 +115,7 @@ TEST_SUITE(BufferTests) { buf_init(&TestBuf); buf_load(&TestBuf, "testdocs/lf.txt"); TestBuf.status = MODIFIED; - buf_save(&TestBuf); + buf_save(&TestBuf, NULL); CHECK(TestBuf.status != MODIFIED); } @@ -123,7 +123,7 @@ TEST_SUITE(BufferTests) { buf_init(&TestBuf); buf_load(&TestBuf, "testdocs/crlf.txt"); TestBuf.status = MODIFIED; - buf_save(&TestBuf); + buf_save(&TestBuf, NULL); CHECK(TestBuf.status != MODIFIED); } diff --git a/tide.c b/tide.c index 6927760..bd699e2 100644 --- a/tide.c +++ b/tide.c @@ -258,22 +258,15 @@ static void quit(char* arg) { } static void put(char* arg) { - View* view = win_view(EDIT); - if (!arg) arg = view->buffer.path; - if (!arg) return; - char* path = realpath(arg, NULL); - if (!path) path = strdup(arg); - free(view->buffer.path); - view->buffer.path = path; - if (buf_save(&(view->buffer)) == NORMAL) { - char* path = realpath(view->buffer.path, NULL); - if (path) { - free(view->buffer.path); - view->buffer.path = path; - } + Buf* buf = win_buf(EDIT); + if (buf_save(buf, arg) == NORMAL) { + /* convert saved path to absolute path */ + char* path = realpath(buf->path, NULL); + buf_setpath(buf, path); + free(path); } - win_title(view->buffer.path); - win_prop_set("TIDE_FILE", "file", view->buffer.path); + win_title(buf->path); + win_prop_set("TIDE_FILE", "file", buf->path); } static void get(char* arg) { @@ -489,7 +482,7 @@ int main(int argc, char** argv) { /* if we still have args left we're going to open it in this instance */ if (*argv) { char* path = realpath(*argv, NULL); - if (!path) path = strdup(*argv); // if file doesnt exist, use the original name + if (!path) path = *argv; /* if file doesnt exist, use the original name */ view_init(win_view(EDIT), path); win_title(path); win_prop_set("TIDE_FILE", "file", path);