From 64760b56c50bd851c9130aa5ed76a3b954c51f5c Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Fri, 15 Feb 2013 14:47:59 +0400 Subject: [PATCH] (edit_buffer_write_file): new editor buffer API. Signed-off-by: Andrew Borodin --- src/editor/editbuffer.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ src/editor/editbuffer.h | 1 + src/editor/editcmd.c | 48 ++++++----------------------------------- 3 files changed, 65 insertions(+), 41 deletions(-) diff --git a/src/editor/editbuffer.c b/src/editor/editbuffer.c index 5df17050c..a2708ed02 100644 --- a/src/editor/editbuffer.c +++ b/src/editor/editbuffer.c @@ -349,3 +349,60 @@ edit_buffer_read_file (edit_buffer_t * buf, int fd, off_t size) } /* --------------------------------------------------------------------------------------------- */ +/** + * Write editor buffer content to file + * + * @param buf pointer to editor buffer + * @param fd file descriptor + * + * @return TRUE if file was written successfullly, FALSE otherswise + */ + +gboolean +edit_buffer_write_file (edit_buffer_t * buf, int fd) +{ + gboolean ret = TRUE; + off_t i; + off_t data_size, sz; + + /* write all fullfilled parts of buffers1 from begin to end */ + data_size = EDIT_BUF_SIZE; + for (i = 0; i < buf->curs1 >> S_EDIT_BUF_SIZE; i++) + if (mc_write (fd, (char *) buf->buffers1[i], data_size) != data_size) + return FALSE; + + /* write last partially filled part of buffers1 */ + data_size = buf->curs1 & M_EDIT_BUF_SIZE; + if (mc_write (fd, (char *) buf->buffers1[i], data_size) != data_size) + return FALSE; + + /* write buffers2 from end to begin, if buffers2 contains some data */ + if (buf->curs2 != 0) + { + buf->curs2--; + + /* write last partially filled part of buffers2 */ + i = buf->curs2 >> S_EDIT_BUF_SIZE; + data_size = (buf->curs2 & M_EDIT_BUF_SIZE) + 1; + if (mc_write (fd, (char *) buf->buffers2[i] + EDIT_BUF_SIZE - data_size, data_size) != data_size) + ret = FALSE; + else + { + data_size = EDIT_BUF_SIZE; + + /* write other fullfilled parts of buffers2 from end to begin */ + while (--i >= 0) + if (mc_write (fd, (char *) buf->buffers2[i], data_size) != data_size) + { + ret = FALSE; + break; + } + } + + buf->curs2++; + } + + return ret; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/editbuffer.h b/src/editor/editbuffer.h index c9b5b2b5b..6d5ab7095 100644 --- a/src/editor/editbuffer.h +++ b/src/editor/editbuffer.h @@ -59,6 +59,7 @@ int edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int * #endif off_t edit_buffer_read_file (edit_buffer_t * buf, int fd, off_t size); +gboolean edit_buffer_write_file (edit_buffer_t * buf, int fd); /*** inline functions ****************************************************************************/ diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index a427d5ea7..72779a7d7 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -309,52 +309,18 @@ edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath) } else if (edit->lb == LB_ASIS) { /* do not change line breaks */ - off_t buf; - buf = 0; filelen = edit->last_byte; - while (buf <= (edit->buffer.curs1 >> S_EDIT_BUF_SIZE) - 1) - { - if (mc_write (fd, (char *) edit->buffer.buffers1[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE) - { - mc_close (fd); - goto error_save; - } - buf++; - } - if (mc_write - (fd, (char *) edit->buffer.buffers1[buf], - edit->buffer.curs1 & M_EDIT_BUF_SIZE) != (edit->buffer.curs1 & M_EDIT_BUF_SIZE)) - { + + if (!edit_buffer_write_file (&edit->buffer, fd)) filelen = -1; - } - else if (edit->buffer.curs2 != 0) + + if (filelen != edit->last_byte) { - edit->buffer.curs2--; - buf = (edit->buffer.curs2 >> S_EDIT_BUF_SIZE); - if (mc_write - (fd, - (char *) edit->buffer.buffers2[buf] + EDIT_BUF_SIZE - - (edit->buffer.curs2 & M_EDIT_BUF_SIZE) - 1, - 1 + (edit->buffer.curs2 & M_EDIT_BUF_SIZE)) != 1 + (edit->buffer.curs2 & M_EDIT_BUF_SIZE)) - { - filelen = -1; - } - else - { - while (--buf >= 0) - { - if (mc_write (fd, (char *) edit->buffer.buffers2[buf], EDIT_BUF_SIZE) != EDIT_BUF_SIZE) - { - filelen = -1; - break; - } - } - } - edit->buffer.curs2++; + mc_close (fd); + goto error_save; } - if (mc_close (fd)) + if (mc_close (fd) != 0) goto error_save; - /* Update the file information, especially the mtime. */ if (mc_stat (savename_vpath, &edit->stat1) == -1) goto error_save; -- 2.11.4.GIT