1
1

(edit_buffer_read_file): new editor buffer API.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Этот коммит содержится в:
Andrew Borodin 2013-02-15 14:10:34 +04:00
родитель cd9a56109d
Коммит fc8044e178
3 изменённых файлов: 54 добавлений и 30 удалений

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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;
}
/* --------------------------------------------------------------------------------------------- */

Просмотреть файл

@ -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