aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2020-05-13 08:56:22 +0200
committerMarc André Tanner <mat@brain-dump.org>2020-05-13 09:11:30 +0200
commit5a38f79952b4333bd6ac39a8af632179df5cc8ab (patch)
tree3b2092a1ddb59d48671c3c950e5fa0ec7f373f53
parenta1e7fb337927a4b76c93b10395f6030defcf2d34 (diff)
downloadvis-5a38f79952b4333bd6ac39a8af632179df5cc8ab.tar.gz
vis-5a38f79952b4333bd6ac39a8af632179df5cc8ab.tar.xz
text: introduce text_save_method, remove text_save_range
This utility function is analogous to text_load_method and allows the caller to specify how the file should be saved. It is implemented as a wrapper around the lower level text_save_{begin,write,commit} primitives. The unused text_save_range function has been removed. If needed, use the aforementioned lower level functionality.
-rw-r--r--text.c18
-rw-r--r--text.h21
2 files changed, 22 insertions, 17 deletions
diff --git a/text.c b/text.c
index 8b45e24..d640b5c 100644
--- a/text.c
+++ b/text.c
@@ -1067,26 +1067,26 @@ void text_save_cancel(TextSave *ctx) {
errno = saved_errno;
}
-bool text_save(Text *txt, const char *filename) {
- Filerange r = (Filerange){ .start = 0, .end = text_size(txt) };
- return text_save_range(txt, &r, filename);
-}
-
/* First try to save the file atomically using rename(2) if this does not
* work overwrite the file in place. However if something goes wrong during
* this overwrite the original file is permanently damaged.
*/
-bool text_save_range(Text *txt, Filerange *range, const char *filename) {
+bool text_save(Text *txt, const char *filename) {
+ return text_save_method(txt, filename, TEXT_SAVE_AUTO);
+}
+
+bool text_save_method(Text *txt, const char *filename, enum TextSaveMethod method) {
if (!filename) {
txt->saved_revision = txt->history;
text_snapshot(txt);
return true;
}
- TextSave *ctx = text_save_begin(txt, filename, TEXT_SAVE_AUTO);
+ TextSave *ctx = text_save_begin(txt, filename, method);
if (!ctx)
return false;
- ssize_t written = text_write_range(txt, range, ctx->fd);
- if (written == -1 || (size_t)written != text_range_size(range)) {
+ Filerange range = (Filerange){ .start = 0, .end = text_size(txt) };
+ ssize_t written = text_save_write_range(ctx, &range);
+ if (written == -1 || (size_t)written != text_range_size(&range)) {
text_save_cancel(ctx);
return false;
}
diff --git a/text.h b/text.h
index be99894..5d653ff 100644
--- a/text.h
+++ b/text.h
@@ -302,14 +302,6 @@ size_t text_mark_get(Text*, Mark);
* @{
*/
/**
- * Save the whole text to the given file name.
- */
-bool text_save(Text*, const char *filename);
-/**
- * Save a file range to the given file name.
- */
-bool text_save_range(Text*, Filerange*, const char *filename);
-/**
* Method used to save the text.
*/
enum TextSaveMethod {
@@ -346,6 +338,19 @@ enum TextSaveMethod {
};
/**
+ * Save the whole text to the given file name.
+ */
+bool text_save(Text*, const char *filename);
+/**
+ * Save the whole text to the given file name, using the specified method.
+ *
+ * @rst
+ * .. note:: Equivalent to ``text_save_method(filename, TEXT_SAVE_AUTO)``.
+ * @endrst
+ */
+bool text_save_method(Text*, const char *filename, enum TextSaveMethod);
+
+/**
* Setup a sequence of write operations.
*
* The returned `TextSave` pointer can be used to write multiple, possibly