From fc8044e178c4c31a94e361a5a9815d00ef1fcd5f Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Fri, 15 Feb 2013 14:10:34 +0400 Subject: [PATCH] (edit_buffer_read_file): new editor buffer API. Signed-off-by: Andrew Borodin --- src/editor/edit.c | 38 ++++++++--------------------------- src/editor/editbuffer.c | 44 +++++++++++++++++++++++++++++++++++++++++ src/editor/editbuffer.h | 2 ++ 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/src/editor/edit.c b/src/editor/edit.c index e7561143a..9610013b4 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -147,12 +147,11 @@ static const struct edit_filters static gboolean edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath) { - off_t buf, buf2; - int file = -1; - gboolean ret = FALSE; + int file; + gboolean ret; file = mc_open (filename_vpath, O_RDONLY | O_BINARY); - if (file == -1) + if (file < 0) { gchar *errmsg; @@ -163,32 +162,10 @@ edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath) return FALSE; } - edit->buffer.curs2 = edit->last_byte; - buf2 = edit->buffer.curs2 >> S_EDIT_BUF_SIZE; - - if (edit->buffer.buffers2[buf2] == NULL) - edit->buffer.buffers2[buf2] = g_malloc0 (EDIT_BUF_SIZE); - - if (mc_read (file, - (char *) edit->buffer.buffers2[buf2] + EDIT_BUF_SIZE - - (edit->buffer.curs2 & M_EDIT_BUF_SIZE), edit->buffer.curs2 & M_EDIT_BUF_SIZE) < 0) - goto done; - - for (buf = buf2 - 1; buf >= 0; buf--) - { - /* edit->buffer.buffers2[0] is already allocated */ - if (edit->buffer.buffers2[buf] == NULL) - edit->buffer.buffers2[buf] = g_malloc0 (EDIT_BUF_SIZE); - if (mc_read (file, (char *) edit->buffer.buffers2[buf], EDIT_BUF_SIZE) < 0) - goto done; - } - - edit->total_lines = edit_count_lines (edit, 0, edit->last_byte); - - ret = TRUE; - - done: - if (!ret) + ret = edit_buffer_read_file (&edit->buffer, file, edit->last_byte); + if (ret) + edit->total_lines = edit_count_lines (edit, 0, edit->last_byte); + else { gchar *errmsg; @@ -196,6 +173,7 @@ edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath) edit_error_dialog (_("Error"), errmsg); g_free (errmsg); } + mc_close (file); return ret; } diff --git a/src/editor/editbuffer.c b/src/editor/editbuffer.c index f63187f53..81b1a0595 100644 --- a/src/editor/editbuffer.c +++ b/src/editor/editbuffer.c @@ -37,6 +37,8 @@ #include "lib/global.h" +#include "lib/vfs/vfs.h" + #include "editbuffer.h" /* --------------------------------------------------------------------------------------------- */ @@ -298,3 +300,45 @@ edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int *char #endif /* HAVE_CHARSET */ /* --------------------------------------------------------------------------------------------- */ +/** + * Load file into editor buffer + * + * @param buf pointer to editor buffer + * @param fd file descriptor + * @param size file size + * + * @return TRUE if file was readed successfully, FALSE otherwise + */ + +gboolean +edit_buffer_read_file (edit_buffer_t * buf, int fd, off_t size) +{ + off_t i; + off_t data_size; + + buf->curs2 = size; + i = buf->curs2 >> S_EDIT_BUF_SIZE; + + if (buf->buffers2[i] == NULL) + buf->buffers2[i] = g_malloc0 (EDIT_BUF_SIZE); + + /* fill last part of buffers2 */ + data_size = buf->curs2 & M_EDIT_BUF_SIZE; + if (mc_read (fd, (char *) buf->buffers2[i] + EDIT_BUF_SIZE - data_size, data_size) < 0) + return FALSE; + + /* fullfill other parts of buffers2 from end to begin */ + data_size = EDIT_BUF_SIZE; + for (--i; i >= 0; i--) + { + /* edit->buffers2[0] is already allocated */ + if (buf->buffers2[i] == NULL) + buf->buffers2[i] = g_malloc0 (data_size); + if (mc_read (fd, (char *) buf->buffers2[i], data_size) < 0) + return FALSE; + } + + return TRUE; +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/editbuffer.h b/src/editor/editbuffer.h index 9364dc8ae..d5e461d4e 100644 --- a/src/editor/editbuffer.h +++ b/src/editor/editbuffer.h @@ -58,6 +58,8 @@ int edit_buffer_get_utf (const edit_buffer_t * buf, off_t byte_index, int *char_ int edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int *char_width); #endif +gboolean edit_buffer_read_file (edit_buffer_t * buf, int fd, off_t size); + /*** inline functions ****************************************************************************/ static inline int