summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-06 00:25:59 +0800
committerruki <[email protected]>2019-07-05 22:11:19 +0800
commit53949acfd132553f697adfe7e33394bec1d6f8dc (patch)
tree32956bbe06be1ffb5b8eee8a1cace46246c215db
parent074c15d4e08fb2f3e130eef8056de82b341f083e (diff)
improve line buffer for io
-rw-r--r--core/src/xmake/io/file.h1
-rw-r--r--core/src/xmake/io/file_close___gc.c3
-rw-r--r--core/src/xmake/io/file_read.c46
-rw-r--r--core/src/xmake/io/open.c3
-rw-r--r--core/src/xmake/io/std.c5
5 files changed, 28 insertions, 30 deletions
diff --git a/core/src/xmake/io/file.h b/core/src/xmake/io/file.h
index af39863a5..09a1d63f3 100644
--- a/core/src/xmake/io/file.h
+++ b/core/src/xmake/io/file.h
@@ -109,6 +109,7 @@ 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
} xm_io_file;
/* //////////////////////////////////////////////////////////////////////////////////////
diff --git a/core/src/xmake/io/file_close___gc.c b/core/src/xmake/io/file_close___gc.c
index b4373b374..283ab1c94 100644
--- a/core/src/xmake/io/file_close___gc.c
+++ b/core/src/xmake/io/file_close___gc.c
@@ -66,6 +66,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);
+
// free file path
if (file->path)
{
diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c
index 9cf183361..c429816d3 100644
--- a/core/src/xmake/io/file_read.c
+++ b/core/src/xmake/io/file_read.c
@@ -95,12 +95,11 @@ static tb_int_t xm_io_file_buffer_pushline(luaL_Buffer* buf, xm_io_file* file, t
keep_crlf = tb_true;
}
- // init line buffer
- tb_buffer_t line;
- if (!tb_buffer_init(&line)) return PL_FAIL;
+ // clear line buffer
+ tb_buffer_clear(&file->line);
// read line data
- tb_long_t size = xm_io_file_buffer_readline(file->file_ref, &line);
+ tb_long_t size = xm_io_file_buffer_readline(file->file_ref, &file->line);
// translate line data
tb_int_t result = PL_FAIL;
@@ -116,10 +115,10 @@ static tb_int_t xm_io_file_buffer_pushline(luaL_Buffer* buf, xm_io_file* file, t
}
// patch two '\0'
- tb_buffer_memncat(&line, (tb_byte_t const*)"\0\0", 2);
+ tb_buffer_memncat(&file->line, (tb_byte_t const*)"\0\0", 2);
// get line data
- data = (tb_char_t*)tb_buffer_data(&line);
+ data = (tb_char_t*)tb_buffer_data(&file->line);
tb_assert_and_check_break(data);
// no lf found
@@ -159,9 +158,6 @@ static tb_int_t xm_io_file_buffer_pushline(luaL_Buffer* buf, xm_io_file* file, t
if (data && size > 0 && (result == PL_FIN || result == PL_CONL))
luaL_addlstring(buf, data, size);
- // exit line buffer
- tb_buffer_exit(&line);
-
// return result
return result;
}
@@ -286,19 +282,14 @@ static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file* file, tb_char_t co
}
else
{
- tb_buffer_t buf;
- if (tb_buffer_init(&buf))
+ tb_byte_t* bufptr = tb_buffer_resize(&file->line, n + 1);
+ if (bufptr)
{
- tb_byte_t* bufptr = tb_buffer_resize(&buf, n + 1);
- if (bufptr)
+ if (tb_stream_bread(file->file_ref, bufptr, n))
{
- if (tb_stream_bread(file->file_ref, bufptr, n))
- {
- lua_pushlstring(lua, (tb_char_t const*)bufptr, n);
- ok = tb_true;
- }
+ lua_pushlstring(lua, (tb_char_t const*)bufptr, n);
+ ok = tb_true;
}
- tb_buffer_exit(&buf);
}
}
if (!ok) lua_pushnil(lua);
@@ -437,18 +428,13 @@ static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file* file, tb_char_
return 1;
}
- // io.read(n)
- tb_buffer_t buf;
- if (tb_buffer_init(&buf))
- {
- tb_byte_t* buf_ptr = tb_buffer_resize(&buf, (tb_size_t)n);
- tb_assert(buf_ptr);
+ // get line buffer
+ tb_byte_t* buf_ptr = tb_buffer_resize(&file->line, (tb_size_t)n);
+ tb_assert(buf_ptr);
- if (tb_stdfile_read(file->std_ref, buf_ptr, (tb_size_t)n))
- lua_pushlstring(lua, (tb_char_t const*)buf_ptr, (size_t)n);
- else lua_pushnil(lua);
- tb_buffer_exit(&buf);
- }
+ // io.read(n)
+ if (tb_stdfile_read(file->std_ref, buf_ptr, (tb_size_t)n))
+ lua_pushlstring(lua, (tb_char_t const*)buf_ptr, (size_t)n);
else lua_pushnil(lua);
return 1;
}
diff --git a/core/src/xmake/io/open.c b/core/src/xmake/io/open.c
index aa1512951..69a082ba2 100644
--- a/core/src/xmake/io/open.c
+++ b/core/src/xmake/io/open.c
@@ -292,6 +292,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
+ if (!tb_buffer_init(&xm_file->line)) return 0;
+
// save file path
tb_size_t pathlen = tb_strlen(path);
xm_file->path = tb_malloc_cstr(pathlen + 1);
diff --git a/core/src/xmake/io/std.c b/core/src/xmake/io/std.c
index 80ded866f..f27d61b2b 100644
--- a/core/src/xmake/io/std.c
+++ b/core/src/xmake/io/std.c
@@ -113,8 +113,13 @@ static tb_void_t xm_io_std_init(lua_State* lua, tb_size_t type)
file->std_ref = fp;
file->stream = tb_null;
file->fstream = tb_null;
+
+ // init name
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);
}
tb_int_t xm_io_std(lua_State* lua)