summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-11-09 22:34:50 +0800
committerruki <[email protected]>2025-11-09 22:34:50 +0800
commitbf2da8a3d7f04bf5ffcdce8fd4c334d5421d3494 (patch)
tree054a98d5d2b875f9bb552c6848a31877e5afde4c
parentf2f3821a0248b24941566c90e7aabcc0e7d95e77 (diff)
format core
-rw-r--r--core/src/xmake/engine.c94
-rw-r--r--core/src/xmake/io/file_close.c6
-rw-r--r--core/src/xmake/io/file_flush.c6
-rw-r--r--core/src/xmake/io/file_isatty.c3
-rw-r--r--core/src/xmake/io/file_open.c90
-rw-r--r--core/src/xmake/io/file_rawfd.c3
-rw-r--r--core/src/xmake/io/file_read.c89
-rw-r--r--core/src/xmake/io/file_readable.c8
-rw-r--r--core/src/xmake/io/file_seek.c14
-rw-r--r--core/src/xmake/io/file_size.c6
-rw-r--r--core/src/xmake/io/file_write.c16
-rw-r--r--core/src/xmake/io/filelock_close.c3
-rw-r--r--core/src/xmake/io/filelock_lock.c3
-rw-r--r--core/src/xmake/io/filelock_open.c8
-rw-r--r--core/src/xmake/io/filelock_trylock.c3
-rw-r--r--core/src/xmake/io/filelock_unlock.c3
-rw-r--r--core/src/xmake/io/iscygpty.c6
-rw-r--r--core/src/xmake/io/pipe_close.c3
-rw-r--r--core/src/xmake/io/pipe_open.c11
-rw-r--r--core/src/xmake/io/pipe_openpair.c6
-rw-r--r--core/src/xmake/io/pipe_read.c6
-rw-r--r--core/src/xmake/io/pipe_wait.c3
-rw-r--r--core/src/xmake/io/pipe_write.c6
-rw-r--r--core/src/xmake/io/poller.c3
-rw-r--r--core/src/xmake/io/poller_wait.c8
-rw-r--r--core/src/xmake/io/socket_accept.c8
-rw-r--r--core/src/xmake/io/socket_close.c3
-rw-r--r--core/src/xmake/io/socket_kill.c3
-rw-r--r--core/src/xmake/io/socket_listen.c3
-rw-r--r--core/src/xmake/io/socket_open.c5
-rw-r--r--core/src/xmake/io/socket_peeraddr.c8
-rw-r--r--core/src/xmake/io/socket_recv.c6
-rw-r--r--core/src/xmake/io/socket_recvfrom.c6
-rw-r--r--core/src/xmake/io/socket_send.c6
-rw-r--r--core/src/xmake/io/socket_sendfile.c6
-rw-r--r--core/src/xmake/io/socket_sendto.c6
-rw-r--r--core/src/xmake/io/socket_wait.c3
-rw-r--r--core/src/xmake/io/stdfile.c14
-rw-r--r--core/src/xmake/xmake.c6
39 files changed, 307 insertions, 183 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 7a07d72da..0405fc230 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -898,18 +898,20 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t *engine,
return tb_true;
}
- tb_bool_t ok = tb_false;
+ tb_bool_t ok = tb_false;
tb_char_t data[TB_PATH_MAXN] = { 0 };
do {
#ifdef XM_EMBED_ENABLE
tb_size_t embedcount = engine->embedcount;
if (embedcount) {
tb_uint32_t crc32 = 0;
- for (tb_size_t i = 0; i < embedcount; i++)
+ for (tb_size_t i = 0; i < embedcount; i++) {
crc32 += tb_crc32_make(engine->embeddata[i], engine->embedsize[i], 0);
+ }
tb_snprintf(path, maxn, "%s/%x", engine->tmpdir, crc32);
- } else
+ } else {
tb_strlcpy(path, engine->tmpdir, maxn);
+ }
ok = tb_true;
break;
#endif
@@ -1011,8 +1013,9 @@ static tb_bool_t xm_engine_get_project_directory(xm_engine_t *engine, tb_char_t
} while (0);
// failed?
- if (!ok)
+ if (!ok) {
tb_printf("error: not found the project directory!\n");
+ }
return ok;
}
@@ -1054,12 +1057,13 @@ static tb_void_t xm_engine_init_host(xm_engine_t *engine) {
#if defined(__COSMOPOLITAN__)
struct utsname buffer;
if (uname(&buffer) == 0) {
- if (tb_strstr(buffer.sysname, "Darwin"))
+ if (tb_strstr(buffer.sysname, "Darwin")) {
syshost = "macosx";
- else if (tb_strstr(buffer.sysname, "Linux"))
+ } else if (tb_strstr(buffer.sysname, "Linux")) {
syshost = "linux";
- else if (tb_strstr(buffer.sysname, "Windows"))
+ } else if (tb_strstr(buffer.sysname, "Windows")) {
syshost = "windows";
+ }
}
#elif defined(TB_CONFIG_OS_WINDOWS)
syshost = "windows";
@@ -1096,8 +1100,9 @@ static tb_void_t xm_engine_init_host(xm_engine_t *engine) {
"clang",
5) // clang32/64 on msys2, @see https://github.com/xmake-io/xmake/issues/3060
|| !tb_stricmp(data, "ucrt64") // ucrt64 https://www.msys2.org/docs/environments/
- || !tb_stricmp(data, "msys")) // on msys2
+ || !tb_stricmp(data, "msys")) { // on msys2
subhost = "msys";
+ }
}
}
#endif
@@ -1145,12 +1150,14 @@ static tb_void_t xm_engine_init_arch(xm_engine_t *engine) {
if (uname(&buffer) == 0) {
sysarch = buffer.machine;
if (tb_strstr(buffer.sysname, "Windows")) {
- if (!tb_strcmp(buffer.machine, "x86_64"))
+ if (!tb_strcmp(buffer.machine, "x86_64")) {
sysarch = "x64";
- else if (!tb_strcmp(buffer.machine, "i686") || !tb_strcmp(buffer.machine, "i386"))
+ } else if (!tb_strcmp(buffer.machine, "i686") || !tb_strcmp(buffer.machine, "i386")) {
sysarch = "x86";
- } else if (!tb_strcmp(buffer.machine, "aarch64"))
+ }
+ } else if (!tb_strcmp(buffer.machine, "aarch64")) {
sysarch = "arm64";
+ }
}
#elif defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX)
// the GetNativeSystemInfo function type
@@ -1160,12 +1167,14 @@ static tb_void_t xm_engine_init_arch(xm_engine_t *engine) {
SYSTEM_INFO systeminfo = { 0 };
GetNativeSystemInfo_t pGetNativeSystemInfo = tb_null;
tb_dynamic_ref_t kernel32 = tb_dynamic_init("kernel32.dll");
- if (kernel32)
+ if (kernel32) {
pGetNativeSystemInfo = (GetNativeSystemInfo_t)tb_dynamic_func(kernel32, "GetNativeSystemInfo");
- if (pGetNativeSystemInfo)
+ }
+ if (pGetNativeSystemInfo) {
pGetNativeSystemInfo(&systeminfo);
- else
+ } else {
GetSystemInfo(&systeminfo);
+ }
// init architecture
switch (systeminfo.wProcessorArchitecture) {
@@ -1187,8 +1196,9 @@ static tb_void_t xm_engine_init_arch(xm_engine_t *engine) {
break;
}
#endif
- if (!sysarch)
+ if (!sysarch) {
sysarch = xmakearch;
+ }
lua_pushstring(engine->lua, sysarch);
lua_setglobal(engine->lua, "_ARCH");
@@ -1198,10 +1208,11 @@ static tb_void_t xm_engine_init_arch(xm_engine_t *engine) {
// get architecture from msys environment
tb_char_t data[64] = { 0 };
if (tb_environment_first("MSYSTEM_CARCH", data, sizeof(data))) {
- if (!tb_strcmp(data, "i686"))
+ if (!tb_strcmp(data, "i686")) {
subarch = "i386";
- else
+ } else {
subarch = data;
+ }
}
#endif
lua_pushstring(engine->lua, subarch);
@@ -1238,8 +1249,9 @@ static tb_void_t xm_engine_init_features(xm_engine_t *engine) {
static tb_void_t xm_engine_init_signal(xm_engine_t *engine) {
// we enable it to catch the current lua stack in ctrl-c signal handler if XMAKE_PROFILE=stuck
tb_char_t data[64] = { 0 };
- if (!tb_environment_first("XMAKE_PROFILE", data, sizeof(data)) || tb_strcmp(data, "stuck"))
+ if (!tb_environment_first("XMAKE_PROFILE", data, sizeof(data)) || tb_strcmp(data, "stuck")) {
return;
+ }
g_lua = engine->lua;
#if defined(TB_CONFIG_OS_WINDOWS)
@@ -1253,14 +1265,15 @@ static tb_void_t xm_engine_init_signal(xm_engine_t *engine) {
// udata is unused, it has been used by engine. see xm_engine_bind_to_lua()
static tb_pointer_t xm_engine_lua_realloc(tb_pointer_t udata, tb_pointer_t data, size_t osize, size_t nsize) {
tb_pointer_t ptr = tb_null;
- if (nsize == 0 && data)
+ if (nsize == 0 && data) {
tb_free(data);
- else if (!data)
+ } else if (!data) {
ptr = tb_malloc((tb_size_t)nsize);
- else if (nsize != osize)
+ } else if (nsize != osize) {
ptr = tb_ralloc(data, (tb_size_t)nsize);
- else
+ } else {
ptr = data;
+ }
return ptr;
}
#endif
@@ -1293,8 +1306,9 @@ static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t *engine,
break;
}
- if (buffer_size == 0)
+ if (buffer_size == 0) {
break;
+ }
data += advance;
size -= advance;
@@ -1428,10 +1442,12 @@ static tb_bool_t xm_engine_load_main_script(xm_engine_t *engine, tb_char_t const
} while (0);
- if (data)
+ if (data) {
tb_free(data);
- if (file)
+ }
+ if (file) {
tb_file_exit(file);
+ }
return ok;
#else
if (luaL_dofile(engine->lua, mainfile)) {
@@ -1560,7 +1576,7 @@ xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_c
// init version string
tb_char_t version_cstr[256] = { 0 };
- if (tb_strcmp(XM_CONFIG_VERSION_BRANCH, "") && tb_strcmp(XM_CONFIG_VERSION_COMMIT, ""))
+ if (tb_strcmp(XM_CONFIG_VERSION_BRANCH, "") && tb_strcmp(XM_CONFIG_VERSION_COMMIT, "")) {
tb_snprintf(version_cstr,
sizeof(version_cstr),
"%u.%u.%u+%s.%s",
@@ -1569,14 +1585,15 @@ xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_c
version->alter,
XM_CONFIG_VERSION_BRANCH,
XM_CONFIG_VERSION_COMMIT);
- else
+ } else {
tb_snprintf(version_cstr,
sizeof(version_cstr),
"%u.%u.%u+%llu",
version->major,
version->minor,
version->alter,
- version->build);
+ (unsigned long long)version->build);
+ }
lua_pushstring(engine->lua, version_cstr);
lua_setglobal(engine->lua, "_VERSION");
@@ -1615,8 +1632,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_c
* we can get the lni modules for _lni or `import("lib.lni.xxx")` in sandbox
*/
lua_newtable(engine->lua);
- if (lni_initalizer)
+ if (lni_initalizer) {
lni_initalizer((xm_engine_ref_t)engine, engine->lua);
+ }
lua_setglobal(engine->lua, "_lni");
#ifdef TB_CONFIG_OS_WINDOWS
@@ -1626,8 +1644,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_c
DWORD mode;
if (GetConsoleMode(output, &mode)) {
// attempt to enable 0x4: ENABLE_VIRTUAL_TERMINAL_PROCESSING
- if (SetConsoleMode(output, mode | 0x4))
+ if (SetConsoleMode(output, mode | 0x4)) {
tb_environment_set("COLORTERM", "color256");
+ }
}
}
#endif
@@ -1636,8 +1655,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_c
} while (0);
if (!ok) {
- if (engine)
+ if (engine) {
xm_engine_exit((xm_engine_ref_t)engine);
+ }
engine = tb_null;
}
return (xm_engine_ref_t)engine;
@@ -1647,13 +1667,15 @@ tb_void_t xm_engine_exit(xm_engine_ref_t self) {
tb_assert_and_check_return(engine);
// exit lua
- if (engine->lua)
+ if (engine->lua) {
lua_close(engine->lua);
+ }
engine->lua = tb_null;
// exit poller
- if (engine->poller)
+ if (engine->poller) {
tb_poller_exit(engine->poller);
+ }
engine->poller = tb_null;
// exit it
@@ -1665,13 +1687,15 @@ tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t **argv, t
#if defined(TB_CONFIG_OS_WINDOWS) && defined(TB_COMPILER_IS_MSVC)
// set "stdin" to have unicode mode
- if (_isatty(_fileno(stdin)))
+ if (_isatty(_fileno(stdin))) {
_setmode(_fileno(stdin), _O_U16TEXT);
+ }
#endif
// save main arguments to the global variable: _ARGV
- if (!xm_engine_save_arguments(engine, argc, argv, taskargv))
+ if (!xm_engine_save_arguments(engine, argc, argv, taskargv)) {
return -1;
+ }
// get the project directory
tb_char_t path[TB_PATH_MAXN] = { 0 };
diff --git a/core/src/xmake/io/file_close.c b/core/src/xmake/io/file_close.c
index 6547b8156..cbcb237af 100644
--- a/core/src/xmake/io/file_close.c
+++ b/core/src/xmake/io/file_close.c
@@ -72,13 +72,15 @@ tb_int_t xm_io_file_close(lua_State *lua) {
file->u.file_ref = tb_null;
// exit fstream
- if (file->fstream)
+ if (file->fstream) {
tb_stream_exit(file->fstream);
+ }
file->fstream = tb_null;
// exit stream
- if (file->stream)
+ if (file->stream) {
tb_stream_exit(file->stream);
+ }
file->stream = tb_null;
// exit the line cache buffer
diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c
index e5776a60e..73652ce20 100644
--- a/core/src/xmake/io/file_flush.c
+++ b/core/src/xmake/io/file_flush.c
@@ -63,8 +63,9 @@ tb_int_t xm_io_file_flush(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is user data?
- if (!lua_isuserdata(lua, 1))
+ 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);
@@ -75,6 +76,7 @@ tb_int_t xm_io_file_flush(lua_State *lua) {
if (ok) {
lua_pushboolean(lua, tb_true);
return 1;
- } else
+ } else {
xm_io_return_error(lua, "failed to flush file");
+ }
}
diff --git a/core/src/xmake/io/file_isatty.c b/core/src/xmake/io/file_isatty.c
index 264cabd53..4f6bf4272 100644
--- a/core/src/xmake/io/file_isatty.c
+++ b/core/src/xmake/io/file_isatty.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_file_isatty(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is user data?
- if (!lua_isuserdata(lua, 1))
+ 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);
diff --git a/core/src/xmake/io/file_open.c b/core/src/xmake/io/file_open.c
index f4a080077..901a3f86d 100644
--- a/core/src/xmake/io/file_open.c
+++ b/core/src/xmake/io/file_open.c
@@ -50,8 +50,9 @@ static tb_size_t xm_io_file_detect_charset(tb_byte_t const **data_ptr, tb_long_t
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')
+ 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) {
@@ -80,34 +81,39 @@ static tb_size_t xm_io_file_detect_charset(tb_byte_t const **data_ptr, tb_long_t
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)
+ if (data[i] == 0) {
zero_count++;
+ }
- if (data[i] < 0x80)
+ if (data[i] < 0x80) {
ascii_conf++;
- else
+ } else {
ascii_conf = TB_MINS16;
+ }
if (i % 2 == 0) {
- if (data[i] == 0)
+ if (data[i] == 0) {
utf16be_conf++;
- if (data[i + 1] == 0)
+ }
+ if (data[i + 1] == 0) {
utf16le_conf++;
+ }
}
- if (IS_UTF8_TAIL(data[i]))
- ;
- else if (data[i] < 0x80)
+ if (IS_UTF8_TAIL(data[i])) {
+ // continue
+ } else if (data[i] < 0x80) {
utf8_conf++;
- else if (data[i] >= 0xc0 && data[i] < 0xe0 && IS_UTF8_TAIL(data[i + 1]))
+ } 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]))
+ } 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]))
+ } 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
+ } else {
utf8_conf = TB_MINS16;
+ }
}
if (ascii_conf > 0 && zero_count <= 1) {
@@ -188,44 +194,46 @@ tb_int_t xm_io_file_open(lua_State *lua) {
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'))
+ 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"))
+ } 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"))
+ } 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"))
+ } 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"))
+ } 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"))
+ } else if (tb_strstr(modestr, "ansi")) {
encoding = TB_CHARSET_TYPE_ANSI;
- else if (tb_strstr(modestr, "gbk"))
+ } else if (tb_strstr(modestr, "gbk")) {
encoding = TB_CHARSET_TYPE_GBK;
- else if (tb_strstr(modestr, "gb2312"))
+ } else if (tb_strstr(modestr, "gb2312")) {
encoding = TB_CHARSET_TYPE_GB2312;
- else if (tb_strstr(modestr, "iso8859"))
+ } 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
+ } 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
- {
+ } 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))
+ if (stream && tb_stream_open(stream)) {
encoding = xm_io_file_detect_encoding(stream, &bomoff);
- else {
- if (stream)
+ } else {
+ if (stream) {
tb_stream_exit(stream);
+ }
xm_io_return_error(lua, "file not found!");
}
- } else
+ } 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"))
+ if (tb_strstr(modestr, "bom")) {
utfbom = tb_true;
+ }
// open file
tb_bool_t open_ok = tb_false;
@@ -239,24 +247,28 @@ tb_int_t xm_io_file_open(lua_State *lua) {
// 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')
+ if (modestr[0] == 'r') {
fstream = tb_stream_init_filter_from_charset(stream, encoding, TB_CHARSET_TYPE_UTF8);
- else
+ } 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
+ } else {
file_ref = stream;
+ }
// open file stream
- if (!tb_stream_open(file_ref))
+ if (!tb_stream_open(file_ref)) {
break;
+ }
// skip bom characters if exists
- if (bomoff > 0 && !tb_stream_seek(stream, bomoff))
+ if (bomoff > 0 && !tb_stream_seek(stream, bomoff)) {
break;
+ }
open_ok = tb_true;
@@ -265,13 +277,15 @@ tb_int_t xm_io_file_open(lua_State *lua) {
// open failed?
if (!open_ok) {
// exit stream
- if (stream)
+ if (stream) {
tb_stream_exit(stream);
+ }
stream = tb_null;
// exit charset stream filter
- if (fstream)
+ if (fstream) {
tb_stream_exit(fstream);
+ }
fstream = tb_null;
// return errors
diff --git a/core/src/xmake/io/file_rawfd.c b/core/src/xmake/io/file_rawfd.c
index c94dc4a98..419955118 100644
--- a/core/src/xmake/io/file_rawfd.c
+++ b/core/src/xmake/io/file_rawfd.c
@@ -55,8 +55,9 @@ tb_int_t xm_io_file_rawfd(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is user data?
- if (!lua_isuserdata(lua, 1))
+ 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);
diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c
index 2be05a05a..0b255f4c1 100644
--- a/core/src/xmake/io/file_read.c
+++ b/core/src/xmake/io/file_read.c
@@ -79,10 +79,11 @@ static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_re
}
tb_size_t linesize = tb_buffer_size(line);
- if (linesize)
+ if (linesize) {
return linesize;
- else
+ } 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,
@@ -122,9 +123,9 @@ static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf,
tb_assert_and_check_break(data);
// no lf found
- if (size > 0 && data[size - 1] != '\n')
+ if (size > 0 && data[size - 1] != '\n') {
result = PL_FIN;
- else if (size > 1) {
+ } else if (size > 1) {
// crlf? => lf
if (!is_binary && data[size - 2] == '\r') {
data[size - 2] = '\n';
@@ -137,27 +138,31 @@ static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf,
0;
// do not keep crlf, strip the last lf
- if (!keep_crlf && !has_conline)
+ if (!keep_crlf && !has_conline) {
size--;
+ }
// strip it if has continuation?
- if (has_conline)
+ if (has_conline) {
size -= conlen + 1;
+ }
data[size] = '\0';
result = has_conline ? PL_CONL : PL_FIN;
} else {
// a single '\n'
- if (!keep_crlf)
+ if (!keep_crlf) {
size = 0;
+ }
result = PL_FIN;
}
} while (0);
// push line data
- if (data && size > 0 && (result == PL_FIN || result == PL_CONL))
+ if (data && size > 0 && (result == PL_FIN || result == PL_CONL)) {
tb_buffer_memncat(buf, (tb_byte_t const *)data, size);
+ }
// return result
return result;
@@ -167,8 +172,9 @@ static tb_int_t xm_io_file_read_all_directly(lua_State *lua, xm_io_file_t *file)
// init buffer
tb_buffer_t buf;
- if (!tb_buffer_init(&buf))
+ if (!tb_buffer_init(&buf)) {
xm_io_return_error(lua, "init buffer failed!");
+ }
tb_byte_t *data = tb_buffer_resize(&file->rcache, XM_IO_BLOCK_MAXN);
if (!data) {
@@ -180,19 +186,21 @@ static tb_int_t xm_io_file_read_all_directly(lua_State *lua, xm_io_file_t *file)
tb_stream_ref_t stream = file->u.file_ref;
while (!tb_stream_beof(stream)) {
tb_long_t real = tb_stream_read(stream, data, XM_IO_BLOCK_MAXN);
- if (real > 0)
+ if (real > 0) {
tb_buffer_memncat(&buf, data, real);
- else if (!real) {
+ } else if (!real) {
real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, -1);
tb_check_break(real > 0);
- } else
+ } else {
break;
+ }
}
- if (tb_buffer_size(&buf))
+ if (tb_buffer_size(&buf)) {
lua_pushlstring(lua, (tb_char_t const *)tb_buffer_data(&buf), tb_buffer_size(&buf));
- else
+ } else {
lua_pushliteral(lua, "");
+ }
tb_buffer_exit(&buf);
return 1;
}
@@ -201,23 +209,26 @@ static tb_int_t xm_io_file_read_all(lua_State *lua, xm_io_file_t *file, tb_char_
// is binary? read all directly
tb_bool_t is_binary = file->encoding == XM_IO_FILE_ENCODING_BINARY;
- if (is_binary)
+ if (is_binary) {
return xm_io_file_read_all_directly(lua, file);
+ }
// init buffer
tb_buffer_t buf;
- if (!tb_buffer_init(&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)
+ if (!has_content) {
lua_pushliteral(lua, "");
- else
+ } 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:
@@ -241,18 +252,20 @@ static tb_int_t xm_io_file_read_line(lua_State *lua,
// init buffer
tb_buffer_t buf;
- if (!tb_buffer_init(&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)
+ if (!has_content) {
lua_pushnil(lua);
- else
+ } 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:
@@ -275,12 +288,14 @@ static tb_int_t xm_io_file_read_n(lua_State *lua, xm_io_file_t *file, tb_char_t
tb_assert(lua && file && continuation && xm_io_file_is_file(file) && file->u.file_ref);
// check continuation
- if (*continuation != '\0')
+ 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)
+ 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) {
@@ -298,8 +313,9 @@ static tb_int_t xm_io_file_read_n(lua_State *lua, xm_io_file_t *file, tb_char_t
}
}
}
- if (!ok)
+ if (!ok) {
lua_pushnil(lua);
+ }
return 1;
}
@@ -313,10 +329,11 @@ static tb_size_t xm_io_file_std_buffer_pushline(tb_buffer_ref_t buf,
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))
+ if (tb_stdfile_gets(file->u.std_ref, strbuf, tb_arrayn(strbuf) - 1)) {
buflen = tb_strlen(strbuf);
- else
+ } else {
return PL_EOF;
+ }
tb_size_t conlen = tb_strlen(continuation);
if (buflen > 0 && strbuf[buflen - 1] != '\n') {
@@ -334,24 +351,28 @@ static tb_size_t xm_io_file_std_buffer_pushline(tb_buffer_ref_t buf,
tb_strncmp(continuation, (strbuf + buflen - conlen - 1), conlen) == 0;
// do not keep crlf, strip the last lf
- if (!keep_crlf && !has_conline)
+ if (!keep_crlf && !has_conline) {
buflen--;
+ }
// strip it if has continuation?
- if (has_conline)
+ if (has_conline) {
buflen -= conlen + 1;
+ }
strbuf[buflen] = '\0';
result = has_conline ? PL_CONL : PL_FIN;
} else {
// a single '\n'
- if (!keep_crlf)
+ if (!keep_crlf) {
buflen = 0;
+ }
result = PL_FIN;
}
- if (result == PL_FIN || result == PL_CONL)
+ if (result == PL_FIN || result == PL_CONL) {
tb_buffer_memncat(buf, (tb_byte_t const *)strbuf, buflen);
+ }
return result;
}
@@ -363,18 +384,20 @@ static tb_int_t xm_io_file_std_read_line(lua_State *lua,
// init buffer
tb_buffer_t buf;
- if (!tb_buffer_init(&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)
+ if (!has_content) {
lua_pushnil(lua);
- else
+ } 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:
diff --git a/core/src/xmake/io/file_readable.c b/core/src/xmake/io/file_readable.c
index 5f9a84329..78ea82888 100644
--- a/core/src/xmake/io/file_readable.c
+++ b/core/src/xmake/io/file_readable.c
@@ -38,8 +38,9 @@ tb_int_t xm_io_file_readable(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is user data?
- if (!lua_isuserdata(lua, 1))
+ 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);
@@ -47,10 +48,11 @@ tb_int_t xm_io_file_readable(lua_State *lua) {
// has readable data?
tb_bool_t ok = tb_false;
- if (xm_io_file_is_file(file))
+ if (xm_io_file_is_file(file)) {
ok = tb_stream_left(file->u.file_ref) > 0;
- else
+ } else {
ok = tb_stdfile_readable(file->u.std_ref);
+ }
lua_pushboolean(lua, ok);
return 1;
}
diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c
index 421e5cdf1..c7c043e7a 100644
--- a/core/src/xmake/io/file_seek.c
+++ b/core/src/xmake/io/file_seek.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_file_seek(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is user data?
- if (!lua_isuserdata(lua, 1))
+ 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);
@@ -60,10 +61,11 @@ tb_int_t xm_io_file_seek(lua_State *lua) {
case 'e': // "end"
{
tb_hong_t size = tb_stream_size(file->u.file_ref);
- if (size > 0 && size + offset <= size)
+ if (size > 0 && size + offset <= size) {
offset = size + offset;
- else
+ } else {
xm_io_return_error(lua, "seek failed, invalid offset!");
+ }
} break;
default: // "cur"
offset = tb_stream_offset(file->u.file_ref) + offset;
@@ -73,8 +75,10 @@ tb_int_t xm_io_file_seek(lua_State *lua) {
if (tb_stream_seek(file->u.file_ref, offset)) {
lua_pushnumber(lua, (lua_Number)offset);
return 1;
- } else
+ } else {
xm_io_return_error(lua, "seek failed!");
- } else
+ }
+ } else {
xm_io_return_error(lua, "seek is not supported on this file");
+ }
}
diff --git a/core/src/xmake/io/file_size.c b/core/src/xmake/io/file_size.c
index 34f519306..351bd7ebf 100644
--- a/core/src/xmake/io/file_size.c
+++ b/core/src/xmake/io/file_size.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_file_size(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is user data?
- if (!lua_isuserdata(lua, 1))
+ 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);
@@ -52,6 +53,7 @@ tb_int_t xm_io_file_size(lua_State *lua) {
tb_assert(file->stream);
lua_pushnumber(lua, (lua_Number)tb_stream_size(file->stream));
return 1;
- } else
+ } else {
xm_io_return_error(lua, "get size for invalid file!");
+ }
}
diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c
index 5f70e1a49..08655379a 100644
--- a/core/src/xmake/io/file_write.c
+++ b/core/src/xmake/io/file_write.c
@@ -137,21 +137,23 @@ tb_int_t xm_io_file_write(lua_State *lua) {
// get data
size_t datasize = 0;
tb_byte_t const *data = tb_null;
- if (lua_isstring(lua, i))
+ if (lua_isstring(lua, i)) {
data = (tb_byte_t const *)luaL_checklstring(lua, i, &datasize);
- else if (lua_istable(lua, i)) {
+ } else if (lua_istable(lua, i)) {
// get bytes data
lua_pushstring(lua, "data");
lua_gettable(lua, i);
- if (xm_lua_isinteger(lua, -1))
+ 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))
+ if (xm_lua_isinteger(lua, -1)) {
datasize = (tb_size_t)lua_tointeger(lua, -1);
+ }
lua_pop(lua, 1);
// mark as binary data
@@ -162,11 +164,11 @@ tb_int_t xm_io_file_write(lua_State *lua) {
tb_assert_and_check_break(data);
// write data to std or file
- if (xm_io_file_is_std(file))
+ if (xm_io_file_is_std(file)) {
xm_io_file_write_std(file, data, (tb_size_t)datasize);
- else if (is_binary)
+ } else if (is_binary) {
xm_io_file_write_file_directly(file, data, (tb_size_t)datasize);
- else {
+ } else {
// write utf bom first?
if (file->utfbom) {
xm_io_file_write_file_utfbom(file);
diff --git a/core/src/xmake/io/filelock_close.c b/core/src/xmake/io/filelock_close.c
index 02a2fb039..69bea4e39 100644
--- a/core/src/xmake/io/filelock_close.c
+++ b/core/src/xmake/io/filelock_close.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_filelock_close(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// check lock?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/filelock_lock.c b/core/src/xmake/io/filelock_lock.c
index 9e0c2493e..070af2eaf 100644
--- a/core/src/xmake/io/filelock_lock.c
+++ b/core/src/xmake/io/filelock_lock.c
@@ -53,8 +53,9 @@ tb_int_t xm_io_filelock_lock(lua_State *lua) {
}
// check lock?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/filelock_open.c b/core/src/xmake/io/filelock_open.c
index e9687ba54..2afb173d3 100644
--- a/core/src/xmake/io/filelock_open.c
+++ b/core/src/xmake/io/filelock_open.c
@@ -47,13 +47,15 @@ tb_int_t xm_io_filelock_open(lua_State *lua) {
// init file lock
tb_long_t tryn = 2;
tb_filelock_ref_t lock = tb_null;
- while (!lock && tryn-- > 0)
+ 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)
+ }
+ if (lock) {
xm_lua_pushpointer(lua, (tb_pointer_t)lock);
- else
+ } else {
lua_pushnil(lua);
+ }
return 1;
}
diff --git a/core/src/xmake/io/filelock_trylock.c b/core/src/xmake/io/filelock_trylock.c
index aaf1da246..d84024834 100644
--- a/core/src/xmake/io/filelock_trylock.c
+++ b/core/src/xmake/io/filelock_trylock.c
@@ -53,8 +53,9 @@ tb_int_t xm_io_filelock_trylock(lua_State *lua) {
}
// check lock?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/filelock_unlock.c b/core/src/xmake/io/filelock_unlock.c
index 94dfe1b7d..007bedd54 100644
--- a/core/src/xmake/io/filelock_unlock.c
+++ b/core/src/xmake/io/filelock_unlock.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_filelock_unlock(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// check lock?
- if (!xm_lua_topointer(lua, 1))
+ if (!xm_lua_topointer(lua, 1)) {
return 0;
+ }
// get lock
tb_filelock_ref_t lock = (tb_filelock_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/iscygpty.c b/core/src/xmake/io/iscygpty.c
index e78a9d8cb..656e86fe7 100644
--- a/core/src/xmake/io/iscygpty.c
+++ b/core/src/xmake/io/iscygpty.c
@@ -131,8 +131,9 @@ int is_cygpty(HANDLE h) {
p = NULL;
}
if (p != NULL) {
- while (*p && isxdigit(*p)) /* Skip 16-digit hexadecimal. */
+ while (*p && isxdigit(*p)) { /* Skip 16-digit hexadecimal. */
++p;
+ }
if (is_wprefix(p, L"-pty")) {
p += 4;
} else {
@@ -140,8 +141,9 @@ int is_cygpty(HANDLE h) {
}
}
if (p != NULL) {
- while (*p && isdigit(*p)) /* Skip pty number. */
+ 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")) {
diff --git a/core/src/xmake/io/pipe_close.c b/core/src/xmake/io/pipe_close.c
index 23e200606..aaf708a31 100644
--- a/core/src/xmake/io/pipe_close.c
+++ b/core/src/xmake/io/pipe_close.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_pipe_close(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// check pipe?
- if (!xm_pipe_file_is_valid(lua, 1))
+ if (!xm_pipe_file_is_valid(lua, 1)) {
return 0;
+ }
// get the pipe file
tb_pipe_file_ref_t pipefile = xm_pipe_file_get(lua, 1);
diff --git a/core/src/xmake/io/pipe_open.c b/core/src/xmake/io/pipe_open.c
index d82406dee..6bfe26347 100644
--- a/core/src/xmake/io/pipe_open.c
+++ b/core/src/xmake/io/pipe_open.c
@@ -47,21 +47,24 @@ tb_int_t xm_io_pipe_open(lua_State *lua) {
// get pipe mode value
tb_size_t mode = TB_PIPE_MODE_RO;
- if (modestr[0] == 'w')
+ if (modestr[0] == 'w') {
mode = TB_PIPE_MODE_WO;
+ }
// set block mode
- if (modestr[1] == 'B')
+ 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)
+ if (pipefile) {
xm_lua_pushpointer(lua, (tb_pointer_t)pipefile);
- else
+ } else {
lua_pushnil(lua);
+ }
return 1;
}
diff --git a/core/src/xmake/io/pipe_openpair.c b/core/src/xmake/io/pipe_openpair.c
index dd2205613..bf4829f6c 100644
--- a/core/src/xmake/io/pipe_openpair.c
+++ b/core/src/xmake/io/pipe_openpair.c
@@ -46,10 +46,12 @@ tb_int_t xm_io_pipe_openpair(lua_State *lua) {
// init mode
tb_size_t mode[2] = { 0 };
- if (modestr[0] == 'B')
+ if (modestr[0] == 'B') {
mode[0] |= TB_PIPE_MODE_BLOCK;
- if (modestr[1] == 'B')
+ }
+ if (modestr[1] == 'B') {
mode[1] |= TB_PIPE_MODE_BLOCK;
+ }
// get buffer size
tb_size_t buffsize = (tb_size_t)luaL_checknumber(lua, 2);
diff --git a/core/src/xmake/io/pipe_read.c b/core/src/xmake/io/pipe_read.c
index 637fb3b7d..e0e383bfb 100644
--- a/core/src/xmake/io/pipe_read.c
+++ b/core/src/xmake/io/pipe_read.c
@@ -51,8 +51,9 @@ tb_int_t xm_io_pipe_read(lua_State *lua) {
// get data
tb_byte_t *data = tb_null;
- if (xm_lua_isinteger(lua, 2))
+ 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);
@@ -62,8 +63,9 @@ tb_int_t xm_io_pipe_read(lua_State *lua) {
// get size
tb_long_t size = 0;
- if (xm_lua_isinteger(lua, 3))
+ 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);
diff --git a/core/src/xmake/io/pipe_wait.c b/core/src/xmake/io/pipe_wait.c
index 785efe950..d2979dcdd 100644
--- a/core/src/xmake/io/pipe_wait.c
+++ b/core/src/xmake/io/pipe_wait.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_pipe_wait(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// check pipe?
- if (!xm_pipe_file_is_valid(lua, 1))
+ if (!xm_pipe_file_is_valid(lua, 1)) {
return 0;
+ }
// get pipe file
tb_pipe_file_ref_t pipefile = xm_pipe_file_get(lua, 1);
diff --git a/core/src/xmake/io/pipe_write.c b/core/src/xmake/io/pipe_write.c
index 2f2865c47..d3098dd60 100644
--- a/core/src/xmake/io/pipe_write.c
+++ b/core/src/xmake/io/pipe_write.c
@@ -52,10 +52,12 @@ tb_int_t xm_io_pipe_write(lua_State *lua) {
// get data and size
tb_size_t size = 0;
tb_byte_t const *data = tb_null;
- if (xm_lua_isinteger(lua, 2))
+ 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))
+ }
+ 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);
diff --git a/core/src/xmake/io/poller.c b/core/src/xmake/io/poller.c
index e4c7cf494..6e155e4a8 100644
--- a/core/src/xmake/io/poller.c
+++ b/core/src/xmake/io/poller.c
@@ -37,8 +37,9 @@
tb_poller_ref_t xm_io_poller(lua_State *lua) {
tb_poller_ref_t poller = tb_null;
xm_engine_ref_t engine = xm_engine_get(lua);
- if (engine)
+ if (engine) {
poller = xm_engine_poller(engine);
+ }
tb_assert(poller);
return poller;
}
diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c
index f1e4ed7a7..dc1418a03 100644
--- a/core/src/xmake/io/poller_wait.c
+++ b/core/src/xmake/io/poller_wait.c
@@ -46,10 +46,11 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller,
lua_newtable(lua);
lua_pushinteger(lua, (tb_int_t)object->type);
lua_rawseti(lua, -2, 1);
- if (priv)
+ if (priv) {
lua_pushstring(lua, (tb_char_t const *)priv);
- else
+ } else {
lua_pushlightuserdata(lua, object->ref.ptr);
+ }
lua_rawseti(lua, -2, 2);
if (object->type == TB_POLLER_OBJECT_FWATCHER) {
lua_newtable(lua);
@@ -63,8 +64,9 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller,
lua_pushinteger(lua, event->event);
lua_settable(lua, -3);
}
- } else
+ } else {
lua_pushinteger(lua, (tb_int_t)events);
+ }
lua_rawseti(lua, -2, 3);
lua_rawseti(lua, -2, ++state->events_count);
}
diff --git a/core/src/xmake/io/socket_accept.c b/core/src/xmake/io/socket_accept.c
index 77a6c6c86..5aed6d263 100644
--- a/core/src/xmake/io/socket_accept.c
+++ b/core/src/xmake/io/socket_accept.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_socket_accept(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is pointer?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
@@ -48,9 +49,10 @@ tb_int_t xm_io_socket_accept(lua_State *lua) {
// accept socket
tb_socket_ref_t client = tb_socket_accept(sock, tb_null);
- if (client)
+ if (client) {
xm_lua_pushpointer(lua, (tb_pointer_t)client);
- else
+ } else {
lua_pushnil(lua);
+ }
return 1;
}
diff --git a/core/src/xmake/io/socket_close.c b/core/src/xmake/io/socket_close.c
index 922987c10..401ffd7b6 100644
--- a/core/src/xmake/io/socket_close.c
+++ b/core/src/xmake/io/socket_close.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_socket_close(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is pointer?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/socket_kill.c b/core/src/xmake/io/socket_kill.c
index 63e110257..33d36ef18 100644
--- a/core/src/xmake/io/socket_kill.c
+++ b/core/src/xmake/io/socket_kill.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_socket_kill(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is pointer?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/socket_listen.c b/core/src/xmake/io/socket_listen.c
index 3aa541016..14971e589 100644
--- a/core/src/xmake/io/socket_listen.c
+++ b/core/src/xmake/io/socket_listen.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_socket_listen(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is pointer?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/socket_open.c b/core/src/xmake/io/socket_open.c
index cae80d182..2ed04ca97 100644
--- a/core/src/xmake/io/socket_open.c
+++ b/core/src/xmake/io/socket_open.c
@@ -61,9 +61,10 @@ tb_int_t xm_io_socket_open(lua_State *lua) {
// init socket
tb_socket_ref_t sock = tb_socket_init(socktype, family);
- if (sock)
+ if (sock) {
xm_lua_pushpointer(lua, (tb_pointer_t)sock);
- else
+ } else {
lua_pushnil(lua);
+ }
return 1;
}
diff --git a/core/src/xmake/io/socket_peeraddr.c b/core/src/xmake/io/socket_peeraddr.c
index e1ad43931..686d46799 100644
--- a/core/src/xmake/io/socket_peeraddr.c
+++ b/core/src/xmake/io/socket_peeraddr.c
@@ -47,8 +47,9 @@ tb_int_t xm_io_socket_peeraddr(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// is pointer?
- if (!xm_lua_ispointer(lua, 1))
+ 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);
@@ -58,9 +59,10 @@ tb_int_t xm_io_socket_peeraddr(lua_State *lua) {
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))))
+ if (tb_socket_peer(sock, &addr) && (cstr = tb_ipaddr_cstr(&addr, data, sizeof(data)))) {
lua_pushstring(lua, cstr);
- else
+ } else {
lua_pushnil(lua);
+ }
return 1;
}
diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c
index ed629a8f5..3ccb9b461 100644
--- a/core/src/xmake/io/socket_recv.c
+++ b/core/src/xmake/io/socket_recv.c
@@ -51,8 +51,9 @@ tb_int_t xm_io_socket_recv(lua_State *lua) {
// get data
tb_byte_t *data = tb_null;
- if (xm_lua_isinteger(lua, 2))
+ 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);
@@ -62,8 +63,9 @@ tb_int_t xm_io_socket_recv(lua_State *lua) {
// get size
tb_long_t size = 0;
- if (xm_lua_isinteger(lua, 3))
+ 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);
diff --git a/core/src/xmake/io/socket_recvfrom.c b/core/src/xmake/io/socket_recvfrom.c
index 8601f6302..5e212bed8 100644
--- a/core/src/xmake/io/socket_recvfrom.c
+++ b/core/src/xmake/io/socket_recvfrom.c
@@ -51,8 +51,9 @@ tb_int_t xm_io_socket_recvfrom(lua_State *lua) {
// get data
tb_byte_t *data = tb_null;
- if (xm_lua_isinteger(lua, 2))
+ 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);
@@ -62,8 +63,9 @@ tb_int_t xm_io_socket_recvfrom(lua_State *lua) {
// get size
tb_long_t size = 0;
- if (xm_lua_isinteger(lua, 3))
+ 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);
diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c
index d83b02add..3a11d0d43 100644
--- a/core/src/xmake/io/socket_send.c
+++ b/core/src/xmake/io/socket_send.c
@@ -52,10 +52,12 @@ tb_int_t xm_io_socket_send(lua_State *lua) {
// get data and size
tb_size_t size = 0;
tb_byte_t const *data = tb_null;
- if (xm_lua_isinteger(lua, 2))
+ 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))
+ }
+ 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);
diff --git a/core/src/xmake/io/socket_sendfile.c b/core/src/xmake/io/socket_sendfile.c
index 0d4be4219..e12e574d7 100644
--- a/core/src/xmake/io/socket_sendfile.c
+++ b/core/src/xmake/io/socket_sendfile.c
@@ -85,8 +85,9 @@ tb_int_t xm_io_socket_sendfile(lua_State *lua) {
// get start
tb_long_t start = 1;
- if (lua_isnumber(lua, 3))
+ 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);
@@ -95,8 +96,9 @@ tb_int_t xm_io_socket_sendfile(lua_State *lua) {
// get last
tb_long_t last = (tb_long_t)filesize;
- if (lua_isnumber(lua, 4))
+ 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);
diff --git a/core/src/xmake/io/socket_sendto.c b/core/src/xmake/io/socket_sendto.c
index 15403d4d8..ccbbb1cc2 100644
--- a/core/src/xmake/io/socket_sendto.c
+++ b/core/src/xmake/io/socket_sendto.c
@@ -52,10 +52,12 @@ tb_int_t xm_io_socket_sendto(lua_State *lua) {
// get data and size
tb_size_t size = 0;
tb_byte_t const *data = tb_null;
- if (xm_lua_isinteger(lua, 2))
+ 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))
+ }
+ 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);
diff --git a/core/src/xmake/io/socket_wait.c b/core/src/xmake/io/socket_wait.c
index 42674f4f9..409d69c91 100644
--- a/core/src/xmake/io/socket_wait.c
+++ b/core/src/xmake/io/socket_wait.c
@@ -39,8 +39,9 @@ tb_int_t xm_io_socket_wait(lua_State *lua) {
tb_assert_and_check_return_val(lua, 0);
// check socket?
- if (!xm_lua_ispointer(lua, 1))
+ if (!xm_lua_ispointer(lua, 1)) {
return 0;
+ }
// get socket
tb_socket_ref_t sock = (tb_socket_ref_t)xm_lua_topointer(lua, 1);
diff --git a/core/src/xmake/io/stdfile.c b/core/src/xmake/io/stdfile.c
index 0dcb43501..ac77b272d 100644
--- a/core/src/xmake/io/stdfile.c
+++ b/core/src/xmake/io/stdfile.c
@@ -71,8 +71,9 @@ static tb_size_t xm_io_stdfile_isatty(tb_size_t type) {
/* 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)
+ if (!answer && type != XM_IO_FILE_TYPE_STDIN) {
answer = is_cygpty(console_handle);
+ }
#else
switch (type) {
case XM_IO_FILE_TYPE_STDIN:
@@ -87,8 +88,9 @@ static tb_size_t xm_io_stdfile_isatty(tb_size_t type) {
}
#endif
- if (answer)
+ if (answer) {
type |= XM_IO_FILE_FLAG_TTY;
+ }
return type;
}
@@ -97,8 +99,9 @@ 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)
+ if (fstat(fileno(stdout), &stats) != -1) {
size = stats.st_blksize;
+ }
setvbuf(stdout, tb_null, _IOLBF, size);
#endif
}
@@ -154,8 +157,9 @@ tb_int_t xm_io_stdfile(lua_State *lua) {
* @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)
+ if (file) {
return 1;
- else
+ } else {
xm_io_return_error(lua, "invalid stdfile type!");
+ }
}
diff --git a/core/src/xmake/xmake.c b/core/src/xmake/xmake.c
index 8d4a77284..c673d776a 100644
--- a/core/src/xmake/xmake.c
+++ b/core/src/xmake/xmake.c
@@ -77,8 +77,9 @@ tb_bool_t xm_init_(tb_size_t mode, tb_hize_t build) {
tb_trace_d("init: ..");
// check mode
- if (!xm_check_mode(mode))
+ if (!xm_check_mode(mode)) {
return tb_false;
+ }
// check version
xm_version_check(build);
@@ -88,8 +89,9 @@ tb_bool_t xm_init_(tb_size_t mode, tb_hize_t build) {
if (!tb_init(tb_null, tb_default_allocator(tb_null, 0))) return tb_false;
#else
// init tbox, since small compilation mode is enabled, it still uses the native allocator
- if (!tb_init(tb_null, tb_null))
+ if (!tb_init(tb_null, tb_null)) {
return tb_false;
+ }
#endif
tb_trace_d("init: ok");