diff options
| author | ruki <[email protected]> | 2019-07-10 22:41:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-07-10 10:06:31 +0800 |
| commit | 306462119f4eac0d30befc50fe0aa5a49e7fd400 (patch) | |
| tree | 91eafd9b913bfd456e9bb186359152fb04226c31 /core/src | |
| parent | b4489894d1bd688c81e1a5d8509fb7d09e987430 (diff) | |
fix io.write cache
Diffstat (limited to 'core/src')
| m--------- | core/src/tbox/tbox | 0 | ||||
| -rw-r--r-- | core/src/xmake/io/file.h | 3 | ||||
| -rw-r--r-- | core/src/xmake/io/file_close___gc.c | 18 | ||||
| -rw-r--r-- | core/src/xmake/io/file_flush.c | 10 | ||||
| -rw-r--r-- | core/src/xmake/io/file_read.c | 12 | ||||
| -rw-r--r-- | core/src/xmake/io/file_write.c | 8 | ||||
| -rw-r--r-- | core/src/xmake/io/open.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/io/std.c | 5 |
8 files changed, 43 insertions, 19 deletions
diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox -Subproject 705f6854794e26f969511e50de61675bf692e48 +Subproject 4ae68758e4e325f5d5ac594790e728c1a0a8ccf diff --git a/core/src/xmake/io/file.h b/core/src/xmake/io/file.h index 3feefdda4..900ef00b6 100644 --- a/core/src/xmake/io/file.h +++ b/core/src/xmake/io/file.h @@ -109,7 +109,8 @@ typedef struct __xm_io_file tb_size_t encoding; // value of xm_io_file_encoding_e or tb_charset_type_e tb_char_t name[64]; tb_char_t const* path; - tb_buffer_t line; // the read line buffer + tb_buffer_t rcache; // the read line cache buffer + tb_buffer_t wcache; // the write line cache buffer } xm_io_file; /* ////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/src/xmake/io/file_close___gc.c b/core/src/xmake/io/file_close___gc.c index 283ab1c94..ebf74a63a 100644 --- a/core/src/xmake/io/file_close___gc.c +++ b/core/src/xmake/io/file_close___gc.c @@ -52,8 +52,19 @@ static tb_int_t xm_io_file_close_impl(lua_State* lua, tb_bool_t allow_closed_fil } if (xm_io_file_is_file(file)) { - // close file + // check tb_assert(file->file_ref); + + // write cached data first + tb_byte_t const* odata = tb_buffer_data(&file->wcache); + tb_size_t osize = tb_buffer_size(&file->wcache); + if (odata && osize) + { + if (!tb_stream_bwrit(file->file_ref, odata, osize)) return tb_false; + tb_buffer_clear(&file->wcache); + } + + // close file if (!tb_stream_clos(file->file_ref)) xm_io_file_return_error(lua, file, "failed to close file"); file->file_ref = tb_null; @@ -66,8 +77,9 @@ static tb_int_t xm_io_file_close_impl(lua_State* lua, tb_bool_t allow_closed_fil if (file->stream) tb_stream_exit(file->stream); file->stream = tb_null; - // exit the line buffer - tb_buffer_exit(&file->line); + // exit the line cache buffer + tb_buffer_exit(&file->rcache); + tb_buffer_exit(&file->wcache); // free file path if (file->path) diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c index 61597f951..8be57d5fc 100644 --- a/core/src/xmake/io/file_flush.c +++ b/core/src/xmake/io/file_flush.c @@ -42,7 +42,17 @@ static tb_bool_t xm_io_std_flush_impl(xm_io_file* file) static tb_bool_t xm_io_file_flush_impl(xm_io_file* file) { + // check tb_assert_and_check_return_val(xm_io_file_is_file(file) && !xm_io_file_is_closed(file), tb_false); + + // write cached data first + tb_byte_t const* odata = tb_buffer_data(&file->wcache); + tb_size_t osize = tb_buffer_size(&file->wcache); + if (odata && osize) + { + if (!tb_stream_bwrit(file->file_ref, odata, osize)) return tb_false; + tb_buffer_clear(&file->wcache); + } return tb_stream_sync(file->file_ref, tb_false); } diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index 6fe039e76..7cfae3a93 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -110,10 +110,10 @@ static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file* file } // clear line buffer - tb_buffer_clear(&file->line); + tb_buffer_clear(&file->rcache); // read line data - tb_long_t size = xm_io_file_buffer_readline(file->file_ref, &file->line); + tb_long_t size = xm_io_file_buffer_readline(file->file_ref, &file->rcache); // translate line data tb_int_t result = PL_FAIL; @@ -129,10 +129,10 @@ static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file* file } // patch two '\0' - tb_buffer_memncat(&file->line, (tb_byte_t const*)"\0\0", 2); + tb_buffer_memncat(&file->rcache, (tb_byte_t const*)"\0\0", 2); // get line data - data = (tb_char_t*)tb_buffer_data(&file->line); + data = (tb_char_t*)tb_buffer_data(&file->rcache); tb_assert_and_check_break(data); // no lf found @@ -308,7 +308,7 @@ static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file* file, tb_char_t co } else { - tb_byte_t* bufptr = tb_buffer_resize(&file->line, n + 1); + tb_byte_t* bufptr = tb_buffer_resize(&file->rcache, n + 1); if (bufptr) { if (tb_stream_bread(file->file_ref, bufptr, n)) @@ -466,7 +466,7 @@ static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file* file, tb_char_ } // get line buffer - tb_byte_t* buf_ptr = tb_buffer_resize(&file->line, (tb_size_t)n); + tb_byte_t* buf_ptr = tb_buffer_resize(&file->rcache, (tb_size_t)n); tb_assert(buf_ptr); // io.read(n) diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c index cf3ccca59..57c2043ea 100644 --- a/core/src/xmake/io/file_write.c +++ b/core/src/xmake/io/file_write.c @@ -48,12 +48,12 @@ static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file* file, tb_char_t con tb_assert(file && data && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); // write cached data first - tb_byte_t const* odata = tb_buffer_data(&file->line); - tb_size_t osize = tb_buffer_size(&file->line); + tb_byte_t const* odata = tb_buffer_data(&file->wcache); + tb_size_t osize = tb_buffer_size(&file->wcache); if (odata && osize) { if (!tb_stream_bwrit(file->file_ref, odata, osize)) return ; - tb_buffer_clear(&file->line); + tb_buffer_clear(&file->wcache); } // write data by lines @@ -81,7 +81,7 @@ static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file* file, tb_char_t con else { // cache the left data - tb_buffer_memncat(&file->line, (tb_byte_t const*)p, e - p); + tb_buffer_memncat(&file->wcache, (tb_byte_t const*)p, e - p); p = e; break; } diff --git a/core/src/xmake/io/open.c b/core/src/xmake/io/open.c index 436617e00..1507e9235 100644 --- a/core/src/xmake/io/open.c +++ b/core/src/xmake/io/open.c @@ -293,9 +293,9 @@ tb_int_t xm_io_open(lua_State* lua) xm_file->type = XM_IO_FILE_TYPE_FILE; xm_file->encoding = encoding; - // init the line buffer - tb_bool_t ok = tb_buffer_init(&xm_file->line); - tb_assert(ok); tb_used(&ok); + // init the read/write line cache buffer + tb_buffer_init(&xm_file->rcache); + tb_buffer_init(&xm_file->wcache); // save file path tb_size_t pathlen = tb_strlen(path); diff --git a/core/src/xmake/io/std.c b/core/src/xmake/io/std.c index f27d61b2b..4ee446d85 100644 --- a/core/src/xmake/io/std.c +++ b/core/src/xmake/io/std.c @@ -118,8 +118,9 @@ static tb_void_t xm_io_std_init(lua_State* lua, tb_size_t type) tb_char_t const* info = xm_io_file_is_tty(file) ? "" : " redirected"; tb_snprintf(file->name, tb_arrayn(file->name), "file: (%s%s)", name, info); - // init the line buffer - tb_buffer_init(&file->line); + // init the line cache buffer + tb_buffer_init(&file->rcache); + tb_buffer_init(&file->wcache); } tb_int_t xm_io_std(lua_State* lua) |
