summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-10-19 21:29:30 +0800
committerGitHub <[email protected]>2025-10-19 21:29:30 +0800
commitb703ec328f42d6f5c4ed5e7185b35bc13730b7b0 (patch)
treeb69613ec014766c8e7ec2734118b0d8f9b6ab653
parent435242713b1d1ed077c264228c0216dfa985685f (diff)
parent68cc4f4b0f271a01d8ea68291537529358332d20 (diff)
Merge pull request #6942 from xmake-io/io
improve file read
-rw-r--r--core/src/xmake/io/file_read.c14
-rw-r--r--core/src/xmake/io/prefix.h2
2 files changed, 12 insertions, 4 deletions
diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c
index 2647a056b..1c3af6dfe 100644
--- a/core/src/xmake/io/file_read.c
+++ b/core/src/xmake/io/file_read.c
@@ -57,7 +57,7 @@ static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_re
tb_hong_t size = tb_stream_size(stream);
while (size < 0 || (offset = tb_stream_offset(stream)) < size)
{
- tb_long_t real = tb_stream_peek(stream, &data, TB_STREAM_BLOCK_MAXN);
+ tb_long_t real = tb_stream_peek(stream, &data, XM_IO_BLOCK_MAXN);
if (real > 0)
{
tb_char_t const* e = tb_strnchr((tb_char_t const*)data, real, '\n');
@@ -184,12 +184,18 @@ static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file_t* file)
if (!tb_buffer_init(&buf))
xm_io_return_error(lua, "init buffer failed!");
+ tb_byte_t* data = tb_buffer_resize(&file->rcache, XM_IO_BLOCK_MAXN);
+ if (!data)
+ {
+ tb_buffer_exit(&buf);
+ xm_io_return_error(lua, "out of memory: failed to resize read cache");
+ }
+
// read all
- tb_byte_t data[TB_STREAM_BLOCK_MAXN];
- tb_stream_ref_t stream = file->u.file_ref;
+ tb_stream_ref_t stream = file->u.file_ref;
while (!tb_stream_beof(stream))
{
- tb_long_t real = tb_stream_read(stream, data, sizeof(data));
+ tb_long_t real = tb_stream_read(stream, data, XM_IO_BLOCK_MAXN);
if (real > 0)
tb_buffer_memncat(&buf, data, real);
else if (!real)
diff --git a/core/src/xmake/io/prefix.h b/core/src/xmake/io/prefix.h
index ec2cc7216..df4d09a17 100644
--- a/core/src/xmake/io/prefix.h
+++ b/core/src/xmake/io/prefix.h
@@ -29,6 +29,8 @@
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
+#define XM_IO_BLOCK_MAXN (TB_STREAM_BLOCK_MAXN * 10)
+
#define xm_io_file_is_file(file) ((file)->type == XM_IO_FILE_TYPE_FILE)
#define xm_io_file_is_std(file) ((file)->type != XM_IO_FILE_TYPE_FILE)
#define xm_io_file_is_tty(file) (!!((file)->type & XM_IO_FILE_FLAG_TTY))