(edit_buffer_write_file): new editor buffer API.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-02-15 14:47:59 +04:00
parent e6ff98d239
commit 64760b56c5
3 changed files with 68 additions and 44 deletions

View File

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

View File

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

View File

@ -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)
if (!edit_buffer_write_file (&edit->buffer, fd))
filelen = -1;
if (filelen != edit->last_byte)
{
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))
{
filelen = -1;
}
else if (edit->buffer.curs2 != 0)
{
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++;
}
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;