Skip to content

Commit c3c6731

Browse files
committed
improve file read
1 parent 4352427 commit c3c6731

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

core/src/xmake/io/file_read.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_re
5757
tb_hong_t size = tb_stream_size(stream);
5858
while (size < 0 || (offset = tb_stream_offset(stream)) < size)
5959
{
60-
tb_long_t real = tb_stream_peek(stream, &data, TB_STREAM_BLOCK_MAXN);
60+
tb_long_t real = tb_stream_peek(stream, &data, XM_IO_BLOCK_MAXN);
6161
if (real > 0)
6262
{
6363
tb_char_t const* e = tb_strnchr((tb_char_t const*)data, real, '\n');
@@ -184,12 +184,14 @@ static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file_t* file)
184184
if (!tb_buffer_init(&buf))
185185
xm_io_return_error(lua, "init buffer failed!");
186186

187+
tb_byte_t* data = tb_buffer_resize(&file->rcache, XM_IO_BLOCK_MAXN);
188+
tb_assert(data);
189+
187190
// read all
188-
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
189-
tb_stream_ref_t stream = file->u.file_ref;
191+
tb_stream_ref_t stream = file->u.file_ref;
190192
while (!tb_stream_beof(stream))
191193
{
192-
tb_long_t real = tb_stream_read(stream, data, sizeof(data));
194+
tb_long_t real = tb_stream_read(stream, data, XM_IO_BLOCK_MAXN);
193195
if (real > 0)
194196
tb_buffer_memncat(&buf, data, real);
195197
else if (!real)

core/src/xmake/io/prefix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
/* //////////////////////////////////////////////////////////////////////////////////////
3030
* macros
3131
*/
32+
#define XM_IO_BLOCK_MAXN (TB_STREAM_BLOCK_MAXN * 10)
33+
3234
#define xm_io_file_is_file(file) ((file)->type == XM_IO_FILE_TYPE_FILE)
3335
#define xm_io_file_is_std(file) ((file)->type != XM_IO_FILE_TYPE_FILE)
3436
#define xm_io_file_is_tty(file) (!!((file)->type & XM_IO_FILE_FLAG_TTY))

0 commit comments

Comments
 (0)