summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2020-01-31 23:34:16 +0800
committerruki <[email protected]>2020-01-31 15:25:40 +0800
commit6dae2cd8c6b930aea276d2b5f83a444136489531 (patch)
treee80cc195613fe39e2157949d5bd90f4e7b389a62 /core/src
parent1278352ff02dcbb52c5a866c3b477581c1c4eebc (diff)
add core/pipe read and write
Diffstat (limited to 'core/src')
-rw-r--r--core/src/xmake/io/pipe_read.c80
-rw-r--r--core/src/xmake/io/pipe_wait.c61
-rw-r--r--core/src/xmake/io/pipe_write.c109
-rw-r--r--core/src/xmake/machine.c6
-rw-r--r--core/src/xmake/makefile3
5 files changed, 259 insertions, 0 deletions
diff --git a/core/src/xmake/io/pipe_read.c b/core/src/xmake/io/pipe_read.c
new file mode 100644
index 000000000..9e14a8c48
--- /dev/null
+++ b/core/src/xmake/io/pipe_read.c
@@ -0,0 +1,80 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-2020, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file pipe_read.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "pipe_read"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// real, data_or_errors = io.pipe_read(pipefile, size)
+tb_int_t xm_io_pipe_read(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // check pipe file
+ if (!lua_isuserdata(lua, 1))
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushliteral(lua, "invalid pipe file!");
+ return 2;
+ }
+
+ // get pipe file
+ tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1);
+ tb_check_return_val(pipefile, 0);
+
+ // get data
+ tb_byte_t* data = tb_null;
+ if (lua_isnumber(lua, 2))
+ data = (tb_byte_t*)(tb_size_t)(tb_long_t)lua_tonumber(lua, 2);
+ if (!data)
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushfstring(lua, "invalid data(%p)!", data);
+ return 2;
+ }
+
+ // get size
+ tb_long_t size = 0;
+ if (lua_isnumber(lua, 3)) size = (tb_long_t)lua_tonumber(lua, 3);
+ if (size <= 0)
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushfstring(lua, "invalid size(%ld)!", size);
+ return 2;
+ }
+
+ // read data
+ tb_long_t real = tb_pipe_file_read(pipefile, data, size);
+ lua_pushinteger(lua, (tb_int_t)real);
+ return 1;
+}
diff --git a/core/src/xmake/io/pipe_wait.c b/core/src/xmake/io/pipe_wait.c
new file mode 100644
index 000000000..ba4d68a7b
--- /dev/null
+++ b/core/src/xmake/io/pipe_wait.c
@@ -0,0 +1,61 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-2020, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file pipe_wait.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "pipe_wait"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * interfaces
+ */
+
+// io.pipe_wait(pipefile, events, timeout)
+tb_int_t xm_io_pipe_wait(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // is user data?
+ if (!lua_isuserdata(lua, 1))
+ return 0;
+
+ // get pipe file
+ tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1);
+ tb_check_return_val(pipefile, 0);
+
+ // get events
+ tb_size_t events = (tb_size_t)luaL_checknumber(lua, 2);
+
+ // get timeout
+ tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 3);
+
+ // wait pipe
+ lua_pushnumber(lua, (tb_int_t)tb_pipe_file_wait(pipefile, events, timeout));
+ return 1;
+}
+
diff --git a/core/src/xmake/io/pipe_write.c b/core/src/xmake/io/pipe_write.c
new file mode 100644
index 000000000..368230955
--- /dev/null
+++ b/core/src/xmake/io/pipe_write.c
@@ -0,0 +1,109 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-2020, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file pipe_write.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "pipe_write"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+// io.pipe_write(pipefile, data, start, last)
+tb_int_t xm_io_pipe_write(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // check pipe file
+ if (!lua_isuserdata(lua, 1))
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushliteral(lua, "invalid pipe file!");
+ return 2;
+ }
+
+ // get pipe file
+ tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)lua_touserdata(lua, 1);
+ tb_check_return_val(pipefile, 0);
+
+ // get data
+ tb_size_t size = 0;
+ tb_byte_t const* data = tb_null;
+ if (lua_istable(lua, 2))
+ {
+ // get data address
+ lua_pushstring(lua, "data");
+ lua_gettable(lua, 2);
+ data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, -1);
+ lua_pop(lua, 1);
+
+ // get data size
+ lua_pushstring(lua, "size");
+ lua_gettable(lua, 2);
+ size = (tb_size_t)lua_tonumber(lua, -1);
+ lua_pop(lua, 1);
+ }
+ else
+ {
+ size_t datasize = 0;
+ data = (tb_byte_t const*)luaL_checklstring(lua, 2, &datasize);
+ size = (tb_size_t)datasize;
+ }
+ if (!data || !size)
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushfstring(lua, "invalid data(%p) and size(%zu)!", data, size);
+ return 2;
+ }
+
+ // get start
+ tb_long_t start = 1;
+ if (lua_isnumber(lua, 3)) start = (tb_long_t)lua_tonumber(lua, 3);
+ if (start < 1 || start > size)
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushfstring(lua, "invalid start position(%ld)!", start);
+ return 2;
+ }
+
+ // get last
+ tb_long_t last = (tb_long_t)size;
+ if (lua_isnumber(lua, 4)) last = (tb_long_t)lua_tonumber(lua, 4);
+ if (last < start - 1 || last > size + start - 1)
+ {
+ lua_pushinteger(lua, -1);
+ lua_pushfstring(lua, "invalid last position(%ld)!", last);
+ return 2;
+ }
+
+ // write data
+ tb_long_t real = tb_pipe_file_write(pipefile, data + start - 1, last - start + 1);
+ lua_pushinteger(lua, (tb_int_t)real);
+ return 1;
+}
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 13be65354..d81044e0e 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -128,6 +128,9 @@ tb_int_t xm_io_socket_close(lua_State* lua);
tb_int_t xm_io_pipe_open(lua_State* lua);
tb_int_t xm_io_pipe_openpair(lua_State* lua);
tb_int_t xm_io_pipe_close(lua_State* lua);
+tb_int_t xm_io_pipe_read(lua_State* lua);
+tb_int_t xm_io_pipe_write(lua_State* lua);
+tb_int_t xm_io_pipe_wait(lua_State* lua);
// the io/poller functions
tb_int_t xm_io_poller_insert(lua_State* lua);
@@ -285,6 +288,9 @@ static luaL_Reg const g_io_functions[] =
, { "pipe_open", xm_io_pipe_open }
, { "pipe_openpair", xm_io_pipe_openpair }
, { "pipe_close", xm_io_pipe_close }
+, { "pipe_read", xm_io_pipe_read }
+, { "pipe_write", xm_io_pipe_write }
+, { "pipe_wait", xm_io_pipe_wait }
, { "poller_insert", xm_io_poller_insert }
, { "poller_modify", xm_io_poller_modify }
, { "poller_remove", xm_io_poller_remove }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index d0dce1f67..3952f4b07 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -83,6 +83,9 @@ xmake_C_FILES += \
io/pipe_open \
io/pipe_openpair \
io/pipe_close \
+ io/pipe_read \
+ io/pipe_write \
+ io/pipe_wait \
path/relative \
path/absolute \
path/translate \