shada: Put pointer to the close function into reader/writer structure

This commit is contained in:
ZyX 2015-07-25 12:56:47 +03:00
parent ebf3c86a55
commit 40bbaa757e

View File

@ -328,6 +328,10 @@ typedef struct {
struct sd_read_def; struct sd_read_def;
/// Function used to close files defined by ShaDaReadDef
typedef void (*ShaDaReadCloser)(struct sd_read_def *const sd_reader)
REAL_FATTR_NONNULL_ALL;
/// Function used to read ShaDa files /// Function used to read ShaDa files
typedef ptrdiff_t (*ShaDaFileReader)(struct sd_read_def *const sd_reader, typedef ptrdiff_t (*ShaDaFileReader)(struct sd_read_def *const sd_reader,
void *const dest, void *const dest,
@ -336,18 +340,23 @@ typedef ptrdiff_t (*ShaDaFileReader)(struct sd_read_def *const sd_reader,
/// Structure containing necessary pointers for reading ShaDa files /// Structure containing necessary pointers for reading ShaDa files
typedef struct sd_read_def { typedef struct sd_read_def {
ShaDaFileReader read; ///< Reader function. ShaDaFileReader read; ///< Reader function.
void *cookie; ///< Reader function last argument. ShaDaReadCloser close; ///< Close function.
bool eof; ///< True if reader reached end of file. void *cookie; ///< Reader function last argument.
char *error; ///< Error message in case of error. bool eof; ///< True if reader reached end of file.
uintmax_t fpos; ///< Current position (amount of bytes read since char *error; ///< Error message in case of error.
///< reader structure initialization). May overflow. uintmax_t fpos; ///< Current position (amount of bytes read since
vimconv_T sd_conv; ///< Structure used for converting encodings of some ///< reader structure initialization). May overflow.
///< items. vimconv_T sd_conv; ///< Structure used for converting encodings of some
///< items.
} ShaDaReadDef; } ShaDaReadDef;
struct sd_write_def; struct sd_write_def;
/// Function used to close files defined by ShaDaWriteDef
typedef void (*ShaDaWriteCloser)(struct sd_write_def *const sd_writer)
REAL_FATTR_NONNULL_ALL;
/// Function used to write ShaDa files /// Function used to write ShaDa files
typedef ptrdiff_t (*ShaDaFileWriter)(struct sd_write_def *const sd_writer, typedef ptrdiff_t (*ShaDaFileWriter)(struct sd_write_def *const sd_writer,
const void *const src, const void *const src,
@ -356,11 +365,12 @@ typedef ptrdiff_t (*ShaDaFileWriter)(struct sd_write_def *const sd_writer,
/// Structure containing necessary pointers for writing ShaDa files /// Structure containing necessary pointers for writing ShaDa files
typedef struct sd_write_def { typedef struct sd_write_def {
ShaDaFileWriter write; ///< Writer function. ShaDaFileWriter write; ///< Writer function.
void *cookie; ///< Writer function last argument. ShaDaWriteCloser close; ///< Close function.
char *error; ///< Error message in case of error. void *cookie; ///< Writer function last argument.
vimconv_T sd_conv; ///< Structure used for converting encodings of some char *error; ///< Error message in case of error.
///< items. vimconv_T sd_conv; ///< Structure used for converting encodings of some
///< items.
} ShaDaWriteDef; } ShaDaWriteDef;
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
@ -506,9 +516,9 @@ static ptrdiff_t read_file(ShaDaReadDef *const sd_reader, void *const dest,
{ {
size_t read_bytes = 0; size_t read_bytes = 0;
bool did_try_to_free = false; bool did_try_to_free = false;
const int fd = (int)(intptr_t) sd_reader->cookie;
while (read_bytes != size) { while (read_bytes != size) {
const ptrdiff_t cur_read_bytes = read((int)(intptr_t) sd_reader->cookie, const ptrdiff_t cur_read_bytes = read(fd, ((char *) dest) + read_bytes,
((char *) dest) + read_bytes,
size - read_bytes); size - read_bytes);
if (cur_read_bytes > 0) { if (cur_read_bytes > 0) {
read_bytes += (size_t) cur_read_bytes; read_bytes += (size_t) cur_read_bytes;
@ -559,9 +569,9 @@ static ptrdiff_t write_file(ShaDaWriteDef *const sd_writer,
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{ {
size_t written_bytes = 0; size_t written_bytes = 0;
const int fd = (int)(intptr_t) sd_writer->cookie;
while (written_bytes != size) { while (written_bytes != size) {
const ptrdiff_t cur_written_bytes = write((int)(intptr_t) sd_writer->cookie, const ptrdiff_t cur_written_bytes = write(fd, (char *) dest + written_bytes,
(char *) dest + written_bytes,
size - written_bytes); size - written_bytes);
if (cur_written_bytes > 0) { if (cur_written_bytes > 0) {
written_bytes += (size_t) cur_written_bytes; written_bytes += (size_t) cur_written_bytes;
@ -584,6 +594,20 @@ static ptrdiff_t write_file(ShaDaWriteDef *const sd_writer,
return (ptrdiff_t) written_bytes; return (ptrdiff_t) written_bytes;
} }
/// Wrapper for closing file descriptors opened for reading
static void close_sd_reader(ShaDaReadDef *const sd_reader)
FUNC_ATTR_NONNULL_ALL
{
close_file((int)(intptr_t) sd_reader->cookie);
}
/// Wrapper for closing file descriptors opened for writing
static void close_sd_writer(ShaDaWriteDef *const sd_writer)
FUNC_ATTR_NONNULL_ALL
{
close_file((int)(intptr_t) sd_writer->cookie);
}
/// Wrapper for opening file descriptors /// Wrapper for opening file descriptors
/// ///
/// All arguments are passed to os_open(). /// All arguments are passed to os_open().
@ -636,6 +660,7 @@ static int open_shada_file_for_reading(const char *const fname,
*sd_reader = (ShaDaReadDef) { *sd_reader = (ShaDaReadDef) {
.read = &read_file, .read = &read_file,
.close = &close_sd_reader,
.error = NULL, .error = NULL,
.eof = false, .eof = false,
.fpos = 0, .fpos = 0,
@ -752,8 +777,8 @@ int shada_read_file(const char *const file, const int flags)
} }
shada_read(&sd_reader, flags); shada_read(&sd_reader, flags);
sd_reader.close(&sd_reader);
close_file((int)(intptr_t) sd_reader.cookie);
return OK; return OK;
} }
@ -2566,6 +2591,7 @@ int shada_write_file(const char *const file, bool nomerge)
char *tempname = NULL; char *tempname = NULL;
ShaDaWriteDef sd_writer = (ShaDaWriteDef) { ShaDaWriteDef sd_writer = (ShaDaWriteDef) {
.write = &write_file, .write = &write_file,
.close = &close_sd_writer,
.error = NULL, .error = NULL,
}; };
ShaDaReadDef sd_reader; ShaDaReadDef sd_reader;
@ -2591,7 +2617,7 @@ int shada_write_file(const char *const file, bool nomerge)
? (old_info.stat.st_mode & 0020) ? (old_info.stat.st_mode & 0020)
: (old_info.stat.st_mode & 0002)))) { : (old_info.stat.st_mode & 0002)))) {
EMSG2(_("E137: ShaDa file is not writable: %s"), fname); EMSG2(_("E137: ShaDa file is not writable: %s"), fname);
close_file((int)(intptr_t) sd_reader.cookie); sd_reader.close(&sd_reader);
xfree(fname); xfree(fname);
return FAIL; return FAIL;
} }
@ -2676,11 +2702,10 @@ shada_write_file_nomerge: {}
convert_setup(&sd_writer.sd_conv, p_enc, "utf-8"); convert_setup(&sd_writer.sd_conv, p_enc, "utf-8");
shada_write(&sd_writer, (nomerge ? NULL : &sd_reader)); shada_write(&sd_writer, (nomerge ? NULL : &sd_reader));
sd_writer.close(&sd_writer);
close_file((int)(intptr_t) sd_writer.cookie);
if (!nomerge) { if (!nomerge) {
close_file((int)(intptr_t) sd_reader.cookie); sd_reader.close(&sd_reader);
if (vim_rename(tempname, fname) == -1) { if (vim_rename(tempname, fname) == -1) {
EMSG3(_("E886: Can't rename ShaDa file from %s to %s!"), EMSG3(_("E886: Can't rename ShaDa file from %s to %s!"),
tempname, fname); tempname, fname);