diff options
| author | ruki <[email protected]> | 2019-07-06 10:07:04 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-07-06 10:07:04 +0800 |
| commit | 7abde4ddc3f27134902bf795771bdd0878127c81 (patch) | |
| tree | edaef1e8b07ae02a11a63965000b03d5920856b2 | |
| parent | 2a916d6ff405fe4bb41c108bfcf79360cdf6e5a3 (diff) | |
optimize to read line
| -rw-r--r-- | core/src/tbox/makefile | 1 | ||||
| m--------- | core/src/tbox/tbox | 0 | ||||
| -rw-r--r-- | core/src/xmake/io/file_read.c | 48 |
3 files changed, 32 insertions, 17 deletions
diff --git a/core/src/tbox/makefile b/core/src/tbox/makefile index a8e57448e..9a98eaa9c 100644 --- a/core/src/tbox/makefile +++ b/core/src/tbox/makefile @@ -47,6 +47,7 @@ tbox_C_FILES += \ tbox/src/tbox/libc/string/memset \ tbox/src/tbox/libc/string/strcat \ tbox/src/tbox/libc/string/strchr \ + tbox/src/tbox/libc/string/strnchr \ tbox/src/tbox/libc/string/strcmp \ tbox/src/tbox/libc/string/strcpy \ tbox/src/tbox/libc/string/strdup \ diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox -Subproject f140928658fbec2ac2aba984dfd95cc6021c8b5 +Subproject 885b925cec48a385c83c76591c3d9e6af9484cd diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index c429816d3..4b771a25c 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -29,12 +29,6 @@ * includes */ #include "prefix.h" -#ifdef TB_CONFIG_OS_WINDOWS -# include "../winos/ansi.h" -# include <io.h> -#else -# include <unistd.h> -#endif #include "file.h" /* ////////////////////////////////////////////////////////////////////////////////////// @@ -58,23 +52,43 @@ static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_re tb_assert_and_check_return_val(stream && line, -1); // read line and reserve crlf - tb_char_t ch = 0; - tb_bool_t eof = tb_false; + tb_char_t ch = 0; + tb_bool_t eof = tb_false; + tb_byte_t* p = tb_null; while (!tb_stream_beof(stream)) { - // read char - if (!tb_stream_bread_s8(stream, (tb_sint8_t*)&ch)) + if (tb_stream_need(stream, &p, TB_STREAM_BLOCK_MAXN) && p) { - eof = tb_true; - break; + tb_char_t const* e = tb_strnchr((tb_char_t const*)p, TB_STREAM_BLOCK_MAXN, '\n'); + if (e) + { + if (!tb_stream_skip(stream, n)) return -1; + tb_size_t n = (tb_byte_t const*)e + 1 - p; + tb_buffer_memncat(line, p, n); + return tb_buffer_size(line); + } + else + { + if (!tb_stream_skip(stream, TB_STREAM_BLOCK_MAXN)) return -1; + tb_buffer_memncat(line, p, TB_STREAM_BLOCK_MAXN); + } } + else + { + // read char + if (!tb_stream_bread_s8(stream, (tb_sint8_t*)&ch)) + { + eof = tb_true; + break; + } - // append char to line - tb_buffer_memncat(line, (tb_byte_t const*)&ch, 1); + // append char to line + tb_buffer_memncat(line, (tb_byte_t const*)&ch, 1); - // is line? - if (ch == '\n') - return tb_buffer_size(line); + // is line? + if (ch == '\n') + return tb_buffer_size(line); + } } // ok? |
