mirror of
https://github.com/neovim/neovim.git
synced 2024-12-31 17:13:26 -07:00
os/fileio: Add msgpack_file_write function
This commit is contained in:
parent
94bd0f9915
commit
5ab9e9f617
@ -26,6 +26,7 @@
|
||||
#include "nvim/globals.h"
|
||||
#include "nvim/rbuffer.h"
|
||||
#include "nvim/macros.h"
|
||||
#include "nvim/message.h"
|
||||
|
||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||
# include "os/fileio.c.generated.h"
|
||||
@ -345,3 +346,23 @@ ptrdiff_t file_skip(FileDescriptor *const fp, const size_t size)
|
||||
|
||||
return (ptrdiff_t)read_bytes;
|
||||
}
|
||||
|
||||
/// Msgpack callback for writing to a file
|
||||
///
|
||||
/// @param data File to write to.
|
||||
/// @param[in] buf Data to write.
|
||||
/// @param[in] len Length of the data to write.
|
||||
///
|
||||
/// @return 0 in case of success, -1 in case of error.
|
||||
int msgpack_file_write(void *data, const char *buf, size_t len)
|
||||
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
|
||||
{
|
||||
assert(len < PTRDIFF_MAX);
|
||||
const ptrdiff_t written_bytes = file_write((FileDescriptor *)data, buf, len);
|
||||
if (written_bytes < 0) {
|
||||
emsgf(_("E5420: Failed to write to file: %s"),
|
||||
os_strerror((int)written_bytes));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -62,6 +62,10 @@ local function file_write(fp, buf)
|
||||
return m.file_write(fp, buf, #buf)
|
||||
end
|
||||
|
||||
local function msgpack_file_write(fp, buf)
|
||||
return m.msgpack_file_write(fp, buf, #buf)
|
||||
end
|
||||
|
||||
local function file_read(fp, size)
|
||||
local buf = nil
|
||||
if size == nil then
|
||||
@ -393,6 +397,18 @@ describe('file_write', function()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('msgpack_file_write', function()
|
||||
itp('can write the whole file at once', function()
|
||||
local err, fp = file_open(filec, m.kFileCreateOnly, 384)
|
||||
eq(0, err)
|
||||
eq(true, fp.wr)
|
||||
local wr = msgpack_file_write(fp, fcontents)
|
||||
eq(0, wr)
|
||||
eq(0, m.file_close(fp, false))
|
||||
eq(fcontents, io.open(filec):read('*a'))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('file_skip', function()
|
||||
itp('can skip 3 bytes', function()
|
||||
local err, fp = file_open(file1, 0, 384)
|
||||
|
Loading…
Reference in New Issue
Block a user