Unnamed: 0
int64 0
0
| repo_id
stringlengths 5
186
| file_path
stringlengths 15
223
| content
stringlengths 1
32.8M
⌀ |
---|---|---|---|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/readline/add_history.c | /*!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-present, TBOOX Open Source Group.
*
* @author TitanSnow
* @file add_history.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "add_history"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
#ifdef XM_CONFIG_API_HAVE_READLINE
// add_history wrapper
tb_int_t xm_readline_add_history(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get history
tb_char_t const* history = luaL_checkstring(lua, 1);
tb_check_return_val(history, 0);
// call add_history
add_history(history);
// ok
return 0;
}
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/readline/readline.c | /*!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-present, TBOOX Open Source Group.
*
* @author TitanSnow
* @file readline.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "readline"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#ifdef XM_CONFIG_API_HAVE_READLINE
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// readline wrapper
tb_int_t xm_readline_readline(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the prompt
tb_char_t const* prompt = luaL_optstring(lua, 1, tb_null);
// call readline
tb_char_t* line = readline(prompt);
if (line)
{
// return line
lua_pushstring(lua, line);
// free it
tb_free(line);
}
else lua_pushnil(lua);
// ok
return 1;
}
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/readline/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author TitanSnow
* @file prefix.h
*
*/
#ifndef XM_READLINE_PREFIX_H
#define XM_READLINE_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#ifdef XM_CONFIG_API_HAVE_READLINE
# include <stdio.h> // on some OS (like centos) required
# include <readline/readline.h>
# include <readline/history.h>
#endif
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/readline/clear_history.c | /*!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-present, TBOOX Open Source Group.
*
* @author TitanSnow
* @file clear_history.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "clear_history"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
#ifdef XM_CONFIG_API_HAVE_READLINE
// clear_history wrapper
tb_int_t xm_readline_clear_history(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
#ifdef TB_CONFIG_OS_MACOSX
// call clear_history (will crash on macOS)
for (tb_int_t i = history_length - 1; i >= 0; --i)
remove_history(i);
#else
clear_history();
#endif
// ok
return 0;
}
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/readline/history_list.c | /*!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-present, TBOOX Open Source Group.
*
* @author TitanSnow
* @file history_list.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "history_list"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
#ifdef XM_CONFIG_API_HAVE_READLINE
// history_list wrapper
tb_int_t xm_readline_history_list(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// history list
lua_newtable(lua);
#ifdef TB_CONFIG_OS_MACOSX
for (tb_int_t i = 1; i <= history_length; ++i)
{
lua_newtable(lua);
// field line
lua_pushstring(lua, "line");
lua_pushstring(lua, history_get(i)->line);
lua_settable(lua, -3);
// set back
lua_rawseti(lua, -2, i);
}
#else
tb_int_t i = 1;
for (HIST_ENTRY **p = history_list(); *p; ++p, ++i)
{
lua_newtable(lua);
// field line
lua_pushstring(lua, "line");
lua_pushstring(lua, (*p)->line);
lua_settable(lua, -3);
// set back
lua_rawseti(lua, -2, i);
}
#endif
// ok
return 1;
}
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/process/close.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file close.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "process.close"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// process.close(p)
tb_int_t xm_process_close(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the process
tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(process, 0);
// exit process
tb_process_exit(process);
// save result: ok
lua_pushboolean(lua, tb_true);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/process/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_PROCESS_PREFIX_H
#define XM_PROCESS_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/process/open.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "process.open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "../io/prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* p = process.open(command,
* {outpath = "", errpath = "", outfile = "",
* errfile = "", outpipe = "", errpipe = "",
* infile = "", inpipe = "", inpipe = "",
* envs = {"PATH=xxx", "XXX=yyy"}})
*/
tb_int_t xm_process_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get command
size_t command_size = 0;
tb_char_t const* command = luaL_checklstring(lua, 1, &command_size);
tb_check_return_val(command, 0);
// init attributes
tb_process_attr_t attr = {0};
// get option arguments
tb_size_t envn = 0;
tb_char_t const* envs[1024] = {0};
tb_char_t const* inpath = tb_null;
tb_char_t const* outpath = tb_null;
tb_char_t const* errpath = tb_null;
xm_io_file_t* infile = tb_null;
xm_io_file_t* outfile = tb_null;
xm_io_file_t* errfile = tb_null;
tb_pipe_file_ref_t inpipe = tb_null;
tb_pipe_file_ref_t outpipe = tb_null;
tb_pipe_file_ref_t errpipe = tb_null;
if (lua_istable(lua, 2))
{
// is detached?
lua_pushstring(lua, "detach");
lua_gettable(lua, 2);
if (lua_toboolean(lua, -1))
attr.flags |= TB_PROCESS_FLAG_DETACH;
lua_pop(lua, 1);
// get curdir
lua_pushstring(lua, "curdir");
lua_gettable(lua, 3);
attr.curdir = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get inpath
lua_pushstring(lua, "inpath");
lua_gettable(lua, 2);
inpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get outpath
lua_pushstring(lua, "outpath");
lua_gettable(lua, 2);
outpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get errpath
lua_pushstring(lua, "errpath");
lua_gettable(lua, 2);
errpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get infile
if (!inpath)
{
lua_pushstring(lua, "infile");
lua_gettable(lua, 3);
infile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get outfile
if (!outpath)
{
lua_pushstring(lua, "outfile");
lua_gettable(lua, 3);
outfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get errfile
if (!errpath)
{
lua_pushstring(lua, "errfile");
lua_gettable(lua, 3);
errfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get inpipe
if (!inpath && !infile)
{
lua_pushstring(lua, "inpipe");
lua_gettable(lua, 3);
inpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get outpipe
if (!outpath && !outfile)
{
lua_pushstring(lua, "outpipe");
lua_gettable(lua, 3);
outpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get errpipe
if (!errpath && !errfile)
{
lua_pushstring(lua, "errpipe");
lua_gettable(lua, 3);
errpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get environments
lua_pushstring(lua, "envs");
lua_gettable(lua, 2);
if (lua_istable(lua, -1))
{
// get environment variables count
tb_size_t count = (tb_size_t)lua_objlen(lua, -1);
// get all passed environment variables
tb_size_t i;
for (i = 0; i < count; i++)
{
// get envs[i]
lua_pushinteger(lua, i + 1);
lua_gettable(lua, -2);
// is string?
if (lua_isstring(lua, -1))
{
// add this environment value
if (envn + 1 < tb_arrayn(envs))
envs[envn++] = lua_tostring(lua, -1);
else
{
// error
lua_pushfstring(lua, "envs is too large(%d > %d) for process.openv", (tb_int_t)envn, tb_arrayn(envs) - 1);
lua_error(lua);
}
}
else
{
// error
lua_pushfstring(lua, "invalid envs[%d] type(%s) for process.openv", (tb_int_t)i, luaL_typename(lua, -1));
lua_error(lua);
}
// pop it
lua_pop(lua, 1);
}
}
lua_pop(lua, 1);
}
// redirect stdin?
if (inpath)
{
// redirect stdin to file
attr.in.path = inpath;
attr.inmode = TB_FILE_MODE_RO;
attr.intype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (infile && xm_io_file_is_file(infile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(infile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.in.file = rawfile;
attr.intype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}
else if (inpipe)
{
attr.in.pipe = inpipe;
attr.intype = TB_PROCESS_REDIRECT_TYPE_PIPE;
}
// redirect stdout?
if (outpath)
{
// redirect stdout to file
attr.out.path = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (outfile && xm_io_file_is_file(outfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(outfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.out.file = rawfile;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}
else if (outpipe)
{
attr.out.pipe = outpipe;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_PIPE;
}
// redirect stderr?
if (errpath)
{
// redirect stderr to file
attr.err.path = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (errfile && xm_io_file_is_file(errfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(errfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.err.file = rawfile;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}
else if (errpipe)
{
attr.err.pipe = errpipe;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_PIPE;
}
// set the new environments
if (envn > 0) attr.envp = envs;
// init process
tb_process_ref_t process = (tb_process_ref_t)tb_process_init_cmd(command, &attr);
if (process) xm_lua_pushpointer(lua, (tb_pointer_t)process);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/process/wait.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file wait.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "process.wait"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// ok, status = process.wait(proc, timeout)
tb_int_t xm_process_wait(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
{
// error
lua_pushfstring(lua, "invalid argument type(%s) for process.wait", luaL_typename(lua, 1));
lua_error(lua);
return 0;
}
// get the process
tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(process, 0);
// get the timeout
tb_long_t timeout = (tb_long_t)luaL_checkinteger(lua, 2);
// wait it
tb_long_t status = 0;
tb_long_t ok = tb_process_wait(process, &status, timeout);
// save result
lua_pushinteger(lua, ok);
lua_pushinteger(lua, status);
// ok
return 2;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/process/kill.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file kill.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "process.kill"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// process.kill(p)
tb_int_t xm_process_kill(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the process
tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(process, 0);
// kill process
tb_process_kill(process);
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/process/openv.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file openv.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "process.openv"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "../io/prefix.h"
#if defined(TB_CONFIG_OS_MACOSX) || defined(TB_CONFIG_OS_LINUX) || defined(TB_CONFIG_OS_BSD) || defined(TB_CONFIG_OS_HAIKU) || defined(TB_COMPILER_IS_MINGW)
# include <signal.h>
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* p = process.openv(shellname, argv,
* {outpath = "", errpath = "", outfile = "",
* errfile = "", outpipe = "", errpipe = "",
* infile = "", inpipe = "", inpipe = "",
* envs = {"PATH=xxx", "XXX=yyy"}})
*/
tb_int_t xm_process_openv(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check argv
if (!lua_istable(lua, 2))
{
// error
lua_pushfstring(lua, "invalid argv type(%s) for process.openv", luaL_typename(lua, 2));
lua_error(lua);
return 0;
}
// get shellname
tb_char_t const* shellname = lua_tostring(lua, 1);
tb_check_return_val(shellname, 0);
// get the arguments count
tb_long_t argn = (tb_long_t)lua_objlen(lua, 2);
tb_check_return_val(argn >= 0, 0);
// get arguments
tb_size_t argi = 0;
tb_char_t const** argv = tb_nalloc0_type(1 + argn + 1, tb_char_t const*);
tb_check_return_val(argv, 0);
// fill arguments
argv[0] = shellname;
for (argi = 0; argi < argn; argi++)
{
// get argv[i]
lua_pushinteger(lua, argi + 1);
lua_gettable(lua, 2);
// is string?
if (lua_isstring(lua, -1))
{
// pass this argument
argv[1 + argi] = lua_tostring(lua, -1);
}
// is path instance?
else if (lua_istable(lua, -1))
{
lua_pushstring(lua, "_STR");
lua_gettable(lua, -2);
argv[1 + argi] = lua_tostring(lua, -1);
lua_pop(lua, 1);
}
else
{
// error
lua_pushfstring(lua, "invalid argv[%d] type(%s) for process.openv", (tb_int_t)argi, luaL_typename(lua, -1));
lua_error(lua);
}
// pop it
lua_pop(lua, 1);
}
// init attributes
tb_process_attr_t attr = {0};
// get option arguments
tb_bool_t exclusive = tb_false;
tb_size_t envn = 0;
tb_char_t const* envs[1024] = {0};
tb_char_t const* inpath = tb_null;
tb_char_t const* outpath = tb_null;
tb_char_t const* errpath = tb_null;
xm_io_file_t* infile = tb_null;
xm_io_file_t* outfile = tb_null;
xm_io_file_t* errfile = tb_null;
tb_pipe_file_ref_t inpipe = tb_null;
tb_pipe_file_ref_t outpipe = tb_null;
tb_pipe_file_ref_t errpipe = tb_null;
if (lua_istable(lua, 3))
{
// is detached?
lua_pushstring(lua, "detach");
lua_gettable(lua, 3);
if (lua_toboolean(lua, -1))
attr.flags |= TB_PROCESS_FLAG_DETACH;
lua_pop(lua, 1);
// is exclusive?
lua_pushstring(lua, "exclusive");
lua_gettable(lua, 3);
if (lua_toboolean(lua, -1))
exclusive = tb_true;
lua_pop(lua, 1);
// get curdir
lua_pushstring(lua, "curdir");
lua_gettable(lua, 3);
attr.curdir = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get inpath
lua_pushstring(lua, "inpath");
lua_gettable(lua, 3);
inpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get outpath
lua_pushstring(lua, "outpath");
lua_gettable(lua, 3);
outpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get errpath
lua_pushstring(lua, "errpath");
lua_gettable(lua, 3);
errpath = lua_tostring(lua, -1);
lua_pop(lua, 1);
// get infile
if (!inpath)
{
lua_pushstring(lua, "infile");
lua_gettable(lua, 3);
infile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get outfile
if (!outpath)
{
lua_pushstring(lua, "outfile");
lua_gettable(lua, 3);
outfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get errfile
if (!errpath)
{
lua_pushstring(lua, "errfile");
lua_gettable(lua, 3);
errfile = (xm_io_file_t*)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get inpipe
if (!inpath && !infile)
{
lua_pushstring(lua, "inpipe");
lua_gettable(lua, 3);
inpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get outpipe
if (!outpath && !outfile)
{
lua_pushstring(lua, "outpipe");
lua_gettable(lua, 3);
outpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get errpipe
if (!errpath && !errfile)
{
lua_pushstring(lua, "errpipe");
lua_gettable(lua, 3);
errpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1);
lua_pop(lua, 1);
}
// get environments
lua_pushstring(lua, "envs");
lua_gettable(lua, 3);
if (lua_istable(lua, -1))
{
// get environment variables count
tb_size_t count = (tb_size_t)lua_objlen(lua, -1);
// get all passed environment variables
tb_size_t i;
for (i = 0; i < count; i++)
{
// get envs[i]
lua_pushinteger(lua, i + 1);
lua_gettable(lua, -2);
// is string?
if (lua_isstring(lua, -1))
{
// add this environment value
if (envn + 1 < tb_arrayn(envs))
envs[envn++] = lua_tostring(lua, -1);
else
{
// error
lua_pushfstring(lua, "envs is too large(%d > %d) for process.openv", (tb_int_t)envn, tb_arrayn(envs) - 1);
lua_error(lua);
}
}
else
{
// error
lua_pushfstring(lua, "invalid envs[%d] type(%s) for process.openv", (tb_int_t)i, luaL_typename(lua, -1));
lua_error(lua);
}
// pop it
lua_pop(lua, 1);
}
}
lua_pop(lua, 1);
}
// redirect stdin?
if (inpath)
{
// redirect stdin to file
attr.in.path = inpath;
attr.inmode = TB_FILE_MODE_RO;
attr.intype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (infile && xm_io_file_is_file(infile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(infile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.in.file = rawfile;
attr.intype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}
else if (inpipe)
{
attr.in.pipe = inpipe;
attr.intype = TB_PROCESS_REDIRECT_TYPE_PIPE;
}
// redirect stdout?
if (outpath)
{
// redirect stdout to file
attr.out.path = outpath;
attr.outmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (outfile && xm_io_file_is_file(outfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(outfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.out.file = rawfile;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}
else if (outpipe)
{
attr.out.pipe = outpipe;
attr.outtype = TB_PROCESS_REDIRECT_TYPE_PIPE;
}
// redirect stderr?
if (errpath)
{
// redirect stderr to file
attr.err.path = errpath;
attr.errmode = TB_FILE_MODE_RW | TB_FILE_MODE_TRUNC | TB_FILE_MODE_CREAT;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILEPATH;
}
else if (errfile && xm_io_file_is_file(errfile))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(errfile->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) && rawfile)
{
attr.err.file = rawfile;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_FILE;
}
}
else if (errpipe)
{
attr.err.pipe = errpipe;
attr.errtype = TB_PROCESS_REDIRECT_TYPE_PIPE;
}
// set the new environments
if (envn > 0) attr.envp = envs;
/* we need to ignore SIGINT and SIGQUIT if we enter exclusive mode
* @see https://github.com/xmake-io/xmake/discussions/2893
*/
#if defined(SIGINT)
if (exclusive) signal(SIGINT, SIG_IGN);
#endif
#if defined(SIGQUIT)
if (exclusive) signal(SIGQUIT, SIG_IGN);
#endif
// init process
tb_process_ref_t process = (tb_process_ref_t)tb_process_init(shellname, argv, &attr);
if (process) xm_lua_pushpointer(lua, (tb_pointer_t)process);
else lua_pushnil(lua);
// exit argv
if (argv) tb_free(argv);
argv = tb_null;
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/tty/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_TTY_PREFIX_H
#define XM_TTY_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/tty/term_mode.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file term_mode.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "term_mode"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#ifdef TB_CONFIG_OS_WINDOWS
# include <windows.h>
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* local oldmode = tty.term_mode(stdtype)
* local oldmode = tty.term_mode(stdtype, newmode)
*/
tb_int_t xm_tty_term_mode(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
#ifdef TB_CONFIG_OS_WINDOWS
// get std type, (stdin: 1, stdout: 2, stderr: 3)
tb_int_t stdtype = (tb_int_t)luaL_checkinteger(lua, 1);
// get and set terminal mode
DWORD mode = 0;
HANDLE console_handle = INVALID_HANDLE_VALUE;
switch (stdtype)
{
case 1: console_handle = GetStdHandle(STD_INPUT_HANDLE); break;
case 2: console_handle = GetStdHandle(STD_OUTPUT_HANDLE); break;
case 3: console_handle = GetStdHandle(STD_ERROR_HANDLE); break;
}
GetConsoleMode(console_handle, &mode);
if (lua_isnumber(lua, 2))
{
tb_int_t newmode = (tb_int_t)lua_tointeger(lua, 2);
if (console_handle != INVALID_HANDLE_VALUE)
SetConsoleMode(console_handle, (DWORD)newmode);
}
#else
tb_int_t mode = 0;
#endif
lua_pushinteger(lua, (tb_int_t)mode);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/free.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file free.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "free"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_free(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// do free
tb_pointer_t data = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
if (data) tb_free(data);
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/dataptr.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file dataptr.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "dataptr"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_dataptr(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
tb_pointer_t data = tb_null;
if (lua_isstring(lua, 1))
data = (tb_pointer_t)luaL_checkstring(lua, 1);
else if (lua_isnumber(lua, 1))
data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1);
else if (xm_lua_ispointer(lua, 1))
data = (tb_pointer_t)xm_lua_topointer(lua, 1);
else xm_libc_return_error(lua, "libc.dataptr(invalid data)!");
lua_pushinteger(lua, (lua_Integer)data);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/memset.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file memset.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "memset"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_memset(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// do memset
tb_pointer_t data = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
tb_char_t ch = (tb_char_t)lua_tointeger(lua, 2);
tb_int_t size = (tb_int_t)lua_tointeger(lua, 3);
if (data && size > 0)
tb_memset(data, ch, size);
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/malloc.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file malloc.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "malloc"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_malloc(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// do malloc
tb_pointer_t data = tb_null;
tb_long_t size = (tb_long_t)luaL_checkinteger(lua, 1);
if (size > 0) data = tb_malloc(size);
lua_pushinteger(lua, (lua_Integer)data);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/strndup.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file strndup.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "strndup"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_strndup(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// do strndup
tb_char_t const* s = tb_null;
if (lua_isnumber(lua, 1))
s = (tb_char_t const*)(tb_size_t)lua_tointeger(lua, 1);
else if (lua_isstring(lua, 2))
s = lua_tostring(lua, 2);
else xm_libc_return_error(lua, "libc.strndup(invalid args)!");
tb_int_t n = (tb_int_t)lua_tointeger(lua, 2);
if (s && n >= 0)
lua_pushlstring(lua, s, n);
else lua_pushliteral(lua, "");
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/byteof.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file byteof.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "byteof"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_byteof(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get data
tb_pointer_t data = tb_null;
if (lua_isnumber(lua, 1))
data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1);
else if (lua_isstring(lua, 1))
data = (tb_pointer_t)luaL_checkstring(lua, 1);
else xm_libc_return_error(lua, "libc.byteof(invalid data)!");
// get offset
tb_int_t offset = 0;
if (lua_isnumber(lua, 2))
offset = (tb_int_t)lua_tointeger(lua, 2);
else xm_libc_return_error(lua, "libc.byteof(invalid offset)!");
lua_pushinteger(lua, ((tb_byte_t const*)data)[offset]);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_LIBC_PREFIX_H
#define XM_LIBC_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// return libc error
#define xm_libc_return_error(lua, error) \
do \
{ \
lua_pushnil(lua); \
lua_pushliteral(lua, error); \
return 2; \
} while (0)
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/memmov.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file memmov.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "memmov"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_memmov(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// do memmov
tb_pointer_t dst = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
tb_pointer_t src = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 2);
tb_int_t size = (tb_int_t)lua_tointeger(lua, 3);
if (dst && src && size > 0)
tb_memmov(dst, src, size);
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/setbyte.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file setbyte.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "setbyte"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_setbyte(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get data
tb_pointer_t data = tb_null;
if (lua_isnumber(lua, 1))
data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1);
else if (lua_isstring(lua, 1))
data = (tb_pointer_t)luaL_checkstring(lua, 1);
else xm_libc_return_error(lua, "libc.setbyte(invalid data)!");
// get offset
tb_int_t offset = 0;
if (lua_isnumber(lua, 2))
offset = (tb_int_t)lua_tointeger(lua, 2);
else xm_libc_return_error(lua, "libc.setbyte(invalid offset)!");
// set byte
if (lua_isnumber(lua, 3))
((tb_byte_t*)data)[offset] = (tb_byte_t)lua_tointeger(lua, 3);
else xm_libc_return_error(lua, "libc.setbyte(invalid value)!");
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/libc/memcpy.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file memcpy.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "memcpy"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_libc_memcpy(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// do memcpy
tb_pointer_t dst = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 1);
tb_pointer_t src = (tb_pointer_t)(tb_size_t)luaL_checkinteger(lua, 2);
tb_int_t size = (tb_int_t)lua_tointeger(lua, 3);
if (dst && src && size > 0)
tb_memcpy(dst, src, size);
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/hash/md5.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file md5.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "md5"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_hash_md5(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is bytes? get data and size
if (xm_lua_isinteger(lua, 1) && xm_lua_isinteger(lua, 2))
{
tb_byte_t const* data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 1);
tb_size_t size = (tb_size_t)lua_tointeger(lua, 2);
if (!data || !size)
{
lua_pushnil(lua);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// compute md5
tb_byte_t buffer[16];
tb_md5_make(data, size, buffer, sizeof(buffer));
// make md5 string
tb_size_t i = 0;
tb_char_t s[256] = {0};
for (i = 0; i < 16; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
// save result
lua_pushstring(lua, s);
return 1;
}
// get the filename
tb_char_t const* filename = luaL_checkstring(lua, 1);
tb_check_return_val(filename, 0);
// load data from file
tb_bool_t ok = tb_false;
tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO);
if (stream)
{
// open stream
if (tb_stream_open(stream))
{
// init md5
tb_md5_t md5;
tb_md5_init(&md5, 0);
// read data and update md5
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
while (!tb_stream_beof(stream))
{
// read data
tb_long_t real = tb_stream_read(stream, data, sizeof(data));
// ok?
if (real > 0) tb_md5_spak(&md5, data, real);
// no data? continue it
else if (!real)
{
// wait
real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream));
tb_check_break(real > 0);
// has read?
tb_assert_and_check_break(real & TB_STREAM_WAIT_READ);
}
// failed or end?
else break;
}
// exit md5
tb_byte_t buffer[16];
tb_md5_exit(&md5, buffer, sizeof(buffer));
// make md5 string
tb_size_t i = 0;
tb_char_t s[256] = {0};
for (i = 0; i < 16; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
// save result
lua_pushstring(lua, s);
// ok
ok = tb_true;
}
// exit stream
tb_stream_exit(stream);
}
if (!ok) lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/hash/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_HASH_PREFIX_H
#define XM_HASH_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/hash/sha.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file sha.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "sha"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_hash_sha(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get mode
tb_size_t mode = (tb_size_t)lua_tointeger(lua, 1);
// is bytes? get data and size
if (xm_lua_isinteger(lua, 2) && xm_lua_isinteger(lua, 3))
{
tb_byte_t const* data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
tb_size_t size = (tb_size_t)lua_tointeger(lua, 3);
if (!data || !size)
{
lua_pushnil(lua);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// compute sha
tb_sha_t sha;
tb_byte_t buffer[32];
tb_sha_init(&sha, mode);
tb_sha_spak(&sha, data, size);
tb_sha_exit(&sha, buffer, sizeof(buffer));
// make sha string
tb_size_t i = 0;
tb_size_t n = sha.digest_len << 2;
tb_char_t s[256] = {0};
for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
// save result
lua_pushstring(lua, s);
return 1;
}
// get the filename
tb_char_t const* filename = luaL_checkstring(lua, 2);
tb_check_return_val(filename, 0);
// load data from file
tb_bool_t ok = tb_false;
tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO);
if (stream)
{
// open stream
if (tb_stream_open(stream))
{
// init sha
tb_sha_t sha;
tb_sha_init(&sha, mode);
// read data and update sha
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
while (!tb_stream_beof(stream))
{
// read data
tb_long_t real = tb_stream_read(stream, data, sizeof(data));
// ok?
if (real > 0) tb_sha_spak(&sha, data, real);
// no data? continue it
else if (!real)
{
// wait
real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream));
tb_check_break(real > 0);
// has read?
tb_assert_and_check_break(real & TB_STREAM_WAIT_READ);
}
// failed or end?
else break;
}
// exit sha
tb_byte_t buffer[32];
tb_sha_exit(&sha, buffer, sizeof(buffer));
// make sha string
tb_size_t i = 0;
tb_size_t n = sha.digest_len << 2;
tb_char_t s[256] = {0};
for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
// save result
lua_pushstring(lua, s);
// ok
ok = tb_true;
}
// exit stream
tb_stream_exit(stream);
}
if (!ok) lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/hash/uuid4.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file uuid4.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "uuid4"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_hash_uuid4(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the name
tb_char_t const* name = luaL_optstring(lua, 1, tb_null);
// make uuid, use version 4
tb_char_t uuid[37];
lua_pushstring(lua, tb_uuid4_make_cstr(uuid, name));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/hash/xxhash.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file xxhash.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "xxhash"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#define XXH_NAMESPACE XM_
#define XXH_STATIC_LINKING_ONLY
#define XXH_IMPLEMENTATION
#include "xxhash/xxhash.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_hash_xxhash(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get mode
tb_size_t mode = (tb_size_t)lua_tointeger(lua, 1);
if (mode != 64 && mode != 128)
{
lua_pushnil(lua);
lua_pushfstring(lua, "invalid mode(%d))!", (tb_int_t)mode);
return 2;
}
// is bytes? get data and size
if (xm_lua_isinteger(lua, 2) && xm_lua_isinteger(lua, 3))
{
tb_byte_t const* data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
tb_size_t size = (tb_size_t)lua_tointeger(lua, 3);
if (!data || !size)
{
lua_pushnil(lua);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// compuate hash
tb_byte_t const* buffer;
XXH64_hash_t value64;
XXH128_hash_t value128;
if (mode == 64)
{
value64 = XM_XXH3_64bits(data, size);
buffer = (tb_byte_t const*)&value64;
}
else if (mode == 128)
{
value128 = XM_XXH3_128bits(data, size);
buffer = (tb_byte_t const*)&value128;
}
// make xxhash string
tb_size_t i = 0;
tb_size_t n = mode >> 3;
tb_char_t s[256] = {0};
for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
// save result
lua_pushstring(lua, s);
return 1;
}
// get the filename
tb_char_t const* filename = luaL_checkstring(lua, 2);
tb_check_return_val(filename, 0);
// load data from file
tb_bool_t ok = tb_false;
tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO);
if (stream)
{
// open stream
XXH3_state_t* state = XM_XXH3_createState();
if (tb_stream_open(stream) && state)
{
// reset xxhash
if (mode == 64) XM_XXH3_64bits_reset(state);
else XM_XXH3_128bits_reset(state);
// read data and update xxhash
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
while (!tb_stream_beof(stream))
{
// read data
tb_long_t real = tb_stream_read(stream, data, sizeof(data));
// ok?
if (real > 0)
{
if (mode == 64) XM_XXH3_64bits_update(state, data, real);
else XM_XXH3_128bits_update(state, data, real);
}
// no data? continue it
else if (!real)
{
// wait
real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream));
tb_check_break(real > 0);
// has read?
tb_assert_and_check_break(real & TB_STREAM_WAIT_READ);
}
// failed or end?
else break;
}
// compuate hash
tb_byte_t const* buffer;
XXH64_hash_t value64;
XXH128_hash_t value128;
if (mode == 64)
{
value64 = XM_XXH3_64bits_digest(state);
buffer = (tb_byte_t const*)&value64;
}
else
{
value128 = XM_XXH3_128bits_digest(state);
buffer = (tb_byte_t const*)&value128;
}
// make xxhash string
tb_size_t i = 0;
tb_size_t n = mode >> 3;
tb_char_t s[256] = {0};
for (i = 0; i < n; ++i) tb_snprintf(s + (i << 1), 3, "%02x", buffer[i]);
// save result
lua_pushstring(lua, s);
ok = tb_true;
}
// exit stream
tb_stream_exit(stream);
// exit xxhash
if (state) XM_XXH3_freeState(state);
}
if (!ok) lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_STRING_PREFIX_H
#define XM_STRING_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/trim.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu
* @file trim.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "string_trim"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_void_t xm_string_trim_space(tb_char_t const** psstr, tb_char_t const** pestr, tb_int_t mode)
{
// check
tb_assert(psstr && pestr && *psstr && *pestr);
tb_char_t const* p = *psstr;
tb_char_t const* e = *pestr;
// trim left?
if (mode <= 0)
while (p < e && tb_isspace(*p))
p++;
// trim right
if (mode >= 0)
{
e--;
while (e >= p && tb_isspace(*e))
e--;
e++;
}
// save trimed string
*psstr = p;
*pestr = e;
}
static tb_char_t const* xm_string_ltrim(tb_char_t const* sstr, tb_char_t const* estr, tb_char_t const* ctrim, size_t ntrim)
{
// check
tb_assert(sstr && estr && ctrim);
tb_char_t const* p = sstr;
while (p < estr && tb_strnchr(ctrim, ntrim, *p))
p++;
return p;
}
static tb_char_t const* xm_string_rtrim(tb_char_t const* sstr, tb_char_t const* estr, tb_char_t const* ctrim, size_t ntrim)
{
// check
tb_assert(sstr && estr && ctrim);
tb_char_t const* p = estr - 1;
while (p >= sstr && tb_strnchr(ctrim, ntrim, *p))
p--;
return p + 1;
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* trim string
*
* @param str the string
* @param trimchars the chars to trim
* @param trimtype 0 to trim left and right, -1 to trim left, 1 to trim right
*
* @code
* local result = string.trim(str, "\r\n \v\t", 0)
* local result = string.trim(str, "(", -1)
* @endcode
*/
tb_int_t xm_string_trim(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
size_t lstr, ltrim;
tb_char_t const* sstr = luaL_checklstring(lua, 1, &lstr);
tb_char_t const* estr = sstr + lstr;
tb_char_t const* trimchars = luaL_optlstring(lua, 2, "", <rim);
tb_int_t const trimtype = (tb_int_t)luaL_optinteger(lua, 3, 0);
do
{
tb_assert_and_check_break(sstr && trimchars);
tb_check_break(lstr != 0);
tb_char_t const* const rsstr = sstr;
tb_char_t const* const restr = estr;
if (ltrim == 0)
xm_string_trim_space(&sstr, &estr, trimtype);
else
{
// trim chars
if (trimtype <= 0) sstr = xm_string_ltrim(sstr, estr, trimchars, ltrim);
if (trimtype >= 0) estr = xm_string_rtrim(sstr, estr, trimchars, ltrim);
}
// no trimed chars
tb_check_break(sstr != rsstr || estr != restr);
// ok
lua_pushlstring(lua, sstr, estr - sstr);
return 1;
} while (0);
// return orignal value
lua_settop(lua, 1);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/endswith.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file endswith.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "endswith"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_string_endswith(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the string and suffix
size_t string_size = 0;
size_t suffix_size = 0;
tb_char_t const* string = luaL_checklstring(lua, 1, &string_size);
tb_char_t const* suffix = luaL_checklstring(lua, 2, &suffix_size);
tb_check_return_val(string && suffix, 0);
// string:endswith(suffix)?
lua_pushboolean(lua, string_size >= suffix_size && !tb_strcmp(string + string_size - suffix_size, suffix));
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/lastof.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file lastof.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "string_lastof"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_void_t xm_string_lastof_str(lua_State* lua, tb_char_t const* cstr, tb_size_t nstr, tb_char_t const* csubstr, tb_size_t nsubstr)
{
// find it
tb_char_t const* curr = tb_null;
tb_char_t const* next = cstr;
do
{
next = tb_strstr(next, csubstr); // faster than tb_strnstr()
if (next)
{
curr = next;
next += nsubstr;
}
} while (!next);
// found?
if (curr) lua_pushinteger(lua, curr - cstr + 1);
else lua_pushnil(lua);
}
static tb_void_t xm_string_lastof_chr(lua_State* lua, tb_char_t const* cstr, tb_size_t nstr, tb_char_t ch)
{
tb_char_t const* pos = tb_strrchr(cstr, ch); // faster than tb_strnrchr()
if (pos) lua_pushinteger(lua, pos - cstr + 1);
else lua_pushnil(lua);
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* lastof string (only support plain text)
*
* @param str the string
* @param substr the substring
*/
tb_int_t xm_string_lastof(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get string
size_t nstr = 0;
tb_char_t const* cstr = luaL_checklstring(lua, 1, &nstr);
// get substring
size_t nsubstr = 0;
tb_char_t const* csubstr = luaL_checklstring(lua, 2, &nsubstr);
// lastof it
lua_newtable(lua);
if (nsubstr == 1) xm_string_lastof_chr(lua, cstr, (tb_size_t)nstr, csubstr[0]);
else xm_string_lastof_str(lua, cstr, (tb_size_t)nstr, csubstr, nsubstr);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/startswith.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file startswith.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "startswith"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_string_startswith(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the string and prefix
size_t prefix_size = 0;
tb_char_t const* string = luaL_checkstring(lua, 1);
tb_char_t const* prefix = luaL_checklstring(lua, 2, &prefix_size);
tb_check_return_val(string && prefix, 0);
// string:startswith(prefix)?
lua_pushboolean(lua, !tb_strncmp(string, prefix, (tb_size_t)prefix_size));
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/split.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file split.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "string_split"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_void_t xm_string_split_str(lua_State* lua, tb_char_t const* cstr, tb_size_t nstr, tb_char_t const* cdls, tb_size_t ndls, tb_bool_t strict, tb_int_t limit)
{
tb_int_t num = 0;
tb_char_t const* end = cstr + nstr;
tb_char_t const* pos = tb_strstr(cstr, cdls); // faster than tb_strnstr()
while (pos && pos < end)
{
if (pos > cstr || strict)
{
if (limit > 0 && num + 1 >= limit)
break;
lua_pushlstring(lua, cstr, pos - cstr);
lua_rawseti(lua, -2, ++num);
}
cstr = pos + ndls;
pos = tb_strstr(cstr, cdls);
}
if (cstr < end)
{
lua_pushlstring(lua, cstr, end - cstr);
lua_rawseti(lua, -2, ++num);
}
else if (strict && (limit < 0 || num < limit) && cstr == end)
{
lua_pushliteral(lua, "");
lua_rawseti(lua, -2, ++num);
}
}
static tb_void_t xm_string_split_chr(lua_State* lua, tb_char_t const* cstr, tb_size_t nstr, tb_char_t ch, tb_bool_t strict, tb_int_t limit)
{
tb_int_t num = 0;
tb_char_t const* end = cstr + nstr;
tb_char_t const* pos = tb_strchr(cstr, ch); // faster than tb_strnchr()
while (pos && pos < end)
{
if (pos > cstr || strict)
{
if (limit > 0 && num + 1 >= limit)
break;
lua_pushlstring(lua, cstr, pos - cstr);
lua_rawseti(lua, -2, ++num);
}
cstr = pos + 1;
pos = tb_strchr(cstr, ch);
}
if (cstr < end)
{
lua_pushlstring(lua, cstr, end - cstr);
lua_rawseti(lua, -2, ++num);
}
else if (strict && (limit < 0 || num < limit) && cstr == end)
{
lua_pushliteral(lua, "");
lua_rawseti(lua, -2, ++num);
}
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* split string (only support plain text)
*
* @param str the string
* @param delimiter the delimiter
* @param strict is strict?
* @param limit the limit count
*/
tb_int_t xm_string_split(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get string
size_t nstr = 0;
tb_char_t const* cstr = luaL_checklstring(lua, 1, &nstr);
// get delimiter
size_t ndls = 0;
tb_char_t const* cdls = luaL_checklstring(lua, 2, &ndls);
// is strict?
tb_bool_t const strict = (tb_bool_t)lua_toboolean(lua, 3);
// get limit count
tb_int_t const limit = (tb_int_t)luaL_optinteger(lua, 4, -1);
// split it
lua_newtable(lua);
if (ndls == 1) xm_string_split_chr(lua, cstr, (tb_size_t)nstr, cdls[0], strict, limit);
else xm_string_split_str(lua, cstr, (tb_size_t)nstr, cdls, ndls, strict, limit);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/string/convert.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file convert.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "convert"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* globals
*/
// the charset entry type
typedef struct __xm_charset_entry_t
{
// the charset type
tb_size_t type;
// the charset name
tb_char_t const* name;
}xm_charset_entry_t, *xm_charset_entry_ref_t;
// the charsets, @note: type & name is sorted
static xm_charset_entry_t g_charsets[] =
{
{TB_CHARSET_TYPE_ANSI, "ansi" }
, {TB_CHARSET_TYPE_ASCII, "ascii" }
, {TB_CHARSET_TYPE_GB2312, "gb2312" }
, {TB_CHARSET_TYPE_GBK, "gbk" }
, {TB_CHARSET_TYPE_ISO8859, "iso8859" }
, {TB_CHARSET_TYPE_UCS2 | TB_CHARSET_TYPE_NE, "ucs2" }
, {TB_CHARSET_TYPE_UCS4 | TB_CHARSET_TYPE_NE, "ucs4" }
, {TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_NE, "utf16" }
, {TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_BE, "utf16be" }
, {TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE, "utf16le" }
, {TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_NE, "utf32" }
, {TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_BE, "utf32be" }
, {TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_LE, "utf32le" }
, {TB_CHARSET_TYPE_UTF8, "utf8" }
};
/* //////////////////////////////////////////////////////////////////////////////////////
* finder
*/
static tb_long_t xm_string_charset_comp_by_name(tb_iterator_ref_t iterator, tb_cpointer_t item, tb_cpointer_t name)
{
return tb_stricmp(((xm_charset_entry_ref_t)item)->name, (tb_char_t const*)name);
}
static xm_charset_entry_ref_t xm_string_charset_find_by_name(tb_char_t const* name)
{
// make iterator
tb_array_iterator_t array_iterator;
tb_iterator_ref_t iterator = tb_array_iterator_init_mem(&array_iterator, g_charsets, tb_arrayn(g_charsets), sizeof(xm_charset_entry_t));
tb_assert_and_check_return_val(iterator, tb_null);
// find it by the binary search
tb_size_t itor = tb_binary_find_all_if(iterator, xm_string_charset_comp_by_name, name);
if (itor != tb_iterator_tail(iterator))
return (xm_charset_entry_ref_t)tb_iterator_item(iterator, itor);
else return tb_null;
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* convert string
*
* @param str the string
* @param ftype the from-charset type, e.g. ascii, gb2312, gbk, ios8859, ucs2, ucs4, utf8, utf16, utf32
* @param ttype the to-charset type
*
* @code
* local result = string.convert(str, "gbk", "utf8")
* local result = string.convert(str, "utf8", "gb2312")
* @endcode
*/
tb_int_t xm_string_convert(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the string and charset types
size_t src_size = 0;
tb_char_t const* src_cstr = luaL_checklstring(lua, 1, &src_size);
tb_char_t const* ftype_cstr = luaL_checkstring(lua, 2);
tb_char_t const* ttype_cstr = luaL_checkstring(lua, 3);
tb_check_return_val(src_cstr && ftype_cstr && ttype_cstr, 0);
// find charsets
xm_charset_entry_ref_t fcharset = xm_string_charset_find_by_name(ftype_cstr);
xm_charset_entry_ref_t tcharset = xm_string_charset_find_by_name(ttype_cstr);
luaL_argcheck(lua, fcharset, 2, "charset not found");
luaL_argcheck(lua, tcharset, 3, "charset not found");
tb_check_return_val(fcharset && tcharset, 0);
// empty string?
if (!src_size) lua_pushstring(lua, "");
else
{
// convert string
tb_long_t dst_size = 0;
tb_size_t dst_maxn = (tb_size_t)src_size << 2;
tb_byte_t* dst_data = tb_malloc_bytes(dst_maxn);
if (dst_data && dst_maxn && (dst_size = tb_charset_conv_data(fcharset->type, tcharset->type, (tb_byte_t const*)src_cstr, (tb_size_t)src_size, dst_data, dst_maxn)) >= 0 && dst_size < dst_maxn)
{
lua_pushlstring(lua, (tb_char_t const *)dst_data, dst_size);
}
else lua_pushnil(lua);
tb_free(dst_data);
}
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_peeraddr.c | /*!A cross-platform build utility based on Lua
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this sock 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-present, TBOOX Open Source Group.
*
* @author ruki
* @sock socket_peeraddr.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_peeraddr"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// socket to fd
#define xm_io_sock2fd(sock) (lua_Number)tb_sock2fd(sock)
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* io.socket_peeraddr(sock)
*/
tb_int_t xm_io_socket_peeraddr(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
xm_io_return_error(lua, "get peer address for invalid sock!");
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get peer address
tb_ipaddr_t addr;
tb_char_t data[256];
tb_char_t const* cstr = tb_null;
if (tb_socket_peer(sock, &addr) && (cstr = tb_ipaddr_cstr(&addr, data, sizeof(data))))
lua_pushstring(lua, cstr);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller_insert.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller_insert.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller_insert"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.poller_insert(obj:otype(), obj:cdata(), events)
tb_int_t xm_io_poller_insert(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 2))
{
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "invalid poller object!");
return 2;
}
// get otype
tb_uint8_t otype = (tb_uint8_t)luaL_checknumber(lua, 1);
// get cdata
tb_char_t const* cdata_str = tb_null;
tb_pointer_t cdata = (tb_pointer_t)xm_lua_topointer2(lua, 2, &cdata_str);
tb_check_return_val(cdata, 0);
// get events
tb_size_t events = (tb_size_t)luaL_checknumber(lua, 3);
// insert events to poller
tb_poller_object_t object;
object.type = otype;
object.ref.ptr = cdata;
lua_pushboolean(lua, tb_poller_insert(xm_io_poller(), &object, events, cdata_str));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/filelock_trylock.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file filelock_trylock.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "filelock_trylock"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* try to lock file
*
* exclusive lock: io.filelock_trylock("/xxxx/filelock")
* shared lock: io.filelock_trylock("/xxxx/filelock", {shared = true})
*/
tb_int_t xm_io_filelock_trylock(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get option argument
tb_bool_t is_shared = tb_false;
if (lua_istable(lua, 2))
{
// is shared lock?
lua_pushstring(lua, "shared");
lua_gettable(lua, 2);
is_shared = (tb_bool_t)lua_toboolean(lua, -1);
lua_pop(lua, 1);
}
// check lock?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(lock, 0);
// try to lock it
tb_bool_t ok = tb_filelock_enter_try(lock, is_shared? TB_FILELOCK_MODE_SH : TB_FILELOCK_MODE_EX);
lua_pushboolean(lua, ok);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_recv.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_recv.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_recv"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// real, data_or_errors = io.socket_recv(sock, size)
tb_int_t xm_io_socket_recv(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get data
tb_byte_t* data = tb_null;
if (xm_lua_isinteger(lua, 2))
data = (tb_byte_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (!data)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p)!", data);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// get size
tb_long_t size = 0;
if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3);
if (size <= 0)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid size(%d)!", (tb_int_t)size);
return 2;
}
// recv data
tb_long_t real = tb_socket_recv(sock, data, size);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller_remove.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller_remove.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller_remove"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.poller_remove(obj:otype(), obj)
tb_int_t xm_io_poller_remove(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 2))
{
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "invalid poller object!");
return 2;
}
// get otype
tb_uint8_t otype = (tb_uint8_t)luaL_checknumber(lua, 1);
// get cdata
tb_pointer_t cdata = (tb_pointer_t)xm_lua_topointer(lua, 2);
tb_check_return_val(cdata, 0);
// remove events from poller
tb_poller_object_t object;
object.type = otype;
object.ref.ptr = cdata;
lua_pushboolean(lua, tb_poller_remove(xm_io_poller(), &object));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_connect.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_connect.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_connect"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.socket_connect(sock, addr, port, family)
tb_int_t xm_io_socket_connect(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushnumber(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get address
tb_char_t const* address = lua_tostring(lua, 2);
tb_assert_and_check_return_val(address, 0);
// get family
tb_uint8_t family = (tb_uint8_t)luaL_checknumber(lua, 4);
// init address
tb_ipaddr_t addr;
if (family == TB_IPADDR_FAMILY_UNIX)
{
tb_bool_t is_abstract = (tb_bool_t)lua_toboolean(lua, 3);
tb_ipaddr_unix_set_cstr(&addr, address, is_abstract);
}
else
{
tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 3);
tb_ipaddr_set(&addr, address, port, family);
}
// connect socket
lua_pushnumber(lua, (tb_int_t)tb_socket_connect(sock, &addr));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller_wait.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller_wait.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller_wait"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* globals
*/
// we need only one global lua state/poller in main thread, so it is thread-safe.
static lua_State* g_lua = tb_null;
static tb_int_t g_events_count = 0;
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref_t object, tb_long_t events, tb_cpointer_t priv)
{
// check
tb_assert_and_check_return(g_lua);
// save object and events
lua_newtable(g_lua);
lua_pushinteger(g_lua, (tb_int_t)object->type);
lua_rawseti(g_lua, -2, 1);
if (priv) lua_pushstring(g_lua, (tb_char_t const*)priv);
else lua_pushlightuserdata(g_lua, object->ref.ptr);
lua_rawseti(g_lua, -2, 2);
if (object->type == TB_POLLER_OBJECT_FWATCHER)
{
lua_newtable(g_lua);
tb_fwatcher_event_t* event = (tb_fwatcher_event_t*)events;
if (event)
{
lua_pushstring(g_lua, "path");
lua_pushstring(g_lua, event->filepath);
lua_settable(g_lua, -3);
lua_pushstring(g_lua, "type");
lua_pushinteger(g_lua, event->event);
lua_settable(g_lua, -3);
}
}
else lua_pushinteger(g_lua, (tb_int_t)events);
lua_rawseti(g_lua, -2, 3);
lua_rawseti(g_lua, -2, ++g_events_count);
}
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// local events, count = io.poller_wait(timeout)
tb_int_t xm_io_poller_wait(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get timeout
tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 1);
// pass lua and count to the events callback
g_lua = lua;
g_events_count = 0;
// wait it
lua_newtable(lua);
tb_long_t count = tb_poller_wait(xm_io_poller(), xm_io_poller_event, timeout);
if (count > 0)
{
lua_pushinteger(lua, (tb_int_t)count);
return 2;
}
else if (!count)
{
// timeout
lua_pop(lua, 1);
lua_pushnil(lua);
lua_pushinteger(lua, 0);
return 2;
}
// failed
lua_pop(lua, 1);
lua_pushnil(lua);
lua_pushinteger(lua, -1);
return 2;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/stdfile.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file stdfile.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "stdfile"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#ifdef TB_CONFIG_OS_WINDOWS
# include <io.h>
# include "iscygpty.c"
#else
# include <stdio.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// the singleton type of stdfile
#define XM_IO_STDFILE_STDIN (TB_SINGLETON_TYPE_USER + 1)
#define XM_IO_STDFILE_STDOUT (TB_SINGLETON_TYPE_USER + 2)
#define XM_IO_STDFILE_STDERR (TB_SINGLETON_TYPE_USER + 3)
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_size_t xm_io_stdfile_isatty(tb_size_t type)
{
tb_bool_t answer = tb_false;
#if defined(TB_CONFIG_OS_WINDOWS)
DWORD mode;
HANDLE console_handle = tb_null;
switch (type)
{
case XM_IO_FILE_TYPE_STDIN: console_handle = GetStdHandle(STD_INPUT_HANDLE); break;
case XM_IO_FILE_TYPE_STDOUT: console_handle = GetStdHandle(STD_OUTPUT_HANDLE); break;
case XM_IO_FILE_TYPE_STDERR: console_handle = GetStdHandle(STD_ERROR_HANDLE); break;
}
answer = GetConsoleMode(console_handle, &mode);
/* we cannot call is_cygpty for stdin, because it will cause io.readable is always true
* https://github.com/xmake-io/xmake/issues/2504#issuecomment-1170130756
*/
if (!answer && type != XM_IO_FILE_TYPE_STDIN)
answer = is_cygpty(console_handle);
#else
switch (type)
{
case XM_IO_FILE_TYPE_STDIN: answer = isatty(fileno(stdin)); break;
case XM_IO_FILE_TYPE_STDOUT: answer = isatty(fileno(stdout)); break;
case XM_IO_FILE_TYPE_STDERR: answer = isatty(fileno(stderr)); break;
}
#endif
if (answer) type |= XM_IO_FILE_FLAG_TTY;
return type;
}
// @see https://github.com/xmake-io/xmake/issues/2580
static tb_void_t xm_io_stdfile_init_buffer(tb_size_t type)
{
#if !defined(TB_CONFIG_OS_WINDOWS)
struct stat stats;
tb_int_t size = BUFSIZ;
if (fstat(fileno(stdout), &stats) != -1)
size = stats.st_blksize;
setvbuf(stdout, tb_null, _IOLBF, size);
#endif
}
static xm_io_file_t* xm_io_stdfile_new(lua_State* lua, tb_size_t type)
{
// init stdfile
tb_stdfile_ref_t fp = tb_null;
switch (type)
{
case XM_IO_FILE_TYPE_STDIN:
fp = tb_stdfile_input();
break;
case XM_IO_FILE_TYPE_STDOUT:
fp = tb_stdfile_output();
break;
case XM_IO_FILE_TYPE_STDERR:
fp = tb_stdfile_error();
break;
}
// new file
xm_io_file_t* file = (xm_io_file_t*)lua_newuserdata(lua, sizeof(xm_io_file_t));
tb_assert_and_check_return_val(file, tb_null);
// init file
file->u.std_ref = fp;
file->stream = tb_null;
file->fstream = tb_null;
file->type = xm_io_stdfile_isatty(type);
file->encoding = TB_CHARSET_TYPE_UTF8;
// init stdio buffer
xm_io_stdfile_init_buffer(type);
// init the read/write line cache buffer
tb_buffer_init(&file->rcache);
tb_buffer_init(&file->wcache);
return file;
}
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.stdfile(stdin: 1, stdout: 2, stderr: 3)
tb_int_t xm_io_stdfile(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get std type
tb_long_t type = (tb_long_t)lua_tointeger(lua, 1);
/* push a new stdfile
*
* @note we need to ensure that it is a singleton in the external lua script, and will only be created once, e.g. io.stdin, io.stdout, io.stderr
*/
xm_io_file_t* file = xm_io_stdfile_new(lua, type);
if (file) return 1;
else xm_io_return_error(lua, "invalid stdfile type!");
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_close.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_close.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_close"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.socket_close(sock)
tb_int_t xm_io_socket_close(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// exit socket
lua_pushboolean(lua, tb_socket_exit(sock));
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller.h
*
*/
#ifndef XM_IO_POLLER_H
#define XM_IO_POLLER_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
/* get io poller
*
* @return the io poller
*/
tb_poller_ref_t xm_io_poller(tb_noarg_t);
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_accept.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_accept.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_accept"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// local sock = io.socket_accept(sock)
tb_int_t xm_io_socket_accept(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// accept socket
tb_socket_ref_t client = tb_socket_accept(sock, tb_null);
if (client) xm_lua_pushpointer(lua, (tb_pointer_t)client);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_connect.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file pipe_connect.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "pipe_connect"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.pipe_connect(pipefile)
tb_int_t xm_io_pipe_connect(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check pipe
if (!xm_lua_ispointer(lua, 1))
{
lua_pushnumber(lua, -1);
lua_pushliteral(lua, "invalid pipe!");
return 2;
}
// get pipe file
tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(pipefile, 0);
// connect pipe
lua_pushnumber(lua, (tb_int_t)tb_pipe_file_connect(pipefile));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_open.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// num of bytes read to guess encoding
#define CHECK_SIZE (1024)
// is utf-8 tail character
#define IS_UTF8_TAIL(c) (c >= 0x80 && c < 0xc0)
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_size_t xm_io_file_detect_charset(tb_byte_t const** data_ptr, tb_long_t size)
{
// check
tb_assert(data_ptr && *data_ptr);
tb_byte_t const* data = *data_ptr;
tb_size_t charset = XM_IO_FILE_ENCODING_BINARY;
do
{
// is luajit bitcode? open as binary
if (size >= 3 && data[0] == 27 && data[1] == 'L' && data[2] == 'J')
break;
// utf-8 with bom
if (size >= 3 && data[0] == 239 && data[1] == 187 && data[2] == 191)
{
charset = TB_CHARSET_TYPE_UTF8;
data += 3; // skip bom
break;
}
if (size >= 2)
{
// utf16be
if (data[0] == 254 && data[1] == 255)
{
charset = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_BE;
data += 2; // skip bom
break;
}
// utf16le
else if (data[0] == 255 && data[1] == 254)
{
charset = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE;
data += 2; // skip bom
break;
}
}
tb_sint16_t utf16be_conf = 0;
tb_sint16_t utf16le_conf = 0;
tb_sint16_t utf8_conf = 0;
tb_sint16_t ascii_conf = 0;
tb_sint16_t zero_count = 0;
for (tb_long_t i = 0; i < (size - 4) && i < CHECK_SIZE; i++)
{
if (data[i] == 0) zero_count++;
if (data[i] < 0x80)
ascii_conf++;
else
ascii_conf = TB_MINS16;
if (i % 2 == 0)
{
if (data[i] == 0) utf16be_conf++;
if (data[i + 1] == 0) utf16le_conf++;
}
if (IS_UTF8_TAIL(data[i]))
;
else if (data[i] < 0x80)
utf8_conf++;
else if (data[i] >= 0xc0 && data[i] < 0xe0 && IS_UTF8_TAIL(data[i + 1]))
utf8_conf++;
else if (data[i] >= 0xe0 && data[i] < 0xf0 && IS_UTF8_TAIL(data[i + 1]) && IS_UTF8_TAIL(data[i + 2]))
utf8_conf++;
else if (data[i] >= 0xf0 && data[i] < 0xf8 && IS_UTF8_TAIL(data[i + 1]) && IS_UTF8_TAIL(data[i + 2]) && IS_UTF8_TAIL(data[i + 3]))
utf8_conf++;
else
utf8_conf = TB_MINS16;
}
if (ascii_conf > 0 && zero_count <= 1)
{
charset = TB_CHARSET_TYPE_UTF8;
break;
}
if (utf8_conf > 0 && zero_count <= 1)
{
charset = TB_CHARSET_TYPE_UTF8;
break;
}
if (utf16be_conf > 0 && utf16be_conf > utf16le_conf)
{
charset = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_BE;
break;
}
if (utf16le_conf > 0 && utf16le_conf >= utf16be_conf)
{
charset = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE;
break;
}
if (utf8_conf > 0)
{
charset = TB_CHARSET_TYPE_UTF8;
break;
}
#ifdef TB_CONFIG_OS_WINDOWS
charset = TB_CHARSET_TYPE_ANSI;
#endif
} while (0);
*data_ptr = data;
return charset;
}
static tb_size_t xm_io_file_detect_encoding(tb_stream_ref_t stream, tb_long_t* pbomoff)
{
// check
tb_assert_and_check_return_val(stream && pbomoff, XM_IO_FILE_ENCODING_BINARY);
// detect encoding
tb_byte_t* data = tb_null;
tb_size_t encoding = XM_IO_FILE_ENCODING_BINARY;
tb_long_t size = tb_stream_peek(stream, &data, CHECK_SIZE);
if (size > 0)
{
tb_byte_t const* p = data;
encoding = xm_io_file_detect_charset(&p, size);
*pbomoff = p - data;
}
return encoding;
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.file_open(path, modestr)
tb_int_t xm_io_file_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get file path and mode
tb_char_t const* path = luaL_checkstring(lua, 1);
tb_char_t const* modestr = luaL_optstring(lua, 2, "r");
tb_assert_and_check_return_val(path && modestr, 0);
// get file mode value
tb_size_t mode;
switch (modestr[0])
{
case 'w': mode = TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC; break;
case 'a': mode = TB_FILE_MODE_RW | TB_FILE_MODE_APPEND | TB_FILE_MODE_CREAT; break;
case 'r': default: mode = TB_FILE_MODE_RO; break;
}
// get file encoding
tb_long_t bomoff = 0;
tb_stream_ref_t stream = tb_null;
tb_bool_t update = !!tb_strchr(modestr, '+');
tb_size_t encoding = XM_IO_FILE_ENCODING_UNKNOWN;
if (modestr[1] == 'b' || (update && modestr[2] == 'b'))
encoding = XM_IO_FILE_ENCODING_BINARY;
else if (tb_strstr(modestr, "utf8") || tb_strstr(modestr, "utf-8"))
encoding = TB_CHARSET_TYPE_UTF8;
else if (tb_strstr(modestr, "utf16le") || tb_strstr(modestr, "utf-16le"))
encoding = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE;
else if (tb_strstr(modestr, "utf16be") || tb_strstr(modestr, "utf-16be"))
encoding = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_BE;
else if (tb_strstr(modestr, "utf16") || tb_strstr(modestr, "utf-16"))
encoding = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_NE;
else if (tb_strstr(modestr, "ansi"))
encoding = TB_CHARSET_TYPE_ANSI;
else if (tb_strstr(modestr, "gbk"))
encoding = TB_CHARSET_TYPE_GBK;
else if (tb_strstr(modestr, "gb2312"))
encoding = TB_CHARSET_TYPE_GB2312;
else if (tb_strstr(modestr, "iso8859"))
encoding = TB_CHARSET_TYPE_ISO8859;
else if (modestr[0] == 'w' || modestr[0] == 'a') // set to utf-8 if not specified for the writing mode
encoding = TB_CHARSET_TYPE_UTF8;
else if (modestr[0] == 'r') // detect encoding if not specified for the reading mode
{
stream = tb_stream_init_from_file(path, mode);
if (stream && tb_stream_open(stream))
encoding = xm_io_file_detect_encoding(stream, &bomoff);
else
{
if (stream) tb_stream_exit(stream);
xm_io_return_error(lua, "file not found!");
}
}
else xm_io_return_error(lua, "invalid open mode!");
tb_assert_and_check_return_val(encoding != XM_IO_FILE_ENCODING_UNKNOWN, 0);
// write data with utf bom? e.g. utf8bom, utf16lebom, utf16bom
tb_bool_t utfbom = tb_false;
if (tb_strstr(modestr, "bom"))
utfbom = tb_true;
// open file
tb_bool_t open_ok = tb_false;
tb_stream_ref_t file_ref = tb_null;
tb_stream_ref_t fstream = tb_null;
do
{
// init stream from file
stream = stream? stream : tb_stream_init_from_file(path, mode);
tb_assert_and_check_break(stream);
// is transcode?
tb_bool_t is_transcode = encoding != TB_CHARSET_TYPE_UTF8 && encoding != XM_IO_FILE_ENCODING_BINARY;
if (is_transcode)
{
if (modestr[0] == 'r')
fstream = tb_stream_init_filter_from_charset(stream, encoding, TB_CHARSET_TYPE_UTF8);
else
fstream = tb_stream_init_filter_from_charset(stream, TB_CHARSET_TYPE_UTF8, encoding);
tb_assert_and_check_break(fstream);
// use fstream as file
file_ref = fstream;
}
else file_ref = stream;
// open file stream
if (!tb_stream_open(file_ref)) break;
// skip bom characters if exists
if (bomoff > 0 && !tb_stream_seek(stream, bomoff)) break;
// ok
open_ok = tb_true;
} while (0);
// open failed?
if (!open_ok)
{
// exit stream
if (stream) tb_stream_exit(stream);
stream = tb_null;
// exit charset stream filter
if (fstream) tb_stream_exit(fstream);
fstream = tb_null;
// return errors
xm_io_return_error(lua, "failed to open file!");
}
// make file
xm_io_file_t* file = (xm_io_file_t*)lua_newuserdata(lua, sizeof(xm_io_file_t));
tb_assert_and_check_return_val(file, 0);
// init file
file->u.file_ref = file_ref;
file->stream = stream;
file->fstream = fstream;
file->mode = mode;
file->type = XM_IO_FILE_TYPE_FILE;
file->encoding = encoding;
file->utfbom = utfbom;
// init the read/write line cache buffer
tb_buffer_init(&file->rcache);
tb_buffer_init(&file->wcache);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// the singleton type of poller
#define XM_IO_POLLER (TB_SINGLETON_TYPE_USER + 4)
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_handle_t xm_io_poller_instance_init(tb_cpointer_t* ppriv)
{
// init poller
tb_poller_ref_t poller = tb_poller_init(tb_null);
tb_assert_and_check_return_val(poller, tb_null);
// attach poller to the current thread
tb_poller_attach(poller);
return (tb_handle_t)poller;
}
static tb_void_t xm_io_poller_instance_exit(tb_handle_t poller, tb_cpointer_t priv)
{
if (poller) tb_poller_exit((tb_poller_ref_t)poller);
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_poller_ref_t xm_io_poller()
{
return (tb_poller_ref_t)tb_singleton_instance(XM_IO_POLLER, xm_io_poller_instance_init, xm_io_poller_instance_exit, tb_null, tb_null);
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/filelock_unlock.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file filelock_unlock.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "filelock_unlock"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.filelock_unlock(lock)
tb_int_t xm_io_filelock_unlock(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check lock?
if (!xm_lua_topointer(lua, 1))
return 0;
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(lock, 0);
// unlock it
tb_bool_t ok = tb_filelock_leave(lock);
lua_pushboolean(lua, ok);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_IO_PREFIX_H
#define XM_IO_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
#define xm_io_file_is_file(file) ((file)->type == XM_IO_FILE_TYPE_FILE)
#define xm_io_file_is_std(file) ((file)->type != XM_IO_FILE_TYPE_FILE)
#define xm_io_file_is_tty(file) (!!((file)->type & XM_IO_FILE_FLAG_TTY))
// return io error
#define xm_io_return_error(lua, error) \
do \
{ \
lua_pushnil(lua); \
lua_pushliteral(lua, error); \
return 2; \
} while (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/
typedef enum __xm_io_file_type_e
{
XM_IO_FILE_TYPE_FILE = 0 //!< disk file
, XM_IO_FILE_TYPE_STDIN = 1
, XM_IO_FILE_TYPE_STDOUT = 2
, XM_IO_FILE_TYPE_STDERR = 3
, XM_IO_FILE_FLAG_TTY = 0x10 //!< mark tty std stream
} xm_io_file_type_e;
/* use negetive numbers for this enum, its a extension for tb_charset_type_e
* before adding new values, make sure they have not conflicts with values in tb_charset_type_e
*/
typedef enum __xm_io_file_encoding_e
{
XM_IO_FILE_ENCODING_UNKNOWN = -1
, XM_IO_FILE_ENCODING_BINARY = -2
} xm_io_file_encoding_e;
// the file type
typedef struct __xm_io_file_t
{
union
{
/* the normal file for XM_IO_FILE_TYPE_FILE
*
* direct: file_ref -> stream -> file
* transcode: file_ref -> fstream -> stream -> file
*/
tb_stream_ref_t file_ref;
// the standard io file
tb_stdfile_ref_t std_ref;
}u;
tb_stream_ref_t stream; // the file stream for XM_IO_FILE_TYPE_FILE
tb_stream_ref_t fstream; // the file charset stream filter
tb_size_t mode; // tb_file_mode_t
tb_size_t type; // xm_io_file_type_e
tb_size_t encoding; // value of xm_io_file_encoding_e or tb_charset_type_e
tb_bool_t utfbom; // write utf-bom for utf encoding?
tb_buffer_t rcache; // the read line cache buffer
tb_buffer_t wcache; // the write line cache buffer
} xm_io_file_t;
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/filelock_lock.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file filelock_lock.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "filelock_lock"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* lock file
*
* exclusive lock: io.filelock_lock(lock, "/xxxx/filelock")
* shared lock: io.filelock_lock(lock, "/xxxx/filelock", {shared = true})
*/
tb_int_t xm_io_filelock_lock(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get option argument
tb_bool_t is_shared = tb_false;
if (lua_istable(lua, 2))
{
// is shared lock?
lua_pushstring(lua, "shared");
lua_gettable(lua, 2);
is_shared = (tb_bool_t)lua_toboolean(lua, -1);
lua_pop(lua, 1);
}
// check lock?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(lock, 0);
// lock it
tb_bool_t ok = tb_filelock_enter(lock, is_shared? TB_FILELOCK_MODE_SH : TB_FILELOCK_MODE_EX);
lua_pushboolean(lua, ok);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_write.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_write.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_write"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_void_t xm_io_file_write_file_utfbom(xm_io_file_t* file)
{
// check
tb_assert(file && xm_io_file_is_file(file) && file->u.file_ref);
// write bom
switch (file->encoding)
{
case TB_CHARSET_TYPE_UTF8:
{
static tb_byte_t bom[] = {0xef, 0xbb, 0xbf};
tb_stream_bwrit(file->u.file_ref, bom, sizeof(bom));
}
break;
case TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE:
{
static tb_byte_t bom[] = {0xff, 0xfe};
tb_stream_bwrit(file->u.file_ref, bom, sizeof(bom));
}
break;
case TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_BE:
{
static tb_byte_t bom[] = {0xfe, 0xff};
tb_stream_bwrit(file->u.file_ref, bom, sizeof(bom));
}
break;
default:
break;
}
}
static tb_void_t xm_io_file_write_file_directly(xm_io_file_t* file, tb_byte_t const* data, tb_size_t size)
{
tb_assert(file && data && xm_io_file_is_file(file) && file->u.file_ref);
tb_stream_bwrit(file->u.file_ref, data, size);
}
static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file_t* file, tb_byte_t const* data, tb_size_t size)
{
// check
tb_assert(file && data && xm_io_file_is_file(file) && file->u.file_ref);
#ifdef TB_CONFIG_OS_WINDOWS
// 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->u.file_ref, odata, osize)) return ;
tb_buffer_clear(&file->wcache);
}
// write data by lines
tb_char_t const* p = (tb_char_t const*)data;
tb_char_t const* e = p + size;
tb_char_t const* lf = tb_null;
while (p < e)
{
lf = tb_strnchr(p, e - p, '\n');
if (lf)
{
if (lf > p && lf[-1] == '\r')
{
if (!tb_stream_bwrit(file->u.file_ref, (tb_byte_t const*)p, lf + 1 - p)) break;
}
else
{
if (lf > p && !tb_stream_bwrit(file->u.file_ref, (tb_byte_t const*)p, lf - p)) break;
if (!tb_stream_bwrit(file->u.file_ref, (tb_byte_t const*)"\r\n", 2)) break;
}
// next line
p = lf + 1;
}
else
{
// cache the left data
tb_buffer_memncat(&file->wcache, (tb_byte_t const*)p, e - p);
p = e;
break;
}
}
#else
return xm_io_file_write_file_directly(file, data, size);
#endif
}
static tb_void_t xm_io_file_write_std(xm_io_file_t* file, tb_byte_t const* data, tb_size_t size)
{
// check
tb_assert(file && data && xm_io_file_is_std(file));
// check type
tb_size_t type = (file->type & ~XM_IO_FILE_FLAG_TTY);
tb_check_return(type != XM_IO_FILE_TYPE_STDIN);
// write data to stdout/stderr
tb_stdfile_writ(file->u.std_ref, data, size);
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.file_write(file, ...)
tb_int_t xm_io_file_write(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "write(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// write file data
tb_int_t narg = lua_gettop(lua);
if (narg > 1)
{
tb_bool_t is_binary = file->encoding == XM_IO_FILE_ENCODING_BINARY;
for (tb_int_t i = 2; i <= narg; i++)
{
// get data
size_t datasize = 0;
tb_byte_t const* data = tb_null;
if (lua_isstring(lua, i))
data = (tb_byte_t const*)luaL_checklstring(lua, i, &datasize);
else if (lua_istable(lua, i))
{
// get bytes data
lua_pushstring(lua, "data");
lua_gettable(lua, i);
if (xm_lua_isinteger(lua, -1))
data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, -1);
lua_pop(lua, 1);
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
lua_pushstring(lua, "size");
lua_gettable(lua, i);
if (xm_lua_isinteger(lua, -1))
datasize = (tb_size_t)lua_tointeger(lua, -1);
lua_pop(lua, 1);
// mark as binary data
is_binary = tb_true;
}
tb_check_continue(datasize);
tb_assert_and_check_break(data);
// write data to std or file
if (xm_io_file_is_std(file))
xm_io_file_write_std(file, data, (tb_size_t)datasize);
else if (is_binary)
xm_io_file_write_file_directly(file, data, (tb_size_t)datasize);
else
{
// write utf bom first?
if (file->utfbom)
{
xm_io_file_write_file_utfbom(file);
file->utfbom = tb_false;
}
xm_io_file_write_file_transcrlf(file, data, (tb_size_t)datasize);
}
}
}
lua_settop(lua, 1);
lua_pushboolean(lua, tb_true);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/filelock_open.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file filelock_open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "filelock_open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/*
* io.filelock_open(path)
*/
tb_int_t xm_io_filelock_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get file path
tb_char_t const* path = luaL_checkstring(lua, 1);
tb_assert_and_check_return_val(path, 0);
// init file lock
tb_long_t tryn = 2;
tb_filelock_ref_t lock = tb_null;
while (!lock && tryn-- > 0)
lock = tb_filelock_init_from_path(path, tb_file_info(path, tb_null)? TB_FILE_MODE_RW : TB_FILE_MODE_RW | TB_FILE_MODE_CREAT);
if (lock) xm_lua_pushpointer(lua, (tb_pointer_t)lock);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_wait.c | /*!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-present, 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);
// check pipe?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get pipe file
tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(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;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller_support.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller_support.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller_support"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.poller_support(events)
tb_int_t xm_io_poller_support(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get events
tb_size_t events = (tb_size_t)luaL_checknumber(lua, 1);
// support events for poller
lua_pushboolean(lua, tb_poller_support(xm_io_poller(), events));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_read.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_read.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_read"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* types
*/
typedef enum __xm_pushline_state_e
{
PL_EOF,
PL_FIN,
PL_CONL,
PL_FAIL,
} xm_pushline_state_e;
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_ref_t line)
{
// check
tb_assert_and_check_return_val(stream && line, -1);
// read line and reserve crlf
tb_bool_t eof = tb_false;
tb_hize_t offset = 0;
tb_byte_t* data = tb_null;
tb_hong_t size = tb_stream_size(stream);
while (size < 0 || (offset = tb_stream_offset(stream)) < size)
{
tb_long_t real = tb_stream_peek(stream, &data, TB_STREAM_BLOCK_MAXN);
if (real > 0)
{
tb_char_t const* e = tb_strnchr((tb_char_t const*)data, real, '\n');
if (e)
{
tb_size_t n = (tb_byte_t const*)e + 1 - data;
if (!tb_stream_skip(stream, n)) return -1;
tb_buffer_memncat(line, data, n);
break;
}
else
{
if (!tb_stream_skip(stream, real)) return -1;
tb_buffer_memncat(line, data, real);
}
}
else if (!real)
{
real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, -1);
if (real <= 0)
{
eof = tb_true;
break;
}
}
else
{
eof = tb_true;
break;
}
}
// ok?
tb_size_t linesize = tb_buffer_size(line);
if (linesize) return linesize;
else return (eof || tb_stream_beof(stream))? -1 : 0;
}
static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf)
{
// check
tb_assert(buf && file && continuation && xm_io_file_is_file(file) && file->u.file_ref);
// is binary?
tb_bool_t is_binary = file->encoding == XM_IO_FILE_ENCODING_BINARY;
if (is_binary)
{
continuation = "";
keep_crlf = tb_true;
}
// clear line buffer
tb_buffer_clear(&file->rcache);
// read line data
tb_long_t size = xm_io_file_buffer_readline(file->u.file_ref, &file->rcache);
// translate line data
tb_int_t result = PL_FAIL;
tb_char_t* data = tb_null;
tb_size_t conlen = tb_strlen(continuation);
do
{
// eof?
if (size < 0)
{
result = PL_EOF;
break;
}
// patch two '\0'
tb_buffer_memncat(&file->rcache, (tb_byte_t const*)"\0\0", 2);
// get line data
data = (tb_char_t*)tb_buffer_data(&file->rcache);
tb_assert_and_check_break(data);
// no lf found
if (size > 0 && data[size - 1] != '\n')
result = PL_FIN;
else if (size > 1)
{
// crlf? => lf
if (!is_binary && data[size - 2] == '\r')
{
data[size - 2] = '\n';
size--;
}
// has continuation?
tb_bool_t has_conline = conlen && size >= conlen + 1 && tb_strncmp(continuation, (tb_char_t const*)(data + size - conlen - 1), conlen) == 0;
// do not keep crlf, strip the last lf
if (!keep_crlf && !has_conline) size--;
// strip it if has continuation?
if (has_conline) size -= conlen + 1;
data[size] = '\0';
result = has_conline ? PL_CONL : PL_FIN;
}
else
{
// a single '\n'
if (!keep_crlf) size = 0;
result = PL_FIN;
}
} while (0);
// push line data
if (data && size > 0 && (result == PL_FIN || result == PL_CONL))
tb_buffer_memncat(buf, (tb_byte_t const*)data, size);
// return result
return result;
}
static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file_t* file)
{
// check
tb_assert(lua && file && xm_io_file_is_file(file) && file->u.file_ref);
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
xm_io_return_error(lua, "init buffer failed!");
// read all
tb_byte_t data[TB_STREAM_BLOCK_MAXN];
tb_stream_ref_t stream = file->u.file_ref;
while (!tb_stream_beof(stream))
{
tb_long_t real = tb_stream_read(stream, data, sizeof(data));
if (real > 0)
tb_buffer_memncat(&buf, data, real);
else if (!real)
{
real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, -1);
tb_check_break(real > 0);
}
else break;
}
if (tb_buffer_size(&buf))
lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
else lua_pushliteral(lua, "");
tb_buffer_exit(&buf);
return 1;
}
static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_file(file) && file->u.file_ref);
// is binary? read all directly
tb_bool_t is_binary = file->encoding == XM_IO_FILE_ENCODING_BINARY;
if (is_binary)
return xm_io_file_read_all_directly(lua, file);
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
xm_io_return_error(lua, "init buffer failed!");
// read all
tb_bool_t has_content = tb_false;
while (1)
{
switch (xm_io_file_buffer_pushline(&buf, file, continuation, tb_true))
{
case PL_EOF:
if (!has_content) lua_pushliteral(lua, "");
else lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
tb_buffer_exit(&buf);
return 1;
case PL_FIN:
case PL_CONL:
has_content = tb_true;
continue;
case PL_FAIL:
default:
tb_buffer_exit(&buf);
xm_io_return_error(lua, "failed to read all");
break;
}
}
}
static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_file(file) && file->u.file_ref);
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
xm_io_return_error(lua, "init buffer failed!");
// read line
tb_bool_t has_content = tb_false;
while (1)
{
switch (xm_io_file_buffer_pushline(&buf, file, continuation, keep_crlf))
{
case PL_EOF:
if (!has_content) lua_pushnil(lua);
else lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
tb_buffer_exit(&buf);
return 1;
case PL_FIN:
lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
tb_buffer_exit(&buf);
return 1;
case PL_CONL:
has_content = tb_true;
continue;
case PL_FAIL:
default:
tb_buffer_exit(&buf);
xm_io_return_error(lua, "failed to readline");
break;
}
}
}
static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_long_t n)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_file(file) && file->u.file_ref);
// check continuation
if (*continuation != '\0')
xm_io_return_error(lua, "continuation is not supported for read number of bytes");
// check encoding
if (file->encoding != XM_IO_FILE_ENCODING_BINARY)
xm_io_return_error(lua, "read number of bytes only allows binary file, reopen with 'rb' and try again");
tb_bool_t ok = tb_false;
if (n == 0)
{
tb_byte_t* data = tb_null;
if (tb_stream_need(file->u.file_ref, &data, 1))
{
lua_pushliteral(lua, "");
ok = tb_true;
}
}
else
{
tb_byte_t* bufptr = tb_buffer_resize(&file->rcache, n + 1);
if (bufptr)
{
if (tb_stream_bread(file->u.file_ref, bufptr, n))
{
lua_pushlstring(lua, (tb_char_t const*)bufptr, n);
ok = tb_true;
}
}
}
if (!ok) lua_pushnil(lua);
return 1;
}
static tb_size_t xm_io_file_std_buffer_pushline(tb_buffer_ref_t buf, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf)
{
// check
tb_assert(buf && file && continuation && xm_io_file_is_std(file));
// get input buffer
tb_char_t strbuf[8192];
tb_size_t buflen = 0;
tb_size_t result = PL_FAIL;
if (tb_stdfile_gets(file->u.std_ref, strbuf, tb_arrayn(strbuf) - 1))
buflen = tb_strlen(strbuf);
else return PL_EOF;
tb_size_t conlen = tb_strlen(continuation);
if (buflen > 0 && strbuf[buflen - 1] != '\n')
{
// end of file, no lf found
result = PL_FIN;
}
else if (buflen > 1)
{
// crlf? => lf
if (strbuf[buflen - 2] == '\r')
{
strbuf[buflen - 2] = '\n';
buflen--;
}
// has continuation?
tb_bool_t has_conline = conlen && buflen >= conlen + 1 && tb_strncmp(continuation, (strbuf + buflen - conlen - 1), conlen) == 0;
// do not keep crlf, strip the last lf
if (!keep_crlf && !has_conline) buflen--;
// strip it if has continuation?
if (has_conline) buflen -= conlen + 1;
strbuf[buflen] = '\0';
result = has_conline? PL_CONL : PL_FIN;
}
else
{
// a single '\n'
if (!keep_crlf) buflen = 0;
result = PL_FIN;
}
if (result == PL_FIN || result == PL_CONL)
tb_buffer_memncat(buf, (tb_byte_t const*)strbuf, buflen);
return result;
}
static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_std(file));
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
xm_io_return_error(lua, "init buffer failed!");
// read line
tb_bool_t has_content = tb_false;
while (1)
{
switch (xm_io_file_std_buffer_pushline(&buf, file, continuation, keep_crlf))
{
case PL_EOF:
if (!has_content) lua_pushnil(lua);
else lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
tb_buffer_exit(&buf);
return 1;
case PL_FIN:
lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
tb_buffer_exit(&buf);
return 1;
case PL_CONL:
has_content = tb_true;
continue;
case PL_FAIL:
default:
tb_buffer_exit(&buf);
xm_io_return_error(lua, "failed to readline");
break;
}
}
}
static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_std(file));
// init buffer
tb_buffer_t buf;
if (!tb_buffer_init(&buf))
xm_io_return_error(lua, "init buffer failed!");
// read all
tb_bool_t has_content = tb_false;
while (1)
{
switch (xm_io_file_std_buffer_pushline(&buf, file, continuation, tb_true))
{
case PL_EOF:
if (!has_content) lua_pushliteral(lua, "");
else lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&buf), tb_buffer_size(&buf));
tb_buffer_exit(&buf);
return 1;
case PL_FIN:
case PL_CONL:
has_content = tb_true;
continue;
case PL_FAIL:
default:
tb_buffer_exit(&buf);
xm_io_return_error(lua, "failed to readline");
break;
}
}
}
static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_long_t n)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_std(file));
// check continuation
if (*continuation != '\0')
xm_io_return_error(lua, "continuation is not supported for std streams");
// io.read(0)
if (n == 0)
{
tb_char_t ch;
if (!tb_stdfile_peek(file->u.std_ref, &ch))
lua_pushnil(lua);
else
lua_pushliteral(lua, "");
return 1;
}
// get line buffer
tb_byte_t* buf_ptr = tb_buffer_resize(&file->rcache, (tb_size_t)n);
tb_assert(buf_ptr);
// io.read(n)
if (tb_stdfile_read(file->u.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;
}
static tb_int_t xm_io_file_std_read_num(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation)
{
// check
tb_assert(lua && file && continuation && xm_io_file_is_std(file));
// check continuation
if (*continuation != '\0')
xm_io_return_error(lua, "continuation is not supported for std streams");
// read number
tb_char_t strbuf[512];
if (tb_stdfile_gets(file->u.std_ref, strbuf, tb_arrayn(strbuf)))
lua_pushnumber(lua, tb_s10tod(strbuf)); // TODO check invalid float number string and push nil
else lua_pushnil(lua);
return 1;
}
/* io.file_read(file, [mode, [continuation]])
* io.file_read(file, "all", "\\")
* io.file_read(file, "L")
* io.file_read(file, "l")
* io.file_read(file, "n")
* io.file_read(file, 10)
*/
tb_int_t xm_io_file_read(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "read(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// get arguments
tb_char_t const* mode = luaL_optstring(lua, 2, "l");
tb_char_t const* continuation = luaL_optstring(lua, 3, "");
tb_assert_and_check_return_val(mode && continuation, 0);
tb_long_t count = -1;
if (lua_isnumber(lua, 2))
{
count = (tb_long_t)lua_tointeger(lua, 2);
if (count < 0) xm_io_return_error(lua, "invalid read size, must be positive nubmber or 0");
}
else if (*mode == '*')
mode++;
if (xm_io_file_is_file(file))
{
if (count >= 0) return xm_io_file_read_n(lua, file, continuation, count);
switch (*mode)
{
case 'a': return xm_io_file_read_all(lua, file, continuation);
case 'L': return xm_io_file_read_line(lua, file, continuation, tb_true);
case 'n': xm_io_return_error(lua, "read number is not implemented");
case 'l': return xm_io_file_read_line(lua, file, continuation, tb_false);
default:
xm_io_return_error(lua, "unknonwn read mode");
return 0;
}
}
else
{
if (count >= 0) return xm_io_file_std_read_n(lua, file, continuation, count);
switch (*mode)
{
case 'a': return xm_io_file_std_read_all(lua, file, continuation);
case 'L': return xm_io_file_std_read_line(lua, file, continuation, tb_true);
case 'n': return xm_io_file_std_read_num(lua, file, continuation);
case 'l': return xm_io_file_std_read_line(lua, file, continuation, tb_false);
default:
xm_io_return_error(lua, "unknonwn read mode");
return 0;
}
}
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_seek.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_seek.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_seek"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.file_seek(file, [whence [, offset]])
tb_int_t xm_io_file_seek(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "seek(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// get whence and offset
tb_char_t const* whence = luaL_optstring(lua, 2, "cur");
tb_hong_t offset = (tb_hong_t)luaL_optnumber(lua, 3, 0);
tb_assert_and_check_return_val(whence, 0);
// seek file
if (xm_io_file_is_file(file))
{
tb_assert(file->u.file_ref);
switch (*whence)
{
case 's': // "set"
break;
case 'e': // "end"
{
tb_hong_t size = tb_stream_size(file->u.file_ref);
if (size > 0 && size + offset <= size)
offset = size + offset;
else xm_io_return_error(lua, "seek failed, invalid offset!");
}
break;
default: // "cur"
offset = tb_stream_offset(file->u.file_ref) + offset;
break;
}
if (tb_stream_seek(file->u.file_ref, offset))
{
lua_pushnumber(lua, (lua_Number)offset);
return 1;
}
else xm_io_return_error(lua, "seek failed!");
}
else xm_io_return_error(lua, "seek is not supported on this file");
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_recvfrom.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_recvfrom.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_recvfrom"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// real, data_or_errors, addr, port = io.socket_recvfrom(sock, size)
tb_int_t xm_io_socket_recvfrom(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get data
tb_byte_t* data = tb_null;
if (xm_lua_isinteger(lua, 2))
data = (tb_byte_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (!data)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p)!", data);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// get size
tb_long_t size = 0;
if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3);
if (size <= 0)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid size(%d)!", (tb_int_t)size);
return 2;
}
// recv data
tb_ipaddr_t ipaddr;
tb_ipaddr_clear(&ipaddr);
tb_int_t retn = 1;
tb_long_t real = tb_socket_urecv(sock, &ipaddr, data, size);
lua_pushinteger(lua, (tb_int_t)real);
if (real > 0)
{
retn = 2;
lua_pushnil(lua);
if (!tb_ipaddr_is_empty(&ipaddr))
{
tb_char_t buffer[256];
tb_char_t const* ipstr = tb_ipaddr_ip_cstr(&ipaddr, buffer, sizeof(buffer));
if (ipstr)
{
lua_pushstring(lua, ipstr);
lua_pushinteger(lua, (tb_int_t)tb_ipaddr_port(&ipaddr));
retn = 4;
}
}
}
return retn;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_size.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_size.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_size"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.file_size(file)
tb_int_t xm_io_file_size(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "get size for invalid file!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// get file length
if (xm_io_file_is_file(file))
{
// get size from raw file stream, because we cannot get size from fstream
tb_assert(file->stream);
lua_pushnumber(lua, (lua_Number)tb_stream_size(file->stream));
return 1;
}
else xm_io_return_error(lua, "get size for invalid file!");
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_rawfd.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file file_rawfd.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_rawfd"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// file to fd
#if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX)
# define xm_io_file2fd(file) (lua_Number)((tb_size_t)(file))
#else
# define xm_io_file2fd(file) (lua_Number)tb_file2fd(file)
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* io.file_rawfd(file)
*
* @note this interface is very dangerous and is only used in some special/hacking cases.
*
* e.g. set VS_UNICODE_OUTPUT=fd to enable vs unicode output, @see https://github.com/xmake-io/xmake/issues/528
*/
tb_int_t xm_io_file_rawfd(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "get rawfd for invalid file!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// get file raw fd
if (xm_io_file_is_file(file))
{
tb_file_ref_t rawfile = tb_null;
if (tb_stream_ctrl(file->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile))
{
lua_pushnumber(lua, xm_io_file2fd(rawfile));
return 1;
}
}
// get rawfd failed
xm_io_return_error(lua, "get rawfd for invalid file!");
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_close.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file pipe_close.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "pipe_close"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.pipe_close(pipe)
tb_int_t xm_io_pipe_close(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check pipe?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the pipe file
tb_pipe_file_ref_t pipefile = (tb_pipe_file_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(pipefile, 0);
// exit pipe file
lua_pushboolean(lua, tb_pipe_file_exit(pipefile));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_kill.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_kill.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_kill"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// local sock = io.socket_kill(sock)
tb_int_t xm_io_socket_kill(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// kill socket
tb_socket_kill(sock, TB_SOCKET_KILL_RW);
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_sendto.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_sendto.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_sendto"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.socket_sendto(sock, data, addr, port)
tb_int_t xm_io_socket_sendto(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
if (xm_lua_isinteger(lua, 2)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (xm_lua_isinteger(lua, 3)) size = (tb_size_t)lua_tointeger(lua, 3);
if (!data || !size)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// get address
tb_char_t const* addr = lua_tostring(lua, 4);
tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 5);
if (!addr || !port)
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid address!");
return 2;
}
// get address family
tb_size_t family = (tb_size_t)luaL_checknumber(lua, 6);
// init ip address
tb_ipaddr_t ipaddr;
tb_ipaddr_set(&ipaddr, addr, port, (tb_uint8_t)family);
// send data
tb_long_t real = tb_socket_usend(sock, &ipaddr, data, size);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_close.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_close.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_close"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.file_close(file)
tb_int_t xm_io_file_close(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "close(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// close file
if (xm_io_file_is_file(file))
{
// check
tb_assert(file->u.file_ref);
#ifdef TB_CONFIG_OS_WINDOWS
// 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->u.file_ref, odata, osize)) return tb_false;
tb_buffer_clear(&file->wcache);
}
#endif
// flush filter stream cache, TODO we should fix it in tbox/stream
if ((file->mode & TB_FILE_MODE_RW) == TB_FILE_MODE_RW && file->fstream)
{
if (!tb_stream_sync(file->u.file_ref, tb_false)) return tb_false;
}
// close file
tb_stream_clos(file->u.file_ref);
file->u.file_ref = tb_null;
// exit fstream
if (file->fstream) tb_stream_exit(file->fstream);
file->fstream = tb_null;
// exit stream
if (file->stream) tb_stream_exit(file->stream);
file->stream = tb_null;
// exit the line cache buffer
tb_buffer_exit(&file->rcache);
tb_buffer_exit(&file->wcache);
// gc will free it if no any refs for lua_newuserdata()
// ...
// ok
lua_pushboolean(lua, tb_true);
return 1;
}
else // for stdfile (gc/close)
{
// exit the line cache buffer
tb_buffer_exit(&file->rcache);
tb_buffer_exit(&file->wcache);
// gc will free it if no any refs for lua_newuserdata()
// ...
// ok
lua_pushboolean(lua, tb_true);
return 1;
}
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_open.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/*
* io.socket_open(socktype, family)
*/
tb_int_t xm_io_socket_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get socket type
tb_size_t socktype = (tb_size_t)luaL_checknumber(lua, 1);
// get address family
tb_size_t family = (tb_size_t)luaL_checknumber(lua, 2);
// map socket type
switch (socktype)
{
case 2:
socktype = TB_SOCKET_TYPE_UDP;
break;
case 3:
socktype = TB_SOCKET_TYPE_ICMP;
break;
default:
socktype = TB_SOCKET_TYPE_TCP;
break;
}
// init socket
tb_socket_ref_t sock = tb_socket_init(socktype, family);
if (sock) xm_lua_pushpointer(lua, (tb_pointer_t)sock);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_readable.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file file_readable.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_readable"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_io_file_readable(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "read(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// has readable data?
tb_bool_t ok = tb_false;
if (xm_io_file_is_file(file)) ok = tb_stream_left(file->u.file_ref) > 0;
else ok = tb_stdfile_readable(file->u.std_ref);
lua_pushboolean(lua, ok);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller_spank.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller_spank.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller_spank"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.poller_spank()
tb_int_t xm_io_poller_spank(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// spank the poller, break the tb_poller_wait() and return all events
tb_poller_spak(xm_io_poller());
return 0;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_listen.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_listen.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_listen"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.socket_listen(sock, backlog)
tb_int_t xm_io_socket_listen(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get backlog
tb_size_t backlog = (tb_size_t)luaL_checknumber(lua, 2);
// listen socket
lua_pushnumber(lua, (tb_int_t)tb_socket_listen(sock, backlog));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_send.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_send.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_send"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.socket_send(sock, data, start, last)
tb_int_t xm_io_socket_send(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
if (xm_lua_isinteger(lua, 2)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (xm_lua_isinteger(lua, 3)) size = (tb_size_t)lua_tointeger(lua, 3);
if (!data || !size)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// send data
tb_long_t real = tb_socket_send(sock, data, size);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_bind.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_bind.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_bind"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.socket_bind(sock, addr, port, family)
tb_int_t xm_io_socket_bind(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushboolean(lua, tb_false);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get address
tb_char_t const* address = lua_tostring(lua, 2);
tb_assert_and_check_return_val(address, 0);
// get family
tb_uint8_t family = (tb_uint8_t)luaL_checknumber(lua, 4);
// init address
tb_ipaddr_t addr;
if (family == TB_IPADDR_FAMILY_UNIX)
{
tb_bool_t is_abstract = (tb_bool_t)lua_toboolean(lua, 3);
tb_ipaddr_unix_set_cstr(&addr, address, is_abstract);
}
else
{
tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 3);
tb_ipaddr_set(&addr, address, port, family);
}
// bind socket
lua_pushboolean(lua, tb_socket_bind(sock, &addr));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_flush.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu, ruki
* @file file_flush.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_flush"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_bool_t xm_io_std_flush_impl(xm_io_file_t* file)
{
tb_assert_and_check_return_val(xm_io_file_is_std(file), tb_false);
return (file->u.std_ref != tb_stdfile_input())? tb_stdfile_flush(file->u.std_ref) : tb_false;
}
static tb_bool_t xm_io_file_flush_impl(xm_io_file_t* file)
{
// check
tb_assert_and_check_return_val(xm_io_file_is_file(file), tb_false);
#ifdef TB_CONFIG_OS_WINDOWS
// 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->u.file_ref, odata, osize)) return tb_false;
tb_buffer_clear(&file->wcache);
}
#endif
return tb_stream_sync(file->u.file_ref, tb_false);
}
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.file_flush(file)
tb_int_t xm_io_file_flush(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "flush(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// flush file
tb_bool_t ok = xm_io_file_is_file(file)? xm_io_file_flush_impl(file) : xm_io_std_flush_impl(file);
if (ok)
{
lua_pushboolean(lua, tb_true);
return 1;
}
else xm_io_return_error(lua, "failed to flush file");
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_read.c | /*!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-present, 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 (!xm_lua_ispointer(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)xm_lua_topointer(lua, 1);
tb_check_return_val(pipefile, 0);
// get data
tb_byte_t* data = tb_null;
if (xm_lua_isinteger(lua, 2))
data = (tb_byte_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (!data)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p)!", data);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// get size
tb_long_t size = 0;
if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3);
if (size <= 0)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid size(%d)!", (tb_int_t)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;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_sendfile.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_sendfile.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_sendfile"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.socket_sendfile(sock, file, start, last)
tb_int_t xm_io_socket_sendfile(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// check file
if (!lua_isuserdata(lua, 2))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid file!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 2);
tb_check_return_val(file, 0);
// does not support stdfile
if (!xm_io_file_is_file(file) || !file->stream)
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid file type!");
return 2;
}
// get file reference
tb_file_ref_t rawfile = tb_null;
if (!tb_stream_ctrl(file->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) || !rawfile)
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "cannot get file reference!");
return 2;
}
// get file size
tb_hize_t filesize = tb_file_size(rawfile);
if (!filesize)
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "cannot send empty file!");
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 > filesize)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid start position(%d)!", (tb_int_t)start);
return 2;
}
// get last
tb_long_t last = (tb_long_t)filesize;
if (lua_isnumber(lua, 4)) last = (tb_long_t)lua_tonumber(lua, 4);
if (last < start - 1 || last > filesize + start - 1)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid last position(%d)!", (tb_int_t)last);
return 2;
}
// send file data
tb_long_t real = (tb_long_t)tb_socket_sendf(sock, rawfile, start - 1, last - start + 1);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_openpair.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file pipe_openpair.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "pipe_openpair"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/*
* io.pipe_openpair(mode, buffsize)
*/
tb_int_t xm_io_pipe_openpair(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get pipe mode
tb_char_t const* modestr = luaL_optstring(lua, 1, "AA");
tb_assert_and_check_return_val(modestr, 0);
// init mode
tb_size_t mode[2] = {0};
if (modestr[0] == 'B') mode[0] |= TB_PIPE_MODE_BLOCK;
if (modestr[1] == 'B') mode[1] |= TB_PIPE_MODE_BLOCK;
// get buffer size
tb_size_t buffsize = (tb_size_t)luaL_checknumber(lua, 2);
// init pipe
tb_pipe_file_ref_t pipefile[2];
if (tb_pipe_file_init_pair(pipefile, mode, buffsize))
{
xm_lua_pushpointer(lua, (tb_pointer_t)pipefile[0]);
xm_lua_pushpointer(lua, (tb_pointer_t)pipefile[1]);
}
else
{
lua_pushnil(lua);
lua_pushnil(lua);
}
return 2;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/poller_modify.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file poller_modify.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "poller_modify"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
#include "poller.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.poller_modify(obj:otype(), obj:cdata(), events)
tb_int_t xm_io_poller_modify(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 2))
{
lua_pushboolean(lua, tb_false);
lua_pushfstring(lua, "invalid poller object!");
return 2;
}
// get otype
tb_uint8_t otype = (tb_uint8_t)luaL_checknumber(lua, 1);
// get cdata
tb_char_t const* cdata_str = tb_null;
tb_pointer_t cdata = (tb_pointer_t)xm_lua_topointer2(lua, 2, &cdata_str);
tb_check_return_val(cdata, 0);
// get events
tb_size_t events = (tb_size_t)luaL_checknumber(lua, 3);
// modify events in poller
tb_poller_object_t object;
object.type = otype;
object.ref.ptr = cdata;
lua_pushboolean(lua, tb_poller_modify(xm_io_poller(), &object, events, cdata_str));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_wait.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_wait.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_wait"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.socket_wait(sock, events, timeout)
tb_int_t xm_io_socket_wait(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 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 socket
lua_pushnumber(lua, (tb_int_t)tb_socket_wait(sock, events, timeout));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/iscygpty.c | /*
* iscygpty.c -- part of ptycheck
* https://github.com/k-takata/ptycheck
*
* Copyright (c) 2015-2017 K.Takata
*
* You can redistribute it and/or modify it under the terms of either
* the MIT license (as described below) or the Vim license.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifdef _WIN32
#include <ctype.h>
#include <io.h>
#include <wchar.h>
#include <windows.h>
#ifdef USE_FILEEXTD
/* VC 7.1 or earlier doesn't support SAL. */
# if !defined(_MSC_VER) || (_MSC_VER < 1400)
# define __out
# define __in
# define __in_opt
# endif
/* Win32 FileID API Library:
* http://www.microsoft.com/en-us/download/details.aspx?id=22599
* Needed for WinXP. */
# include <fileextd.h>
#else /* USE_FILEEXTD */
/* VC 8 or earlier. */
# if defined(_MSC_VER) && (_MSC_VER < 1500)
# ifdef ENABLE_STUB_IMPL
# define STUB_IMPL
# else
# error "Win32 FileID API Library is required for VC2005 or earlier."
# endif
# endif
#endif /* USE_FILEEXTD */
#if _WIN32_WINNT >= 0x0600
# define USE_DYNFILEID // we need to enable it for supporting xp
#endif
#ifdef USE_DYNFILEID
typedef BOOL (WINAPI *pfnGetFileInformationByHandleEx)(
HANDLE hFile,
FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
LPVOID lpFileInformation,
DWORD dwBufferSize);
static pfnGetFileInformationByHandleEx pGetFileInformationByHandleEx = NULL;
# ifndef USE_FILEEXTD
static BOOL WINAPI stub_GetFileInformationByHandleEx(
HANDLE hFile,
FILE_INFO_BY_HANDLE_CLASS FileInformationClass,
LPVOID lpFileInformation,
DWORD dwBufferSize)
{
return FALSE;
}
# endif
static void setup_fileid_api(void)
{
if (pGetFileInformationByHandleEx != NULL) {
return;
}
pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx)
GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),
"GetFileInformationByHandleEx");
if (pGetFileInformationByHandleEx == NULL) {
# ifdef USE_FILEEXTD
pGetFileInformationByHandleEx = GetFileInformationByHandleEx;
# else
pGetFileInformationByHandleEx = stub_GetFileInformationByHandleEx;
# endif
}
}
#else
# define pGetFileInformationByHandleEx GetFileInformationByHandleEx
# define setup_fileid_api()
#endif
#define is_wprefix(s, prefix) \
(wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0)
/* Check if the fd handle is a cygwin/msys's pty. */
int is_cygpty(HANDLE h)
{
#if defined(STUB_IMPL)
return 0;
#elif _WIN32_WINNT >= 0x0600
int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * (MAX_PATH - 1);
FILE_NAME_INFO *nameinfo;
WCHAR *p = NULL;
setup_fileid_api();
if (h == INVALID_HANDLE_VALUE) {
return 0;
}
/* Cygwin/msys's pty is a pipe. */
if (GetFileType(h) != FILE_TYPE_PIPE) {
return 0;
}
nameinfo = (FILE_NAME_INFO*)malloc(size + sizeof(WCHAR));
if (nameinfo == NULL) {
return 0;
}
/* Check the name of the pipe:
* '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' */
if (pGetFileInformationByHandleEx(h, FileNameInfo, nameinfo, size)) {
nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0';
p = nameinfo->FileName;
if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */
p += 8;
} else if (is_wprefix(p, L"\\msys-")) { /* MSYS and MSYS2 */
p += 6;
} else {
p = NULL;
}
if (p != NULL) {
while (*p && isxdigit(*p)) /* Skip 16-digit hexadecimal. */
++p;
if (is_wprefix(p, L"-pty")) {
p += 4;
} else {
p = NULL;
}
}
if (p != NULL) {
while (*p && isdigit(*p)) /* Skip pty number. */
++p;
if (is_wprefix(p, L"-from-master")) {
//p += 12;
} else if (is_wprefix(p, L"-to-master")) {
//p += 10;
} else {
p = NULL;
}
}
}
free(nameinfo);
return (p != NULL);
#else
return 0;
#endif
}
#endif /* _WIN32 */
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/filelock_close.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file filelock_close.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "filelock_close"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.filelock_close(lock)
tb_int_t xm_io_filelock_close(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check lock?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(lock, 0);
// exit lock
tb_filelock_exit(lock);
// save result: ok
lua_pushboolean(lua, tb_true);
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/file_isatty.c | /*!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-present, TBOOX Open Source Group.
*
* @author OpportunityLiu
* @file file_isatty.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "file_isatty"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// io.file_isatty(file)
tb_int_t xm_io_file_isatty(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is user data?
if (!lua_isuserdata(lua, 1))
xm_io_return_error(lua, "isatty(invalid file)!");
// get file
xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
tb_check_return_val(file, 0);
// is tty?
lua_pushboolean(lua, xm_io_file_is_tty(file));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_rawfd.c | /*!A cross-platform build utility based on Lua
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this sock 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-present, TBOOX Open Source Group.
*
* @author ruki
* @sock socket_rawfd.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_rawfd"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// socket to fd
#define xm_io_sock2fd(sock) (lua_Number)tb_sock2fd(sock)
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* io.socket_rawfd(sock)
*/
tb_int_t xm_io_socket_rawfd(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
xm_io_return_error(lua, "get rawfd for invalid sock!");
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// return result
lua_pushnumber(lua, xm_io_sock2fd(sock));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_write.c | /*!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-present, 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
if (!xm_lua_ispointer(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)xm_lua_topointer(lua, 1);
tb_check_return_val(pipefile, 0);
// get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
if (xm_lua_isinteger(lua, 2)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (xm_lua_isinteger(lua, 3)) size = (tb_size_t)lua_tointeger(lua, 3);
if (!data || !size)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// write data
tb_long_t real = tb_pipe_file_write(pipefile, data, size);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/socket_ctrl.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file socket_ctrl.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "socket_ctrl"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
// io.socket_ctrl(sock, code, value)
tb_int_t xm_io_socket_ctrl(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check socket
if (!xm_lua_ispointer(lua, 1))
{
lua_pushnumber(lua, -1);
lua_pushliteral(lua, "invalid socket!");
return 2;
}
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(sock, 0);
// get code
tb_size_t code = (tb_size_t)luaL_checkinteger(lua, 2);
// get value
tb_size_t value = (tb_size_t)luaL_checkinteger(lua, 3);
// control socket
lua_pushboolean(lua, tb_socket_ctrl(sock, code, value));
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/io/pipe_open.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file pipe_open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "pipe_open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/*
* io.pipe_open(name, mode, buffsize)
*/
tb_int_t xm_io_pipe_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get pipe name and mode
tb_char_t const* name = luaL_checkstring(lua, 1);
tb_char_t const* modestr = luaL_optstring(lua, 2, "r");
tb_assert_and_check_return_val(name && modestr, 0);
// get pipe mode value
tb_size_t mode = TB_PIPE_MODE_RO;
if (modestr[0] == 'w') mode = TB_PIPE_MODE_WO;
// set block mode
if (modestr[1] == 'B') mode |= TB_PIPE_MODE_BLOCK;
// get buffer size
tb_size_t buffsize = (tb_size_t)luaL_checknumber(lua, 3);
// open pipe file
tb_pipe_file_ref_t pipefile = tb_pipe_file_init(name, mode, buffsize);
if (pipefile) xm_lua_pushpointer(lua, (tb_pointer_t)pipefile);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/fwatcher/close.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file close.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "fwatcher.close"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// fwatcher.close(p)
tb_int_t xm_fwatcher_close(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the fwatcher
tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(fwatcher, 0);
// exit fwatcher
tb_fwatcher_exit(fwatcher);
// save result: ok
lua_pushboolean(lua, tb_true);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/fwatcher/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file prefix.h
*
*/
#ifndef XM_FWATCHER_PREFIX_H
#define XM_FWATCHER_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/fwatcher/open.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "fwatcher.open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_fwatcher_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// init fwatcher
tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)tb_fwatcher_init();
if (fwatcher) xm_lua_pushpointer(lua, (tb_pointer_t)fwatcher);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/fwatcher/wait.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file wait.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "fwatcher.wait"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// fwatcher.wait(p)
tb_int_t xm_fwatcher_wait(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the fwatcher
tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(fwatcher, 0);
// get the timeout
tb_long_t timeout = (tb_long_t)luaL_checkinteger(lua, 2);
// wait fwatcher event
tb_fwatcher_event_t event;
tb_long_t ok = tb_fwatcher_wait(fwatcher, &event, timeout);
// save result
lua_pushinteger(lua, ok);
if (ok > 0)
{
lua_newtable(lua);
lua_pushstring(lua, "path");
lua_pushstring(lua, event.filepath);
lua_settable(lua, -3);
lua_pushstring(lua, "type");
lua_pushinteger(lua, event.event);
lua_settable(lua, -3);
return 2;
}
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/fwatcher/remove.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file remove.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "fwatcher.remove"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// fwatcher.remove(watchdir)
tb_int_t xm_fwatcher_remove(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the fwatcher
tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(fwatcher, 0);
// get watchdir
tb_char_t const* watchdir = luaL_checkstring(lua, 2);
tb_check_return_val(watchdir, 0);
// remove watchdir
tb_bool_t ok = tb_fwatcher_remove(fwatcher, watchdir);
// save result
lua_pushboolean(lua, ok);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/fwatcher/add.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file add.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "fwatcher.add"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// fwatcher.add(watchdir, recursion)
tb_int_t xm_fwatcher_add(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// is pointer?
if (!xm_lua_ispointer(lua, 1))
return 0;
// get the fwatcher
tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1);
tb_check_return_val(fwatcher, 0);
// get watchdir
tb_char_t const* watchdir = luaL_checkstring(lua, 2);
tb_check_return_val(watchdir, 0);
// get recursion
tb_bool_t recursion = lua_toboolean(lua, 3);
// add watchdir
tb_bool_t ok = tb_fwatcher_add(fwatcher, watchdir, recursion);
// save result
lua_pushboolean(lua, ok);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/semver/semver.c | /*!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-present, TBOOX Open Source Group.
*
* @author uael
* @file semver.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "semver"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_void_t lua_pushsemver(lua_State *lua, semver_t const* semver)
{
// check
tb_assert(lua && semver);
// return a semver table
lua_createtable(lua, 0, 7);
lua_pushstring(lua, semver->raw);
lua_setfield(lua, -2, "raw");
lua_pushlstring(lua, semver->raw, semver->len);
lua_setfield(lua, -2, "version");
lua_pushinteger(lua, semver->major);
lua_setfield(lua, -2, "major");
lua_pushinteger(lua, semver->minor);
lua_setfield(lua, -2, "minor");
lua_pushinteger(lua, semver->patch);
lua_setfield(lua, -2, "patch");
// push prelease table
lua_pushstring(lua, "prerelease");
lua_newtable(lua);
tb_uchar_t i = 0;
semver_id_t const* id = &semver->prerelease;
while (id && id->len)
{
if (id->numeric) lua_pushinteger(lua, id->num);
else lua_pushlstring(lua, id->raw, id->len);
id = id->next;
lua_rawseti(lua, -2, ++i);
}
lua_settable(lua, -3);
// push the build table
i = 0;
lua_pushstring(lua, "build");
lua_newtable(lua);
id = &semver->build;
while (id && id->len)
{
if (id->numeric) lua_pushinteger(lua, id->num);
else lua_pushlstring(lua, id->raw, id->len);
id = id->next;
lua_rawseti(lua, -2, ++i);
}
lua_settable(lua, -3);
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/semver/select.c | /*!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-present, TBOOX Open Source Group.
*
* @author uael
* @file select.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "select"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
static tb_bool_t xm_semver_select_from_versions_tags1(lua_State* lua, tb_int_t fromidx, semver_t* semver, semver_range_t const* range, semvers_t* matches)
{
// clear matches
semvers_pclear(matches);
// select all matches
lua_Integer i = 0;
luaL_checktype(lua, fromidx, LUA_TTABLE);
for (i = lua_objlen(lua, fromidx); i > 0; --i)
{
lua_pushinteger(lua, i);
lua_gettable(lua, fromidx);
tb_char_t const* source_str = luaL_checkstring(lua, -1);
if (source_str && semver_tryn(semver, source_str, tb_strlen(source_str)) == 0)
{
if (semver_range_pmatch(semver, range)) semvers_ppush(matches, *semver);
else semver_dtor(semver);
}
lua_pop(lua, 1);
}
// no matches?
tb_check_return_val(matches->length, tb_false);
// sort matches
semvers_psort(matches);
// get the newest version
semver_t top = semvers_ppop(matches);
lua_createtable(lua, 0, 2);
// return results
lua_pushstring(lua, top.raw);
lua_setfield(lua, -2, "version");
lua_pushstring(lua, fromidx == 2? "version" : "tag");
lua_setfield(lua, -2, "source");
// exit the popped semver
semver_dtor(&top);
// ok
return tb_true;
}
static tb_bool_t xm_semver_select_from_versions_tags2(lua_State* lua, tb_int_t fromidx, semver_t* semver, tb_char_t const* version_str, tb_size_t version_len)
{
lua_Integer i = 0;
luaL_checktype(lua, fromidx, LUA_TTABLE);
for (i = lua_objlen(lua, fromidx); i > 0; --i)
{
lua_pushinteger(lua, i);
lua_gettable(lua, fromidx);
tb_char_t const* source_str = luaL_checkstring(lua, -1);
tb_size_t source_len = tb_strlen(source_str);
lua_pop(lua, 1);
if (source_len == version_len && tb_strncmp(source_str, version_str, version_len) == 0)
{
lua_createtable(lua, 0, 2);
lua_pushlstring(lua, source_str, source_len);
lua_setfield(lua, -2, "version");
lua_pushstring(lua, fromidx == 2? "version" : "tag");
lua_setfield(lua, -2, "source");
return tb_true;
}
}
return tb_false;
}
static tb_bool_t xm_semver_select_from_branches(lua_State* lua, tb_int_t fromidx, tb_char_t const* range_str, tb_size_t range_len)
{
lua_Integer i = 0;
luaL_checktype(lua, fromidx, LUA_TTABLE);
for (i = lua_objlen(lua, fromidx); i > 0; --i)
{
lua_pushinteger(lua, i);
lua_gettable(lua, fromidx);
tb_char_t const* source_str = luaL_checkstring(lua, -1);
tb_size_t source_len = tb_strlen(source_str);
lua_pop(lua, 1);
if (source_len == range_len && tb_memcmp(source_str, range_str, source_len) == 0)
{
lua_createtable(lua, 0, 2);
lua_pushlstring(lua, source_str, source_len);
lua_setfield(lua, -2, "version");
lua_pushstring(lua, "branch");
lua_setfield(lua, -2, "source");
return tb_true;
}
}
return tb_false;
}
static tb_bool_t xm_semver_select_latest_from_versions_tags(lua_State* lua, tb_int_t fromidx, semver_t* semver, semvers_t* matches)
{
// clear matches
semvers_pclear(matches);
// push all versions to matches
lua_Integer i = 0;
luaL_checktype(lua, fromidx, LUA_TTABLE);
for (i = lua_objlen(lua, fromidx); i > 0; --i)
{
lua_pushinteger(lua, i);
lua_gettable(lua, fromidx);
tb_char_t const* source_str = luaL_checkstring(lua, -1);
if (source_str && semver_tryn(semver, source_str, tb_strlen(source_str)) == 0)
semvers_ppush(matches, *semver);
lua_pop(lua, 1);
}
tb_check_return_val(matches->length, tb_false);
// sort matches
semvers_psort(matches);
// get the newest match
semver_t top = semvers_ppop(matches);
lua_createtable(lua, 0, 2);
// return results
lua_pushstring(lua, top.raw);
lua_setfield(lua, -2, "version");
lua_pushstring(lua, fromidx == 2? "version" : "tag");
lua_setfield(lua, -2, "source");
semver_dtor(&top);
return tb_true;
}
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* select version
*
* local versioninfo, errors = semver.select(">=1.5.0 <1.6", {"1.5.0", "1.5.1"}, {"v1.5.0", ..}, {"latest", "dev"})
*/
tb_int_t xm_semver_select(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// select version
tb_bool_t ok = tb_false;
tb_bool_t is_range = tb_false;
tb_char_t const* range_str = tb_null;
semver_t semver = {0};
semvers_t matches = {0};
semver_range_t range = {0};
do
{
// get the version range string
range_str = luaL_checkstring(lua, 1);
tb_check_break(range_str);
// get the range string length
tb_size_t range_len = tb_strlen(range_str);
// parse the version range string
is_range = semver_rangen(&range, range_str, range_len) == 0;
if (is_range)
{
// attempt to select version from the versions list first
if (xm_semver_select_from_versions_tags1(lua, 2, &semver, &range, &matches))
{
ok = tb_true;
break;
}
// attempt to select version from the tags list
if (xm_semver_select_from_versions_tags1(lua, 3, &semver, &range, &matches))
{
ok = tb_true;
break;
}
}
else
{
// attempt to select version from the versions list first
if (xm_semver_select_from_versions_tags2(lua, 2, &semver, range_str, range_len))
{
ok = tb_true;
break;
}
// attempt to select version from the tags list
if (xm_semver_select_from_versions_tags2(lua, 3, &semver, range_str, range_len))
{
ok = tb_true;
break;
}
}
// attempt to select version from the branches
if (xm_semver_select_from_branches(lua, 4, range_str, range_len))
{
ok = tb_true;
break;
}
// select the latest version from the tags and versions if be latest
if (!tb_strcmp(range_str, "latest"))
{
// attempt to select latest version from the versions list
if (xm_semver_select_latest_from_versions_tags(lua, 2, &semver, &matches))
{
ok = tb_true;
break;
}
// attempt to select latest version from the tags list
if (xm_semver_select_latest_from_versions_tags(lua, 3, &semver, &matches))
{
ok = tb_true;
break;
}
}
} while (0);
// exit matches
semvers_dtor(matches);
// exit range
semver_range_dtor(&range);
// failed?
if (!ok)
{
if (!is_range)
{
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver range '%s'", range_str);
}
lua_pushnil(lua);
lua_pushfstring(lua, "unable to select version for range '%s'", range_str);
return 2;
}
// ok
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/semver/prefix.h | /*!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-present, TBOOX Open Source Group.
*
* @author uael
* @file prefix.h
*
*/
#ifndef XM_SEMVER_PREFIX_H
#define XM_SEMVER_PREFIX_H
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "../prefix.h"
#include "semver.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_enter__
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
/* push struct semver
*
* @param lua the lua context
* @param semver the semver struct
*
*/
tb_void_t lua_pushsemver(lua_State *lua, semver_t const* semver);
/* //////////////////////////////////////////////////////////////////////////////////////
* leave
*/
__tb_extern_c_leave__
#endif
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/semver/compare.c | /*!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-present, TBOOX Open Source Group.
*
* @author uael
* @file compare.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "compare"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
// semver.compare("v1.0.1-beta", "1.2") > 0?
tb_int_t xm_semver_compare(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the version1 string
tb_char_t const* version1_str = luaL_checkstring(lua, 1);
tb_check_return_val(version1_str, 0);
// get the version2 string
tb_char_t const* version2_str = luaL_checkstring(lua, 2);
tb_check_return_val(version2_str, 0);
// try to parse version1 string
semver_t semver1 = {0};
if (semver_tryn(&semver1, version1_str, tb_strlen(version1_str)))
{
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver '%s'", version1_str);
return 2;
}
// try to parse version2 string
semver_t semver2 = {0};
if (semver_tryn(&semver2, version2_str, tb_strlen(version2_str)))
{
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver '%s'", version2_str);
return 2;
}
// do compare
lua_pushinteger(lua, semver_pcmp(&semver1, &semver2));
// end
semver_dtor(&semver1);
semver_dtor(&semver2);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/semver/satisfies.c | /*!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-present, TBOOX Open Source Group.
*
* @author uael
* @file satisfies.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "satisfies"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* satisfies the given version range?
*
* semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') => true
*/
tb_int_t xm_semver_satisfies(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the version string
tb_char_t const* version_str = luaL_checkstring(lua, 1);
tb_char_t const* range_str = luaL_checkstring(lua, 2);
tb_assert_and_check_return_val(version_str && range_str, 0);
// parse the version range string
semver_range_t range = {0};
if (semver_rangen(&range, range_str, tb_strlen(range_str)))
{
// range is branch name? try to match it
semver_t range_semver = {0};
if (!tb_strcmp(version_str, range_str))
{
lua_pushboolean(lua, tb_true);
return 1;
}
// range is a single version? try to compare it
else if (!semver_tryn(&range_semver, range_str, tb_strlen(range_str)))
{
semver_t semver = {0};
if (!semver_tryn(&semver, version_str, tb_strlen(version_str)))
{
lua_pushboolean(lua, semver_pcmp(&semver, &range_semver) == 0);
semver_dtor(&semver);
semver_dtor(&range_semver);
return 1;
}
else
{
semver_dtor(&range_semver);
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver '%s'", version_str);
return 2;
}
}
else
{
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver range '%s'", range_str);
return 2;
}
}
// try to parse the version string
semver_t semver = {0};
if (semver_tryn(&semver, version_str, tb_strlen(version_str)))
{
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver '%s'", version_str);
return 2;
}
// satisfies this range?
lua_pushboolean(lua, semver_range_match(semver, range));
semver_dtor(&semver);
semver_range_dtor(&range);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/semver/parse.c | /*!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-present, TBOOX Open Source Group.
*
* @author uael
* @file parse.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "parse"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
/* version = semver.parse("v1.0.1-beta")
*
*
{
patch = 1
, raw = v1.0.1-beta
, version = v1.0.1-beta
, major = 1
, build =
{
}
, minor = 0
, prerelease =
{
beta
}
}
*
*/
tb_int_t xm_semver_parse(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get the version string
tb_char_t const* version_str = luaL_checkstring(lua, 1);
tb_check_return_val(version_str, 0);
// try to parse version string
semver_t semver = {0};
if (semver_tryn(&semver, version_str, tb_strlen(version_str)))
{
lua_pushnil(lua);
lua_pushfstring(lua, "unable to parse semver '%s'", version_str);
return 2;
}
// ok
lua_pushsemver(lua, &semver);
semver_dtor(&semver);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/lz4/decompress_stream_open.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file decompress_stream_open.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "decompress_stream_open"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_lz4_decompress_stream_open(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
xm_lz4_dstream_t* stream = xm_lz4_dstream_init();
if (stream) xm_lua_pushpointer(lua, (tb_pointer_t)stream);
else lua_pushnil(lua);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/lz4/decompress.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file decompress.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "decompress"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_lz4_decompress(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
if (xm_lua_isinteger(lua, 1)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 1);
if (xm_lua_isinteger(lua, 2)) size = (tb_size_t)lua_tointeger(lua, 2);
if (!data || !size)
{
lua_pushnil(lua);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// do decompress
tb_bool_t ok = tb_false;
LZ4F_errorCode_t code;
LZ4F_decompressionContext_t ctx = tb_null;
tb_buffer_t result;
do
{
tb_buffer_init(&result);
code = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
if (LZ4F_isError(code)) break;
tb_byte_t buffer[8192];
tb_bool_t failed = tb_false;
while (1)
{
size_t advance = (size_t)size;
size_t buffer_size = sizeof(buffer);
code = LZ4F_decompress(ctx, buffer, &buffer_size, data, &advance, tb_null);
if (LZ4F_isError(code))
{
failed = tb_true;
break;
}
if (buffer_size == 0) break;
data += advance;
size -= advance;
tb_buffer_memncat(&result, buffer, buffer_size);
}
tb_assert_and_check_break(!failed && tb_buffer_size(&result));
lua_pushlstring(lua, (tb_char_t const*)tb_buffer_data(&result), tb_buffer_size(&result));
ok = tb_true;
} while (0);
if (ctx)
{
LZ4F_freeDecompressionContext(ctx);
ctx = tb_null;
}
tb_buffer_exit(&result);
if (!ok)
{
tb_char_t const* error = LZ4F_getErrorName(code);
lua_pushnil(lua);
lua_pushstring(lua, error? error : "unknown");
return 2;
}
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/lz4/compress_stream_write.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file compress_stream_write.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "compress_stream_write"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_lz4_compress_stream_write(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check handle
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid handle!");
return 2;
}
// get stream
xm_lz4_cstream_t* stream = (xm_lz4_cstream_t*)xm_lua_topointer(lua, 1);
tb_check_return_val(stream, 0);
// get data and size
tb_size_t size = 0;
tb_byte_t const* data = tb_null;
if (xm_lua_isinteger(lua, 2)) data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (xm_lua_isinteger(lua, 3)) size = (tb_size_t)lua_tointeger(lua, 3);
if (!data || !size)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// is end?
tb_bool_t end = tb_false;
if (lua_isboolean(lua, 4)) end = lua_toboolean(lua, 4);
// write data
tb_long_t real = xm_lz4_cstream_write(stream, data, size, end);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
0 | repos/xmake/core/src/xmake | repos/xmake/core/src/xmake/lz4/compress_stream_read.c | /*!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-present, TBOOX Open Source Group.
*
* @author ruki
* @file compress_stream_read.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
#define TB_TRACE_MODULE_NAME "compress_stream_read"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_int_t xm_lz4_compress_stream_read(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
// check handle
if (!xm_lua_ispointer(lua, 1))
{
lua_pushinteger(lua, -1);
lua_pushliteral(lua, "invalid handle!");
return 2;
}
// get stream
xm_lz4_cstream_t* stream = (xm_lz4_cstream_t*)xm_lua_topointer(lua, 1);
tb_check_return_val(stream, 0);
// get data
tb_byte_t* data = tb_null;
if (xm_lua_isinteger(lua, 2))
data = (tb_byte_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, 2);
if (!data)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid data(%p)!", data);
return 2;
}
tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t));
// get size
tb_long_t size = 0;
if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3);
if (size <= 0)
{
lua_pushinteger(lua, -1);
lua_pushfstring(lua, "invalid size(%d)!", (tb_int_t)size);
return 2;
}
// read data
tb_long_t real = xm_lz4_cstream_read(stream, data, size);
lua_pushinteger(lua, (tb_int_t)real);
return 1;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.