diff options
| author | ruki <[email protected]> | 2025-11-10 06:35:04 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-11-10 06:35:04 +0800 |
| commit | 2b01e46fcf619aaecb8435b0fac975e3aff0587f (patch) | |
| tree | 8f005a8dfc3272e9f027560e5e1b5881a7a8bc70 | |
| parent | a1b0ec856e2c22aac84022feb05b6687912e3d6e (diff) | |
| parent | 2c387895657ae13cd717d7f42f5baed5eb028a8e (diff) | |
Merge pull request #7008 from xmake-io/format
format code
387 files changed, 5157 insertions, 5966 deletions
diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..8f5c54331 --- /dev/null +++ b/.clang-format @@ -0,0 +1,92 @@ +--- +Language: Cpp +BasedOnStyle: LLVM +DisableFormat: false + +# Indentation +IndentWidth: 4 +ContinuationIndentWidth: 4 +TabWidth: 4 +UseTab: Never + +# Braces +BreakBeforeBraces: Attach +BraceWrapping: + AfterControlStatement: false + AfterFunction: false + AfterEnum: false + AfterClass: false + AfterStruct: false + AfterUnion: false + BeforeElse: false + BeforeCatch: false + BeforeLambdaBody: false + +# Alignment +AlignAfterOpenBracket: Align +AlignTrailingComments: true +AlignConsecutiveAssignments: Consecutive +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: Consecutive +AlignConsecutiveMacros: None + +# Spacing +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpacesBeforeTrailingComments: 1 + +# Column limit +ColumnLimit: 120 + +# Short constructs +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false + +# Line breaking +AlwaysBreakAfterReturnType: None +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true + +# Functions +IndentWrappedFunctionNames: false +MaxEmptyLinesToKeep: 1 + +# Includes +IncludeBlocks: Preserve +SortIncludes: Never + +# Initializers +AllowAllConstructorInitializersOnNextLine: true +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +Cpp11BracedListStyle: false +BinPackArguments: false +BinPackParameters: false + +# Preprocessor/Macros +IndentPPDirectives: None + +# Misc +KeepEmptyLinesAtTheStartOfBlocks: false +ReflowComments: false +SeparateDefinitionBlocks: Leave + +# Penalties +PenaltyBreakAssignment: 100 +PenaltyBreakBeforeFirstCallParameter: 100 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 1000 +PenaltyBreakTemplateDeclaration: 1000 + +--- +Language: ObjC +BasedOnStyle: LLVM +DisableFormat: true diff --git a/core/.clang-format b/core/.clang-format deleted file mode 100644 index 92782d2e0..000000000 --- a/core/.clang-format +++ /dev/null @@ -1,111 +0,0 @@ ---- -Language: Cpp -# BasedOnStyle: LLVM -AccessModifierOffset: -2 -AlignAfterOpenBracket: Align -AlignConsecutiveAssignments: true -AlignConsecutiveDeclarations: true -AlignEscapedNewlines: Right -AlignOperands: true -AlignTrailingComments: true -AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: false -AllowShortCaseLabelsOnASingleLine: true -AllowShortFunctionsOnASingleLine: All -AllowShortIfStatementsOnASingleLine: true -AllowShortLoopsOnASingleLine: false -AlwaysBreakAfterDefinitionReturnType: None -AlwaysBreakAfterReturnType: None -AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: false -BinPackArguments: true -BinPackParameters: true -BraceWrapping: - AfterClass: false - AfterControlStatement: false - AfterEnum: false - AfterFunction: false - AfterNamespace: false - AfterObjCDeclaration: false - AfterStruct: false - AfterUnion: false - AfterExternBlock: false - BeforeCatch: false - BeforeElse: false - IndentBraces: false - SplitEmptyFunction: true - SplitEmptyRecord: true - SplitEmptyNamespace: true -BreakBeforeBinaryOperators: None -BreakBeforeBraces: Allman -BreakBeforeInheritanceComma: false -BreakBeforeTernaryOperators: true -BreakConstructorInitializersBeforeComma: false -BreakConstructorInitializers: BeforeColon -BreakAfterJavaFieldAnnotations: false -BreakStringLiterals: true -ColumnLimit: 120 -CommentPragmas: '^ IWYU pragma:' -CompactNamespaces: false -ConstructorInitializerAllOnOneLineOrOnePerLine: false -ConstructorInitializerIndentWidth: 4 -ContinuationIndentWidth: 4 -Cpp11BracedListStyle: true -DerivePointerAlignment: false -DisableFormat: false -ExperimentalAutoDetectBinPacking: false -FixNamespaceComments: true -ForEachMacros: - - foreach - - Q_FOREACH - - BOOST_FOREACH -IncludeBlocks: Preserve -IncludeCategories: - - Regex: '^"(llvm|llvm-c|clang|clang-c)/' - Priority: 2 - - Regex: '^(<|"(gtest|gmock|isl|json)/)' - Priority: 3 - - Regex: '.*' - Priority: 1 -IncludeIsMainRegex: '(Test)?$' -IndentCaseLabels: false -IndentPPDirectives: AfterHash -IndentWidth: 4 -IndentWrappedFunctionNames: false -JavaScriptQuotes: Leave -JavaScriptWrapImports: true -KeepEmptyLinesAtTheStartOfBlocks: true -MacroBlockBegin: '' -MacroBlockEnd: '' -MaxEmptyLinesToKeep: 1 -NamespaceIndentation: None -ObjCBlockIndentWidth: 2 -ObjCSpaceAfterProperty: false -ObjCSpaceBeforeProtocolList: true -PenaltyBreakAssignment: 2 -PenaltyBreakBeforeFirstCallParameter: 19 -PenaltyBreakComment: 300 -PenaltyBreakFirstLessLess: 120 -PenaltyBreakString: 1000 -PenaltyExcessCharacter: 1000000 -PenaltyReturnTypeOnItsOwnLine: 60 -PointerAlignment: Left -ReflowComments: true -SortIncludes: true -SortUsingDeclarations: true -SpaceAfterCStyleCast: false -SpaceAfterTemplateKeyword: true -SpaceBeforeAssignmentOperators: true -SpaceBeforeParens: ControlStatements -SpaceInEmptyParentheses: false -SpacesBeforeTrailingComments: 1 -SpacesInAngles: false -SpacesInContainerLiterals: true -SpacesInCStyleCastParentheses: false -SpacesInParentheses: false -SpacesInSquareBrackets: false -Standard: Cpp11 -TabWidth: 4 -UseTab: Never -... - diff --git a/core/src/xmake/base64/decode.c b/core/src/xmake/base64/decode.c index abb9abcbd..cb1b7eb82 100644 --- a/core/src/xmake/base64/decode.c +++ b/core/src/xmake/base64/decode.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "base64" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "base64" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,24 +33,20 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_base64_decode(lua_State* lua) -{ - // check +tb_int_t xm_base64_decode(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the string - size_t size = 0; - tb_char_t const* cstr = luaL_checklstring(lua, 1, &size); + size_t size = 0; + tb_char_t const *cstr = luaL_checklstring(lua, 1, &size); tb_check_return_val(cstr && size, 0); // decode it tb_byte_t buff[8192]; - if (size < sizeof(buff)) - { + if (size < sizeof(buff)) { tb_size_t real = tb_base64_decode(cstr, size, buff, sizeof(buff)); - if (real > 0) - { - lua_pushlstring(lua, (tb_char_t const*)buff, (tb_int_t)real); + if (real > 0) { + lua_pushlstring(lua, (tb_char_t const *)buff, (tb_int_t)real); return 1; } } diff --git a/core/src/xmake/base64/encode.c b/core/src/xmake/base64/encode.c index b99ee3922..d5ce56306 100644 --- a/core/src/xmake/base64/encode.c +++ b/core/src/xmake/base64/encode.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "base64" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "base64" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_base64_encode(lua_State* lua) -{ - // check +tb_int_t xm_base64_encode(lua_State *lua) { 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) - { + 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; @@ -53,11 +54,9 @@ tb_int_t xm_base64_encode(lua_State* lua) // encode it tb_char_t buff[8192]; - if (size * 3 / 2 < sizeof(buff)) - { + if (size * 3 / 2 < sizeof(buff)) { tb_size_t real = tb_base64_encode(data, size, buff, sizeof(buff)); - if (real > 0) - { + if (real > 0) { lua_pushlstring(lua, buff, (tb_int_t)real); return 1; } diff --git a/core/src/xmake/base64/prefix.h b/core/src/xmake/base64/prefix.h index a22be8e7c..4e7ba2bf3 100644 --- a/core/src/xmake/base64/prefix.h +++ b/core/src/xmake/base64/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/bloom_filter/bloom_filter_clear.c b/core/src/xmake/bloom_filter/bloom_filter_clear.c index c68419e7d..7693baa48 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_clear.c +++ b/core/src/xmake/bloom_filter/bloom_filter_clear.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_clear" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_clear" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_clear(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_clear(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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); @@ -51,4 +50,3 @@ tb_int_t xm_bloom_filter_clear(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/bloom_filter/bloom_filter_close.c b/core/src/xmake/bloom_filter/bloom_filter_close.c index fd1d3e206..ba47860a5 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_close.c +++ b/core/src/xmake/bloom_filter/bloom_filter_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_close(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); @@ -53,4 +52,3 @@ tb_int_t xm_bloom_filter_close(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/bloom_filter/bloom_filter_data.c b/core/src/xmake/bloom_filter/bloom_filter_data.c index 3911d5e08..d622a6f31 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_data.c +++ b/core/src/xmake/bloom_filter/bloom_filter_data.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_data" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_data" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_data(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_data(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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); @@ -48,8 +47,10 @@ tb_int_t xm_bloom_filter_data(lua_State* lua) // get data tb_pointer_t data = (tb_pointer_t)tb_bloom_filter_data(filter); - if (data) xm_lua_pushpointer(lua, data); - else lua_pushnil(lua); + if (data) { + xm_lua_pushpointer(lua, data); + } else { + lua_pushnil(lua); + } return 1; } - diff --git a/core/src/xmake/bloom_filter/bloom_filter_data_set.c b/core/src/xmake/bloom_filter/bloom_filter_data_set.c index d7b036289..6d2d48606 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_data_set.c +++ b/core/src/xmake/bloom_filter/bloom_filter_data_set.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_data_set" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_data_set" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_data_set(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_data_set(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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); @@ -48,11 +47,14 @@ tb_int_t xm_bloom_filter_data_set(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)) 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) - { + 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; @@ -64,4 +66,3 @@ tb_int_t xm_bloom_filter_data_set(lua_State* lua) lua_pushboolean(lua, ok); return 1; } - diff --git a/core/src/xmake/bloom_filter/bloom_filter_get.c b/core/src/xmake/bloom_filter/bloom_filter_get.c index 8ad695515..ffd1f82c7 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_get.c +++ b/core/src/xmake/bloom_filter/bloom_filter_get.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_get" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_get" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,20 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_get(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_get(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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(filter, 0); // get item - tb_char_t const* item = luaL_checkstring(lua, 2); + tb_char_t const *item = luaL_checkstring(lua, 2); tb_assert_and_check_return_val(item, 0); // get item @@ -55,5 +54,3 @@ tb_int_t xm_bloom_filter_get(lua_State* lua) lua_pushboolean(lua, ok); return 1; } - - diff --git a/core/src/xmake/bloom_filter/bloom_filter_open.c b/core/src/xmake/bloom_filter/bloom_filter_open.c index 65d8d4951..4cd79e73f 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_open.c +++ b/core/src/xmake/bloom_filter/bloom_filter_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_open(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_open(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get arguments tb_int_t probability = (tb_int_t)lua_tointeger(lua, 1); - tb_int_t hash_count = (tb_int_t)lua_tointeger(lua, 2); - tb_int_t item_maxn = (tb_int_t)lua_tointeger(lua, 3); - if (hash_count > 16 || item_maxn < 0) - { + tb_int_t hash_count = (tb_int_t)lua_tointeger(lua, 2); + tb_int_t item_maxn = (tb_int_t)lua_tointeger(lua, 3); + if (hash_count > 16 || item_maxn < 0) { lua_pushnil(lua); lua_pushfstring(lua, "invalid hash count(%p) and item maxn(%d)!", hash_count, item_maxn); return 2; @@ -51,8 +48,10 @@ tb_int_t xm_bloom_filter_open(lua_State* lua) // init the bloom filter tb_bloom_filter_ref_t filter = tb_bloom_filter_init(probability, hash_count, item_maxn, tb_element_str(tb_true)); - if (filter) xm_lua_pushpointer(lua, (tb_pointer_t)filter); - else lua_pushnil(lua); + if (filter) { + xm_lua_pushpointer(lua, (tb_pointer_t)filter); + } else { + lua_pushnil(lua); + } return 1; } - diff --git a/core/src/xmake/bloom_filter/bloom_filter_set.c b/core/src/xmake/bloom_filter/bloom_filter_set.c index ceaaeef66..6762ea5bc 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_set.c +++ b/core/src/xmake/bloom_filter/bloom_filter_set.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_set" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_set" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,20 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_set(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_set(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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); tb_check_return_val(filter, 0); // get item - tb_char_t const* item = luaL_checkstring(lua, 2); + tb_char_t const *item = luaL_checkstring(lua, 2); tb_assert_and_check_return_val(item, 0); // set item @@ -55,4 +54,3 @@ tb_int_t xm_bloom_filter_set(lua_State* lua) lua_pushboolean(lua, ok); return 1; } - diff --git a/core/src/xmake/bloom_filter/bloom_filter_size.c b/core/src/xmake/bloom_filter/bloom_filter_size.c index ee8de73b9..730968a12 100644 --- a/core/src/xmake/bloom_filter/bloom_filter_size.c +++ b/core/src/xmake/bloom_filter/bloom_filter_size.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bloom_filter_size" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bloom_filter_size" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_bloom_filter_size(lua_State* lua) -{ - // check +tb_int_t xm_bloom_filter_size(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 the bloom filter tb_bloom_filter_ref_t filter = (tb_bloom_filter_ref_t)xm_lua_topointer(lua, 1); @@ -51,5 +50,3 @@ tb_int_t xm_bloom_filter_size(lua_State* lua) lua_pushinteger(lua, (tb_int_t)size); return 1; } - - diff --git a/core/src/xmake/bloom_filter/prefix.h b/core/src/xmake/bloom_filter/prefix.h index 8419d3ae1..ef70c7aaa 100644 --- a/core/src/xmake/bloom_filter/prefix.h +++ b/core/src/xmake/bloom_filter/prefix.h @@ -27,5 +27,3 @@ #include "../prefix.h" #endif - - diff --git a/core/src/xmake/config.h b/core/src/xmake/config.h index 65f1d7808..b0d39830c 100644 --- a/core/src/xmake/config.h +++ b/core/src/xmake/config.h @@ -27,5 +27,3 @@ #include "xmake.config.h" #endif - - diff --git a/core/src/xmake/curses/curses.c b/core/src/xmake/curses/curses.c index 3321090f1..4f8c03028 100644 --- a/core/src/xmake/curses/curses.c +++ b/core/src/xmake/curses/curses.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "curses" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "curses" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,71 +33,66 @@ #include <stdlib.h> #if defined(PDCURSES) // fix macro redefinition -# undef MOUSE_MOVED +#undef MOUSE_MOVED #endif #define NCURSES_MOUSE_VERSION 2 #ifdef TB_COMPILER_IS_MINGW -# include <ncursesw/curses.h> +#include <ncursesw/curses.h> #else -# include <curses.h> +#include <curses.h> #endif #if defined(NCURSES_VERSION) -# include <locale.h> +#include <locale.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * macros */ -#define XM_CURSES_STDSCR "curses.stdscr" -#define XM_CURSES_WINDOW "curses.window" -#define XM_CURSES_OK(v) (((v) == ERR) ? 0 : 1) +#define XM_CURSES_STDSCR "curses.stdscr" +#define XM_CURSES_WINDOW "curses.window" +#define XM_CURSES_OK(v) (((v) == ERR) ? 0 : 1) // define functions -#define XM_CURSES_NUMBER(n) \ - static int xm_curses_ ## n(lua_State* lua) \ - { \ - lua_pushnumber(lua, n()); \ - return 1; \ +#define XM_CURSES_NUMBER(n) \ + static int xm_curses_##n(lua_State *lua) { \ + lua_pushnumber(lua, n()); \ + return 1; \ } -#define XM_CURSES_NUMBER2(n, v) \ - static int xm_curses_ ## n(lua_State* lua) \ - { \ - lua_pushnumber(lua, v); \ - return 1; \ +#define XM_CURSES_NUMBER2(n, v) \ + static int xm_curses_##n(lua_State *lua) { \ + lua_pushnumber(lua, v); \ + return 1; \ } -#define XM_CURSES_BOOL(n) \ - static int xm_curses_ ## n(lua_State* lua) \ - { \ - lua_pushboolean(lua, n()); \ - return 1; \ +#define XM_CURSES_BOOL(n) \ + static int xm_curses_##n(lua_State *lua) { \ + lua_pushboolean(lua, n()); \ + return 1; \ } -#define XM_CURSES_BOOLOK(n) \ - static int xm_curses_ ## n(lua_State* lua) \ - { \ - lua_pushboolean(lua, XM_CURSES_OK(n())); \ - return 1; \ +#define XM_CURSES_BOOLOK(n) \ + static int xm_curses_##n(lua_State *lua) { \ + lua_pushboolean(lua, XM_CURSES_OK(n())); \ + return 1; \ } -#define XM_CURSES_WINDOW_BOOLOK(n) \ - static int xm_curses_window_ ## n(lua_State* lua) \ - { \ - WINDOW* w = xm_curses_window_check(lua, 1); \ - lua_pushboolean(lua, XM_CURSES_OK(n(w))); \ - return 1; \ +#define XM_CURSES_WINDOW_BOOLOK(n) \ + static int xm_curses_window_##n(lua_State *lua) { \ + WINDOW *w = xm_curses_window_check(lua, 1); \ + lua_pushboolean(lua, XM_CURSES_OK(n(w))); \ + return 1; \ } // define constants -#define XM_CURSES_CONST_(n, v) \ - lua_pushstring(lua, n); \ - lua_pushnumber(lua, v); \ +#define XM_CURSES_CONST_(n, v) \ + lua_pushstring(lua, n); \ + lua_pushnumber(lua, v); \ lua_settable(lua, lua_upvalueindex(1)); -#define XM_CURSES_CONST(s) XM_CURSES_CONST_(#s, s) -#define XM_CURSES_CONST2(s, v) XM_CURSES_CONST_(#s, v) +#define XM_CURSES_CONST(s) XM_CURSES_CONST_(#s, s) +#define XM_CURSES_CONST2(s, v) XM_CURSES_CONST_(#s, v) /* ////////////////////////////////////////////////////////////////////////////////////// * globals @@ -109,12 +104,13 @@ static int g_mapkey = 0; /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static chtype xm_curses_checkch(lua_State* lua, int index) -{ - if (lua_type(lua, index) == LUA_TNUMBER) +static chtype xm_curses_checkch(lua_State *lua, int index) { + if (lua_type(lua, index) == LUA_TNUMBER) { return (chtype)luaL_checknumber(lua, index); - if (lua_type(lua, index) == LUA_TSTRING) + } + if (lua_type(lua, index) == LUA_TSTRING) { return *lua_tostring(lua, index); + } #ifdef USE_LUAJIT luaL_typerror(lua, index, "chtype"); #endif @@ -122,48 +118,65 @@ static chtype xm_curses_checkch(lua_State* lua, int index) } // get character and map key -static int xm_curses_window_getch_impl(WINDOW* w) -{ +static int xm_curses_window_getch_impl(WINDOW *w) { #ifdef PDCURSES - static int has_key = 0; + static int has_key = 0; static int temp_key = 0; int key; - if (g_mapkey && has_key) - { + if (g_mapkey && has_key) { has_key = 0; return temp_key; } key = wgetch(w); - if (key == KEY_RESIZE) resize_term(0, 0); - if (key == ERR || !g_mapkey) return key; - if (key >= ALT_A && key <= ALT_Z) - { - has_key = 1; + if (key == KEY_RESIZE) + resize_term(0, 0); + if (key == ERR || !g_mapkey) + return key; + if (key >= ALT_A && key <= ALT_Z) { + has_key = 1; temp_key = key - ALT_A + 'A'; - } - else if (key >= ALT_0 && key <= ALT_9) - { - has_key = 1; + } else if (key >= ALT_0 && key <= ALT_9) { + has_key = 1; temp_key = key - ALT_0 + '0'; - } - else - { - switch (key) - { - case ALT_DEL: temp_key = KEY_DC; break; - case ALT_INS: temp_key = KEY_IC; break; - case ALT_HOME: temp_key = KEY_HOME; break; - case ALT_END: temp_key = KEY_END; break; - case ALT_PGUP: temp_key = KEY_PPAGE; break; - case ALT_PGDN: temp_key = KEY_NPAGE; break; - case ALT_UP: temp_key = KEY_UP; break; - case ALT_DOWN: temp_key = KEY_DOWN; break; - case ALT_RIGHT: temp_key = KEY_RIGHT; break; - case ALT_LEFT: temp_key = KEY_LEFT; break; - case ALT_BKSP: temp_key = KEY_BACKSPACE; break; - default: return key; + } else { + switch (key) { + case ALT_DEL: + temp_key = KEY_DC; + break; + case ALT_INS: + temp_key = KEY_IC; + break; + case ALT_HOME: + temp_key = KEY_HOME; + break; + case ALT_END: + temp_key = KEY_END; + break; + case ALT_PGUP: + temp_key = KEY_PPAGE; + break; + case ALT_PGDN: + temp_key = KEY_NPAGE; + break; + case ALT_UP: + temp_key = KEY_UP; + break; + case ALT_DOWN: + temp_key = KEY_DOWN; + break; + case ALT_RIGHT: + temp_key = KEY_RIGHT; + break; + case ALT_LEFT: + temp_key = KEY_LEFT; + break; + case ALT_BKSP: + temp_key = KEY_BACKSPACE; + break; + default: + return key; } } has_key = 1; @@ -174,63 +187,57 @@ static int xm_curses_window_getch_impl(WINDOW* w) } // new a window object -static void xm_curses_window_new(lua_State* lua, WINDOW* nw) -{ - if (nw) - { - WINDOW** w = (WINDOW**)lua_newuserdata(lua, sizeof(WINDOW*)); +static void xm_curses_window_new(lua_State *lua, WINDOW *nw) { + if (nw) { + WINDOW **w = (WINDOW **)lua_newuserdata(lua, sizeof(WINDOW *)); luaL_getmetatable(lua, XM_CURSES_WINDOW); lua_setmetatable(lua, -2); *w = nw; - } - else - { + } else { lua_pushliteral(lua, "failed to create window"); lua_error(lua); } } // get window -static WINDOW** xm_curses_window_get(lua_State* lua, int index) -{ - WINDOW** w = (WINDOW**)luaL_checkudata(lua, index, XM_CURSES_WINDOW); - if (w == NULL) luaL_argerror(lua, index, "bad curses window"); +static WINDOW **xm_curses_window_get(lua_State *lua, int index) { + WINDOW **w = (WINDOW **)luaL_checkudata(lua, index, XM_CURSES_WINDOW); + if (w == NULL) + luaL_argerror(lua, index, "bad curses window"); return w; } // get and check window -static WINDOW* xm_curses_window_check(lua_State* lua, int index) -{ - WINDOW** w = xm_curses_window_get(lua, index); - if (*w == NULL) luaL_argerror(lua, index, "attempt to use closed curses window"); +static WINDOW *xm_curses_window_check(lua_State *lua, int index) { + WINDOW **w = xm_curses_window_get(lua, index); + if (*w == NULL) + luaL_argerror(lua, index, "attempt to use closed curses window"); return *w; } // tostring(window) -static int xm_curses_window_tostring(lua_State* lua) -{ - WINDOW** w = xm_curses_window_get(lua, 1); - char const* s = NULL; - if (*w) s = (char const*)lua_touserdata(lua, 1); - lua_pushfstring(lua, "curses window (%s)", s? s : "closed"); +static int xm_curses_window_tostring(lua_State *lua) { + WINDOW **w = xm_curses_window_get(lua, 1); + char const *s = NULL; + if (*w) + s = (char const *)lua_touserdata(lua, 1); + lua_pushfstring(lua, "curses window (%s)", s ? s : "closed"); return 1; } // window:move(y, x) -static int xm_curses_window_move(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int y = luaL_checkint(lua, 2); - int x = luaL_checkint(lua, 3); +static int xm_curses_window_move(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int y = luaL_checkint(lua, 2); + int x = luaL_checkint(lua, 3); lua_pushboolean(lua, XM_CURSES_OK(wmove(w, y, x))); return 1; } // window:getyx(y, x) -static int xm_curses_window_getyx(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int y, x; +static int xm_curses_window_getyx(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int y, x; getyx(w, y, x); lua_pushnumber(lua, y); lua_pushnumber(lua, x); @@ -238,10 +245,9 @@ static int xm_curses_window_getyx(lua_State* lua) } // window:getmaxyx(y, x) -static int xm_curses_window_getmaxyx(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int y, x; +static int xm_curses_window_getmaxyx(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int y, x; getmaxyx(w, y, x); lua_pushnumber(lua, y); lua_pushnumber(lua, x); @@ -249,11 +255,9 @@ static int xm_curses_window_getmaxyx(lua_State* lua) } // window:delwin() -static int xm_curses_window_delwin(lua_State* lua) -{ - WINDOW** w = xm_curses_window_get(lua, 1); - if (*w && *w != stdscr) - { +static int xm_curses_window_delwin(lua_State *lua) { + WINDOW **w = xm_curses_window_get(lua, 1); + if (*w && *w != stdscr) { delwin(*w); *w = NULL; } @@ -261,32 +265,29 @@ static int xm_curses_window_delwin(lua_State* lua) } // window:addch(ch) -static int xm_curses_window_addch(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - chtype ch = xm_curses_checkch(lua, 2); +static int xm_curses_window_addch(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + chtype ch = xm_curses_checkch(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(waddch(w, ch))); return 1; } // window:addnstr(str) -static int xm_curses_window_addnstr(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - const char* str = luaL_checkstring(lua, 2); - int n = luaL_optint(lua, 3, -1); - if (n < 0) n = (int)lua_strlen(lua, 2); +static int xm_curses_window_addnstr(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + const char *str = luaL_checkstring(lua, 2); + int n = luaL_optint(lua, 3, -1); + if (n < 0) + n = (int)lua_strlen(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(waddnstr(w, str, n))); return 1; } // window:keypad(true) -static int xm_curses_window_keypad(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int enabled = lua_isnoneornil(lua, 2) ? 1 : lua_toboolean(lua, 2); - if (enabled) - { +static int xm_curses_window_keypad(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int enabled = lua_isnoneornil(lua, 2) ? 1 : lua_toboolean(lua, 2); + if (enabled) { // on WIN32 ALT keys need to be mapped, so to make sure you get the wanted keys, // only makes sense when using keypad(true) and echo(false) g_mapkey = 1; @@ -296,91 +297,83 @@ static int xm_curses_window_keypad(lua_State* lua) } // window:meta(true) -static int xm_curses_window_meta(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int enabled = lua_toboolean(lua, 2); +static int xm_curses_window_meta(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int enabled = lua_toboolean(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(meta(w, enabled))); return 1; } // window:nodelay(true) -static int xm_curses_window_nodelay(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int enabled = lua_toboolean(lua, 2); +static int xm_curses_window_nodelay(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int enabled = lua_toboolean(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(nodelay(w, enabled))); return 1; } // window:leaveok(true) -static int xm_curses_window_leaveok(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int enabled = lua_toboolean(lua, 2); +static int xm_curses_window_leaveok(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int enabled = lua_toboolean(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(leaveok(w, enabled))); return 1; } // window:getch() -static int xm_curses_window_getch(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int c = xm_curses_window_getch_impl(w); - if (c == ERR) return 0; +static int xm_curses_window_getch(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int c = xm_curses_window_getch_impl(w); + if (c == ERR) + return 0; lua_pushnumber(lua, c); return 1; } // window:attroff(attrs) -static int xm_curses_window_attroff(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int attrs = luaL_checkint(lua, 2); +static int xm_curses_window_attroff(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int attrs = luaL_checkint(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(wattroff(w, attrs))); return 1; } // window:attron(attrs) -static int xm_curses_window_attron(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int attrs = luaL_checkint(lua, 2); +static int xm_curses_window_attron(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int attrs = luaL_checkint(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(wattron(w, attrs))); return 1; } // window:attrset(attrs) -static int xm_curses_window_attrset(lua_State* lua) -{ - WINDOW* w = xm_curses_window_check(lua, 1); - int attrs = luaL_checkint(lua, 2); +static int xm_curses_window_attrset(lua_State *lua) { + WINDOW *w = xm_curses_window_check(lua, 1); + int attrs = luaL_checkint(lua, 2); lua_pushboolean(lua, XM_CURSES_OK(wattrset(w, attrs))); return 1; } // window:copywin(...) -static int xm_curses_window_copywin(lua_State* lua) -{ - WINDOW* srcwin = xm_curses_window_check(lua, 1); - WINDOW* dstwin = xm_curses_window_check(lua, 2); - int sminrow = luaL_checkint(lua, 3); - int smincol = luaL_checkint(lua, 4); - int dminrow = luaL_checkint(lua, 5); - int dmincol = luaL_checkint(lua, 6); - int dmaxrow = luaL_checkint(lua, 7); - int dmaxcol = luaL_checkint(lua, 8); - int overlay = lua_toboolean(lua, 9); - lua_pushboolean(lua, XM_CURSES_OK(copywin(srcwin, dstwin, sminrow, - smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay))); +static int xm_curses_window_copywin(lua_State *lua) { + WINDOW *srcwin = xm_curses_window_check(lua, 1); + WINDOW *dstwin = xm_curses_window_check(lua, 2); + int sminrow = luaL_checkint(lua, 3); + int smincol = luaL_checkint(lua, 4); + int dminrow = luaL_checkint(lua, 5); + int dmincol = luaL_checkint(lua, 6); + int dmaxrow = luaL_checkint(lua, 7); + int dmaxcol = luaL_checkint(lua, 8); + int overlay = lua_toboolean(lua, 9); + lua_pushboolean(lua, + XM_CURSES_OK( + copywin(srcwin, dstwin, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, overlay))); return 1; } // clean window after exiting program -static void xm_curses_cleanup() -{ - if (!isendwin()) - { +static void xm_curses_cleanup() { + if (!isendwin()) { wclear(stdscr); wrefresh(stdscr); endwin(); @@ -388,8 +381,7 @@ static void xm_curses_cleanup() } // register constants -static void xm_curses_register_constants(lua_State* lua) -{ +static void xm_curses_register_constants(lua_State *lua) { // colors XM_CURSES_CONST(COLOR_BLACK) XM_CURSES_CONST(COLOR_RED) @@ -495,9 +487,9 @@ static void xm_curses_register_constants(lua_State* lua) XM_CURSES_CONST(KEY_B3) #endif #if !defined(XCURSES) -# ifndef NOMOUSE +#ifndef NOMOUSE XM_CURSES_CONST(KEY_MOUSE) -# endif +#endif #endif XM_CURSES_CONST(KEY_MOVE) XM_CURSES_CONST(KEY_NEXT) @@ -560,7 +552,7 @@ static void xm_curses_register_constants(lua_State* lua) XM_CURSES_CONST2(KEY_F12, KEY_F(12)) #if !defined(XCURSES) -# ifndef NOMOUSE +#ifndef NOMOUSE // mouse constants XM_CURSES_CONST(BUTTON1_RELEASED) XM_CURSES_CONST(BUTTON1_PRESSED) @@ -587,26 +579,26 @@ static void xm_curses_register_constants(lua_State* lua) XM_CURSES_CONST(BUTTON_ALT) XM_CURSES_CONST(REPORT_MOUSE_POSITION) XM_CURSES_CONST(ALL_MOUSE_EVENTS) -# if NCURSES_MOUSE_VERSION > 1 +#if NCURSES_MOUSE_VERSION > 1 XM_CURSES_CONST(BUTTON5_RELEASED) XM_CURSES_CONST(BUTTON5_PRESSED) XM_CURSES_CONST(BUTTON5_CLICKED) XM_CURSES_CONST(BUTTON5_DOUBLE_CLICKED) XM_CURSES_CONST(BUTTON5_TRIPLE_CLICKED) -# endif -# endif +#endif +#endif #endif } // init curses -static int xm_curses_initscr(lua_State* lua) -{ - WINDOW* w = initscr(); - if (!w) return 0; +static int xm_curses_initscr(lua_State *lua) { + WINDOW *w = initscr(); + if (!w) + return 0; xm_curses_window_new(lua, w); #if defined(NCURSES_VERSION) -// ESCDELAY = 0; + // ESCDELAY = 0; set_escdelay(0); #endif @@ -622,8 +614,7 @@ static int xm_curses_initscr(lua_State* lua) return 1; } -static int xm_curses_endwin(lua_State* lua) -{ +static int xm_curses_endwin(lua_State *lua) { endwin(); #ifdef XCURSES XCursesExit(); @@ -632,19 +623,16 @@ static int xm_curses_endwin(lua_State* lua) return 0; } -static int xm_curses_stdscr(lua_State* lua) -{ +static int xm_curses_stdscr(lua_State *lua) { lua_pushstring(lua, XM_CURSES_STDSCR); lua_rawget(lua, LUA_REGISTRYINDEX); return 1; } #if !defined(XCURSES) && !defined(NOMOUSE) -static int xm_curses_getmouse(lua_State* lua) -{ +static int xm_curses_getmouse(lua_State *lua) { MEVENT e; - if (getmouse(&e) == OK) - { + if (getmouse(&e) == OK) { lua_pushinteger(lua, e.bstate); lua_pushinteger(lua, e.x); lua_pushinteger(lua, e.y); @@ -657,8 +645,7 @@ static int xm_curses_getmouse(lua_State* lua) return 1; } -static int xm_curses_mousemask(lua_State* lua) -{ +static int xm_curses_mousemask(lua_State *lua) { mmask_t m = luaL_checkint(lua, 1); mmask_t om; m = mousemask(m, &om); @@ -668,26 +655,23 @@ static int xm_curses_mousemask(lua_State* lua) } #endif -static int xm_curses_init_pair(lua_State* lua) -{ +static int xm_curses_init_pair(lua_State *lua) { short pair = luaL_checkint(lua, 1); - short f = luaL_checkint(lua, 2); - short b = luaL_checkint(lua, 3); + short f = luaL_checkint(lua, 2); + short b = luaL_checkint(lua, 3); lua_pushboolean(lua, XM_CURSES_OK(init_pair(pair, f, b))); return 1; } -static int xm_curses_COLOR_PAIR(lua_State* lua) -{ +static int xm_curses_COLOR_PAIR(lua_State *lua) { int n = luaL_checkint(lua, 1); lua_pushnumber(lua, COLOR_PAIR(n)); return 1; } -static int xm_curses_curs_set(lua_State* lua) -{ - int vis = luaL_checkint(lua, 1); +static int xm_curses_curs_set(lua_State *lua) { + int vis = luaL_checkint(lua, 1); int state = curs_set(vis); if (state == ERR) return 0; @@ -696,44 +680,42 @@ static int xm_curses_curs_set(lua_State* lua) return 1; } -static int xm_curses_napms(lua_State* lua) -{ +static int xm_curses_napms(lua_State *lua) { int ms = luaL_checkint(lua, 1); lua_pushboolean(lua, XM_CURSES_OK(napms(ms))); return 1; } -static int xm_curses_cbreak(lua_State* lua) -{ - if (lua_isnoneornil(lua, 1) || lua_toboolean(lua, 1)) +static int xm_curses_cbreak(lua_State *lua) { + if (lua_isnoneornil(lua, 1) || lua_toboolean(lua, 1)) { lua_pushboolean(lua, XM_CURSES_OK(cbreak())); - else + } else { lua_pushboolean(lua, XM_CURSES_OK(nocbreak())); + } return 1; } -static int xm_curses_echo(lua_State* lua) -{ - if (lua_isnoneornil(lua, 1) || lua_toboolean(lua, 1)) +static int xm_curses_echo(lua_State *lua) { + if (lua_isnoneornil(lua, 1) || lua_toboolean(lua, 1)) { lua_pushboolean(lua, XM_CURSES_OK(echo())); - else + } else { lua_pushboolean(lua, XM_CURSES_OK(noecho())); + } return 1; } -static int xm_curses_nl(lua_State* lua) -{ - if (lua_isnoneornil(lua, 1) || lua_toboolean(lua, 1)) +static int xm_curses_nl(lua_State *lua) { + if (lua_isnoneornil(lua, 1) || lua_toboolean(lua, 1)) { lua_pushboolean(lua, XM_CURSES_OK(nl())); - else + } else { lua_pushboolean(lua, XM_CURSES_OK(nonl())); + } return 1; } -static int xm_curses_newpad(lua_State* lua) -{ +static int xm_curses_newpad(lua_State *lua) { int nlines = luaL_checkint(lua, 1); - int ncols = luaL_checkint(lua, 2); + int ncols = luaL_checkint(lua, 2); xm_curses_window_new(lua, newpad(nlines, ncols)); return 1; } @@ -751,69 +733,66 @@ XM_CURSES_WINDOW_BOOLOK(wnoutrefresh) * globals */ -static const luaL_Reg g_window_functions[] = -{ - { "close", xm_curses_window_delwin }, - { "keypad", xm_curses_window_keypad }, - { "meta", xm_curses_window_meta }, - { "nodelay", xm_curses_window_nodelay }, - { "leaveok", xm_curses_window_leaveok }, - { "move", xm_curses_window_move }, - { "clear", xm_curses_window_wclear }, - { "noutrefresh",xm_curses_window_wnoutrefresh }, - { "attroff", xm_curses_window_attroff }, - { "attron", xm_curses_window_attron }, - { "attrset", xm_curses_window_attrset }, - { "getch", xm_curses_window_getch }, - { "getyx", xm_curses_window_getyx }, - { "getmaxyx", xm_curses_window_getmaxyx }, - { "addch", xm_curses_window_addch }, - { "addstr", xm_curses_window_addnstr }, - { "copy", xm_curses_window_copywin }, - {"__gc", xm_curses_window_delwin }, - {"__tostring", xm_curses_window_tostring }, - {NULL, NULL } +static const luaL_Reg g_window_functions[] = { + { "close", xm_curses_window_delwin }, + { "keypad", xm_curses_window_keypad }, + { "meta", xm_curses_window_meta }, + { "nodelay", xm_curses_window_nodelay }, + { "leaveok", xm_curses_window_leaveok }, + { "move", xm_curses_window_move }, + { "clear", xm_curses_window_wclear }, + { "noutrefresh", xm_curses_window_wnoutrefresh }, + { "attroff", xm_curses_window_attroff }, + { "attron", xm_curses_window_attron }, + { "attrset", xm_curses_window_attrset }, + { "getch", xm_curses_window_getch }, + { "getyx", xm_curses_window_getyx }, + { "getmaxyx", xm_curses_window_getmaxyx }, + { "addch", xm_curses_window_addch }, + { "addstr", xm_curses_window_addnstr }, + { "copy", xm_curses_window_copywin }, + { "__gc", xm_curses_window_delwin }, + { "__tostring", xm_curses_window_tostring }, + { NULL, NULL }, }; -static const luaL_Reg g_curses_functions[] = -{ - { "done", xm_curses_endwin }, - { "isdone", xm_curses_isendwin }, - { "main_window", xm_curses_stdscr }, - { "columns", xm_curses_COLS }, - { "lines", xm_curses_LINES }, - { "start_color", xm_curses_start_color }, - { "has_colors", xm_curses_has_colors }, - { "init_pair", xm_curses_init_pair }, - { "color_pair", xm_curses_COLOR_PAIR }, - { "napms", xm_curses_napms }, - { "cursor_set", xm_curses_curs_set }, - { "new_pad", xm_curses_newpad }, - { "doupdate", xm_curses_doupdate }, - { "cbreak", xm_curses_cbreak }, - { "echo", xm_curses_echo }, - { "nl", xm_curses_nl }, +static const luaL_Reg g_curses_functions[] = { + { "done", xm_curses_endwin }, + { "isdone", xm_curses_isendwin }, + { "main_window", xm_curses_stdscr }, + { "columns", xm_curses_COLS }, + { "lines", xm_curses_LINES }, + { "start_color", xm_curses_start_color }, + { "has_colors", xm_curses_has_colors }, + { "init_pair", xm_curses_init_pair }, + { "color_pair", xm_curses_COLOR_PAIR }, + { "napms", xm_curses_napms }, + { "cursor_set", xm_curses_curs_set }, + { "new_pad", xm_curses_newpad }, + { "doupdate", xm_curses_doupdate }, + { "cbreak", xm_curses_cbreak }, + { "echo", xm_curses_echo }, + { "nl", xm_curses_nl }, #if !defined(XCURSES) #ifndef NOMOUSE - { "mousemask", xm_curses_mousemask }, - { "getmouse", xm_curses_getmouse }, + { "mousemask", xm_curses_mousemask }, + { "getmouse", xm_curses_getmouse }, #endif #endif - {NULL, NULL} + { NULL, NULL }, }; /* ////////////////////////////////////////////////////////////////////////////////////// * implementations */ -int xm_lua_curses_register(lua_State* lua, const char* module) -{ +int xm_lua_curses_register(lua_State *lua, const char *module) { // create new metatable for window objects luaL_newmetatable(lua, XM_CURSES_WINDOW); lua_pushliteral(lua, "__index"); - lua_pushvalue(lua, -2); /* push metatable */ - lua_rawset(lua, -3); /* metatable.__index = metatable */ + lua_pushvalue(lua, -2); /* push metatable */ + lua_rawset(lua, -3); /* metatable.__index = metatable */ luaL_setfuncs(lua, g_window_functions, 0); - lua_pop(lua, 1); /* remove metatable from stack */ + lua_pop(lua, 1); /* remove metatable from stack */ // create global table with curses methods/variables/constants lua_newtable(lua); diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index 61ba79af1..0405fc230 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "engine" -#define TB_TRACE_MODULE_DEBUG (1) +#define TB_TRACE_MODULE_NAME "engine" +#define TB_TRACE_MODULE_DEBUG (1) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -31,38 +31,39 @@ #include "xmake.h" #include "io/poller.h" #if defined(TB_CONFIG_OS_WINDOWS) -# include <windows.h> -# include <io.h> -# include <fcntl.h> +#include <windows.h> +#include <io.h> +#include <fcntl.h> #elif defined(TB_CONFIG_OS_MACOSX) || defined(TB_CONFIG_OS_IOS) -# include <unistd.h> -# include <mach-o/dyld.h> -# include <signal.h> -#elif defined(TB_CONFIG_OS_LINUX) || defined(TB_CONFIG_OS_BSD) || defined(TB_CONFIG_OS_ANDROID) || defined(TB_CONFIG_OS_HAIKU) -# include <unistd.h> -# include <signal.h> +#include <unistd.h> +#include <mach-o/dyld.h> +#include <signal.h> +#elif defined(TB_CONFIG_OS_LINUX) || defined(TB_CONFIG_OS_BSD) || defined(TB_CONFIG_OS_ANDROID) || \ + defined(TB_CONFIG_OS_HAIKU) +#include <unistd.h> +#include <signal.h> #endif #ifdef TB_CONFIG_OS_BSD -# include <sys/types.h> -# include <sys/sysctl.h> -# include <signal.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <signal.h> #endif #ifdef TB_CONFIG_OS_HAIKU -# include <image.h> +#include <image.h> #endif #ifdef __COSMOPOLITAN__ -# include <sys/utsname.h> +#include <sys/utsname.h> #endif // for uid #ifndef TB_CONFIG_OS_WINDOWS -# include <unistd.h> -# include <errno.h> +#include <unistd.h> +#include <errno.h> #endif // for embed files #ifdef XM_EMBED_ENABLE -# include "lz4/prefix.h" +#include "lz4/prefix.h" #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -71,321 +72,318 @@ // proc/self #if defined(TB_CONFIG_OS_LINUX) -# define XM_PROC_SELF_FILE "/proc/self/exe" +#define XM_PROC_SELF_FILE "/proc/self/exe" #elif defined(TB_CONFIG_OS_BSD) && !defined(__OpenBSD__) && !defined(__FreeBSD__) -# if defined(__NetBSD__) -# define XM_PROC_SELF_FILE "/proc/curproc/exe" -# else -# define XM_PROC_SELF_FILE "/proc/curproc/file" -# endif +#if defined(__NetBSD__) +#define XM_PROC_SELF_FILE "/proc/curproc/exe" +#else +#define XM_PROC_SELF_FILE "/proc/curproc/file" +#endif #endif // hook lua memory allocator -#define XM_HOOK_LUA_MEMALLOC (0) +#define XM_HOOK_LUA_MEMALLOC (0) /* ////////////////////////////////////////////////////////////////////////////////////// * types */ // the engine type -typedef struct __xm_engine_t -{ +typedef struct __xm_engine_t { // the lua - lua_State* lua; + lua_State *lua; // the engine name - tb_char_t name[64]; + tb_char_t name[64]; // the io poller - tb_poller_ref_t poller; - xm_poller_state_t poller_state; + tb_poller_ref_t poller; + xm_poller_state_t poller_state; #ifdef XM_EMBED_ENABLE // the temporary directory - tb_char_t tmpdir[TB_PATH_MAXN]; + tb_char_t tmpdir[TB_PATH_MAXN]; // the embed files - tb_byte_t const* embeddata[32]; - tb_size_t embedsize[32]; - tb_size_t embedcount; + tb_byte_t const *embeddata[32]; + tb_size_t embedsize[32]; + tb_size_t embedcount; #endif - -}xm_engine_t; +} xm_engine_t; /* ////////////////////////////////////////////////////////////////////////////////////// * declaration */ // the os functions -tb_int_t xm_os_argv(lua_State* lua); -tb_int_t xm_os_args(lua_State* lua); -tb_int_t xm_os_find(lua_State* lua); -tb_int_t xm_os_link(lua_State* lua); -tb_int_t xm_os_isdir(lua_State* lua); -tb_int_t xm_os_rmdir(lua_State* lua); -tb_int_t xm_os_mkdir(lua_State* lua); -tb_int_t xm_os_cpdir(lua_State* lua); -tb_int_t xm_os_chdir(lua_State* lua); -tb_int_t xm_os_mtime(lua_State* lua); -tb_int_t xm_os_sleep(lua_State* lua); -tb_int_t xm_os_mclock(lua_State* lua); -tb_int_t xm_os_curdir(lua_State* lua); -tb_int_t xm_os_tmpdir(lua_State* lua); -tb_int_t xm_os_islink(lua_State* lua); -tb_int_t xm_os_isfile(lua_State* lua); -tb_int_t xm_os_touch(lua_State* lua); -tb_int_t xm_os_rmfile(lua_State* lua); -tb_int_t xm_os_cpfile(lua_State* lua); -tb_int_t xm_os_fscase(lua_State* lua); -tb_int_t xm_os_rename(lua_State* lua); -tb_int_t xm_os_exists(lua_State* lua); -tb_int_t xm_os_setenv(lua_State* lua); -tb_int_t xm_os_getenv(lua_State* lua); -tb_int_t xm_os_getenvs(lua_State* lua); -tb_int_t xm_os_cpuinfo(lua_State* lua); -tb_int_t xm_os_meminfo(lua_State* lua); -tb_int_t xm_os_readlink(lua_State* lua); -tb_int_t xm_os_filesize(lua_State* lua); -tb_int_t xm_os_emptydir(lua_State* lua); -tb_int_t xm_os_syserror(lua_State* lua); -tb_int_t xm_os_strerror(lua_State* lua); -tb_int_t xm_os_getwinsize(lua_State* lua); -tb_int_t xm_os_getpid(lua_State* lua); -tb_int_t xm_os_signal(lua_State* lua); +tb_int_t xm_os_argv(lua_State *lua); +tb_int_t xm_os_args(lua_State *lua); +tb_int_t xm_os_find(lua_State *lua); +tb_int_t xm_os_link(lua_State *lua); +tb_int_t xm_os_isdir(lua_State *lua); +tb_int_t xm_os_rmdir(lua_State *lua); +tb_int_t xm_os_mkdir(lua_State *lua); +tb_int_t xm_os_cpdir(lua_State *lua); +tb_int_t xm_os_chdir(lua_State *lua); +tb_int_t xm_os_mtime(lua_State *lua); +tb_int_t xm_os_sleep(lua_State *lua); +tb_int_t xm_os_mclock(lua_State *lua); +tb_int_t xm_os_curdir(lua_State *lua); +tb_int_t xm_os_tmpdir(lua_State *lua); +tb_int_t xm_os_islink(lua_State *lua); +tb_int_t xm_os_isfile(lua_State *lua); +tb_int_t xm_os_touch(lua_State *lua); +tb_int_t xm_os_rmfile(lua_State *lua); +tb_int_t xm_os_cpfile(lua_State *lua); +tb_int_t xm_os_fscase(lua_State *lua); +tb_int_t xm_os_rename(lua_State *lua); +tb_int_t xm_os_exists(lua_State *lua); +tb_int_t xm_os_setenv(lua_State *lua); +tb_int_t xm_os_getenv(lua_State *lua); +tb_int_t xm_os_getenvs(lua_State *lua); +tb_int_t xm_os_cpuinfo(lua_State *lua); +tb_int_t xm_os_meminfo(lua_State *lua); +tb_int_t xm_os_readlink(lua_State *lua); +tb_int_t xm_os_filesize(lua_State *lua); +tb_int_t xm_os_emptydir(lua_State *lua); +tb_int_t xm_os_syserror(lua_State *lua); +tb_int_t xm_os_strerror(lua_State *lua); +tb_int_t xm_os_getwinsize(lua_State *lua); +tb_int_t xm_os_getpid(lua_State *lua); +tb_int_t xm_os_signal(lua_State *lua); #ifndef TB_CONFIG_OS_WINDOWS -tb_int_t xm_os_uid(lua_State* lua); -tb_int_t xm_os_gid(lua_State* lua); -tb_int_t xm_os_getown(lua_State* lua); +tb_int_t xm_os_uid(lua_State *lua); +tb_int_t xm_os_gid(lua_State *lua); +tb_int_t xm_os_getown(lua_State *lua); #endif // the io/file functions -tb_int_t xm_io_stdfile(lua_State* lua); -tb_int_t xm_io_file_open(lua_State* lua); -tb_int_t xm_io_file_read(lua_State* lua); -tb_int_t xm_io_file_readable(lua_State* lua); -tb_int_t xm_io_file_seek(lua_State* lua); -tb_int_t xm_io_file_size(lua_State* lua); -tb_int_t xm_io_file_rawfd(lua_State* lua); -tb_int_t xm_io_file_write(lua_State* lua); -tb_int_t xm_io_file_flush(lua_State* lua); -tb_int_t xm_io_file_close(lua_State* lua); -tb_int_t xm_io_file_isatty(lua_State* lua); +tb_int_t xm_io_stdfile(lua_State *lua); +tb_int_t xm_io_file_open(lua_State *lua); +tb_int_t xm_io_file_read(lua_State *lua); +tb_int_t xm_io_file_readable(lua_State *lua); +tb_int_t xm_io_file_seek(lua_State *lua); +tb_int_t xm_io_file_size(lua_State *lua); +tb_int_t xm_io_file_rawfd(lua_State *lua); +tb_int_t xm_io_file_write(lua_State *lua); +tb_int_t xm_io_file_flush(lua_State *lua); +tb_int_t xm_io_file_close(lua_State *lua); +tb_int_t xm_io_file_isatty(lua_State *lua); // the io/filelock functions -tb_int_t xm_io_filelock_open(lua_State* lua); -tb_int_t xm_io_filelock_lock(lua_State* lua); -tb_int_t xm_io_filelock_unlock(lua_State* lua); -tb_int_t xm_io_filelock_trylock(lua_State* lua); -tb_int_t xm_io_filelock_close(lua_State* lua); +tb_int_t xm_io_filelock_open(lua_State *lua); +tb_int_t xm_io_filelock_lock(lua_State *lua); +tb_int_t xm_io_filelock_unlock(lua_State *lua); +tb_int_t xm_io_filelock_trylock(lua_State *lua); +tb_int_t xm_io_filelock_close(lua_State *lua); // the io/socket functions -tb_int_t xm_io_socket_open(lua_State* lua); -tb_int_t xm_io_socket_rawfd(lua_State* lua); -tb_int_t xm_io_socket_peeraddr(lua_State* lua); -tb_int_t xm_io_socket_wait(lua_State* lua); -tb_int_t xm_io_socket_bind(lua_State* lua); -tb_int_t xm_io_socket_ctrl(lua_State* lua); -tb_int_t xm_io_socket_listen(lua_State* lua); -tb_int_t xm_io_socket_accept(lua_State* lua); -tb_int_t xm_io_socket_connect(lua_State* lua); -tb_int_t xm_io_socket_send(lua_State* lua); -tb_int_t xm_io_socket_sendto(lua_State* lua); -tb_int_t xm_io_socket_sendfile(lua_State* lua); -tb_int_t xm_io_socket_recv(lua_State* lua); -tb_int_t xm_io_socket_recvfrom(lua_State* lua); -tb_int_t xm_io_socket_kill(lua_State* lua); -tb_int_t xm_io_socket_close(lua_State* lua); +tb_int_t xm_io_socket_open(lua_State *lua); +tb_int_t xm_io_socket_rawfd(lua_State *lua); +tb_int_t xm_io_socket_peeraddr(lua_State *lua); +tb_int_t xm_io_socket_wait(lua_State *lua); +tb_int_t xm_io_socket_bind(lua_State *lua); +tb_int_t xm_io_socket_ctrl(lua_State *lua); +tb_int_t xm_io_socket_listen(lua_State *lua); +tb_int_t xm_io_socket_accept(lua_State *lua); +tb_int_t xm_io_socket_connect(lua_State *lua); +tb_int_t xm_io_socket_send(lua_State *lua); +tb_int_t xm_io_socket_sendto(lua_State *lua); +tb_int_t xm_io_socket_sendfile(lua_State *lua); +tb_int_t xm_io_socket_recv(lua_State *lua); +tb_int_t xm_io_socket_recvfrom(lua_State *lua); +tb_int_t xm_io_socket_kill(lua_State *lua); +tb_int_t xm_io_socket_close(lua_State *lua); // the io/pipe functions -tb_int_t xm_io_pipe_open(lua_State* lua); -tb_int_t xm_io_pipe_openpair(lua_State* lua); -tb_int_t xm_io_pipe_close(lua_State* lua); -tb_int_t xm_io_pipe_read(lua_State* lua); -tb_int_t xm_io_pipe_write(lua_State* lua); -tb_int_t xm_io_pipe_wait(lua_State* lua); -tb_int_t xm_io_pipe_connect(lua_State* lua); +tb_int_t xm_io_pipe_open(lua_State *lua); +tb_int_t xm_io_pipe_openpair(lua_State *lua); +tb_int_t xm_io_pipe_close(lua_State *lua); +tb_int_t xm_io_pipe_read(lua_State *lua); +tb_int_t xm_io_pipe_write(lua_State *lua); +tb_int_t xm_io_pipe_wait(lua_State *lua); +tb_int_t xm_io_pipe_connect(lua_State *lua); // the io/poller functions -tb_int_t xm_io_poller_insert(lua_State* lua); -tb_int_t xm_io_poller_modify(lua_State* lua); -tb_int_t xm_io_poller_remove(lua_State* lua); -tb_int_t xm_io_poller_spank(lua_State* lua); -tb_int_t xm_io_poller_support(lua_State* lua); -tb_int_t xm_io_poller_wait(lua_State* lua); +tb_int_t xm_io_poller_insert(lua_State *lua); +tb_int_t xm_io_poller_modify(lua_State *lua); +tb_int_t xm_io_poller_remove(lua_State *lua); +tb_int_t xm_io_poller_spank(lua_State *lua); +tb_int_t xm_io_poller_support(lua_State *lua); +tb_int_t xm_io_poller_wait(lua_State *lua); // the path functions -tb_int_t xm_path_relative(lua_State* lua); -tb_int_t xm_path_absolute(lua_State* lua); -tb_int_t xm_path_translate(lua_State* lua); -tb_int_t xm_path_directory(lua_State* lua); -tb_int_t xm_path_is_absolute(lua_State* lua); +tb_int_t xm_path_relative(lua_State *lua); +tb_int_t xm_path_absolute(lua_State *lua); +tb_int_t xm_path_translate(lua_State *lua); +tb_int_t xm_path_directory(lua_State *lua); +tb_int_t xm_path_is_absolute(lua_State *lua); // the hash functions -tb_int_t xm_hash_uuid4(lua_State* lua); -tb_int_t xm_hash_sha(lua_State* lua); -tb_int_t xm_hash_md5(lua_State* lua); -tb_int_t xm_hash_xxhash(lua_State* lua); -tb_int_t xm_hash_rand32(lua_State* lua); -tb_int_t xm_hash_rand64(lua_State* lua); -tb_int_t xm_hash_rand128(lua_State* lua); +tb_int_t xm_hash_uuid4(lua_State *lua); +tb_int_t xm_hash_sha(lua_State *lua); +tb_int_t xm_hash_md5(lua_State *lua); +tb_int_t xm_hash_xxhash(lua_State *lua); +tb_int_t xm_hash_rand32(lua_State *lua); +tb_int_t xm_hash_rand64(lua_State *lua); +tb_int_t xm_hash_rand128(lua_State *lua); // the base64 functions -tb_int_t xm_base64_encode(lua_State* lua); -tb_int_t xm_base64_decode(lua_State* lua); +tb_int_t xm_base64_encode(lua_State *lua); +tb_int_t xm_base64_decode(lua_State *lua); // the lz4 functions -tb_int_t xm_lz4_compress(lua_State* lua); -tb_int_t xm_lz4_decompress(lua_State* lua); -tb_int_t xm_lz4_block_compress(lua_State* lua); -tb_int_t xm_lz4_block_decompress(lua_State* lua); -tb_int_t xm_lz4_compress_file(lua_State* lua); -tb_int_t xm_lz4_decompress_file(lua_State* lua); -tb_int_t xm_lz4_compress_stream_open(lua_State* lua); -tb_int_t xm_lz4_compress_stream_read(lua_State* lua); -tb_int_t xm_lz4_compress_stream_write(lua_State* lua); -tb_int_t xm_lz4_compress_stream_close(lua_State* lua); -tb_int_t xm_lz4_decompress_stream_open(lua_State* lua); -tb_int_t xm_lz4_decompress_stream_read(lua_State* lua); -tb_int_t xm_lz4_decompress_stream_write(lua_State* lua); -tb_int_t xm_lz4_decompress_stream_close(lua_State* lua); +tb_int_t xm_lz4_compress(lua_State *lua); +tb_int_t xm_lz4_decompress(lua_State *lua); +tb_int_t xm_lz4_block_compress(lua_State *lua); +tb_int_t xm_lz4_block_decompress(lua_State *lua); +tb_int_t xm_lz4_compress_file(lua_State *lua); +tb_int_t xm_lz4_decompress_file(lua_State *lua); +tb_int_t xm_lz4_compress_stream_open(lua_State *lua); +tb_int_t xm_lz4_compress_stream_read(lua_State *lua); +tb_int_t xm_lz4_compress_stream_write(lua_State *lua); +tb_int_t xm_lz4_compress_stream_close(lua_State *lua); +tb_int_t xm_lz4_decompress_stream_open(lua_State *lua); +tb_int_t xm_lz4_decompress_stream_read(lua_State *lua); +tb_int_t xm_lz4_decompress_stream_write(lua_State *lua); +tb_int_t xm_lz4_decompress_stream_close(lua_State *lua); // the bloom filter functions -tb_int_t xm_bloom_filter_open(lua_State* lua); -tb_int_t xm_bloom_filter_close(lua_State* lua); -tb_int_t xm_bloom_filter_clear(lua_State* lua); -tb_int_t xm_bloom_filter_data(lua_State* lua); -tb_int_t xm_bloom_filter_size(lua_State* lua); -tb_int_t xm_bloom_filter_get(lua_State* lua); -tb_int_t xm_bloom_filter_set(lua_State* lua); -tb_int_t xm_bloom_filter_data_set(lua_State* lua); +tb_int_t xm_bloom_filter_open(lua_State *lua); +tb_int_t xm_bloom_filter_close(lua_State *lua); +tb_int_t xm_bloom_filter_clear(lua_State *lua); +tb_int_t xm_bloom_filter_data(lua_State *lua); +tb_int_t xm_bloom_filter_size(lua_State *lua); +tb_int_t xm_bloom_filter_get(lua_State *lua); +tb_int_t xm_bloom_filter_set(lua_State *lua); +tb_int_t xm_bloom_filter_data_set(lua_State *lua); // the windows functions #ifdef TB_CONFIG_OS_WINDOWS -tb_int_t xm_winos_cp_info(lua_State* lua); -tb_int_t xm_winos_console_cp(lua_State* lua); -tb_int_t xm_winos_console_output_cp(lua_State* lua); -tb_int_t xm_winos_ansi_cp(lua_State* lua); -tb_int_t xm_winos_oem_cp(lua_State* lua); -tb_int_t xm_winos_logical_drives(lua_State* lua); -tb_int_t xm_winos_registry_query(lua_State* lua); -tb_int_t xm_winos_registry_keys(lua_State* lua); -tb_int_t xm_winos_registry_values(lua_State* lua); -tb_int_t xm_winos_short_path(lua_State* lua); +tb_int_t xm_winos_cp_info(lua_State *lua); +tb_int_t xm_winos_console_cp(lua_State *lua); +tb_int_t xm_winos_console_output_cp(lua_State *lua); +tb_int_t xm_winos_ansi_cp(lua_State *lua); +tb_int_t xm_winos_oem_cp(lua_State *lua); +tb_int_t xm_winos_logical_drives(lua_State *lua); +tb_int_t xm_winos_registry_query(lua_State *lua); +tb_int_t xm_winos_registry_keys(lua_State *lua); +tb_int_t xm_winos_registry_values(lua_State *lua); +tb_int_t xm_winos_short_path(lua_State *lua); #endif // the string functions -tb_int_t xm_string_trim(lua_State* lua); -tb_int_t xm_string_split(lua_State* lua); -tb_int_t xm_string_lastof(lua_State* lua); -tb_int_t xm_string_convert(lua_State* lua); -tb_int_t xm_string_endswith(lua_State* lua); -tb_int_t xm_string_startswith(lua_State* lua); +tb_int_t xm_string_trim(lua_State *lua); +tb_int_t xm_string_split(lua_State *lua); +tb_int_t xm_string_lastof(lua_State *lua); +tb_int_t xm_string_convert(lua_State *lua); +tb_int_t xm_string_endswith(lua_State *lua); +tb_int_t xm_string_startswith(lua_State *lua); // the process functions -tb_int_t xm_process_open(lua_State* lua); -tb_int_t xm_process_openv(lua_State* lua); -tb_int_t xm_process_wait(lua_State* lua); -tb_int_t xm_process_kill(lua_State* lua); -tb_int_t xm_process_close(lua_State* lua); +tb_int_t xm_process_open(lua_State *lua); +tb_int_t xm_process_openv(lua_State *lua); +tb_int_t xm_process_wait(lua_State *lua); +tb_int_t xm_process_kill(lua_State *lua); +tb_int_t xm_process_close(lua_State *lua); // the fwatcher functions -tb_int_t xm_fwatcher_open(lua_State* lua); -tb_int_t xm_fwatcher_add(lua_State* lua); -tb_int_t xm_fwatcher_remove(lua_State* lua); -tb_int_t xm_fwatcher_wait(lua_State* lua); -tb_int_t xm_fwatcher_close(lua_State* lua); +tb_int_t xm_fwatcher_open(lua_State *lua); +tb_int_t xm_fwatcher_add(lua_State *lua); +tb_int_t xm_fwatcher_remove(lua_State *lua); +tb_int_t xm_fwatcher_wait(lua_State *lua); +tb_int_t xm_fwatcher_close(lua_State *lua); // the sandbox functions -tb_int_t xm_sandbox_interactive(lua_State* lua); +tb_int_t xm_sandbox_interactive(lua_State *lua); #ifdef XM_CONFIG_API_HAVE_READLINE // the readline functions -tb_int_t xm_readline_readline(lua_State* lua); -tb_int_t xm_readline_history_list(lua_State* lua); -tb_int_t xm_readline_add_history(lua_State* lua); -tb_int_t xm_readline_clear_history(lua_State* lua); +tb_int_t xm_readline_readline(lua_State *lua); +tb_int_t xm_readline_history_list(lua_State *lua); +tb_int_t xm_readline_add_history(lua_State *lua); +tb_int_t xm_readline_clear_history(lua_State *lua); #endif // the semver functions -tb_int_t xm_semver_parse(lua_State* lua); -tb_int_t xm_semver_compare(lua_State* lua); -tb_int_t xm_semver_satisfies(lua_State* lua); -tb_int_t xm_semver_select(lua_State* lua); +tb_int_t xm_semver_parse(lua_State *lua); +tb_int_t xm_semver_compare(lua_State *lua); +tb_int_t xm_semver_satisfies(lua_State *lua); +tb_int_t xm_semver_select(lua_State *lua); // the libc functions -tb_int_t xm_libc_malloc(lua_State* lua); -tb_int_t xm_libc_free(lua_State* lua); -tb_int_t xm_libc_memcpy(lua_State* lua); -tb_int_t xm_libc_memmov(lua_State* lua); -tb_int_t xm_libc_memset(lua_State* lua); -tb_int_t xm_libc_strndup(lua_State* lua); -tb_int_t xm_libc_dataptr(lua_State* lua); -tb_int_t xm_libc_byteof(lua_State* lua); -tb_int_t xm_libc_setbyte(lua_State* lua); +tb_int_t xm_libc_malloc(lua_State *lua); +tb_int_t xm_libc_free(lua_State *lua); +tb_int_t xm_libc_memcpy(lua_State *lua); +tb_int_t xm_libc_memmov(lua_State *lua); +tb_int_t xm_libc_memset(lua_State *lua); +tb_int_t xm_libc_strndup(lua_State *lua); +tb_int_t xm_libc_dataptr(lua_State *lua); +tb_int_t xm_libc_byteof(lua_State *lua); +tb_int_t xm_libc_setbyte(lua_State *lua); // the tty functions -tb_int_t xm_tty_term_mode(lua_State* lua); +tb_int_t xm_tty_term_mode(lua_State *lua); // the package functions -tb_int_t xm_package_loadxmi(lua_State* lua); +tb_int_t xm_package_loadxmi(lua_State *lua); // the utils functions -tb_int_t xm_utils_bin2c(lua_State* lua); +tb_int_t xm_utils_bin2c(lua_State *lua); #ifdef XM_CONFIG_API_HAVE_CURSES // register curses functions -tb_int_t xm_lua_curses_register(lua_State* lua, tb_char_t const* module); +tb_int_t xm_lua_curses_register(lua_State *lua, tb_char_t const *module); #endif // the thread functions -tb_int_t xm_thread_init(lua_State* lua); -tb_int_t xm_thread_exit(lua_State* lua); -tb_int_t xm_thread_wait(lua_State* lua); -tb_int_t xm_thread_suspend(lua_State* lua); -tb_int_t xm_thread_resume(lua_State* lua); +tb_int_t xm_thread_init(lua_State *lua); +tb_int_t xm_thread_exit(lua_State *lua); +tb_int_t xm_thread_wait(lua_State *lua); +tb_int_t xm_thread_suspend(lua_State *lua); +tb_int_t xm_thread_resume(lua_State *lua); // the thread/mutex functions -tb_int_t xm_thread_mutex_init(lua_State* lua); -tb_int_t xm_thread_mutex_exit(lua_State* lua); -tb_int_t xm_thread_mutex_lock(lua_State* lua); -tb_int_t xm_thread_mutex_trylock(lua_State* lua); -tb_int_t xm_thread_mutex_unlock(lua_State* lua); -tb_int_t xm_thread_mutex_incref(lua_State* lua); +tb_int_t xm_thread_mutex_init(lua_State *lua); +tb_int_t xm_thread_mutex_exit(lua_State *lua); +tb_int_t xm_thread_mutex_lock(lua_State *lua); +tb_int_t xm_thread_mutex_trylock(lua_State *lua); +tb_int_t xm_thread_mutex_unlock(lua_State *lua); +tb_int_t xm_thread_mutex_incref(lua_State *lua); // the thread/event functions -tb_int_t xm_thread_event_init(lua_State* lua); -tb_int_t xm_thread_event_exit(lua_State* lua); -tb_int_t xm_thread_event_post(lua_State* lua); -tb_int_t xm_thread_event_wait(lua_State* lua); -tb_int_t xm_thread_event_incref(lua_State* lua); +tb_int_t xm_thread_event_init(lua_State *lua); +tb_int_t xm_thread_event_exit(lua_State *lua); +tb_int_t xm_thread_event_post(lua_State *lua); +tb_int_t xm_thread_event_wait(lua_State *lua); +tb_int_t xm_thread_event_incref(lua_State *lua); // the thread/semaphore functions -tb_int_t xm_thread_semaphore_init(lua_State* lua); -tb_int_t xm_thread_semaphore_exit(lua_State* lua); -tb_int_t xm_thread_semaphore_post(lua_State* lua); -tb_int_t xm_thread_semaphore_wait(lua_State* lua); -tb_int_t xm_thread_semaphore_incref(lua_State* lua); +tb_int_t xm_thread_semaphore_init(lua_State *lua); +tb_int_t xm_thread_semaphore_exit(lua_State *lua); +tb_int_t xm_thread_semaphore_post(lua_State *lua); +tb_int_t xm_thread_semaphore_wait(lua_State *lua); +tb_int_t xm_thread_semaphore_incref(lua_State *lua); // the thread/queue functions -tb_int_t xm_thread_queue_init(lua_State* lua); -tb_int_t xm_thread_queue_exit(lua_State* lua); -tb_int_t xm_thread_queue_size(lua_State* lua); -tb_int_t xm_thread_queue_clear(lua_State* lua); -tb_int_t xm_thread_queue_incref(lua_State* lua); -tb_int_t xm_thread_queue_push(lua_State* lua); -tb_int_t xm_thread_queue_pop(lua_State* lua); +tb_int_t xm_thread_queue_init(lua_State *lua); +tb_int_t xm_thread_queue_exit(lua_State *lua); +tb_int_t xm_thread_queue_size(lua_State *lua); +tb_int_t xm_thread_queue_clear(lua_State *lua); +tb_int_t xm_thread_queue_incref(lua_State *lua); +tb_int_t xm_thread_queue_push(lua_State *lua); +tb_int_t xm_thread_queue_pop(lua_State *lua); // the thread/sharedata functions -tb_int_t xm_thread_sharedata_init(lua_State* lua); -tb_int_t xm_thread_sharedata_exit(lua_State* lua); -tb_int_t xm_thread_sharedata_clear(lua_State* lua); -tb_int_t xm_thread_sharedata_incref(lua_State* lua); -tb_int_t xm_thread_sharedata_set(lua_State* lua); -tb_int_t xm_thread_sharedata_get_(lua_State* lua); +tb_int_t xm_thread_sharedata_init(lua_State *lua); +tb_int_t xm_thread_sharedata_exit(lua_State *lua); +tb_int_t xm_thread_sharedata_clear(lua_State *lua); +tb_int_t xm_thread_sharedata_incref(lua_State *lua); +tb_int_t xm_thread_sharedata_set(lua_State *lua); +tb_int_t xm_thread_sharedata_get_(lua_State *lua); // open cjson -__tb_extern_c_enter__ -tb_int_t luaopen_cjson(lua_State *l); +__tb_extern_c_enter__ tb_int_t luaopen_cjson(lua_State *l); __tb_extern_c_leave__ /* ////////////////////////////////////////////////////////////////////////////////////// @@ -393,341 +391,320 @@ __tb_extern_c_leave__ */ // the os functions -static luaL_Reg const g_os_functions[] = -{ - { "argv", xm_os_argv } -, { "args", xm_os_args } -, { "find", xm_os_find } -, { "link", xm_os_link } -, { "isdir", xm_os_isdir } -, { "rmdir", xm_os_rmdir } -, { "mkdir", xm_os_mkdir } -, { "cpdir", xm_os_cpdir } -, { "chdir", xm_os_chdir } -, { "mtime", xm_os_mtime } -, { "sleep", xm_os_sleep } -, { "mclock", xm_os_mclock } -, { "curdir", xm_os_curdir } -, { "tmpdir", xm_os_tmpdir } -, { "islink", xm_os_islink } -, { "isfile", xm_os_isfile } -, { "touch", xm_os_touch } -, { "rmfile", xm_os_rmfile } -, { "cpfile", xm_os_cpfile } -, { "fscase", xm_os_fscase } -, { "rename", xm_os_rename } -, { "exists", xm_os_exists } -, { "setenv", xm_os_setenv } -, { "getenv", xm_os_getenv } -, { "getenvs", xm_os_getenvs } -, { "cpuinfo", xm_os_cpuinfo } -, { "meminfo", xm_os_meminfo } -, { "readlink", xm_os_readlink } -, { "emptydir", xm_os_emptydir } -, { "strerror", xm_os_strerror } -, { "syserror", xm_os_syserror } -, { "filesize", xm_os_filesize } -, { "getwinsize", xm_os_getwinsize} -, { "getpid", xm_os_getpid } -, { "signal", xm_os_signal } +static luaL_Reg const g_os_functions[] = { + { "argv", xm_os_argv }, + { "args", xm_os_args }, + { "find", xm_os_find }, + { "link", xm_os_link }, + { "isdir", xm_os_isdir }, + { "rmdir", xm_os_rmdir }, + { "mkdir", xm_os_mkdir }, + { "cpdir", xm_os_cpdir }, + { "chdir", xm_os_chdir }, + { "mtime", xm_os_mtime }, + { "sleep", xm_os_sleep }, + { "mclock", xm_os_mclock }, + { "curdir", xm_os_curdir }, + { "tmpdir", xm_os_tmpdir }, + { "islink", xm_os_islink }, + { "isfile", xm_os_isfile }, + { "touch", xm_os_touch }, + { "rmfile", xm_os_rmfile }, + { "cpfile", xm_os_cpfile }, + { "fscase", xm_os_fscase }, + { "rename", xm_os_rename }, + { "exists", xm_os_exists }, + { "setenv", xm_os_setenv }, + { "getenv", xm_os_getenv }, + { "getenvs", xm_os_getenvs }, + { "cpuinfo", xm_os_cpuinfo }, + { "meminfo", xm_os_meminfo }, + { "readlink", xm_os_readlink }, + { "emptydir", xm_os_emptydir }, + { "strerror", xm_os_strerror }, + { "syserror", xm_os_syserror }, + { "filesize", xm_os_filesize }, + { "getwinsize", xm_os_getwinsize }, + { "getpid", xm_os_getpid }, + { "signal", xm_os_signal }, #ifndef TB_CONFIG_OS_WINDOWS -, { "uid", xm_os_uid } -, { "gid", xm_os_gid } -, { "getown", xm_os_getown } + { "uid", xm_os_uid }, + { "gid", xm_os_gid }, + { "getown", xm_os_getown }, #endif -, { tb_null, tb_null } + { tb_null, tb_null }, }; // the windows functions #ifdef TB_CONFIG_OS_WINDOWS -static luaL_Reg const g_winos_functions[] = -{ - { "cp_info", xm_winos_cp_info } -, { "console_cp", xm_winos_console_cp } -, { "console_output_cp", xm_winos_console_output_cp } -, { "oem_cp", xm_winos_oem_cp } -, { "ansi_cp", xm_winos_ansi_cp } -, { "logical_drives", xm_winos_logical_drives } -, { "registry_query", xm_winos_registry_query } -, { "registry_keys", xm_winos_registry_keys } -, { "registry_values", xm_winos_registry_values } -, { "short_path", xm_winos_short_path } -, { tb_null, tb_null } +static luaL_Reg const g_winos_functions[] = { + { "cp_info", xm_winos_cp_info }, + { "console_cp", xm_winos_console_cp }, + { "console_output_cp", xm_winos_console_output_cp }, + { "oem_cp", xm_winos_oem_cp }, + { "ansi_cp", xm_winos_ansi_cp }, + { "logical_drives", xm_winos_logical_drives }, + { "registry_query", xm_winos_registry_query }, + { "registry_keys", xm_winos_registry_keys }, + { "registry_values", xm_winos_registry_values }, + { "short_path", xm_winos_short_path }, + { tb_null, tb_null }, }; #endif // the io functions -static luaL_Reg const g_io_functions[] = -{ - { "stdfile", xm_io_stdfile } -, { "file_open", xm_io_file_open } -, { "file_read", xm_io_file_read } -, { "file_readable", xm_io_file_readable } -, { "file_seek", xm_io_file_seek } -, { "file_size", xm_io_file_size } -, { "file_write", xm_io_file_write } -, { "file_flush", xm_io_file_flush } -, { "file_isatty", xm_io_file_isatty } -, { "file_close", xm_io_file_close } -, { "file_rawfd", xm_io_file_rawfd } -, { "filelock_open", xm_io_filelock_open } -, { "filelock_lock", xm_io_filelock_lock } -, { "filelock_trylock", xm_io_filelock_trylock } -, { "filelock_unlock", xm_io_filelock_unlock } -, { "filelock_close", xm_io_filelock_close } -, { "socket_open", xm_io_socket_open } -, { "socket_rawfd", xm_io_socket_rawfd } -, { "socket_peeraddr", xm_io_socket_peeraddr } -, { "socket_wait", xm_io_socket_wait } -, { "socket_bind", xm_io_socket_bind } -, { "socket_ctrl", xm_io_socket_ctrl } -, { "socket_listen", xm_io_socket_listen } -, { "socket_accept", xm_io_socket_accept } -, { "socket_connect", xm_io_socket_connect } -, { "socket_send", xm_io_socket_send } -, { "socket_sendto", xm_io_socket_sendto } -, { "socket_sendfile", xm_io_socket_sendfile } -, { "socket_recv", xm_io_socket_recv } -, { "socket_recvfrom", xm_io_socket_recvfrom } -, { "socket_kill" , xm_io_socket_kill } -, { "socket_close", xm_io_socket_close } -, { "pipe_open", xm_io_pipe_open } -, { "pipe_openpair", xm_io_pipe_openpair } -, { "pipe_close", xm_io_pipe_close } -, { "pipe_read", xm_io_pipe_read } -, { "pipe_write", xm_io_pipe_write } -, { "pipe_wait", xm_io_pipe_wait } -, { "pipe_connect", xm_io_pipe_connect } -, { "poller_insert", xm_io_poller_insert } -, { "poller_modify", xm_io_poller_modify } -, { "poller_remove", xm_io_poller_remove } -, { "poller_spank", xm_io_poller_spank } -, { "poller_support", xm_io_poller_support } -, { "poller_wait", xm_io_poller_wait } -, { tb_null, tb_null } +static luaL_Reg const g_io_functions[] = { + { "stdfile", xm_io_stdfile }, + { "file_open", xm_io_file_open }, + { "file_read", xm_io_file_read }, + { "file_readable", xm_io_file_readable }, + { "file_seek", xm_io_file_seek }, + { "file_size", xm_io_file_size }, + { "file_write", xm_io_file_write }, + { "file_flush", xm_io_file_flush }, + { "file_isatty", xm_io_file_isatty }, + { "file_close", xm_io_file_close }, + { "file_rawfd", xm_io_file_rawfd }, + { "filelock_open", xm_io_filelock_open }, + { "filelock_lock", xm_io_filelock_lock }, + { "filelock_trylock", xm_io_filelock_trylock }, + { "filelock_unlock", xm_io_filelock_unlock }, + { "filelock_close", xm_io_filelock_close }, + { "socket_open", xm_io_socket_open }, + { "socket_rawfd", xm_io_socket_rawfd }, + { "socket_peeraddr", xm_io_socket_peeraddr }, + { "socket_wait", xm_io_socket_wait }, + { "socket_bind", xm_io_socket_bind }, + { "socket_ctrl", xm_io_socket_ctrl }, + { "socket_listen", xm_io_socket_listen }, + { "socket_accept", xm_io_socket_accept }, + { "socket_connect", xm_io_socket_connect }, + { "socket_send", xm_io_socket_send }, + { "socket_sendto", xm_io_socket_sendto }, + { "socket_sendfile", xm_io_socket_sendfile }, + { "socket_recv", xm_io_socket_recv }, + { "socket_recvfrom", xm_io_socket_recvfrom }, + { "socket_kill", xm_io_socket_kill }, + { "socket_close", xm_io_socket_close }, + { "pipe_open", xm_io_pipe_open }, + { "pipe_openpair", xm_io_pipe_openpair }, + { "pipe_close", xm_io_pipe_close }, + { "pipe_read", xm_io_pipe_read }, + { "pipe_write", xm_io_pipe_write }, + { "pipe_wait", xm_io_pipe_wait }, + { "pipe_connect", xm_io_pipe_connect }, + { "poller_insert", xm_io_poller_insert }, + { "poller_modify", xm_io_poller_modify }, + { "poller_remove", xm_io_poller_remove }, + { "poller_spank", xm_io_poller_spank }, + { "poller_support", xm_io_poller_support }, + { "poller_wait", xm_io_poller_wait }, + { tb_null, tb_null }, }; // the path functions -static luaL_Reg const g_path_functions[] = -{ - { "relative", xm_path_relative } -, { "absolute", xm_path_absolute } -, { "translate", xm_path_translate } -, { "directory", xm_path_directory } -, { "is_absolute", xm_path_is_absolute } -, { tb_null, tb_null } +static luaL_Reg const g_path_functions[] = { + { "relative", xm_path_relative }, + { "absolute", xm_path_absolute }, + { "translate", xm_path_translate }, + { "directory", xm_path_directory }, + { "is_absolute", xm_path_is_absolute }, + { tb_null, tb_null }, }; // the hash functions -static luaL_Reg const g_hash_functions[] = -{ - { "uuid4", xm_hash_uuid4 } -, { "sha", xm_hash_sha } -, { "md5", xm_hash_md5 } -, { "xxhash", xm_hash_xxhash } -, { "rand32", xm_hash_rand32 } -, { "rand64", xm_hash_rand64 } -, { "rand128", xm_hash_rand128 } -, { tb_null, tb_null } +static luaL_Reg const g_hash_functions[] = { + { "uuid4", xm_hash_uuid4 }, + { "sha", xm_hash_sha }, + { "md5", xm_hash_md5 }, + { "xxhash", xm_hash_xxhash }, + { "rand32", xm_hash_rand32 }, + { "rand64", xm_hash_rand64 }, + { "rand128", xm_hash_rand128 }, + { tb_null, tb_null }, }; // the base64 functions -static luaL_Reg const g_base64_functions[] = -{ - { "encode", xm_base64_encode } -, { "decode", xm_base64_decode } -, { tb_null, tb_null } +static luaL_Reg const g_base64_functions[] = { + { "encode", xm_base64_encode }, + { "decode", xm_base64_decode }, + { tb_null, tb_null }, }; // the lz4 functions -static luaL_Reg const g_lz4_functions[] = -{ - { "compress", xm_lz4_compress } -, { "decompress", xm_lz4_decompress } -, { "block_compress", xm_lz4_block_compress } -, { "block_decompress", xm_lz4_block_decompress } -, { "compress_file", xm_lz4_compress_file } -, { "decompress_file", xm_lz4_decompress_file } -, { "compress_stream_open", xm_lz4_compress_stream_open } -, { "compress_stream_read", xm_lz4_compress_stream_read } -, { "compress_stream_write", xm_lz4_compress_stream_write } -, { "compress_stream_close", xm_lz4_compress_stream_close } -, { "decompress_stream_open", xm_lz4_decompress_stream_open } -, { "decompress_stream_read", xm_lz4_decompress_stream_read } -, { "decompress_stream_write",xm_lz4_decompress_stream_write } -, { "decompress_stream_close",xm_lz4_decompress_stream_close } -, { tb_null, tb_null } +static luaL_Reg const g_lz4_functions[] = { + { "compress", xm_lz4_compress }, + { "decompress", xm_lz4_decompress }, + { "block_compress", xm_lz4_block_compress }, + { "block_decompress", xm_lz4_block_decompress }, + { "compress_file", xm_lz4_compress_file }, + { "decompress_file", xm_lz4_decompress_file }, + { "compress_stream_open", xm_lz4_compress_stream_open }, + { "compress_stream_read", xm_lz4_compress_stream_read }, + { "compress_stream_write", xm_lz4_compress_stream_write }, + { "compress_stream_close", xm_lz4_compress_stream_close }, + { "decompress_stream_open", xm_lz4_decompress_stream_open }, + { "decompress_stream_read", xm_lz4_decompress_stream_read }, + { "decompress_stream_write", xm_lz4_decompress_stream_write }, + { "decompress_stream_close", xm_lz4_decompress_stream_close }, + { tb_null, tb_null }, }; // the bloom filter functions -static luaL_Reg const g_bloom_filter_functions[] = -{ - { "open", xm_bloom_filter_open } -, { "close", xm_bloom_filter_close } -, { "clear", xm_bloom_filter_clear } -, { "data", xm_bloom_filter_data } -, { "size", xm_bloom_filter_size } -, { "get", xm_bloom_filter_get } -, { "set", xm_bloom_filter_set } -, { "data_set", xm_bloom_filter_data_set } -, { tb_null, tb_null } +static luaL_Reg const g_bloom_filter_functions[] = { + { "open", xm_bloom_filter_open }, + { "close", xm_bloom_filter_close }, + { "clear", xm_bloom_filter_clear }, + { "data", xm_bloom_filter_data }, + { "size", xm_bloom_filter_size }, + { "get", xm_bloom_filter_get }, + { "set", xm_bloom_filter_set }, + { "data_set", xm_bloom_filter_data_set }, + { tb_null, tb_null }, }; // the string functions -static luaL_Reg const g_string_functions[] = -{ - { "trim", xm_string_trim } -, { "split", xm_string_split } -, { "lastof", xm_string_lastof } -, { "convert", xm_string_convert } -, { "endswith", xm_string_endswith } -, { "startswith", xm_string_startswith } -, { tb_null, tb_null } +static luaL_Reg const g_string_functions[] = { + { "trim", xm_string_trim }, + { "split", xm_string_split }, + { "lastof", xm_string_lastof }, + { "convert", xm_string_convert }, + { "endswith", xm_string_endswith }, + { "startswith", xm_string_startswith }, + { tb_null, tb_null }, }; // the process functions -static luaL_Reg const g_process_functions[] = -{ - { "open", xm_process_open } -, { "openv", xm_process_openv } -, { "wait", xm_process_wait } -, { "kill", xm_process_kill } -, { "close", xm_process_close } -, { tb_null, tb_null } +static luaL_Reg const g_process_functions[] = { + { "open", xm_process_open }, + { "openv", xm_process_openv }, + { "wait", xm_process_wait }, + { "kill", xm_process_kill }, + { "close", xm_process_close }, + { tb_null, tb_null }, }; // the fwatcher functions -static luaL_Reg const g_fwatcher_functions[] = -{ - { "open", xm_fwatcher_open } -, { "add", xm_fwatcher_add } -, { "remove", xm_fwatcher_remove } -, { "wait", xm_fwatcher_wait } -, { "close", xm_fwatcher_close } -, { tb_null, tb_null } +static luaL_Reg const g_fwatcher_functions[] = { + { "open", xm_fwatcher_open }, + { "add", xm_fwatcher_add }, + { "remove", xm_fwatcher_remove }, + { "wait", xm_fwatcher_wait }, + { "close", xm_fwatcher_close }, + { tb_null, tb_null }, }; // the sandbox functions -static luaL_Reg const g_sandbox_functions[] = -{ - { "interactive", xm_sandbox_interactive } -, { tb_null, tb_null } +static luaL_Reg const g_sandbox_functions[] = { + { "interactive", xm_sandbox_interactive }, + { tb_null, tb_null }, }; #ifdef XM_CONFIG_API_HAVE_READLINE // the readline functions -static luaL_Reg const g_readline_functions[] = -{ - { "readline", xm_readline_readline } -, { "history_list", xm_readline_history_list } -, { "add_history", xm_readline_add_history } -, { "clear_history", xm_readline_clear_history} -, { tb_null, tb_null } +static luaL_Reg const g_readline_functions[] = { + { "readline", xm_readline_readline }, + { "history_list", xm_readline_history_list }, + { "add_history", xm_readline_add_history }, + { "clear_history", xm_readline_clear_history }, + { tb_null, tb_null }, }; #endif // the semver functions -static luaL_Reg const g_semver_functions[] = -{ - { "parse", xm_semver_parse } -, { "compare", xm_semver_compare } -, { "satisfies", xm_semver_satisfies } -, { "select", xm_semver_select } -, { tb_null, tb_null } +static luaL_Reg const g_semver_functions[] = { + { "parse", xm_semver_parse }, + { "compare", xm_semver_compare }, + { "satisfies", xm_semver_satisfies }, + { "select", xm_semver_select }, + { tb_null, tb_null }, }; // the libc functions -static luaL_Reg const g_libc_functions[] = -{ - { "malloc", xm_libc_malloc } -, { "free", xm_libc_free } -, { "memcpy", xm_libc_memcpy } -, { "memset", xm_libc_memset } -, { "memmov", xm_libc_memmov } -, { "strndup", xm_libc_strndup } -, { "dataptr", xm_libc_dataptr } -, { "byteof", xm_libc_byteof } -, { "setbyte", xm_libc_setbyte } -, { tb_null, tb_null } +static luaL_Reg const g_libc_functions[] = { + { "malloc", xm_libc_malloc }, + { "free", xm_libc_free }, + { "memcpy", xm_libc_memcpy }, + { "memset", xm_libc_memset }, + { "memmov", xm_libc_memmov }, + { "strndup", xm_libc_strndup }, + { "dataptr", xm_libc_dataptr }, + { "byteof", xm_libc_byteof }, + { "setbyte", xm_libc_setbyte }, + { tb_null, tb_null }, }; // the tty functions -static luaL_Reg const g_tty_functions[] = -{ - { "term_mode", xm_tty_term_mode } -, { tb_null, tb_null } +static luaL_Reg const g_tty_functions[] = { + { "term_mode", xm_tty_term_mode }, + { tb_null, tb_null }, }; // the package functions -static luaL_Reg const g_package_functions[] = -{ - { "loadxmi", xm_package_loadxmi } -, { tb_null, tb_null } +static luaL_Reg const g_package_functions[] = { + { "loadxmi", xm_package_loadxmi }, + { tb_null, tb_null }, }; // the utils functions -static luaL_Reg const g_utils_functions[] = -{ - { "bin2c", xm_utils_bin2c } -, { tb_null, tb_null } +static luaL_Reg const g_utils_functions[] = { + { "bin2c", xm_utils_bin2c }, + { tb_null, tb_null }, }; // the thread functions -static luaL_Reg const g_thread_functions[] = -{ - { "thread_init", xm_thread_init } -, { "thread_exit", xm_thread_exit } -, { "thread_wait", xm_thread_wait } -, { "thread_resume", xm_thread_resume } -, { "thread_suspend", xm_thread_suspend } -, { "mutex_init", xm_thread_mutex_init } -, { "mutex_exit", xm_thread_mutex_exit } -, { "mutex_lock", xm_thread_mutex_lock } -, { "mutex_trylock", xm_thread_mutex_trylock } -, { "mutex_unlock", xm_thread_mutex_unlock } -, { "mutex_incref", xm_thread_mutex_incref } -, { "event_init", xm_thread_event_init } -, { "event_exit", xm_thread_event_exit } -, { "event_post", xm_thread_event_post } -, { "event_wait", xm_thread_event_wait } -, { "event_incref", xm_thread_event_incref } -, { "semaphore_init", xm_thread_semaphore_init } -, { "semaphore_exit", xm_thread_semaphore_exit } -, { "semaphore_post", xm_thread_semaphore_post } -, { "semaphore_wait", xm_thread_semaphore_wait } -, { "semaphore_incref", xm_thread_semaphore_incref } -, { "queue_init", xm_thread_queue_init } -, { "queue_exit", xm_thread_queue_exit } -, { "queue_size", xm_thread_queue_size } -, { "queue_clear", xm_thread_queue_clear } -, { "queue_incref", xm_thread_queue_incref } -, { "queue_push", xm_thread_queue_push } -, { "queue_pop", xm_thread_queue_pop } -, { "sharedata_init", xm_thread_sharedata_init } -, { "sharedata_exit", xm_thread_sharedata_exit } -, { "sharedata_clear", xm_thread_sharedata_clear } -, { "sharedata_incref", xm_thread_sharedata_incref } -, { "sharedata_set", xm_thread_sharedata_set } -, { "sharedata_get", xm_thread_sharedata_get_ } -, { tb_null, tb_null } +static luaL_Reg const g_thread_functions[] = { + { "thread_init", xm_thread_init }, + { "thread_exit", xm_thread_exit }, + { "thread_wait", xm_thread_wait }, + { "thread_resume", xm_thread_resume }, + { "thread_suspend", xm_thread_suspend }, + { "mutex_init", xm_thread_mutex_init }, + { "mutex_exit", xm_thread_mutex_exit }, + { "mutex_lock", xm_thread_mutex_lock }, + { "mutex_trylock", xm_thread_mutex_trylock }, + { "mutex_unlock", xm_thread_mutex_unlock }, + { "mutex_incref", xm_thread_mutex_incref }, + { "event_init", xm_thread_event_init }, + { "event_exit", xm_thread_event_exit }, + { "event_post", xm_thread_event_post }, + { "event_wait", xm_thread_event_wait }, + { "event_incref", xm_thread_event_incref }, + { "semaphore_init", xm_thread_semaphore_init }, + { "semaphore_exit", xm_thread_semaphore_exit }, + { "semaphore_post", xm_thread_semaphore_post }, + { "semaphore_wait", xm_thread_semaphore_wait }, + { "semaphore_incref", xm_thread_semaphore_incref }, + { "queue_init", xm_thread_queue_init }, + { "queue_exit", xm_thread_queue_exit }, + { "queue_size", xm_thread_queue_size }, + { "queue_clear", xm_thread_queue_clear }, + { "queue_incref", xm_thread_queue_incref }, + { "queue_push", xm_thread_queue_push }, + { "queue_pop", xm_thread_queue_pop }, + { "sharedata_init", xm_thread_sharedata_init }, + { "sharedata_exit", xm_thread_sharedata_exit }, + { "sharedata_clear", xm_thread_sharedata_clear }, + { "sharedata_incref", xm_thread_sharedata_incref }, + { "sharedata_set", xm_thread_sharedata_set }, + { "sharedata_get", xm_thread_sharedata_get_ }, + { tb_null, tb_null }, }; // the lua global instance for signal handler -static lua_State* g_lua = tb_null; +static lua_State *g_lua = tb_null; // the xmake script files data #ifdef XM_EMBED_ENABLE static tb_byte_t g_xmake_xmz_data[] = { - #include "xmake.xmz.h" +#include "xmake.xmz.h" }; #endif /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_bool_t xm_engine_save_arguments(xm_engine_t* engine, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv) -{ - // check +static tb_bool_t xm_engine_save_arguments(xm_engine_t *engine, tb_int_t argc, tb_char_t **argv, tb_char_t **taskargv) { tb_assert_and_check_return_val(engine && engine->lua && argc >= 1 && argv, tb_false); #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) @@ -738,11 +715,9 @@ static tb_bool_t xm_engine_save_arguments(xm_engine_t* engine, tb_int_t argc, tb lua_newtable(engine->lua); // patch the task arguments list - if (taskargv) - { - tb_char_t** taskarg = taskargv; - while (*taskarg) - { + if (taskargv) { + tb_char_t **taskarg = taskargv; + while (*taskarg) { lua_pushstring(engine->lua, *taskarg); lua_rawseti(engine->lua, -2, (int)lua_objlen(engine->lua, -2) + 1); taskarg++; @@ -751,10 +726,9 @@ static tb_bool_t xm_engine_save_arguments(xm_engine_t* engine, tb_int_t argc, tb // save all arguments to the new table tb_int_t i = 0; - for (i = 1; i < argc; i++) - { + for (i = 1; i < argc; i++) { #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) - tb_char_t argvbuf[4096] = {0}; + tb_char_t argvbuf[4096] = { 0 }; tb_wcstombs(argvbuf, argvw[i], tb_arrayn(argvbuf)); // table_new[table.getn(table_new) + 1] = argv[i] lua_pushstring(engine->lua, argvbuf); @@ -769,9 +743,7 @@ static tb_bool_t xm_engine_save_arguments(xm_engine_t* engine, tb_int_t argc, tb return tb_true; } -static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** argv, tb_char_t* path, tb_size_t maxn) -{ - // check +static tb_bool_t xm_engine_get_program_file(xm_engine_t *engine, tb_char_t **argv, tb_char_t *path, tb_size_t maxn) { tb_assert_and_check_return_val(engine && path && maxn, tb_false); /* we cache it, because the current path will be changed in thread. @@ -779,9 +751,8 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** arg * The executable file compiled using cosmocc on macOS might retrieve a relative path to the programfile. * If the root directory has been changed, the retrieved programfile will be a non-existent path. */ - static tb_char_t s_program_filepath[TB_PATH_MAXN] = {0}; - if (s_program_filepath[0]) - { + static tb_char_t s_program_filepath[TB_PATH_MAXN] = { 0 }; + if (s_program_filepath[0]) { tb_strlcpy(path, s_program_filepath, maxn); lua_pushstring(engine->lua, s_program_filepath); lua_setglobal(engine->lua, "_PROGRAM_FILE"); @@ -789,27 +760,23 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** arg } tb_bool_t ok = tb_false; - do - { + do { // get it from the environment variable first - if (tb_environment_first("XMAKE_PROGRAM_FILE", path, maxn) && tb_file_info(path, tb_null)) - { + if (tb_environment_first("XMAKE_PROGRAM_FILE", path, maxn) && tb_file_info(path, tb_null)) { ok = tb_true; break; } #if defined(TB_CONFIG_OS_WINDOWS) // get the executale file path as program directory - tb_wchar_t buf[TB_PATH_MAXN] = {0}; + tb_wchar_t buf[TB_PATH_MAXN] = { 0 }; tb_size_t size = (tb_size_t)GetModuleFileNameW(tb_null, buf, (DWORD)TB_PATH_MAXN); tb_assert_and_check_break(size < TB_PATH_MAXN); - // end - buf[size] = L'\0'; - size = tb_wcstombs(path, buf, maxn); + buf[size] = L'\0'; + size = tb_wcstombs(path, buf, maxn); tb_assert_and_check_break(size < maxn); path[size] = '\0'; - // ok ok = tb_true; #elif defined(TB_CONFIG_OS_MACOSX) || defined(TB_CONFIG_OS_IOS) @@ -832,44 +799,41 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** arg * @see it may be a relative path */ ssize_t size = readlink(XM_PROC_SELF_FILE, path, (size_t)maxn); - if (size > 0 && size < maxn) - { + if (size > 0 && size < maxn) { path[size] = '\0'; - ok = tb_true; + ok = tb_true; } #elif defined(TB_CONFIG_OS_BSD) && defined(KERN_PROC_PATHNAME) // only for FreeBSD and OpenBSD, https://github.com/xmake-io/xmake/issues/2948 - tb_int_t mib[4]; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PATHNAME; mib[3] = -1; + tb_int_t mib[4]; + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = KERN_PROC_PATHNAME; + mib[3] = -1; size_t size = maxn; - if (sysctl(mib, 4, path, &size, tb_null, 0) == 0 && size < maxn) - { + if (sysctl(mib, 4, path, &size, tb_null, 0) == 0 && size < maxn) { path[size] = '\0'; - ok = tb_true; + ok = tb_true; } #elif defined(TB_CONFIG_OS_HAIKU) - int32 cookie = 0; + int32 cookie = 0; image_info info; - while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) - { - if (info.type == B_APP_IMAGE) - { + while (get_next_image_info(B_CURRENT_TEAM, &cookie, &info) == B_OK) { + if (info.type == B_APP_IMAGE) { tb_strlcpy(path, info.name, maxn); ok = tb_true; break; } } #else - static tb_char_t const* s_paths[] = - { + static tb_char_t const *s_paths[] = { "~/.local/bin/xmake", "/usr/local/bin/xmake", - "/usr/bin/xmake" + "/usr/bin/xmake", }; - for (tb_size_t i = 0; i < tb_arrayn(s_paths); i++) - { - tb_char_t const* p = s_paths[i]; - if (tb_file_info(p, tb_null)) - { + for (tb_size_t i = 0; i < tb_arrayn(s_paths); i++) { + tb_char_t const *p = s_paths[i]; + if (tb_file_info(p, tb_null)) { tb_strlcpy(path, p, maxn); ok = tb_true; break; @@ -877,11 +841,9 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** arg } #endif - if (!ok && argv) - { - tb_char_t const* p = argv[0]; - if (p && tb_file_info(p, tb_null)) - { + if (!ok && argv) { + tb_char_t const *p = argv[0]; + if (p && tb_file_info(p, tb_null)) { tb_strlcpy(path, p, maxn); ok = tb_true; } @@ -889,10 +851,7 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** arg } while (0); - // ok? - if (ok) - { - // trace + if (ok) { tb_trace_d("programfile: %s", path); // cache it @@ -906,11 +865,12 @@ static tb_bool_t xm_engine_get_program_file(xm_engine_t* engine, tb_char_t** arg } #ifdef XM_EMBED_ENABLE -static tb_bool_t xm_engine_get_temporary_directory(tb_char_t* path, tb_size_t maxn, tb_char_t const* name, tb_char_t const* version_cstr) -{ - tb_char_t data[TB_PATH_MAXN] = {0}; - if (tb_directory_temporary(data, sizeof(data))) - { +static tb_bool_t xm_engine_get_temporary_directory(tb_char_t *path, + tb_size_t maxn, + tb_char_t const *name, + tb_char_t const *version_cstr) { + tb_char_t data[TB_PATH_MAXN] = { 0 }; + if (tb_directory_temporary(data, sizeof(data))) { // get euid tb_int_t euid = 0; #ifndef TB_CONFIG_OS_WINDOWS @@ -924,14 +884,14 @@ static tb_bool_t xm_engine_get_temporary_directory(tb_char_t* path, tb_size_t ma } #endif -static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t* path, tb_size_t maxn, tb_char_t const* programfile) -{ - // check +static tb_bool_t xm_engine_get_program_directory(xm_engine_t *engine, + tb_char_t *path, + tb_size_t maxn, + tb_char_t const *programfile) { tb_assert_and_check_return_val(engine && path && maxn, tb_false); - static tb_char_t s_program_directory[TB_PATH_MAXN] = {0}; - if (s_program_directory[0]) - { + static tb_char_t s_program_directory[TB_PATH_MAXN] = { 0 }; + if (s_program_directory[0]) { tb_strlcpy(path, s_program_directory, maxn); lua_pushstring(engine->lua, s_program_directory); lua_setglobal(engine->lua, "_PROGRAM_DIR"); @@ -939,58 +899,55 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t* } tb_bool_t ok = tb_false; - tb_char_t data[TB_PATH_MAXN] = {0}; - do - { + tb_char_t data[TB_PATH_MAXN] = { 0 }; + do { #ifdef XM_EMBED_ENABLE tb_size_t embedcount = engine->embedcount; - if (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 { + tb_strlcpy(path, engine->tmpdir, maxn); } - else tb_strlcpy(path, engine->tmpdir, maxn); ok = tb_true; break; #endif // get it from the environment variable first - if (tb_environment_first("XMAKE_PROGRAM_DIR", data, sizeof(data)) && tb_path_absolute(data, path, maxn)) - { + if (tb_environment_first("XMAKE_PROGRAM_DIR", data, sizeof(data)) && tb_path_absolute(data, path, maxn)) { ok = tb_true; break; } // get it from program file path - if (programfile) - { + if (programfile) { // get real program file path from the symbol link #if !defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_CONFIG_OS_IOS) tb_char_t programpath[TB_PATH_MAXN]; tb_long_t size = readlink(programfile, programpath, sizeof(programpath)); - if (size >= 0 && size < sizeof(programpath)) - { + if (size >= 0 && size < sizeof(programpath)) { programpath[size] = '\0'; // soft link to relative path? fix it - if (!tb_path_is_absolute(programpath)) - { - tb_char_t buff[TB_PATH_MAXN]; - tb_char_t const* rootdir = tb_path_directory(programfile, buff, sizeof(buff)); - if (rootdir && tb_path_absolute_to(rootdir, programpath, path, maxn)) // @note path and programfile are same buffer + if (!tb_path_is_absolute(programpath)) { + tb_char_t buff[TB_PATH_MAXN]; + tb_char_t const *rootdir = tb_path_directory(programfile, buff, sizeof(buff)); + if (rootdir && tb_path_absolute_to( + rootdir, programpath, path, maxn)) // @note path and programfile are same buffer tb_strlcpy(programpath, path, maxn); } - } - else tb_strlcpy(programpath, programfile, sizeof(programpath)); + } else + tb_strlcpy(programpath, programfile, sizeof(programpath)); #else - tb_char_t const* programpath = programfile; + tb_char_t const *programpath = programfile; #endif // get the root directory - tb_char_t data[TB_PATH_MAXN]; - tb_char_t const* rootdir = tb_path_directory(programpath, data, sizeof(data)); + tb_char_t data[TB_PATH_MAXN]; + tb_char_t const *rootdir = tb_path_directory(programpath, data, sizeof(data)); tb_assert_and_check_break(rootdir); // init share/name sub-directory @@ -998,17 +955,18 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t* tb_snprintf(sharedir, sizeof(sharedir), "../share/%s", engine->name); // find the program (lua) directory - tb_size_t i; - tb_file_info_t info; - tb_char_t scriptpath[TB_PATH_MAXN]; - tb_char_t const* subdirs[] = {".", sharedir}; - for (i = 0; i < tb_arrayn(subdirs); i++) - { + tb_size_t i; + tb_file_info_t info; + tb_char_t scriptpath[TB_PATH_MAXN]; + tb_char_t const *subdirs[] = { + ".", + sharedir, + }; + for (i = 0; i < tb_arrayn(subdirs); i++) { // get program directory if (tb_path_absolute_to(rootdir, subdirs[i], path, maxn) && tb_path_absolute_to(path, "core/_xmake_main.lua", scriptpath, sizeof(scriptpath)) && - tb_file_info(scriptpath, &info) && info.type == TB_FILE_TYPE_FILE) - { + tb_file_info(scriptpath, &info) && info.type == TB_FILE_TYPE_FILE) { ok = tb_true; break; } @@ -1017,10 +975,7 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t* } while (0); - // ok? - if (ok) - { - // trace + if (ok) { tb_trace_d("programdir: %s", path); // cache it @@ -1031,49 +986,42 @@ static tb_bool_t xm_engine_get_program_directory(xm_engine_t* engine, tb_char_t* lua_setglobal(engine->lua, "_PROGRAM_DIR"); } - // ok? return ok; } -static tb_bool_t xm_engine_get_project_directory(xm_engine_t* engine, tb_char_t* path, tb_size_t maxn) -{ - // check +static tb_bool_t xm_engine_get_project_directory(xm_engine_t *engine, tb_char_t *path, tb_size_t maxn) { tb_assert_and_check_return_val(engine && path && maxn, tb_false); tb_bool_t ok = tb_false; - do - { + do { // attempt to get it from the environment variable first - tb_char_t data[TB_PATH_MAXN] = {0}; - if ( !tb_environment_first("XMAKE_PROJECT_DIR", data, sizeof(data)) - || !tb_path_absolute(data, path, maxn)) - { + tb_char_t data[TB_PATH_MAXN] = { 0 }; + if (!tb_environment_first("XMAKE_PROJECT_DIR", data, sizeof(data)) || !tb_path_absolute(data, path, maxn)) { // get it from the current directory - if (!tb_directory_current(path, maxn)) break; + if (!tb_directory_current(path, maxn)) + break; } - // trace tb_trace_d("project: %s", path); // save the directory to the global variable: _PROJECT_DIR lua_pushstring(engine->lua, path); lua_setglobal(engine->lua, "_PROJECT_DIR"); - // ok ok = tb_true; } while (0); // failed? - if (!ok) tb_printf("error: not found the project directory!\n"); + if (!ok) { + tb_printf("error: not found the project directory!\n"); + } - // ok? return ok; } #if defined(TB_CONFIG_OS_WINDOWS) || defined(SIGINT) -static tb_void_t xm_engine_dump_traceback(lua_State* lua) -{ +static tb_void_t xm_engine_dump_traceback(lua_State *lua) { // @note it's not safe, but it doesn't matter, we're just trying to get the stack backtrace for debugging lua_getglobal(lua, "debug"); lua_getfield(lua, -1, "traceback"); @@ -1085,43 +1033,37 @@ static tb_void_t xm_engine_dump_traceback(lua_State* lua) #endif #if defined(TB_CONFIG_OS_WINDOWS) -static BOOL WINAPI xm_engine_signal_handler(DWORD signo) -{ - if (signo == CTRL_C_EVENT && g_lua) - { +static BOOL WINAPI xm_engine_signal_handler(DWORD signo) { + if (signo == CTRL_C_EVENT && g_lua) { xm_engine_dump_traceback(g_lua); tb_abort(); } return TRUE; } #elif defined(SIGINT) -static tb_void_t xm_engine_signal_handler(tb_int_t signo) -{ - if (signo == SIGINT && g_lua) - { +static tb_void_t xm_engine_signal_handler(tb_int_t signo) { + if (signo == SIGINT && g_lua) { xm_engine_dump_traceback(g_lua); tb_abort(); } } #endif -static tb_void_t xm_engine_init_host(xm_engine_t* engine) -{ - // check +static tb_void_t xm_engine_init_host(xm_engine_t *engine) { tb_assert_and_check_return(engine && engine->lua); // init system host - tb_char_t const* syshost = tb_null; + tb_char_t const *syshost = tb_null; #if defined(__COSMOPOLITAN__) struct utsname buffer; - if (uname(&buffer) == 0) - { - if (tb_strstr(buffer.sysname, "Darwin")) + if (uname(&buffer) == 0) { + 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"; @@ -1138,48 +1080,49 @@ static tb_void_t xm_engine_init_host(xm_engine_t* engine) #elif defined(TB_CONFIG_OS_HAIKU) syshost = "haiku"; #endif - lua_pushstring(engine->lua, syshost? syshost : "unknown"); + lua_pushstring(engine->lua, syshost ? syshost : "unknown"); lua_setglobal(engine->lua, "_HOST"); // init subsystem host - tb_char_t const* subhost = syshost; + tb_char_t const *subhost = syshost; #if defined(TB_CONFIG_OS_WINDOWS) -# if defined(TB_COMPILER_ON_MSYS) +#if defined(TB_COMPILER_ON_MSYS) subhost = "msys"; -# elif defined(TB_COMPILER_ON_CYGWIN) +#elif defined(TB_COMPILER_ON_CYGWIN) subhost = "cygwin"; -# else +#else { - tb_char_t data[64] = {0}; - if (tb_environment_first("MSYSTEM", data, sizeof(data))) - { + tb_char_t data[64] = { 0 }; + if (tb_environment_first("MSYSTEM", data, sizeof(data))) { // on msys? if (!tb_strnicmp(data, "mingw", 5) // mingw32/64 on msys2 - || !tb_strnicmp(data, "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_strnicmp(data, + "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 subhost = "msys"; + } } } -# endif #endif - lua_pushstring(engine->lua, subhost? subhost : "unknown"); +#endif + lua_pushstring(engine->lua, subhost ? subhost : "unknown"); lua_setglobal(engine->lua, "_SUBHOST"); } -static __tb_inline__ tb_char_t const* xm_engine_xmake_arch() -{ - tb_char_t const* arch = tb_null; +static __tb_inline__ tb_char_t const *xm_engine_xmake_arch() { + tb_char_t const *arch = tb_null; #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) -# if defined(TB_ARCH_x64) +#if defined(TB_ARCH_x64) arch = "x64"; -# elif defined(TB_ARCH_ARM64) +#elif defined(TB_ARCH_ARM64) arch = "arm64"; -# elif defined(TB_ARCH_ARM) +#elif defined(TB_ARCH_ARM) arch = "arm"; -# else +#else arch = "x86"; -# endif +#endif #elif defined(TB_ARCH_x64) arch = "x86_64"; #elif defined(TB_ARCH_x86) @@ -1192,48 +1135,49 @@ static __tb_inline__ tb_char_t const* xm_engine_xmake_arch() return arch; } -static tb_void_t xm_engine_init_arch(xm_engine_t* engine) -{ - // check +static tb_void_t xm_engine_init_arch(xm_engine_t *engine) { tb_assert_and_check_return(engine && engine->lua); // init xmake arch - tb_char_t const* xmakearch = xm_engine_xmake_arch(); + tb_char_t const *xmakearch = xm_engine_xmake_arch(); lua_pushstring(engine->lua, xmakearch); lua_setglobal(engine->lua, "_XMAKE_ARCH"); // init system architecture - tb_char_t const* sysarch = tb_null; + tb_char_t const *sysarch = tb_null; #if defined(__COSMOPOLITAN__) struct utsname buffer; - if (uname(&buffer) == 0) - { + if (uname(&buffer) == 0) { sysarch = buffer.machine; - if (tb_strstr(buffer.sysname, "Windows")) - { - if (!tb_strcmp(buffer.machine, "x86_64")) + if (tb_strstr(buffer.sysname, "Windows")) { + 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 - typedef void (WINAPI *GetNativeSystemInfo_t)(LPSYSTEM_INFO); + typedef void(WINAPI * GetNativeSystemInfo_t)(LPSYSTEM_INFO); // get system info - SYSTEM_INFO systeminfo = {0}; + SYSTEM_INFO systeminfo = { 0 }; GetNativeSystemInfo_t pGetNativeSystemInfo = tb_null; - tb_dynamic_ref_t kernel32 = tb_dynamic_init("kernel32.dll"); - if (kernel32) pGetNativeSystemInfo = (GetNativeSystemInfo_t)tb_dynamic_func(kernel32, "GetNativeSystemInfo"); - if (pGetNativeSystemInfo) pGetNativeSystemInfo(&systeminfo); - else GetSystemInfo(&systeminfo); + tb_dynamic_ref_t kernel32 = tb_dynamic_init("kernel32.dll"); + if (kernel32) { + pGetNativeSystemInfo = (GetNativeSystemInfo_t)tb_dynamic_func(kernel32, "GetNativeSystemInfo"); + } + if (pGetNativeSystemInfo) { + pGetNativeSystemInfo(&systeminfo); + } else { + GetSystemInfo(&systeminfo); + } // init architecture - switch (systeminfo.wProcessorArchitecture) - { + switch (systeminfo.wProcessorArchitecture) { case PROCESSOR_ARCHITECTURE_AMD64: sysarch = "x64"; break; @@ -1252,30 +1196,30 @@ static tb_void_t xm_engine_init_arch(xm_engine_t* engine) break; } #endif - if (!sysarch) sysarch = xmakearch; + if (!sysarch) { + sysarch = xmakearch; + } lua_pushstring(engine->lua, sysarch); lua_setglobal(engine->lua, "_ARCH"); // init subsystem architecture - tb_char_t const* subarch = sysarch; + tb_char_t const *subarch = sysarch; #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) // 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")) + tb_char_t data[64] = { 0 }; + if (tb_environment_first("MSYSTEM_CARCH", data, sizeof(data))) { + if (!tb_strcmp(data, "i686")) { subarch = "i386"; - else + } else { subarch = data; + } } #endif lua_pushstring(engine->lua, subarch); lua_setglobal(engine->lua, "_SUBARCH"); } -static tb_void_t xm_engine_init_features(xm_engine_t* engine) -{ - // check +static tb_void_t xm_engine_init_features(xm_engine_t *engine) { tb_assert_and_check_return(engine && engine->lua); // init features @@ -1302,12 +1246,12 @@ static tb_void_t xm_engine_init_features(xm_engine_t* engine) lua_setglobal(engine->lua, "_FEATURES"); } -static tb_void_t xm_engine_init_signal(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")) - return ; + tb_char_t data[64] = { 0 }; + if (!tb_environment_first("XMAKE_PROFILE", data, sizeof(data)) || tb_strcmp(data, "stuck")) { + return; + } g_lua = engine->lua; #if defined(TB_CONFIG_OS_WINDOWS) @@ -1319,46 +1263,52 @@ static tb_void_t xm_engine_init_signal(xm_engine_t* engine) #if XM_HOOK_LUA_MEMALLOC // 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) -{ +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) tb_free(data); - else if (!data) ptr = tb_malloc((tb_size_t)nsize); - else if (nsize != osize) ptr = tb_ralloc(data, (tb_size_t)nsize); - else ptr = data; + if (nsize == 0 && data) { + tb_free(data); + } else if (!data) { + ptr = tb_malloc((tb_size_t)nsize); + } else if (nsize != osize) { + ptr = tb_ralloc(data, (tb_size_t)nsize); + } else { + ptr = data; + } return ptr; } #endif #ifdef XM_EMBED_ENABLE -static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t* engine, tb_char_t const* programdir, tb_byte_t const* data, tb_size_t size) -{ +static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t *engine, + tb_char_t const *programdir, + tb_byte_t const *data, + tb_size_t size) { // do decompress - tb_bool_t ok = tb_false; - LZ4F_errorCode_t code; + tb_bool_t ok = tb_false; + LZ4F_errorCode_t code; LZ4F_decompressionContext_t ctx = tb_null; - tb_buffer_t result; - do - { + tb_buffer_t result; + do { tb_buffer_init(&result); code = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION); - if (LZ4F_isError(code)) break; + if (LZ4F_isError(code)) + break; tb_byte_t buffer[8192]; tb_bool_t failed = tb_false; - while (1) - { - size_t advance = (size_t)size; + 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)) - { + code = LZ4F_decompress(ctx, buffer, &buffer_size, data, &advance, tb_null); + if (LZ4F_isError(code)) { failed = tb_true; break; } - if (buffer_size == 0) break; + if (buffer_size == 0) { + break; + } data += advance; size -= advance; @@ -1370,22 +1320,20 @@ static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t* engine, tb_cha } while (0); // extract files to programdir - if (ok) - { - data = tb_buffer_data(&result); - size = tb_buffer_size(&result); - tb_byte_t const* p = data; - tb_byte_t const* e = data + size; - tb_size_t n = 0; - tb_char_t filepath[TB_PATH_MAXN]; - tb_long_t pos = tb_snprintf(filepath, sizeof(filepath), "%s/", programdir); - while (p < e) - { + if (ok) { + data = tb_buffer_data(&result); + size = tb_buffer_size(&result); + tb_byte_t const *p = data; + tb_byte_t const *e = data + size; + tb_size_t n = 0; + tb_char_t filepath[TB_PATH_MAXN]; + tb_long_t pos = tb_snprintf(filepath, sizeof(filepath), "%s/", programdir); + while (p < e) { // get filepath n = (tb_size_t)tb_bits_get_u16_be(p); p += 2; tb_assert_and_check_break(pos + n + 1 < sizeof(filepath)); - tb_strncpy(filepath + pos, (tb_char_t const*)p, n); + tb_strncpy(filepath + pos, (tb_char_t const *)p, n); filepath[pos + n] = '\0'; p += n; @@ -1395,11 +1343,12 @@ static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t* engine, tb_cha // write file tb_trace_d("extracting %s, %lu bytes ..", filepath, n); - tb_stream_ref_t stream = tb_stream_init_from_file(filepath, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + tb_stream_ref_t stream = tb_stream_init_from_file(filepath, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | + TB_FILE_MODE_TRUNC); tb_assert_and_check_break(stream); - if (tb_stream_open(stream)) - { + if (tb_stream_open(stream)) { tb_stream_bwrit(stream, p, n); tb_stream_exit(stream); } @@ -1407,18 +1356,14 @@ static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t* engine, tb_cha p += n; } ok = (p == e); - if (!ok) - { + if (!ok) { tb_trace_e("extract program files failed"); } - } - else - { + } else { tb_trace_e("decompress program files failed, %s", LZ4F_getErrorName(code)); } - if (ctx) - { + if (ctx) { LZ4F_freeDecompressionContext(ctx); ctx = tb_null; } @@ -1426,19 +1371,16 @@ static tb_bool_t xm_engine_extract_programfiles_impl(xm_engine_t* engine, tb_cha return ok; } -static tb_bool_t xm_engine_extract_programfiles(xm_engine_t* engine, tb_char_t const* programdir) -{ - tb_file_info_t info = {0}; - if (!tb_file_info(programdir, &info)) - { - tb_byte_t const* data = g_xmake_xmz_data; - tb_size_t size = sizeof(g_xmake_xmz_data); +static tb_bool_t xm_engine_extract_programfiles(xm_engine_t *engine, tb_char_t const *programdir) { + tb_file_info_t info = { 0 }; + if (!tb_file_info(programdir, &info)) { + tb_byte_t const *data = g_xmake_xmz_data; + tb_size_t size = sizeof(g_xmake_xmz_data); if (!xm_engine_extract_programfiles_impl(engine, programdir, data, size)) return tb_false; tb_size_t embedcount = engine->embedcount; - for (tb_size_t i = 0; i < embedcount; i++) - { + for (tb_size_t i = 0; i < embedcount; i++) { data = engine->embeddata[i]; size = engine->embedsize[i]; if (!xm_engine_extract_programfiles_impl(engine, programdir, data, size)) @@ -1449,27 +1391,23 @@ static tb_bool_t xm_engine_extract_programfiles(xm_engine_t* engine, tb_char_t c } #endif -static tb_void_t xm_engine_bind_to_lua(lua_State* lua, xm_engine_t* engine) -{ +static tb_void_t xm_engine_bind_to_lua(lua_State *lua, xm_engine_t *engine) { lua_pushlightuserdata(lua, engine); lua_setglobal(lua, "__global_engine"); } // load and execute the main script -static tb_bool_t xm_engine_load_main_script(xm_engine_t* engine, tb_char_t const* mainfile) -{ +static tb_bool_t xm_engine_load_main_script(xm_engine_t *engine, tb_char_t const *mainfile) { #ifdef TB_CONFIG_OS_WINDOWS // use tb_file_init to support unicode file path on windows - tb_bool_t ok = tb_false; + tb_bool_t ok = tb_false; tb_file_ref_t file = tb_null; - tb_byte_t* data = tb_null; + tb_byte_t *data = tb_null; - do - { + do { // open file file = tb_file_init(mainfile, TB_FILE_MODE_RO); - if (!file) - { + if (!file) { tb_printf("error: cannot open file: %s\n", mainfile); break; } @@ -1479,26 +1417,23 @@ static tb_bool_t xm_engine_load_main_script(xm_engine_t* engine, tb_char_t const tb_assert_and_check_break(size); // allocate buffer - data = (tb_byte_t*)tb_malloc(size); + data = (tb_byte_t *)tb_malloc(size); tb_assert_and_check_break(data); // read file content - if (!tb_file_read(file, data, size)) - { + if (!tb_file_read(file, data, size)) { tb_printf("error: cannot read file: %s\n", mainfile); break; } // load lua buffer - if (luaL_loadbuffer(engine->lua, (tb_char_t const*)data, size, mainfile)) - { + if (luaL_loadbuffer(engine->lua, (tb_char_t const *)data, size, mainfile)) { tb_printf("error: %s\n", lua_tostring(engine->lua, -1)); break; } // execute lua script - if (lua_pcall(engine->lua, 0, LUA_MULTRET, 0)) - { + if (lua_pcall(engine->lua, 0, LUA_MULTRET, 0)) { tb_printf("error: %s\n", lua_tostring(engine->lua, -1)); break; } @@ -1507,12 +1442,15 @@ static tb_bool_t xm_engine_load_main_script(xm_engine_t* engine, tb_char_t const } while (0); - if (data) tb_free(data); - if (file) tb_file_exit(file); + if (data) { + tb_free(data); + } + if (file) { + tb_file_exit(file); + } return ok; #else - if (luaL_dofile(engine->lua, mainfile)) - { + if (luaL_dofile(engine->lua, mainfile)) { tb_printf("error: %s\n", lua_tostring(engine->lua, -1)); return tb_false; } @@ -1523,12 +1461,10 @@ static tb_bool_t xm_engine_load_main_script(xm_engine_t* engine, tb_char_t const /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_cb_t lni_initalizer) -{ - tb_bool_t ok = tb_false; - xm_engine_t* engine = tb_null; - do - { +xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_cb_t lni_initalizer) { + tb_bool_t ok = tb_false; + xm_engine_t *engine = tb_null; + do { // init self engine = tb_malloc0_type(xm_engine_t); tb_assert_and_check_break(engine); @@ -1635,14 +1571,29 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c xm_engine_init_signal(engine); // get version - tb_version_t const* version = xm_version(); + tb_version_t const *version = xm_version(); tb_assert_and_check_break(version); // init version string - tb_char_t version_cstr[256] = {0}; - 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", version->major, version->minor, version->alter, XM_CONFIG_VERSION_BRANCH, XM_CONFIG_VERSION_COMMIT); - else tb_snprintf(version_cstr, sizeof(version_cstr), "%u.%u.%u+%llu", version->major, version->minor, version->alter, version->build); + tb_char_t version_cstr[256] = { 0 }; + 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", + version->major, + version->minor, + version->alter, + XM_CONFIG_VERSION_BRANCH, + XM_CONFIG_VERSION_COMMIT); + } else { + tb_snprintf(version_cstr, + sizeof(version_cstr), + "%u.%u.%u+%llu", + version->major, + version->minor, + version->alter, + (unsigned long long)version->build); + } lua_pushstring(engine->lua, version_cstr); lua_setglobal(engine->lua, "_VERSION"); @@ -1661,7 +1612,7 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c lua_setglobal(engine->lua, "_VERSION_SHORT"); // init engine name - lua_pushstring(engine->lua, name? name : "xmake"); + lua_pushstring(engine->lua, name ? name : "xmake"); lua_setglobal(engine->lua, "_NAME"); // use luajit as runtime? @@ -1681,20 +1632,21 @@ 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) lni_initalizer((xm_engine_ref_t)engine, engine->lua); + if (lni_initalizer) { + lni_initalizer((xm_engine_ref_t)engine, engine->lua); + } lua_setglobal(engine->lua, "_lni"); #ifdef TB_CONFIG_OS_WINDOWS // enable terminal colors output for windows cmd - HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); - if (output != INVALID_HANDLE_VALUE) - { + HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE); + if (output != INVALID_HANDLE_VALUE) { DWORD mode; - if (GetConsoleMode(output, &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 @@ -1702,69 +1654,76 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c } while (0); - if (!ok) - { - if (engine) xm_engine_exit((xm_engine_ref_t)engine); + if (!ok) { + if (engine) { + xm_engine_exit((xm_engine_ref_t)engine); + } engine = tb_null; } return (xm_engine_ref_t)engine; } -tb_void_t xm_engine_exit(xm_engine_ref_t self) -{ - // check - xm_engine_t* engine = (xm_engine_t*)self; +tb_void_t xm_engine_exit(xm_engine_ref_t self) { + xm_engine_t *engine = (xm_engine_t *)self; tb_assert_and_check_return(engine); // exit lua - if (engine->lua) lua_close(engine->lua); + if (engine->lua) { + lua_close(engine->lua); + } engine->lua = tb_null; // exit poller - if (engine->poller) tb_poller_exit(engine->poller); + if (engine->poller) { + tb_poller_exit(engine->poller); + } engine->poller = tb_null; // exit it tb_free(engine); } -tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv) -{ - // check - xm_engine_t* engine = (xm_engine_t*)self; +tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t **argv, tb_char_t **taskargv) { + xm_engine_t *engine = (xm_engine_t *)self; tb_assert_and_check_return_val(engine && engine->lua, -1); #if defined(TB_CONFIG_OS_WINDOWS) && defined(TB_COMPILER_IS_MSVC) // set "stdin" to have unicode mode - if (_isatty(_fileno(stdin))) _setmode(_fileno(stdin), _O_U16TEXT); + 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)) return -1; + if (!xm_engine_save_arguments(engine, argc, argv, taskargv)) { + return -1; + } // get the project directory - tb_char_t path[TB_PATH_MAXN] = {0}; - if (!xm_engine_get_project_directory(engine, path, sizeof(path))) return -1; + tb_char_t path[TB_PATH_MAXN] = { 0 }; + if (!xm_engine_get_project_directory(engine, path, sizeof(path))) + return -1; // get the program file - if (!xm_engine_get_program_file(engine, argv, path, sizeof(path))) return -1; + if (!xm_engine_get_program_file(engine, argv, path, sizeof(path))) + return -1; // get the program directory - if (!xm_engine_get_program_directory(engine, path, sizeof(path), path)) return -1; + if (!xm_engine_get_program_directory(engine, path, sizeof(path), path)) + return -1; #ifdef XM_EMBED_ENABLE - if (!xm_engine_extract_programfiles(engine, path)) return -1; + if (!xm_engine_extract_programfiles(engine, path)) + return -1; #endif // append the main script path tb_strcat(path, "/core/_xmake_main.lua"); // exists this script? - if (!tb_file_info(path, tb_null)) - { + if (!tb_file_info(path, tb_null)) { tb_printf("not found main script: %s\n", path); return -1; } - // trace tb_trace_d("main: %s", path); // load and execute the main script @@ -1777,8 +1736,7 @@ tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t** argv, t // call the main function lua_getglobal(engine->lua, "_xmake_main"); - if (lua_pcall(engine->lua, 0, 1, -2)) - { + if (lua_pcall(engine->lua, 0, 1, -2)) { tb_printf("error: %s\n", lua_tostring(engine->lua, -1)); return -1; } @@ -1786,10 +1744,8 @@ tb_int_t xm_engine_main(xm_engine_ref_t self, tb_int_t argc, tb_char_t** argv, t // get the error code return (tb_int_t)lua_tonumber(engine->lua, -1); } -tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const* module, luaL_Reg const funcs[]) -{ - // check - xm_engine_t* engine = (xm_engine_t*)self; +tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const *module, luaL_Reg const funcs[]) { + xm_engine_t *engine = (xm_engine_t *)self; tb_assert_and_check_return(engine && engine->lua && module && funcs); // do register @@ -1799,10 +1755,8 @@ tb_void_t xm_engine_register(xm_engine_ref_t self, tb_char_t const* module, luaL lua_rawset(engine->lua, -3); } #ifdef XM_EMBED_ENABLE -tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t self, tb_byte_t const* data, tb_size_t size) -{ - // check - xm_engine_t* engine = (xm_engine_t*)self; +tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t self, tb_byte_t const *data, tb_size_t size) { + xm_engine_t *engine = (xm_engine_t *)self; tb_assert_and_check_return(engine && engine->embedcount < tb_arrayn(engine->embedsize) && data && size); engine->embeddata[engine->embedcount] = data; @@ -1810,25 +1764,20 @@ tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t self, tb_byte_t const* data, engine->embedcount++; } #endif -lua_State* xm_engine_lua(xm_engine_ref_t self) -{ - // check - xm_engine_t* engine = (xm_engine_t*)self; +lua_State *xm_engine_lua(xm_engine_ref_t self) { + xm_engine_t *engine = (xm_engine_t *)self; tb_assert_and_check_return_val(engine, tb_null); return engine->lua; } -tb_poller_ref_t xm_engine_poller(xm_engine_ref_t self) -{ - // check - xm_engine_t* engine = (xm_engine_t*)self; +tb_poller_ref_t xm_engine_poller(xm_engine_ref_t self) { + xm_engine_t *engine = (xm_engine_t *)self; tb_assert_and_check_return_val(engine, tb_null); - if (!engine->poller) - { + if (!engine->poller) { // init poller engine->poller_state.lua = engine->lua; - tb_poller_ref_t poller = tb_poller_init(&engine->poller_state); + tb_poller_ref_t poller = tb_poller_init(&engine->poller_state); tb_assert_and_check_return_val(poller, tb_null); // attach poller to the current thread @@ -1837,14 +1786,15 @@ tb_poller_ref_t xm_engine_poller(xm_engine_ref_t self) } return engine->poller; } -tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv, xm_engine_lni_initalizer_cb_t lni_initalizer) -{ +tb_int_t xm_engine_run(tb_char_t const *name, + tb_int_t argc, + tb_char_t **argv, + tb_char_t **taskargv, + xm_engine_lni_initalizer_cb_t lni_initalizer) { tb_int_t ok = -1; - if (xm_init()) - { + if (xm_init()) { xm_engine_ref_t engine = xm_engine_init(name, lni_initalizer); - if (engine) - { + if (engine) { ok = xm_engine_main(engine, argc, argv, taskargv); xm_engine_exit(engine); } @@ -1852,11 +1802,9 @@ tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, t } return ok; } -xm_engine_ref_t xm_engine_get(lua_State* lua) -{ +xm_engine_ref_t xm_engine_get(lua_State *lua) { tb_assert_and_check_return_val(lua, tb_null); lua_getglobal(lua, "__global_engine"); return (xm_engine_ref_t)lua_touserdata(lua, -1); } - diff --git a/core/src/xmake/engine.h b/core/src/xmake/engine.h index 04ef958d9..a2f7058c6 100644 --- a/core/src/xmake/engine.h +++ b/core/src/xmake/engine.h @@ -36,10 +36,12 @@ __tb_extern_c_enter__ */ /// the xmake engine type -typedef struct {tb_int_t dummy;} const* xm_engine_ref_t; +typedef struct { + tb_int_t dummy; +} const *xm_engine_ref_t; /// the lni initializer callback type -typedef tb_void_t (*xm_engine_lni_initalizer_cb_t)(xm_engine_ref_t engine, lua_State* lua); +typedef tb_void_t (*xm_engine_lni_initalizer_cb_t)(xm_engine_ref_t engine, lua_State *lua); /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces @@ -52,13 +54,13 @@ typedef tb_void_t (*xm_engine_lni_initalizer_cb_t)(xm_engine_ref_t engine, lua_S * * @return the engine */ -xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_cb_t lni_initalizer); +xm_engine_ref_t xm_engine_init(tb_char_t const *name, xm_engine_lni_initalizer_cb_t lni_initalizer); /*! exit the engine * * @param engine the engine */ -tb_void_t xm_engine_exit(xm_engine_ref_t engine); +tb_void_t xm_engine_exit(xm_engine_ref_t engine); /*! do the main entry of the engine * @@ -69,7 +71,7 @@ tb_void_t xm_engine_exit(xm_engine_ref_t engine); * * @return the error code of main() */ -tb_int_t xm_engine_main(xm_engine_ref_t engine, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv); +tb_int_t xm_engine_main(xm_engine_ref_t engine, tb_int_t argc, tb_char_t **argv, tb_char_t **taskargv); /*! register lni modules in the engine, @note we need to call it in lni_initalizer() * @@ -77,7 +79,7 @@ tb_int_t xm_engine_main(xm_engine_ref_t engine, tb_int_t argc * @param module the lni module name * @param funcs the lni module functions */ -tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t const* module, luaL_Reg const funcs[]); +tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t const *module, luaL_Reg const funcs[]); /*! add the embed files * @@ -85,7 +87,7 @@ tb_void_t xm_engine_register(xm_engine_ref_t engine, tb_char_t * @param data the embedfiles data * @param size the data size */ -tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t engine, tb_byte_t const* data, tb_size_t size); +tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t engine, tb_byte_t const *data, tb_size_t size); /* get lua state from engine * @@ -93,7 +95,7 @@ tb_void_t xm_engine_add_embedfiles(xm_engine_ref_t engine, tb_ * * @return the lua state */ -lua_State* xm_engine_lua(xm_engine_ref_t engine); +lua_State *xm_engine_lua(xm_engine_ref_t engine); /* get poller from engine * @@ -101,7 +103,7 @@ lua_State* xm_engine_lua(xm_engine_ref_t engine); * * @return the poller */ -tb_poller_ref_t xm_engine_poller(xm_engine_ref_t engine); +tb_poller_ref_t xm_engine_poller(xm_engine_ref_t engine); /*! run main entry of the engine singleton * @@ -113,7 +115,11 @@ tb_poller_ref_t xm_engine_poller(xm_engine_ref_t engine); * * @return the error code of main() */ -tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, tb_char_t** argv, tb_char_t** taskargv, xm_engine_lni_initalizer_cb_t lni_initalizer); +tb_int_t xm_engine_run(tb_char_t const *name, + tb_int_t argc, + tb_char_t **argv, + tb_char_t **taskargv, + xm_engine_lni_initalizer_cb_t lni_initalizer); /*! get engine from the given lua state * @@ -121,7 +127,7 @@ tb_int_t xm_engine_run(tb_char_t const* name, tb_int_t argc, * * @return the engine */ -xm_engine_ref_t xm_engine_get(lua_State* lua); +xm_engine_ref_t xm_engine_get(lua_State *lua); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/core/src/xmake/engine_pool.c b/core/src/xmake/engine_pool.c index 78081399c..39bdb1339 100644 --- a/core/src/xmake/engine_pool.c +++ b/core/src/xmake/engine_pool.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "engine_pool" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "engine_pool" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,71 +37,63 @@ */ // the max engine pool count -#define XM_ENGINE_POOL_MAXN (128) +#define XM_ENGINE_POOL_MAXN (128) // the singleton type of engine pool -#define XM_ENGINE_POOL (TB_SINGLETON_TYPE_USER + 4) +#define XM_ENGINE_POOL (TB_SINGLETON_TYPE_USER + 4) /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_handle_t xm_engine_pool_instance_init(tb_cpointer_t* ppriv) -{ +static tb_handle_t xm_engine_pool_instance_init(tb_cpointer_t *ppriv) { xm_engine_pool_ref_t engine_pool = xm_engine_pool_init(); tb_assert_and_check_return_val(engine_pool, tb_null); return (tb_handle_t)engine_pool; } -static tb_void_t xm_engine_pool_instance_exit(tb_handle_t engine_pool, tb_cpointer_t priv) -{ - if (engine_pool) xm_engine_pool_exit((xm_engine_pool_ref_t)engine_pool); +static tb_void_t xm_engine_pool_instance_exit(tb_handle_t engine_pool, tb_cpointer_t priv) { + if (engine_pool) { + xm_engine_pool_exit((xm_engine_pool_ref_t)engine_pool); + } } /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -xm_engine_pool_ref_t xm_engine_pool() -{ - return (xm_engine_pool_ref_t)tb_singleton_instance(XM_ENGINE_POOL, xm_engine_pool_instance_init, xm_engine_pool_instance_exit, tb_null, tb_null); +xm_engine_pool_ref_t xm_engine_pool() { + return (xm_engine_pool_ref_t)tb_singleton_instance( + XM_ENGINE_POOL, xm_engine_pool_instance_init, xm_engine_pool_instance_exit, tb_null, tb_null); } -xm_engine_pool_ref_t xm_engine_pool_init() -{ +xm_engine_pool_ref_t xm_engine_pool_init() { return tb_single_list_init(0, tb_element_ptr(tb_null, tb_null)); } -tb_void_t xm_engine_pool_exit(xm_engine_pool_ref_t engine_pool) -{ - if (engine_pool) - { - tb_for_all (xm_engine_ref_t, engine, engine_pool) - { - if (engine) +tb_void_t xm_engine_pool_exit(xm_engine_pool_ref_t engine_pool) { + if (engine_pool) { + tb_for_all(xm_engine_ref_t, engine, engine_pool) { + if (engine) { xm_engine_exit(engine); + } } tb_single_list_exit(engine_pool); } } -xm_engine_ref_t xm_engine_pool_alloc(xm_engine_pool_ref_t engine_pool) -{ +xm_engine_ref_t xm_engine_pool_alloc(xm_engine_pool_ref_t engine_pool) { xm_engine_ref_t engine = tb_null; - if (tb_single_list_size(engine_pool) > 0) - { + if (tb_single_list_size(engine_pool) > 0) { engine = (xm_engine_ref_t)tb_single_list_head(engine_pool); tb_single_list_remove_head(engine_pool); } return engine; } -tb_bool_t xm_engine_pool_free(xm_engine_pool_ref_t engine_pool, xm_engine_ref_t engine) -{ - if (tb_single_list_size(engine_pool) < XM_ENGINE_POOL_MAXN) - { +tb_bool_t xm_engine_pool_free(xm_engine_pool_ref_t engine_pool, xm_engine_ref_t engine) { + if (tb_single_list_size(engine_pool) < XM_ENGINE_POOL_MAXN) { tb_single_list_insert_tail(engine_pool, engine); return tb_true; } return tb_false; } - diff --git a/core/src/xmake/engine_pool.h b/core/src/xmake/engine_pool.h index 853856d1c..05bdc4668 100644 --- a/core/src/xmake/engine_pool.h +++ b/core/src/xmake/engine_pool.h @@ -43,19 +43,19 @@ typedef tb_single_list_ref_t xm_engine_pool_ref_t; */ // get the engine pool singleton -xm_engine_pool_ref_t xm_engine_pool(tb_void_t); +xm_engine_pool_ref_t xm_engine_pool(tb_void_t); /*! init the engine_pool * * @return the engine pool */ -xm_engine_pool_ref_t xm_engine_pool_init(tb_void_t); +xm_engine_pool_ref_t xm_engine_pool_init(tb_void_t); /*! exit the engine_pool * * @param engine_pool the engine_pool */ -tb_void_t xm_engine_pool_exit(xm_engine_pool_ref_t engine_pool); +tb_void_t xm_engine_pool_exit(xm_engine_pool_ref_t engine_pool); /*! alloc a engine from the engine_pool * @@ -63,7 +63,7 @@ tb_void_t xm_engine_pool_exit(xm_engine_pool_ref_t engine_pool * * @return the engine */ -xm_engine_ref_t xm_engine_pool_alloc(xm_engine_pool_ref_t engine_pool); +xm_engine_ref_t xm_engine_pool_alloc(xm_engine_pool_ref_t engine_pool); /*! free a engine to the engine_pool * @@ -72,7 +72,7 @@ xm_engine_ref_t xm_engine_pool_alloc(xm_engine_pool_ref_t engine_poo * * @return tb_true or tb_false */ -tb_bool_t xm_engine_pool_free(xm_engine_pool_ref_t engine_pool, xm_engine_ref_t engine); +tb_bool_t xm_engine_pool_free(xm_engine_pool_ref_t engine_pool, xm_engine_ref_t engine); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/core/src/xmake/fwatcher/add.c b/core/src/xmake/fwatcher/add.c index 90c655eda..10c31b6b0 100644 --- a/core/src/xmake/fwatcher/add.c +++ b/core/src/xmake/fwatcher/add.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "fwatcher.add" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "fwatcher.add" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,21 +35,20 @@ */ // fwatcher.add(watchdir, recursion) -tb_int_t xm_fwatcher_add(lua_State* lua) -{ - // check +tb_int_t xm_fwatcher_add(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 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_char_t const *watchdir = luaL_checkstring(lua, 2); tb_check_return_val(watchdir, 0); // get recursion diff --git a/core/src/xmake/fwatcher/close.c b/core/src/xmake/fwatcher/close.c index 6d9eced7a..61c78868a 100644 --- a/core/src/xmake/fwatcher/close.c +++ b/core/src/xmake/fwatcher/close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "fwatcher.close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "fwatcher.close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // fwatcher.close(p) -tb_int_t xm_fwatcher_close(lua_State* lua) -{ - // check +tb_int_t xm_fwatcher_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 the fwatcher tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1); diff --git a/core/src/xmake/fwatcher/open.c b/core/src/xmake/fwatcher/open.c index 333c26616..032921fb3 100644 --- a/core/src/xmake/fwatcher/open.c +++ b/core/src/xmake/fwatcher/open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "fwatcher.open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "fwatcher.open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_fwatcher_open(lua_State* lua) -{ - // check +tb_int_t xm_fwatcher_open(lua_State *lua) { 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); + if (fwatcher) { + xm_lua_pushpointer(lua, (tb_pointer_t)fwatcher); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/fwatcher/prefix.h b/core/src/xmake/fwatcher/prefix.h index c11421b55..767b8f022 100644 --- a/core/src/xmake/fwatcher/prefix.h +++ b/core/src/xmake/fwatcher/prefix.h @@ -27,5 +27,3 @@ #include "../prefix.h" #endif - - diff --git a/core/src/xmake/fwatcher/remove.c b/core/src/xmake/fwatcher/remove.c index c0e4d30bd..a3636dbe3 100644 --- a/core/src/xmake/fwatcher/remove.c +++ b/core/src/xmake/fwatcher/remove.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "fwatcher.remove" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "fwatcher.remove" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,21 +35,20 @@ */ // fwatcher.remove(watchdir) -tb_int_t xm_fwatcher_remove(lua_State* lua) -{ - // check +tb_int_t xm_fwatcher_remove(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 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_char_t const *watchdir = luaL_checkstring(lua, 2); tb_check_return_val(watchdir, 0); // remove watchdir @@ -59,4 +58,3 @@ tb_int_t xm_fwatcher_remove(lua_State* lua) lua_pushboolean(lua, ok); return 1; } - diff --git a/core/src/xmake/fwatcher/wait.c b/core/src/xmake/fwatcher/wait.c index 72f48b50a..0602c931c 100644 --- a/core/src/xmake/fwatcher/wait.c +++ b/core/src/xmake/fwatcher/wait.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "fwatcher.wait" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "fwatcher.wait" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // fwatcher.wait(p) -tb_int_t xm_fwatcher_wait(lua_State* lua) -{ - // check +tb_int_t xm_fwatcher_wait(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 the fwatcher tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1); @@ -53,12 +52,11 @@ tb_int_t xm_fwatcher_wait(lua_State* lua) // wait fwatcher event tb_fwatcher_event_t event; - tb_long_t ok = tb_fwatcher_wait(fwatcher, &event, timeout); + tb_long_t ok = tb_fwatcher_wait(fwatcher, &event, timeout); // save result lua_pushinteger(lua, ok); - if (ok > 0) - { + if (ok > 0) { lua_newtable(lua); lua_pushstring(lua, "path"); lua_pushstring(lua, event.filepath); diff --git a/core/src/xmake/hash/md5.c b/core/src/xmake/hash/md5.c index 39fa82b15..6d18b72ba 100644 --- a/core/src/xmake/hash/md5.c +++ b/core/src/xmake/hash/md5.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "md5" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "md5" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_md5(lua_State* lua) -{ - // check +tb_int_t xm_hash_md5(lua_State *lua) { 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) - { + 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; @@ -65,33 +61,29 @@ tb_int_t xm_hash_md5(lua_State* lua) } // get the filename - tb_char_t const* filename = luaL_checkstring(lua, 1); + 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_bool_t ok = tb_false; tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO); - if (stream) - { + if (stream) { // open stream - if (tb_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)) - { + 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); + if (real > 0) { + tb_md5_spak(&md5, data, real); // no data? continue it - else if (!real) - { + } else if (!real) { // wait real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream)); tb_check_break(real > 0); @@ -100,7 +92,9 @@ tb_int_t xm_hash_md5(lua_State* lua) tb_assert_and_check_break(real & TB_STREAM_WAIT_READ); } // failed or end? - else break; + else { + break; + } } // exit md5 @@ -112,13 +106,15 @@ tb_int_t xm_hash_md5(lua_State* lua) tb_size_t n = xm_hash_make_cstr(s, buffer, 16); // save result - lua_pushlstring(lua, s, n); + lua_pushlstring(lua, s, n); ok = tb_true; } // exit stream tb_stream_exit(stream); } - if (!ok) lua_pushnil(lua); + if (!ok) { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/hash/prefix.h b/core/src/xmake/hash/prefix.h index 8a321e1eb..99a8e4e6d 100644 --- a/core/src/xmake/hash/prefix.h +++ b/core/src/xmake/hash/prefix.h @@ -30,8 +30,7 @@ * helper implementation */ -static __tb_inline__ tb_uint64_t xm_hash_xorshift64(tb_uint64_t x) -{ +static __tb_inline__ tb_uint64_t xm_hash_xorshift64(tb_uint64_t x) { x ^= x << 13; x ^= x >> 7; x ^= x << 17; @@ -39,27 +38,24 @@ static __tb_inline__ tb_uint64_t xm_hash_xorshift64(tb_uint64_t x) } // http://xorshift.di.unimi.it/xorshift128plus.c -static __tb_inline__ tb_uint64_t xm_hash_xorshift128(tb_uint64_t* s) -{ - tb_uint64_t s1 = s[0]; +static __tb_inline__ tb_uint64_t xm_hash_xorshift128(tb_uint64_t *s) { + tb_uint64_t s1 = s[0]; tb_uint64_t const s0 = s[1]; - s[0] = s0; + s[0] = s0; s1 ^= s1 << 23; s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5); return s[1] + s0; } -static __tb_inline__ tb_size_t xm_hash_make_cstr(tb_char_t hash[256], tb_byte_t const* data, tb_size_t size) -{ - static tb_char_t const* digits_table = "0123456789abcdef"; - tb_size_t i = 0; - tb_byte_t value; - tb_char_t* s = hash; - for (i = 0; i < size; ++i) - { +static __tb_inline__ tb_size_t xm_hash_make_cstr(tb_char_t hash[256], tb_byte_t const *data, tb_size_t size) { + static tb_char_t const *digits_table = "0123456789abcdef"; + tb_size_t i = 0; + tb_byte_t value; + tb_char_t *s = hash; + for (i = 0; i < size; ++i) { value = data[i]; - s[0] = digits_table[(value >> 4) & 15]; - s[1] = digits_table[value & 15]; + s[0] = digits_table[(value >> 4) & 15]; + s[1] = digits_table[value & 15]; s += 2; } *s = '\0'; @@ -67,5 +63,3 @@ static __tb_inline__ tb_size_t xm_hash_make_cstr(tb_char_t hash[256], tb_byte_t } #endif - - diff --git a/core/src/xmake/hash/rand128.c b/core/src/xmake/hash/rand128.c index 1e1f34fde..625e75326 100644 --- a/core/src/xmake/hash/rand128.c +++ b/core/src/xmake/hash/rand128.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "rand128" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "rand128" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_rand128(lua_State* lua) -{ - // check +tb_int_t xm_hash_rand128(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - static union { tb_byte_t b[16]; tb_uint64_t word[2]; } s_seed = {0}; - if (!s_seed.word[0] && !s_seed.word[1]) - { + static union { + tb_byte_t b[16]; + tb_uint64_t word[2]; + } s_seed = { 0 }; + if (!s_seed.word[0] && !s_seed.word[1]) { s_seed.word[0] = (tb_uint64_t)tb_uclock(); s_seed.word[1] = (tb_uint64_t)tb_uclock(); } diff --git a/core/src/xmake/hash/rand32.c b/core/src/xmake/hash/rand32.c index bbf565acd..7f5286b8e 100644 --- a/core/src/xmake/hash/rand32.c +++ b/core/src/xmake/hash/rand32.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "rand32" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "rand32" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,18 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_rand32(lua_State* lua) -{ - // check +tb_int_t xm_hash_rand32(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); static tb_uint64_t s_seed = 0; - if (!s_seed) + if (!s_seed) { s_seed = (tb_uint64_t)tb_uclock(); + } s_seed = xm_hash_xorshift64(s_seed); - tb_char_t s[256]; + tb_char_t s[256]; tb_uint32_t word = (s_seed >> 32) ^ (s_seed & 0xffffffff); - tb_size_t n = xm_hash_make_cstr(s, (tb_byte_t const*)&word, 4); + tb_size_t n = xm_hash_make_cstr(s, (tb_byte_t const *)&word, 4); lua_pushlstring(lua, s, n); return 1; diff --git a/core/src/xmake/hash/rand64.c b/core/src/xmake/hash/rand64.c index 31732bc53..ea9539ced 100644 --- a/core/src/xmake/hash/rand64.c +++ b/core/src/xmake/hash/rand64.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "rand64" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "rand64" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_rand64(lua_State* lua) -{ - // check +tb_int_t xm_hash_rand64(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - static union { tb_byte_t b[8]; tb_uint64_t word; } s_seed = {0}; - if (!s_seed.word) + static union { + tb_byte_t b[8]; + tb_uint64_t word; + } s_seed = { 0 }; + if (!s_seed.word) { s_seed.word = (tb_uint64_t)tb_uclock(); + } s_seed.word = xm_hash_xorshift64(s_seed.word); tb_char_t s[256]; diff --git a/core/src/xmake/hash/sha.c b/core/src/xmake/hash/sha.c index ba5408279..7ddcab62f 100644 --- a/core/src/xmake/hash/sha.c +++ b/core/src/xmake/hash/sha.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "sha" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "sha" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,17 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_sha(lua_State* lua) -{ - // check +tb_int_t xm_hash_sha(lua_State *lua) { 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) - { + 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; @@ -55,7 +51,7 @@ tb_int_t xm_hash_sha(lua_State* lua) tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t)); // compute sha - tb_sha_t sha; + tb_sha_t sha; tb_byte_t buffer[32]; tb_sha_init(&sha, mode); tb_sha_spak(&sha, data, size); @@ -63,7 +59,7 @@ tb_int_t xm_hash_sha(lua_State* lua) // make sha string tb_char_t s[256]; - tb_size_t n = sha.digest_len << 2; + tb_size_t n = sha.digest_len << 2; tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result @@ -72,33 +68,29 @@ tb_int_t xm_hash_sha(lua_State* lua) } // get the filename - tb_char_t const* filename = luaL_checkstring(lua, 2); + 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_bool_t ok = tb_false; tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO); - if (stream) - { + if (stream) { // open stream - if (tb_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)) - { + 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); + if (real > 0) { + tb_sha_spak(&sha, data, real); // no data? continue it - else if (!real) - { + } else if (!real) { // wait real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream)); tb_check_break(real > 0); @@ -107,7 +99,9 @@ tb_int_t xm_hash_sha(lua_State* lua) tb_assert_and_check_break(real & TB_STREAM_WAIT_READ); } // failed or end? - else break; + else { + break; + } } // exit sha @@ -116,17 +110,19 @@ tb_int_t xm_hash_sha(lua_State* lua) // make sha string tb_char_t s[256]; - tb_size_t n = sha.digest_len << 2; + tb_size_t n = sha.digest_len << 2; tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result - lua_pushlstring(lua, s, len); + lua_pushlstring(lua, s, len); ok = tb_true; } // exit stream tb_stream_exit(stream); } - if (!ok) lua_pushnil(lua); + if (!ok) { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/hash/uuid4.c b/core/src/xmake/hash/uuid4.c index a3c4e2360..8712e9efa 100644 --- a/core/src/xmake/hash/uuid4.c +++ b/core/src/xmake/hash/uuid4.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "uuid4" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "uuid4" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,16 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_uuid4(lua_State* lua) -{ - // check +tb_int_t xm_hash_uuid4(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the name - tb_char_t const* name = luaL_optstring(lua, 1, tb_null); + 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)); + lua_pushstring(lua, tb_uuid4_make_cstr(uuid, name)); return 1; } diff --git a/core/src/xmake/hash/xxhash.c b/core/src/xmake/hash/xxhash.c index cf4f67e37..a7fe4a985 100644 --- a/core/src/xmake/hash/xxhash.c +++ b/core/src/xmake/hash/xxhash.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "xxhash" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "xxhash" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,27 +37,22 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_hash_xxhash(lua_State* lua) -{ - // check +tb_int_t xm_hash_xxhash(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get mode tb_size_t mode = (tb_size_t)lua_tointeger(lua, 1); - if (mode != 32 && mode != 64 && mode != 128) - { + if (mode != 32 && 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) - { + 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; @@ -65,28 +60,22 @@ tb_int_t xm_hash_xxhash(lua_State* lua) tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t)); // compuate hash - tb_byte_t const* buffer = tb_null; - tb_uint32_t value32; - XXH64_hash_t value64; - XXH128_hash_t value128; - if (mode == 128) - { + tb_byte_t const *buffer = tb_null; + tb_uint32_t value32; + XXH64_hash_t value64; + XXH128_hash_t value128; + if (mode == 128) { value128 = XM_XXH3_128bits(data, size); - buffer = (tb_byte_t const*)&value128; - } - else if (mode == 64) - { + buffer = (tb_byte_t const *)&value128; + } else if (mode == 64) { value64 = XM_XXH3_64bits(data, size); - buffer = (tb_byte_t const*)&value64; - } - else if (mode == 32) - { + buffer = (tb_byte_t const *)&value64; + } else if (mode == 32) { value64 = XM_XXH3_64bits(data, size); value32 = (value64 >> 32) ^ (value64 & 0xffffffff); - buffer = (tb_byte_t const*)&value32; + buffer = (tb_byte_t const *)&value32; } - if (!buffer) - { + if (!buffer) { lua_pushnil(lua); lua_pushfstring(lua, "empty buffer!"); return 2; @@ -94,7 +83,7 @@ tb_int_t xm_hash_xxhash(lua_State* lua) // make xxhash string tb_char_t s[256]; - tb_size_t n = mode >> 3; + tb_size_t n = mode >> 3; tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result @@ -103,38 +92,38 @@ tb_int_t xm_hash_xxhash(lua_State* lua) } // get the filename - tb_char_t const* filename = luaL_checkstring(lua, 2); + 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_bool_t ok = tb_false; tb_stream_ref_t stream = tb_stream_init_from_file(filename, TB_FILE_MODE_RO); - if (stream) - { + if (stream) { // open stream - XXH3_state_t* state = XM_XXH3_createState(); - if (tb_stream_open(stream) && state) - { + XXH3_state_t *state = XM_XXH3_createState(); + if (tb_stream_open(stream) && state) { // reset xxhash - if (mode == 32 || mode == 64) XM_XXH3_64bits_reset(state); - else XM_XXH3_128bits_reset(state); + if (mode == 32 || 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)) - { + while (!tb_stream_beof(stream)) { // read data tb_long_t real = tb_stream_read(stream, data, sizeof(data)); - // ok? - if (real > 0) - { - if (mode == 32 || mode == 64) XM_XXH3_64bits_update(state, data, real); - else XM_XXH3_128bits_update(state, data, real); + if (real > 0) { + if (mode == 32 || mode == 64) { + XM_XXH3_64bits_update(state, data, real); + } else { + XM_XXH3_128bits_update(state, data, real); + } } // no data? continue it - else if (!real) - { + else if (!real) { // wait real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, tb_stream_timeout(stream)); tb_check_break(real > 0); @@ -143,38 +132,35 @@ tb_int_t xm_hash_xxhash(lua_State* lua) tb_assert_and_check_break(real & TB_STREAM_WAIT_READ); } // failed or end? - else break; + else { + break; + } } // compuate hash - tb_byte_t const* buffer; - tb_uint32_t value32; - XXH64_hash_t value64; - XXH128_hash_t value128; - if (mode == 128) - { + tb_byte_t const *buffer; + tb_uint32_t value32; + XXH64_hash_t value64; + XXH128_hash_t value128; + if (mode == 128) { value128 = XM_XXH3_128bits_digest(state); - buffer = (tb_byte_t const*)&value128; - } - else if (mode == 64) - { + buffer = (tb_byte_t const *)&value128; + } else if (mode == 64) { value64 = XM_XXH3_64bits_digest(state); - buffer = (tb_byte_t const*)&value64; - } - else if (mode == 32) - { + buffer = (tb_byte_t const *)&value64; + } else if (mode == 32) { value64 = XM_XXH3_64bits_digest(state); value32 = (value64 >> 32) ^ (value64 & 0xffffffff); - buffer = (tb_byte_t const*)&value32; + buffer = (tb_byte_t const *)&value32; } // make xxhash string tb_char_t s[256]; - tb_size_t n = mode >> 3; + tb_size_t n = mode >> 3; tb_size_t len = xm_hash_make_cstr(s, buffer, n); // save result - lua_pushlstring(lua, s, len); + lua_pushlstring(lua, s, len); ok = tb_true; } @@ -182,8 +168,12 @@ tb_int_t xm_hash_xxhash(lua_State* lua) tb_stream_exit(stream); // exit xxhash - if (state) XM_XXH3_freeState(state); + if (state) { + XM_XXH3_freeState(state); + } + } + if (!ok) { + lua_pushnil(lua); } - if (!ok) lua_pushnil(lua); return 1; } diff --git a/core/src/xmake/io/file_close.c b/core/src/xmake/io/file_close.c index dfd698457..cbcb237af 100644 --- a/core/src/xmake/io/file_close.c +++ b/core/src/xmake/io/file_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,9 +35,7 @@ */ // io.file_close(file) -tb_int_t xm_io_file_close(lua_State* lua) -{ - // check +tb_int_t xm_io_file_close(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is user data? @@ -45,30 +43,28 @@ tb_int_t xm_io_file_close(lua_State* lua) xm_io_return_error(lua, "close(invalid file)!"); // get file - xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); + 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 + if (xm_io_file_is_file(file)) { 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_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; + 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; + 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 @@ -76,11 +72,15 @@ tb_int_t xm_io_file_close(lua_State* lua) file->u.file_ref = tb_null; // exit fstream - if (file->fstream) tb_stream_exit(file->fstream); + if (file->fstream) { + tb_stream_exit(file->fstream); + } file->fstream = tb_null; // exit stream - if (file->stream) tb_stream_exit(file->stream); + if (file->stream) { + tb_stream_exit(file->stream); + } file->stream = tb_null; // exit the line cache buffer @@ -90,11 +90,9 @@ tb_int_t xm_io_file_close(lua_State* lua) // gc will free it if no any refs for lua_newuserdata() // ... - // ok lua_pushboolean(lua, tb_true); return 1; - } - else // for stdfile (gc/close) + } else // for stdfile (gc/close) { // exit the line cache buffer tb_buffer_exit(&file->rcache); @@ -103,9 +101,7 @@ tb_int_t xm_io_file_close(lua_State* lua) // gc will free it if no any refs for lua_newuserdata() // ... - // ok lua_pushboolean(lua, tb_true); return 1; } } - diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c index e7650e29b..73652ce20 100644 --- a/core/src/xmake/io/file_flush.c +++ b/core/src/xmake/io/file_flush.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_flush" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_flush" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,24 +33,21 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_bool_t xm_io_std_flush_impl(xm_io_file_t* file) -{ +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; + 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 +static tb_bool_t xm_io_file_flush_impl(xm_io_file_t *file) { 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_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; + if (odata && osize) { + if (!tb_stream_bwrit(file->u.file_ref, odata, osize)) + return tb_false; tb_buffer_clear(&file->wcache); } #endif @@ -62,25 +59,24 @@ static tb_bool_t xm_io_file_flush_impl(xm_io_file_t* file) */ // io.file_flush(file) -tb_int_t xm_io_file_flush(lua_State* lua) -{ - // check +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); + 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) - { + 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"); } - 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 53a9c4ac8..4f6bf4272 100644 --- a/core/src/xmake/io/file_isatty.c +++ b/core/src/xmake/io/file_isatty.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_isatty" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_isatty" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,17 +35,16 @@ */ // io.file_isatty(file) -tb_int_t xm_io_file_isatty(lua_State* lua) -{ - // check +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); + xm_io_file_t *file = (xm_io_file_t *)lua_touserdata(lua, 1); tb_check_return_val(file, 0); // is tty? diff --git a/core/src/xmake/io/file_open.c b/core/src/xmake/io/file_open.c index 0cd9c6cb6..901a3f86d 100644 --- a/core/src/xmake/io/file_open.c +++ b/core/src/xmake/io/file_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,46 +35,40 @@ */ // num of bytes read to guess encoding -#define CHECK_SIZE (1024) +#define CHECK_SIZE (1024) // is utf-8 tail character -#define IS_UTF8_TAIL(c) (c >= 0x80 && c < 0xc0) +#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 +static tb_size_t xm_io_file_detect_charset(tb_byte_t const **data_ptr, tb_long_t size) { tb_assert(data_ptr && *data_ptr); - tb_byte_t const* data = *data_ptr; + tb_byte_t const *data = *data_ptr; tb_size_t charset = XM_IO_FILE_ENCODING_BINARY; - do - { + 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) - { + 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) - { + if (size >= 2) { // utf16be - if (data[0] == 254 && data[1] == 255) - { + 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) - { + else if (data[0] == 255 && data[1] == 254) { charset = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE; data += 2; // skip bom break; @@ -86,57 +80,59 @@ static tb_size_t xm_io_file_detect_charset(tb_byte_t const** data_ptr, tb_long_t 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++; + for (tb_long_t i = 0; i < (size - 4) && i < CHECK_SIZE; i++) { + 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) utf16be_conf++; - if (data[i + 1] == 0) utf16le_conf++; + 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) + 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) - { + if (ascii_conf > 0 && zero_count <= 1) { charset = TB_CHARSET_TYPE_UTF8; break; } - if (utf8_conf > 0 && zero_count <= 1) - { + if (utf8_conf > 0 && zero_count <= 1) { charset = TB_CHARSET_TYPE_UTF8; break; } - if (utf16be_conf > 0 && utf16be_conf > utf16le_conf) - { + 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) - { + if (utf16le_conf > 0 && utf16le_conf >= utf16be_conf) { charset = TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE; break; } - if (utf8_conf > 0) - { + if (utf8_conf > 0) { charset = TB_CHARSET_TYPE_UTF8; break; } @@ -150,20 +146,17 @@ static tb_size_t xm_io_file_detect_charset(tb_byte_t const** data_ptr, tb_long_t *data_ptr = data; return charset; } -static tb_size_t xm_io_file_detect_encoding(tb_stream_ref_t stream, tb_long_t* pbomoff) -{ - // check +static tb_size_t xm_io_file_detect_encoding(tb_stream_ref_t stream, tb_long_t *pbomoff) { 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; + 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; } @@ -173,114 +166,126 @@ static tb_size_t xm_io_file_detect_encoding(tb_stream_ref_t stream, tb_long_t* p */ // io.file_open(path, modestr) -tb_int_t xm_io_file_open(lua_State* lua) -{ - // check +tb_int_t xm_io_file_open(lua_State *lua) { 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_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; + 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_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')) + 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) tb_stream_exit(stream); + } else { + if (stream) { + tb_stream_exit(stream); + } xm_io_return_error(lua, "file not found!"); } + } else { + xm_io_return_error(lua, "invalid open mode!"); } - 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; + tb_bool_t open_ok = tb_false; tb_stream_ref_t file_ref = tb_null; - tb_stream_ref_t fstream = tb_null; - do - { + tb_stream_ref_t fstream = tb_null; + do { // init stream from file - stream = stream? stream : tb_stream_init_from_file(path, mode); + 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') + if (is_transcode) { + 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 { + file_ref = stream; } - else file_ref = stream; // open file stream - if (!tb_stream_open(file_ref)) break; + if (!tb_stream_open(file_ref)) { + break; + } // skip bom characters if exists - if (bomoff > 0 && !tb_stream_seek(stream, bomoff)) break; + if (bomoff > 0 && !tb_stream_seek(stream, bomoff)) { + break; + } - // ok open_ok = tb_true; } while (0); // open failed? - if (!open_ok) - { + if (!open_ok) { // exit stream - if (stream) tb_stream_exit(stream); + if (stream) { + tb_stream_exit(stream); + } stream = tb_null; // exit charset stream filter - if (fstream) tb_stream_exit(fstream); + if (fstream) { + tb_stream_exit(fstream); + } fstream = tb_null; // return errors @@ -288,7 +293,7 @@ tb_int_t xm_io_file_open(lua_State* lua) } // make file - xm_io_file_t* file = (xm_io_file_t*)lua_newuserdata(lua, sizeof(xm_io_file_t)); + 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 diff --git a/core/src/xmake/io/file_rawfd.c b/core/src/xmake/io/file_rawfd.c index 9e2383e0a..419955118 100644 --- a/core/src/xmake/io/file_rawfd.c +++ b/core/src/xmake/io/file_rawfd.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_rawfd" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_rawfd" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,9 +36,9 @@ // 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)) +#define xm_io_file2fd(file) (lua_Number)((tb_size_t)(file)) #else -# define xm_io_file2fd(file) (lua_Number)tb_file2fd(file) +#define xm_io_file2fd(file) (lua_Number) tb_file2fd(file) #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -51,25 +51,22 @@ * * 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_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); + 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)) - { + 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)) - { + if (tb_stream_ctrl(file->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile)) { lua_pushnumber(lua, xm_io_file2fd(rawfile)); return 1; } diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index 1c3af6dfe..0b255f4c1 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_read" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_read" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,79 +33,69 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * types */ -typedef enum __xm_pushline_state_e -{ +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 +static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_ref_t line) { 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_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, XM_IO_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; + 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; + } else { + if (!tb_stream_skip(stream, real)) + return -1; tb_buffer_memncat(line, data, real); } - } - else if (!real) - { + } else if (!real) { real = tb_stream_wait(stream, TB_STREAM_WAIT_READ, -1); - if (real <= 0) - { + if (real <= 0) { eof = tb_true; break; } - } - else - { + } 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; + 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 +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) { 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) - { + if (is_binary) { continuation = ""; - keep_crlf = tb_true; + keep_crlf = tb_true; } // clear line buffer @@ -115,127 +105,130 @@ static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file_t* fi 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 - { + tb_int_t result = PL_FAIL; + tb_char_t *data = tb_null; + tb_size_t conlen = tb_strlen(continuation); + do { // eof? - if (size < 0) - { + if (size < 0) { result = PL_EOF; break; } // patch two '\0' - tb_buffer_memncat(&file->rcache, (tb_byte_t const*)"\0\0", 2); + tb_buffer_memncat(&file->rcache, (tb_byte_t const *)"\0\0", 2); // get line data - data = (tb_char_t*)tb_buffer_data(&file->rcache); + 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') + 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') - { + 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; + 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--; + if (!keep_crlf && !has_conline) { + size--; + } // strip it if has continuation? - if (has_conline) size -= conlen + 1; + if (has_conline) { + size -= conlen + 1; + } data[size] = '\0'; result = has_conline ? PL_CONL : PL_FIN; - } - else - { + } else { // a single '\n' - if (!keep_crlf) size = 0; + 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); + 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 +static tb_int_t xm_io_file_read_all_directly(lua_State *lua, xm_io_file_t *file) { 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)) + 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) - { + tb_byte_t *data = tb_buffer_resize(&file->rcache, XM_IO_BLOCK_MAXN); + if (!data) { tb_buffer_exit(&buf); xm_io_return_error(lua, "out of memory: failed to resize read cache"); } // read all tb_stream_ref_t stream = file->u.file_ref; - while (!tb_stream_beof(stream)) - { + 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 { + break; } - 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, ""); + 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 +static tb_int_t xm_io_file_read_all(lua_State *lua, xm_io_file_t *file, tb_char_t const *continuation) { 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) + 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)) - { + 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)); + 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: @@ -251,29 +244,32 @@ static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file_t* file, tb_char_ } } -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 +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) { 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)) + 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)) - { + 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)); + 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)); + lua_pushlstring(lua, (tb_char_t const *)tb_buffer_data(&buf), tb_buffer_size(&buf)); tb_buffer_exit(&buf); return 1; case PL_CONL: @@ -288,120 +284,124 @@ static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file_t* file, tb_char } } -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 +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) { 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) - { - tb_byte_t* data = tb_null; - if (tb_stream_need(file->u.file_ref, &data, 1)) - { + 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); + } 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); + 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 +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) { 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)) + if (tb_stdfile_gets(file->u.std_ref, strbuf, tb_arrayn(strbuf) - 1)) { buflen = tb_strlen(strbuf); - else return PL_EOF; + } else { + return PL_EOF; + } tb_size_t conlen = tb_strlen(continuation); - if (buflen > 0 && strbuf[buflen - 1] != '\n') - { + if (buflen > 0 && strbuf[buflen - 1] != '\n') { // end of file, no lf found result = PL_FIN; - } - else if (buflen > 1) - { + } else if (buflen > 1) { // crlf? => lf - if (strbuf[buflen - 2] == '\r') - { + 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; + 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--; + if (!keep_crlf && !has_conline) { + buflen--; + } // strip it if has continuation? - if (has_conline) buflen -= conlen + 1; - strbuf[buflen] = '\0'; + if (has_conline) { + buflen -= conlen + 1; + } + strbuf[buflen] = '\0'; - result = has_conline? PL_CONL : PL_FIN; - } - else - { + result = has_conline ? PL_CONL : PL_FIN; + } else { // a single '\n' - if (!keep_crlf) buflen = 0; + 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); + 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 +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) { tb_assert(lua && file && continuation && xm_io_file_is_std(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 line tb_bool_t has_content = tb_false; - while (1) - { - switch (xm_io_file_std_buffer_pushline(&buf, file, continuation, keep_crlf)) - { + 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)); + 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)); + lua_pushlstring(lua, (tb_char_t const *)tb_buffer_data(&buf), tb_buffer_size(&buf)); tb_buffer_exit(&buf); return 1; case PL_CONL: @@ -416,9 +416,7 @@ static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file_t* file, tb_ } } -static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation) -{ - // check +static tb_int_t xm_io_file_std_read_all(lua_State *lua, xm_io_file_t *file, tb_char_t const *continuation) { tb_assert(lua && file && continuation && xm_io_file_is_std(file)); // init buffer @@ -428,13 +426,13 @@ static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_c // read all tb_bool_t has_content = tb_false; - while (1) - { - switch (xm_io_file_std_buffer_pushline(&buf, file, continuation, tb_true)) - { + 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)); + 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: @@ -450,9 +448,7 @@ static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_c } } -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 +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) { tb_assert(lua && file && continuation && xm_io_file_is_std(file)); // check continuation @@ -460,8 +456,7 @@ static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file_t* file, tb_cha xm_io_return_error(lua, "continuation is not supported for std streams"); // io.read(0) - if (n == 0) - { + if (n == 0) { tb_char_t ch; if (!tb_stdfile_peek(file->u.std_ref, &ch)) lua_pushnil(lua); @@ -471,19 +466,18 @@ static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file_t* file, tb_cha } // get line buffer - tb_byte_t* buf_ptr = tb_buffer_resize(&file->rcache, (tb_size_t)n); + 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); + 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 +static tb_int_t xm_io_file_std_read_num(lua_State *lua, xm_io_file_t *file, tb_char_t const *continuation) { tb_assert(lua && file && continuation && xm_io_file_is_std(file)); // check continuation @@ -494,7 +488,8 @@ static tb_int_t xm_io_file_std_read_num(lua_State* lua, xm_io_file_t* file, tb_c 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); + else + lua_pushnil(lua); return 1; } @@ -505,9 +500,7 @@ static tb_int_t xm_io_file_std_read_num(lua_State* lua, xm_io_file_t* file, tb_c * io.file_read(file, "n") * io.file_read(file, 10) */ -tb_int_t xm_io_file_read(lua_State* lua) -{ - // check +tb_int_t xm_io_file_read(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is user data? @@ -515,46 +508,50 @@ tb_int_t xm_io_file_read(lua_State* lua) xm_io_return_error(lua, "read(invalid file)!"); // get file - xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); + 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_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)) - { + 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 == '*') + 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); + 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); + } 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; diff --git a/core/src/xmake/io/file_readable.c b/core/src/xmake/io/file_readable.c index edfe496d0..78ea82888 100644 --- a/core/src/xmake/io/file_readable.c +++ b/core/src/xmake/io/file_readable.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_readable" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_readable" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,23 +34,25 @@ * implementation */ -tb_int_t xm_io_file_readable(lua_State* lua) -{ - // check +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); + 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); + 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; } diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c index a0ace5408..c7c043e7a 100644 --- a/core/src/xmake/io/file_seek.c +++ b/core/src/xmake/io/file_seek.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_seek" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_seek" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,51 +35,50 @@ */ // io.file_seek(file, [whence [, offset]]) -tb_int_t xm_io_file_seek(lua_State* lua) -{ - // check +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); + 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_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)) - { + if (xm_io_file_is_file(file)) { tb_assert(file->u.file_ref); - switch (*whence) - { + 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!"); + { + 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" + } break; + default: // "cur" offset = tb_stream_offset(file->u.file_ref) + offset; break; } - if (tb_stream_seek(file->u.file_ref, offset)) - { + 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 failed!"); + } else { + xm_io_return_error(lua, "seek is not supported on this file"); } - 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 39c556896..351bd7ebf 100644 --- a/core/src/xmake/io/file_size.c +++ b/core/src/xmake/io/file_size.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_size" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_size" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,26 +35,25 @@ */ // io.file_size(file) -tb_int_t xm_io_file_size(lua_State* lua) -{ - // check +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); + 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)) - { + 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!"); } - 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 b04303f62..08655379a 100644 --- a/core/src/xmake/io/file_write.c +++ b/core/src/xmake/io/file_write.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "file_write" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "file_write" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,83 +33,67 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t xm_io_file_write_file_utfbom(xm_io_file_t* file) -{ - // check +static tb_void_t xm_io_file_write_file_utfbom(xm_io_file_t *file) { 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; + 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) -{ +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 +static tb_void_t xm_io_file_write_file_transcrlf(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); #ifdef TB_CONFIG_OS_WINDOWS // write cached data first - tb_byte_t const* odata = tb_buffer_data(&file->wcache); + 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 ; + 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) - { + 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; + 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 - { + } else { // cache the left data - tb_buffer_memncat(&file->wcache, (tb_byte_t const*)p, e - p); + tb_buffer_memncat(&file->wcache, (tb_byte_t const *)p, e - p); p = e; break; } @@ -118,9 +102,7 @@ static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file_t* file, tb_byte_t c 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 +static tb_void_t xm_io_file_write_std(xm_io_file_t *file, tb_byte_t const *data, tb_size_t size) { tb_assert(file && data && xm_io_file_is_std(file)); // check type @@ -136,9 +118,7 @@ static tb_void_t xm_io_file_write_std(xm_io_file_t* file, tb_byte_t const* data, */ // io.file_write(file, ...) -tb_int_t xm_io_file_write(lua_State* lua) -{ - // check +tb_int_t xm_io_file_write(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is user data? @@ -146,35 +126,34 @@ tb_int_t xm_io_file_write(lua_State* lua) xm_io_return_error(lua, "write(invalid file)!"); // get file - xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1); + 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) - { + if (narg > 1) { tb_bool_t is_binary = file->encoding == XM_IO_FILE_ENCODING_BINARY; - for (tb_int_t i = 2; i <= narg; i++) - { + 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)) - { + 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); + 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 @@ -185,15 +164,13 @@ 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) - { + if (file->utfbom) { xm_io_file_write_file_utfbom(file); file->utfbom = tb_false; } diff --git a/core/src/xmake/io/filelock_close.c b/core/src/xmake/io/filelock_close.c index 9e73e8d01..69bea4e39 100644 --- a/core/src/xmake/io/filelock_close.c +++ b/core/src/xmake/io/filelock_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "filelock_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "filelock_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.filelock_close(lock) -tb_int_t xm_io_filelock_close(lua_State* lua) -{ - // check +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); @@ -54,7 +53,5 @@ tb_int_t xm_io_filelock_close(lua_State* lua) // save result: ok lua_pushboolean(lua, tb_true); - // ok return 1; } - diff --git a/core/src/xmake/io/filelock_lock.c b/core/src/xmake/io/filelock_lock.c index 00fea1415..070af2eaf 100644 --- a/core/src/xmake/io/filelock_lock.c +++ b/core/src/xmake/io/filelock_lock.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "filelock_lock" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "filelock_lock" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -39,15 +39,12 @@ * 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_int_t xm_io_filelock_lock(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get option argument tb_bool_t is_shared = tb_false; - if (lua_istable(lua, 2)) - { + if (lua_istable(lua, 2)) { // is shared lock? lua_pushstring(lua, "shared"); lua_gettable(lua, 2); @@ -56,15 +53,16 @@ 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); 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); + tb_bool_t ok = tb_filelock_enter(lock, is_shared ? TB_FILELOCK_MODE_SH : TB_FILELOCK_MODE_EX); lua_pushboolean(lua, ok); return 1; } diff --git a/core/src/xmake/io/filelock_open.c b/core/src/xmake/io/filelock_open.c index bd00aaf47..2afb173d3 100644 --- a/core/src/xmake/io/filelock_open.c +++ b/core/src/xmake/io/filelock_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "filelock_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "filelock_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,21 +37,25 @@ /* * io.filelock_open(path) */ -tb_int_t xm_io_filelock_open(lua_State* lua) -{ - // check +tb_int_t xm_io_filelock_open(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get file path - tb_char_t const* path = luaL_checkstring(lua, 1); + 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_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); + 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; } diff --git a/core/src/xmake/io/filelock_trylock.c b/core/src/xmake/io/filelock_trylock.c index 741d33744..d84024834 100644 --- a/core/src/xmake/io/filelock_trylock.c +++ b/core/src/xmake/io/filelock_trylock.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "filelock_trylock" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "filelock_trylock" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -39,15 +39,12 @@ * 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_int_t xm_io_filelock_trylock(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get option argument tb_bool_t is_shared = tb_false; - if (lua_istable(lua, 2)) - { + if (lua_istable(lua, 2)) { // is shared lock? lua_pushstring(lua, "shared"); lua_gettable(lua, 2); @@ -56,15 +53,16 @@ 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); 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); + 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; } diff --git a/core/src/xmake/io/filelock_unlock.c b/core/src/xmake/io/filelock_unlock.c index 62726e106..007bedd54 100644 --- a/core/src/xmake/io/filelock_unlock.c +++ b/core/src/xmake/io/filelock_unlock.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "filelock_unlock" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "filelock_unlock" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.filelock_unlock(lock) -tb_int_t xm_io_filelock_unlock(lua_State* lua) -{ - // check +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 49850e25c..656e86fe7 100644 --- a/core/src/xmake/io/iscygpty.c +++ b/core/src/xmake/io/iscygpty.c @@ -36,82 +36,74 @@ #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 +#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> +#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 +#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 +#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); +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) -{ +#ifndef USE_FILEEXTD +static BOOL WINAPI stub_GetFileInformationByHandleEx(HANDLE hFile, + FILE_INFO_BY_HANDLE_CLASS FileInformationClass, + LPVOID lpFileInformation, + DWORD dwBufferSize) { return FALSE; } -# endif +#endif -static void setup_fileid_api(void) -{ +static void setup_fileid_api(void) { if (pGetFileInformationByHandleEx != NULL) { return; } pGetFileInformationByHandleEx = (pfnGetFileInformationByHandleEx) - GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), - "GetFileInformationByHandleEx"); + GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetFileInformationByHandleEx"); if (pGetFileInformationByHandleEx == NULL) { -# ifdef USE_FILEEXTD +#ifdef USE_FILEEXTD pGetFileInformationByHandleEx = GetFileInformationByHandleEx; -# else +#else pGetFileInformationByHandleEx = stub_GetFileInformationByHandleEx; -# endif +#endif } } #else -# define pGetFileInformationByHandleEx GetFileInformationByHandleEx -# define setup_fileid_api() +#define pGetFileInformationByHandleEx GetFileInformationByHandleEx +#define setup_fileid_api() #endif - -#define is_wprefix(s, prefix) \ - (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0) +#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) -{ +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); + int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * (MAX_PATH - 1); FILE_NAME_INFO *nameinfo; - WCHAR *p = NULL; + WCHAR *p = NULL; setup_fileid_api(); @@ -122,7 +114,7 @@ int is_cygpty(HANDLE h) if (GetFileType(h) != FILE_TYPE_PIPE) { return 0; } - nameinfo = (FILE_NAME_INFO*)malloc(size + sizeof(WCHAR)); + nameinfo = (FILE_NAME_INFO *)malloc(size + sizeof(WCHAR)); if (nameinfo == NULL) { return 0; } @@ -130,8 +122,8 @@ int is_cygpty(HANDLE h) * '\{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 = nameinfo->FileName; + if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */ p += 8; } else if (is_wprefix(p, L"\\msys-")) { /* MSYS and MSYS2 */ p += 6; @@ -139,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 { @@ -148,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")) { @@ -167,5 +161,3 @@ int is_cygpty(HANDLE h) } #endif /* _WIN32 */ - - diff --git a/core/src/xmake/io/pipe_close.c b/core/src/xmake/io/pipe_close.c index 062e7009d..aaf708a31 100644 --- a/core/src/xmake/io/pipe_close.c +++ b/core/src/xmake/io/pipe_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.pipe_close(pipe) -tb_int_t xm_io_pipe_close(lua_State* lua) -{ - // check +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); @@ -52,4 +51,3 @@ tb_int_t xm_io_pipe_close(lua_State* lua) lua_pushboolean(lua, tb_pipe_file_exit(pipefile)); return 1; } - diff --git a/core/src/xmake/io/pipe_connect.c b/core/src/xmake/io/pipe_connect.c index aebf8b7a0..f13f612f9 100644 --- a/core/src/xmake/io/pipe_connect.c +++ b/core/src/xmake/io/pipe_connect.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_connect" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_connect" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.pipe_connect(pipefile) -tb_int_t xm_io_pipe_connect(lua_State* lua) -{ - // check +tb_int_t xm_io_pipe_connect(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check pipe - if (!xm_lua_ispointer(lua, 1)) - { + if (!xm_lua_ispointer(lua, 1)) { lua_pushnumber(lua, -1); lua_pushliteral(lua, "invalid pipe!"); return 2; @@ -56,4 +53,3 @@ tb_int_t xm_io_pipe_connect(lua_State* lua) lua_pushnumber(lua, (tb_int_t)tb_pipe_file_connect(pipefile)); return 1; } - diff --git a/core/src/xmake/io/pipe_open.c b/core/src/xmake/io/pipe_open.c index a77cda6e8..6bfe26347 100644 --- a/core/src/xmake/io/pipe_open.c +++ b/core/src/xmake/io/pipe_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,29 +37,34 @@ /* * io.pipe_open(name, mode, buffsize) */ -tb_int_t xm_io_pipe_open(lua_State* lua) -{ - // check +tb_int_t xm_io_pipe_open(lua_State *lua) { 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_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; + if (modestr[0] == 'w') { + mode = TB_PIPE_MODE_WO; + } // set block mode - if (modestr[1] == 'B') mode |= TB_PIPE_MODE_BLOCK; + 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); + if (pipefile) { + xm_lua_pushpointer(lua, (tb_pointer_t)pipefile); + } 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 03908b418..bf4829f6c 100644 --- a/core/src/xmake/io/pipe_openpair.c +++ b/core/src/xmake/io/pipe_openpair.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_openpair" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_openpair" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,32 +37,31 @@ /* * io.pipe_openpair(mode, buffsize) */ -tb_int_t xm_io_pipe_openpair(lua_State* lua) -{ - // check +tb_int_t xm_io_pipe_openpair(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get pipe mode - tb_char_t const* modestr = luaL_optstring(lua, 1, "AA"); + 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; + 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)) - { + 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 - { + } else { lua_pushnil(lua); lua_pushnil(lua); } diff --git a/core/src/xmake/io/pipe_read.c b/core/src/xmake/io/pipe_read.c index cb3457fec..e0e383bfb 100644 --- a/core/src/xmake/io/pipe_read.c +++ b/core/src/xmake/io/pipe_read.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_read" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_read" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // real, data_or_errors = io.pipe_read(pipefile, size) -tb_int_t xm_io_pipe_read(lua_State* lua) -{ - // check +tb_int_t xm_io_pipe_read(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check pipe file - if (!xm_pipe_file_is_valid(lua, 1)) - { + if (!xm_pipe_file_is_valid(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid pipe file!"); return 2; @@ -53,11 +50,11 @@ tb_int_t xm_io_pipe_read(lua_State* lua) 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) - { + 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; @@ -66,9 +63,10 @@ tb_int_t xm_io_pipe_read(lua_State* lua) // get size tb_long_t size = 0; - if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3); - if (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; diff --git a/core/src/xmake/io/pipe_wait.c b/core/src/xmake/io/pipe_wait.c index f39220d41..d2979dcdd 100644 --- a/core/src/xmake/io/pipe_wait.c +++ b/core/src/xmake/io/pipe_wait.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_wait" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_wait" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.pipe_wait(pipefile, events, timeout) -tb_int_t xm_io_pipe_wait(lua_State* lua) -{ - // check +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); @@ -58,4 +57,3 @@ tb_int_t xm_io_pipe_wait(lua_State* lua) lua_pushnumber(lua, (tb_int_t)tb_pipe_file_wait(pipefile, events, timeout)); return 1; } - diff --git a/core/src/xmake/io/pipe_write.c b/core/src/xmake/io/pipe_write.c index 140894428..d3098dd60 100644 --- a/core/src/xmake/io/pipe_write.c +++ b/core/src/xmake/io/pipe_write.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "pipe_write" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "pipe_write" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.pipe_write(pipefile, data, start, last) -tb_int_t xm_io_pipe_write(lua_State* lua) -{ - // check +tb_int_t xm_io_pipe_write(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)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid pipe file!"); return 2; @@ -54,11 +51,14 @@ 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)) 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) - { + 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; diff --git a/core/src/xmake/io/poller.c b/core/src/xmake/io/poller.c index 7700c6858..6e155e4a8 100644 --- a/core/src/xmake/io/poller.c +++ b/core/src/xmake/io/poller.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,12 +34,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_poller_ref_t xm_io_poller(lua_State* lua) -{ +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) poller = xm_engine_poller(engine); + if (engine) { + poller = xm_engine_poller(engine); + } tb_assert(poller); return poller; } - diff --git a/core/src/xmake/io/poller.h b/core/src/xmake/io/poller.h index 887fc25ba..3c5937a68 100644 --- a/core/src/xmake/io/poller.h +++ b/core/src/xmake/io/poller.h @@ -31,12 +31,10 @@ */ // the poller state in wait events -typedef struct __xm_poller_state_t -{ - lua_State* lua; - tb_int_t events_count; - -}xm_poller_state_t; +typedef struct __xm_poller_state_t { + lua_State *lua; + tb_int_t events_count; +} xm_poller_state_t; /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces @@ -46,8 +44,6 @@ typedef struct __xm_poller_state_t * * @return the io poller */ -tb_poller_ref_t xm_io_poller(lua_State* lua); +tb_poller_ref_t xm_io_poller(lua_State *lua); #endif - - diff --git a/core/src/xmake/io/poller_insert.c b/core/src/xmake/io/poller_insert.c index e78c3c18f..81f66cce9 100644 --- a/core/src/xmake/io/poller_insert.c +++ b/core/src/xmake/io/poller_insert.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller_insert" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller_insert" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,14 +36,11 @@ */ // io.poller_insert(obj:otype(), obj:cdata(), events) -tb_int_t xm_io_poller_insert(lua_State* lua) -{ - // check +tb_int_t xm_io_poller_insert(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is pointer? - if (!xm_lua_ispointer(lua, 2)) - { + if (!xm_lua_ispointer(lua, 2)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "invalid poller object!"); return 2; @@ -53,8 +50,8 @@ tb_int_t xm_io_poller_insert(lua_State* lua) 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_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 @@ -67,4 +64,3 @@ tb_int_t xm_io_poller_insert(lua_State* lua) lua_pushboolean(lua, tb_poller_insert(xm_io_poller(lua), &object, events, cdata_str)); return 1; } - diff --git a/core/src/xmake/io/poller_modify.c b/core/src/xmake/io/poller_modify.c index 5ed858030..f9283aecb 100644 --- a/core/src/xmake/io/poller_modify.c +++ b/core/src/xmake/io/poller_modify.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller_modify" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller_modify" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,14 +36,11 @@ */ // io.poller_modify(obj:otype(), obj:cdata(), events) -tb_int_t xm_io_poller_modify(lua_State* lua) -{ - // check +tb_int_t xm_io_poller_modify(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is pointer? - if (!xm_lua_ispointer(lua, 2)) - { + if (!xm_lua_ispointer(lua, 2)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "invalid poller object!"); return 2; @@ -53,8 +50,8 @@ tb_int_t xm_io_poller_modify(lua_State* lua) 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_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 @@ -67,4 +64,3 @@ tb_int_t xm_io_poller_modify(lua_State* lua) lua_pushboolean(lua, tb_poller_modify(xm_io_poller(lua), &object, events, cdata_str)); return 1; } - diff --git a/core/src/xmake/io/poller_remove.c b/core/src/xmake/io/poller_remove.c index 3cad1aa0a..3127bf2b4 100644 --- a/core/src/xmake/io/poller_remove.c +++ b/core/src/xmake/io/poller_remove.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller_remove" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller_remove" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,14 +36,11 @@ */ // io.poller_remove(obj:otype(), obj) -tb_int_t xm_io_poller_remove(lua_State* lua) -{ - // check +tb_int_t xm_io_poller_remove(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is pointer? - if (!xm_lua_ispointer(lua, 2)) - { + if (!xm_lua_ispointer(lua, 2)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "invalid poller object!"); return 2; @@ -63,4 +60,3 @@ tb_int_t xm_io_poller_remove(lua_State* lua) lua_pushboolean(lua, tb_poller_remove(xm_io_poller(lua), &object)); return 1; } - diff --git a/core/src/xmake/io/poller_spank.c b/core/src/xmake/io/poller_spank.c index ac7207763..7fdb38605 100644 --- a/core/src/xmake/io/poller_spank.c +++ b/core/src/xmake/io/poller_spank.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller_spank" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller_spank" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,13 +36,10 @@ */ // io.poller_spank() -tb_int_t xm_io_poller_spank(lua_State* lua) -{ - // check +tb_int_t xm_io_poller_spank(lua_State *lua) { 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(lua)); return 0; } - diff --git a/core/src/xmake/io/poller_support.c b/core/src/xmake/io/poller_support.c index 36ae52b34..d63f8da34 100644 --- a/core/src/xmake/io/poller_support.c +++ b/core/src/xmake/io/poller_support.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller_support" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller_support" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,9 +36,7 @@ */ // io.poller_support(events) -tb_int_t xm_io_poller_support(lua_State* lua) -{ - // check +tb_int_t xm_io_poller_support(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get events @@ -48,4 +46,3 @@ tb_int_t xm_io_poller_support(lua_State* lua) lua_pushboolean(lua, tb_poller_support(xm_io_poller(lua), events)); return 1; } - diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c index 341893f74..dc1418a03 100644 --- a/core/src/xmake/io/poller_wait.c +++ b/core/src/xmake/io/poller_wait.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "poller_wait" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "poller_wait" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,26 +34,28 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * 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 - xm_poller_state_t* state = (xm_poller_state_t*)tb_poller_priv(poller); +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) { + xm_poller_state_t *state = (xm_poller_state_t *)tb_poller_priv(poller); tb_assert_and_check_return(state && state->lua); // save object and events - lua_State* lua = state->lua; + lua_State *lua = state->lua; lua_newtable(lua); lua_pushinteger(lua, (tb_int_t)object->type); lua_rawseti(lua, -2, 1); - if (priv) lua_pushstring(lua, (tb_char_t const*)priv); - else lua_pushlightuserdata(lua, object->ref.ptr); + if (priv) { + lua_pushstring(lua, (tb_char_t const *)priv); + } else { + lua_pushlightuserdata(lua, object->ref.ptr); + } lua_rawseti(lua, -2, 2); - if (object->type == TB_POLLER_OBJECT_FWATCHER) - { + if (object->type == TB_POLLER_OBJECT_FWATCHER) { lua_newtable(lua); - tb_fwatcher_event_t* event = (tb_fwatcher_event_t*)events; - if (event) - { + tb_fwatcher_event_t *event = (tb_fwatcher_event_t *)events; + if (event) { lua_pushstring(lua, "path"); lua_pushstring(lua, event->filepath); lua_settable(lua, -3); @@ -62,8 +64,9 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref lua_pushinteger(lua, event->event); lua_settable(lua, -3); } + } else { + lua_pushinteger(lua, (tb_int_t)events); } - else lua_pushinteger(lua, (tb_int_t)events); lua_rawseti(lua, -2, 3); lua_rawseti(lua, -2, ++state->events_count); } @@ -73,9 +76,7 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref */ // local events, count = io.poller_wait(timeout) -tb_int_t xm_io_poller_wait(lua_State* lua) -{ - // check +tb_int_t xm_io_poller_wait(lua_State *lua) { tb_poller_ref_t poller = xm_io_poller(lua); tb_assert_and_check_return_val(poller && lua, 0); @@ -83,19 +84,16 @@ tb_int_t xm_io_poller_wait(lua_State* lua) tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 1); // reset events count - xm_poller_state_t* state = (xm_poller_state_t*)tb_poller_priv(poller); - state->events_count = 0; + xm_poller_state_t *state = (xm_poller_state_t *)tb_poller_priv(poller); + state->events_count = 0; // wait it lua_newtable(lua); tb_long_t count = tb_poller_wait(poller, xm_io_poller_event, timeout); - if (count > 0) - { + if (count > 0) { lua_pushinteger(lua, (tb_int_t)count); return 2; - } - else if (!count) - { + } else if (!count) { // timeout lua_pop(lua, 1); lua_pushnil(lua); @@ -103,10 +101,8 @@ tb_int_t xm_io_poller_wait(lua_State* lua) return 2; } - // failed lua_pop(lua, 1); lua_pushnil(lua); lua_pushinteger(lua, -1); return 2; } - diff --git a/core/src/xmake/io/prefix.h b/core/src/xmake/io/prefix.h index df4d09a17..4d312583c 100644 --- a/core/src/xmake/io/prefix.h +++ b/core/src/xmake/io/prefix.h @@ -29,88 +29,80 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * macros */ -#define XM_IO_BLOCK_MAXN (TB_STREAM_BLOCK_MAXN * 10) +#define XM_IO_BLOCK_MAXN (TB_STREAM_BLOCK_MAXN * 10) -#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)) +#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; \ +#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 +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_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 - +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 - { +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; + 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 + 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; // check pipe file -static __tb_inline__ tb_bool_t xm_pipe_file_is_valid(lua_State* lua, tb_int_t index) -{ +static __tb_inline__ tb_bool_t xm_pipe_file_is_valid(lua_State *lua, tb_int_t index) { return xm_lua_ispointer(lua, index) || xm_lua_isinteger(lua, index); } // get the pipe file from arguments -static __tb_inline__ tb_pipe_file_ref_t xm_pipe_file_get(lua_State* lua, tb_int_t index) -{ +static __tb_inline__ tb_pipe_file_ref_t xm_pipe_file_get(lua_State *lua, tb_int_t index) { tb_pipe_file_ref_t pipe_file = tb_null; - if (xm_lua_isinteger(lua, index)) pipe_file = (tb_pipe_file_ref_t)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); - else if (xm_lua_ispointer(lua, index)) pipe_file = (tb_pipe_file_ref_t)xm_lua_topointer(lua, index); + if (xm_lua_isinteger(lua, index)) + pipe_file = (tb_pipe_file_ref_t)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + else if (xm_lua_ispointer(lua, index)) + pipe_file = (tb_pipe_file_ref_t)xm_lua_topointer(lua, index); return pipe_file; } #endif - - diff --git a/core/src/xmake/io/socket_accept.c b/core/src/xmake/io/socket_accept.c index 6a08d1908..5aed6d263 100644 --- a/core/src/xmake/io/socket_accept.c +++ b/core/src/xmake/io/socket_accept.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_accept" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_accept" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // local sock = io.socket_accept(sock) -tb_int_t xm_io_socket_accept(lua_State* lua) -{ - // check +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); @@ -50,8 +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) xm_lua_pushpointer(lua, (tb_pointer_t)client); - else lua_pushnil(lua); + if (client) { + xm_lua_pushpointer(lua, (tb_pointer_t)client); + } else { + lua_pushnil(lua); + } return 1; } - diff --git a/core/src/xmake/io/socket_bind.c b/core/src/xmake/io/socket_bind.c index e546c72c6..d1a30789a 100644 --- a/core/src/xmake/io/socket_bind.c +++ b/core/src/xmake/io/socket_bind.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_bind" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_bind" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.socket_bind(sock, addr, port, family) -tb_int_t xm_io_socket_bind(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_bind(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)) { lua_pushboolean(lua, tb_false); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -53,7 +50,7 @@ tb_int_t xm_io_socket_bind(lua_State* lua) tb_check_return_val(sock, 0); // get address - tb_char_t const* address = lua_tostring(lua, 2); + tb_char_t const *address = lua_tostring(lua, 2); tb_assert_and_check_return_val(address, 0); // get family @@ -61,13 +58,10 @@ tb_int_t xm_io_socket_bind(lua_State* lua) // init address tb_ipaddr_t addr; - if (family == TB_IPADDR_FAMILY_UNIX) - { + 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 - { + } else { tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 3); tb_ipaddr_set(&addr, address, port, family); } @@ -76,4 +70,3 @@ tb_int_t xm_io_socket_bind(lua_State* lua) lua_pushboolean(lua, tb_socket_bind(sock, &addr)); return 1; } - diff --git a/core/src/xmake/io/socket_close.c b/core/src/xmake/io/socket_close.c index a424a707f..401ffd7b6 100644 --- a/core/src/xmake/io/socket_close.c +++ b/core/src/xmake/io/socket_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.socket_close(sock) -tb_int_t xm_io_socket_close(lua_State* lua) -{ - // check +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); @@ -52,4 +51,3 @@ tb_int_t xm_io_socket_close(lua_State* lua) lua_pushboolean(lua, tb_socket_exit(sock)); return 1; } - diff --git a/core/src/xmake/io/socket_connect.c b/core/src/xmake/io/socket_connect.c index 636fe92b1..94c2f1e8d 100644 --- a/core/src/xmake/io/socket_connect.c +++ b/core/src/xmake/io/socket_connect.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_connect" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_connect" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.socket_connect(sock, addr, port, family) -tb_int_t xm_io_socket_connect(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_connect(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)) { lua_pushnumber(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -53,7 +50,7 @@ tb_int_t xm_io_socket_connect(lua_State* lua) tb_check_return_val(sock, 0); // get address - tb_char_t const* address = lua_tostring(lua, 2); + tb_char_t const *address = lua_tostring(lua, 2); tb_assert_and_check_return_val(address, 0); // get family @@ -61,13 +58,10 @@ tb_int_t xm_io_socket_connect(lua_State* lua) // init address tb_ipaddr_t addr; - if (family == TB_IPADDR_FAMILY_UNIX) - { + 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 - { + } else { tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 3); tb_ipaddr_set(&addr, address, port, family); } @@ -76,4 +70,3 @@ tb_int_t xm_io_socket_connect(lua_State* lua) lua_pushnumber(lua, (tb_int_t)tb_socket_connect(sock, &addr)); return 1; } - diff --git a/core/src/xmake/io/socket_ctrl.c b/core/src/xmake/io/socket_ctrl.c index a4192850a..0a51d65bc 100644 --- a/core/src/xmake/io/socket_ctrl.c +++ b/core/src/xmake/io/socket_ctrl.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_ctrl" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_ctrl" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.socket_ctrl(sock, code, value) -tb_int_t xm_io_socket_ctrl(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_ctrl(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)) { lua_pushnumber(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -62,4 +59,3 @@ tb_int_t xm_io_socket_ctrl(lua_State* lua) lua_pushboolean(lua, tb_socket_ctrl(sock, code, value)); return 1; } - diff --git a/core/src/xmake/io/socket_kill.c b/core/src/xmake/io/socket_kill.c index e590ece72..33d36ef18 100644 --- a/core/src/xmake/io/socket_kill.c +++ b/core/src/xmake/io/socket_kill.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_kill" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_kill" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // local sock = io.socket_kill(sock) -tb_int_t xm_io_socket_kill(lua_State* lua) -{ - // check +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); @@ -52,4 +51,3 @@ tb_int_t xm_io_socket_kill(lua_State* lua) tb_socket_kill(sock, TB_SOCKET_KILL_RW); return 0; } - diff --git a/core/src/xmake/io/socket_listen.c b/core/src/xmake/io/socket_listen.c index 67c10b7e0..14971e589 100644 --- a/core/src/xmake/io/socket_listen.c +++ b/core/src/xmake/io/socket_listen.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_listen" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_listen" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.socket_listen(sock, backlog) -tb_int_t xm_io_socket_listen(lua_State* lua) -{ - // check +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); @@ -55,4 +54,3 @@ tb_int_t xm_io_socket_listen(lua_State* lua) lua_pushnumber(lua, (tb_int_t)tb_socket_listen(sock, backlog)); return 1; } - diff --git a/core/src/xmake/io/socket_open.c b/core/src/xmake/io/socket_open.c index 6453fa4d7..2ed04ca97 100644 --- a/core/src/xmake/io/socket_open.c +++ b/core/src/xmake/io/socket_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,9 +37,7 @@ /* * io.socket_open(socktype, family) */ -tb_int_t xm_io_socket_open(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_open(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get socket type @@ -49,8 +47,7 @@ tb_int_t xm_io_socket_open(lua_State* lua) tb_size_t family = (tb_size_t)luaL_checknumber(lua, 2); // map socket type - switch (socktype) - { + switch (socktype) { case 2: socktype = TB_SOCKET_TYPE_UDP; break; @@ -64,7 +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) xm_lua_pushpointer(lua, (tb_pointer_t)sock); - else lua_pushnil(lua); + if (sock) { + xm_lua_pushpointer(lua, (tb_pointer_t)sock); + } 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 f955672df..686d46799 100644 --- a/core/src/xmake/io/socket_peeraddr.c +++ b/core/src/xmake/io/socket_peeraddr.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_peeraddr" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_peeraddr" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,7 +35,7 @@ */ // socket to fd -#define xm_io_sock2fd(sock) (lua_Number)tb_sock2fd(sock) +#define xm_io_sock2fd(sock) (lua_Number) tb_sock2fd(sock) /* ////////////////////////////////////////////////////////////////////////////////////// * implementation @@ -43,25 +43,26 @@ /* io.socket_peeraddr(sock) */ -tb_int_t xm_io_socket_peeraddr(lua_State* lua) -{ - // check +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); 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)))) + 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); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/io/socket_rawfd.c b/core/src/xmake/io/socket_rawfd.c index 5e179b836..6340c0c19 100644 --- a/core/src/xmake/io/socket_rawfd.c +++ b/core/src/xmake/io/socket_rawfd.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_rawfd" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_rawfd" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,7 +35,7 @@ */ // socket to fd -#define xm_io_sock2fd(sock) (lua_Number)tb_sock2fd(sock) +#define xm_io_sock2fd(sock) (lua_Number) tb_sock2fd(sock) /* ////////////////////////////////////////////////////////////////////////////////////// * implementation @@ -43,9 +43,7 @@ /* io.socket_rawfd(sock) */ -tb_int_t xm_io_socket_rawfd(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_rawfd(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // is pointer? diff --git a/core/src/xmake/io/socket_recv.c b/core/src/xmake/io/socket_recv.c index be6a91f93..3ccb9b461 100644 --- a/core/src/xmake/io/socket_recv.c +++ b/core/src/xmake/io/socket_recv.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_recv" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_recv" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // real, data_or_errors = io.socket_recv(sock, size) -tb_int_t xm_io_socket_recv(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_recv(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)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -53,11 +50,11 @@ tb_int_t xm_io_socket_recv(lua_State* lua) 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) - { + 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; @@ -66,9 +63,10 @@ tb_int_t xm_io_socket_recv(lua_State* lua) // get size tb_long_t size = 0; - if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3); - if (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; diff --git a/core/src/xmake/io/socket_recvfrom.c b/core/src/xmake/io/socket_recvfrom.c index 46beb6dc1..5e212bed8 100644 --- a/core/src/xmake/io/socket_recvfrom.c +++ b/core/src/xmake/io/socket_recvfrom.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_recvfrom" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_recvfrom" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // real, data_or_errors, addr, port = io.socket_recvfrom(sock, size) -tb_int_t xm_io_socket_recvfrom(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_recvfrom(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)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -53,11 +50,11 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) 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) - { + 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; @@ -66,9 +63,10 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) // get size tb_long_t size = 0; - if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3); - if (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; @@ -77,19 +75,16 @@ tb_int_t xm_io_socket_recvfrom(lua_State* lua) // 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); + 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) - { + 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) - { + 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; diff --git a/core/src/xmake/io/socket_send.c b/core/src/xmake/io/socket_send.c index 4355db653..3a11d0d43 100644 --- a/core/src/xmake/io/socket_send.c +++ b/core/src/xmake/io/socket_send.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_send" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_send" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.socket_send(sock, data, start, last) -tb_int_t xm_io_socket_send(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_send(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)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -54,11 +51,14 @@ 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)) 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) - { + 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; diff --git a/core/src/xmake/io/socket_sendfile.c b/core/src/xmake/io/socket_sendfile.c index 3dbd83ca8..e12e574d7 100644 --- a/core/src/xmake/io/socket_sendfile.c +++ b/core/src/xmake/io/socket_sendfile.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_sendfile" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_sendfile" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,22 +35,18 @@ */ // io.socket_sendfile(sock, file, start, last) -tb_int_t xm_io_socket_sendfile(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_sendfile(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)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; } // check file - if (!lua_isuserdata(lua, 2)) - { + if (!lua_isuserdata(lua, 2)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid file!"); return 2; @@ -61,12 +57,11 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua) tb_check_return_val(sock, 0); // get file - xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 2); + 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) - { + if (!xm_io_file_is_file(file) || !file->stream) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid file type!"); return 2; @@ -74,8 +69,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua) // get file reference tb_file_ref_t rawfile = tb_null; - if (!tb_stream_ctrl(file->stream, TB_STREAM_CTRL_FILE_GET_FILE, &rawfile) || !rawfile) - { + 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; @@ -83,8 +77,7 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua) // get file size tb_hize_t filesize = tb_file_size(rawfile); - if (!filesize) - { + if (!filesize) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "cannot send empty file!"); return 2; @@ -92,9 +85,10 @@ tb_int_t xm_io_socket_sendfile(lua_State* lua) // 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) - { + 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; @@ -102,9 +96,10 @@ 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)) last = (tb_long_t)lua_tonumber(lua, 4); - if (last < start - 1 || last > filesize + start - 1) - { + 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; diff --git a/core/src/xmake/io/socket_sendto.c b/core/src/xmake/io/socket_sendto.c index 5d5b4c8aa..ccbbb1cc2 100644 --- a/core/src/xmake/io/socket_sendto.c +++ b/core/src/xmake/io/socket_sendto.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_sendto" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_sendto" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // io.socket_sendto(sock, data, addr, port) -tb_int_t xm_io_socket_sendto(lua_State* lua) -{ - // check +tb_int_t xm_io_socket_sendto(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)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid socket!"); return 2; @@ -54,11 +51,14 @@ 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)) 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) - { + 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; @@ -66,10 +66,9 @@ tb_int_t xm_io_socket_sendto(lua_State* lua) tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t)); // get address - tb_char_t const* addr = lua_tostring(lua, 4); + tb_char_t const *addr = lua_tostring(lua, 4); tb_uint16_t port = (tb_uint16_t)luaL_checknumber(lua, 5); - if (!addr || !port) - { + if (!addr || !port) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid address!"); return 2; diff --git a/core/src/xmake/io/socket_wait.c b/core/src/xmake/io/socket_wait.c index 681cf20e8..409d69c91 100644 --- a/core/src/xmake/io/socket_wait.c +++ b/core/src/xmake/io/socket_wait.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "socket_wait" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "socket_wait" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // io.socket_wait(sock, events, timeout) -tb_int_t xm_io_socket_wait(lua_State* lua) -{ - // check +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); @@ -58,4 +57,3 @@ tb_int_t xm_io_socket_wait(lua_State* lua) lua_pushnumber(lua, (tb_int_t)tb_socket_wait(sock, events, timeout)); return 1; } - diff --git a/core/src/xmake/io/stdfile.c b/core/src/xmake/io/stdfile.c index 949aa6c3a..ac77b272d 100644 --- a/core/src/xmake/io/stdfile.c +++ b/core/src/xmake/io/stdfile.c @@ -22,21 +22,21 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "stdfile" -#define TB_TRACE_MODULE_DEBUG (0) +#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" +#include <io.h> +#include "iscygpty.c" #else -# include <stdio.h> -# include <unistd.h> -# include <sys/types.h> -# include <sys/stat.h> +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -44,62 +44,72 @@ */ // 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) +#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) -{ +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; + 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) + 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; + 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; + 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) -{ +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) + 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) -{ +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) - { + switch (type) { case XM_IO_FILE_TYPE_STDIN: fp = tb_stdfile_input(); break; @@ -112,15 +122,15 @@ static xm_io_file_t* xm_io_stdfile_new(lua_State* lua, tb_size_t type) } // new file - xm_io_file_t* file = (xm_io_file_t*)lua_newuserdata(lua, sizeof(xm_io_file_t)); + 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; + 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); @@ -136,9 +146,7 @@ static xm_io_file_t* xm_io_stdfile_new(lua_State* lua, tb_size_t type) */ // io.stdfile(stdin: 1, stdout: 2, stderr: 3) -tb_int_t xm_io_stdfile(lua_State* lua) -{ - // check +tb_int_t xm_io_stdfile(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get std type @@ -148,9 +156,10 @@ 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) return 1; - else xm_io_return_error(lua, "invalid stdfile type!"); + xm_io_file_t *file = xm_io_stdfile_new(lua, type); + if (file) { + return 1; + } else { + xm_io_return_error(lua, "invalid stdfile type!"); + } } - - diff --git a/core/src/xmake/libc/byteof.c b/core/src/xmake/libc/byteof.c index 6dff49100..1d49bf815 100644 --- a/core/src/xmake/libc/byteof.c +++ b/core/src/xmake/libc/byteof.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "byteof" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "byteof" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,26 +33,26 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_byteof(lua_State* lua) -{ - // check +tb_int_t xm_libc_byteof(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get data tb_pointer_t data = tb_null; - if (lua_isnumber(lua, 1)) + if (lua_isnumber(lua, 1)) { data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1); - else if (lua_isstring(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)!"); + } else { + xm_libc_return_error(lua, "libc.byteof(invalid data)!"); + } // get offset tb_int_t offset = 0; - if (lua_isnumber(lua, 2)) + 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]); + } else { + xm_libc_return_error(lua, "libc.byteof(invalid offset)!"); + } + lua_pushinteger(lua, ((tb_byte_t const *)data)[offset]); return 1; } - - diff --git a/core/src/xmake/libc/dataptr.c b/core/src/xmake/libc/dataptr.c index b78b32558..881b373d2 100644 --- a/core/src/xmake/libc/dataptr.c +++ b/core/src/xmake/libc/dataptr.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "dataptr" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "dataptr" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,20 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_dataptr(lua_State* lua) -{ - // check +tb_int_t xm_libc_dataptr(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_pointer_t data = tb_null; - if (lua_isstring(lua, 1)) + if (lua_isstring(lua, 1)) { data = (tb_pointer_t)luaL_checkstring(lua, 1); - else if (lua_isnumber(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)) + } 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)!"); + } else { + xm_libc_return_error(lua, "libc.dataptr(invalid data)!"); + } lua_pushinteger(lua, (lua_Integer)(tb_long_t)data); return 1; } - diff --git a/core/src/xmake/libc/free.c b/core/src/xmake/libc/free.c index 89dc65ed0..0b686aeb5 100644 --- a/core/src/xmake/libc/free.c +++ b/core/src/xmake/libc/free.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "free" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "free" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_free(lua_State* lua) -{ - // check +tb_int_t xm_libc_free(lua_State *lua) { 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); + if (data) { + tb_free(data); + } return 0; } - diff --git a/core/src/xmake/libc/malloc.c b/core/src/xmake/libc/malloc.c index 989cb6aac..6bda4167d 100644 --- a/core/src/xmake/libc/malloc.c +++ b/core/src/xmake/libc/malloc.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "malloc" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "malloc" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,16 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_malloc(lua_State* lua) -{ - // check +tb_int_t xm_libc_malloc(lua_State *lua) { 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); + tb_long_t size = (tb_long_t)luaL_checkinteger(lua, 1); + if (size > 0) { + data = tb_malloc(size); + } lua_pushinteger(lua, (lua_Integer)(tb_long_t)data); return 1; } - diff --git a/core/src/xmake/libc/memcpy.c b/core/src/xmake/libc/memcpy.c index d565b300e..ce5e34312 100644 --- a/core/src/xmake/libc/memcpy.c +++ b/core/src/xmake/libc/memcpy.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "memcpy" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "memcpy" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_memcpy(lua_State* lua) -{ - // check +tb_int_t xm_libc_memcpy(lua_State *lua) { 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_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; } - diff --git a/core/src/xmake/libc/memmov.c b/core/src/xmake/libc/memmov.c index eaa6cc8bc..1843a4301 100644 --- a/core/src/xmake/libc/memmov.c +++ b/core/src/xmake/libc/memmov.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "memmov" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "memmov" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_memmov(lua_State* lua) -{ - // check +tb_int_t xm_libc_memmov(lua_State *lua) { 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_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; } - diff --git a/core/src/xmake/libc/memset.c b/core/src/xmake/libc/memset.c index 4ac025fa2..01b4f3f89 100644 --- a/core/src/xmake/libc/memset.c +++ b/core/src/xmake/libc/memset.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "memset" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "memset" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_memset(lua_State* lua) -{ - // check +tb_int_t xm_libc_memset(lua_State *lua) { 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_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; } - diff --git a/core/src/xmake/libc/prefix.h b/core/src/xmake/libc/prefix.h index dd6dcb2ec..3bed4da3d 100644 --- a/core/src/xmake/libc/prefix.h +++ b/core/src/xmake/libc/prefix.h @@ -31,14 +31,11 @@ */ // return libc error -#define xm_libc_return_error(lua, error) \ - do \ - { \ - lua_pushnil(lua); \ - lua_pushliteral(lua, error); \ - return 2; \ +#define xm_libc_return_error(lua, error) \ + do { \ + lua_pushnil(lua); \ + lua_pushliteral(lua, error); \ + return 2; \ } while (0) #endif - - diff --git a/core/src/xmake/libc/setbyte.c b/core/src/xmake/libc/setbyte.c index 5dd77396c..3bcc3fcdc 100644 --- a/core/src/xmake/libc/setbyte.c +++ b/core/src/xmake/libc/setbyte.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "setbyte" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "setbyte" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,30 +33,32 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_setbyte(lua_State* lua) -{ - // check +tb_int_t xm_libc_setbyte(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get data tb_pointer_t data = tb_null; - if (lua_isnumber(lua, 1)) + if (lua_isnumber(lua, 1)) { data = (tb_pointer_t)(tb_size_t)lua_tointeger(lua, 1); - else if (lua_isstring(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)!"); + } else { + xm_libc_return_error(lua, "libc.setbyte(invalid data)!"); + } // get offset tb_int_t offset = 0; - if (lua_isnumber(lua, 2)) + if (lua_isnumber(lua, 2)) { offset = (tb_int_t)lua_tointeger(lua, 2); - else xm_libc_return_error(lua, "libc.setbyte(invalid offset)!"); + } 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)!"); + 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; } - - diff --git a/core/src/xmake/libc/strndup.c b/core/src/xmake/libc/strndup.c index f8b32b27d..138a4907f 100644 --- a/core/src/xmake/libc/strndup.c +++ b/core/src/xmake/libc/strndup.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "strndup" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "strndup" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,22 +33,23 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_libc_strndup(lua_State* lua) -{ - // check +tb_int_t xm_libc_strndup(lua_State *lua) { 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)) + 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)!"); + } 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) + if (s && n >= 0) { lua_pushlstring(lua, s, n); - else lua_pushliteral(lua, ""); + } else { + lua_pushliteral(lua, ""); + } return 1; } - diff --git a/core/src/xmake/lz4/block_compress.c b/core/src/xmake/lz4/block_compress.c index 8e07cea24..34fb06def 100644 --- a/core/src/xmake/lz4/block_compress.c +++ b/core/src/xmake/lz4/block_compress.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "block_compress" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "block_compress" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_block_compress(lua_State* lua) -{ - // check +tb_int_t xm_lz4_block_compress(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get data and size tb_int_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_int_t)lua_tointeger(lua, 2); - if (!data || !size || size > LZ4_MAX_INPUT_SIZE) - { + 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_int_t)lua_tointeger(lua, 2); + } + if (!data || !size || size > LZ4_MAX_INPUT_SIZE) { lua_pushnil(lua); lua_pushfstring(lua, "invalid data(%p) and size(%d)!", data, (tb_int_t)size); return 2; @@ -52,30 +53,30 @@ tb_int_t xm_lz4_block_compress(lua_State* lua) tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t)); // do compress - tb_bool_t ok = tb_false; - tb_byte_t* output_data = tb_null; - tb_byte_t buffer[8192]; - do - { + tb_bool_t ok = tb_false; + tb_byte_t *output_data = tb_null; + tb_byte_t buffer[8192]; + do { tb_int_t output_size = LZ4_compressBound(size); tb_assert_and_check_break(output_size); - output_data = output_size <= sizeof(buffer)? buffer : (tb_byte_t*)tb_malloc(output_size); + output_data = output_size <= sizeof(buffer) ? buffer : (tb_byte_t *)tb_malloc(output_size); tb_assert_and_check_break(output_data); - tb_int_t real = LZ4_compress_default((tb_char_t const*)data, (tb_char_t*)output_data, size, output_size); + tb_int_t real = LZ4_compress_default((tb_char_t const *)data, (tb_char_t *)output_data, size, output_size); tb_assert_and_check_break(real > 0); - lua_pushlstring(lua, (tb_char_t const*)output_data, real); + lua_pushlstring(lua, (tb_char_t const *)output_data, real); ok = tb_true; } while (0); - if (output_data && output_data != buffer) - { + if (output_data && output_data != buffer) { tb_free(output_data); output_data = tb_null; } - if (!ok) lua_pushnil(lua); + if (!ok) { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/lz4/block_decompress.c b/core/src/xmake/lz4/block_decompress.c index c62b81489..07c80236d 100644 --- a/core/src/xmake/lz4/block_decompress.c +++ b/core/src/xmake/lz4/block_decompress.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "block_decompress" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "block_decompress" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_block_decompress(lua_State* lua) -{ - // check +tb_int_t xm_lz4_block_decompress(lua_State *lua) { 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) - { + 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; @@ -53,36 +54,34 @@ tb_int_t xm_lz4_block_decompress(lua_State* lua) // get real size tb_int_t real = (tb_int_t)lua_tointeger(lua, 3); - if (real <= 0) - { + if (real <= 0) { lua_pushnil(lua); lua_pushfstring(lua, "invalid output size(%d)!", real); return 2; } // do decompress - tb_bool_t ok = tb_false; - tb_byte_t* output_data = tb_null; - tb_byte_t buffer[8192]; - do - { - output_data = real <= sizeof(buffer)? buffer : (tb_byte_t*)tb_malloc(real); + tb_bool_t ok = tb_false; + tb_byte_t *output_data = tb_null; + tb_byte_t buffer[8192]; + do { + output_data = real <= sizeof(buffer) ? buffer : (tb_byte_t *)tb_malloc(real); tb_assert_and_check_break(output_data); - tb_int_t r = LZ4_decompress_safe((tb_char_t const*)data, (tb_char_t*)output_data, (tb_int_t)size, real); + tb_int_t r = LZ4_decompress_safe((tb_char_t const *)data, (tb_char_t *)output_data, (tb_int_t)size, real); tb_assert_and_check_break(r > 0); - lua_pushlstring(lua, (tb_char_t const*)output_data, r); + lua_pushlstring(lua, (tb_char_t const *)output_data, r); ok = tb_true; } while (0); - if (output_data && output_data != buffer) - { + if (output_data && output_data != buffer) { tb_free(output_data); output_data = tb_null; } - if (!ok) lua_pushnil(lua); + if (!ok) { + lua_pushnil(lua); + } return 1; } - diff --git a/core/src/xmake/lz4/compress.c b/core/src/xmake/lz4/compress.c index 50ddc509c..dffda1f23 100644 --- a/core/src/xmake/lz4/compress.c +++ b/core/src/xmake/lz4/compress.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compress" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compress" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_compress(lua_State* lua) -{ - // check +tb_int_t xm_lz4_compress(lua_State *lua) { 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) - { + 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; @@ -52,39 +53,35 @@ tb_int_t xm_lz4_compress(lua_State* lua) tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t)); // do compress - tb_bool_t ok = tb_false; - tb_char_t const* error = tb_null; - tb_byte_t* output_data = tb_null; - tb_byte_t buffer[8192]; - do - { + tb_bool_t ok = tb_false; + tb_char_t const *error = tb_null; + tb_byte_t *output_data = tb_null; + tb_byte_t buffer[8192]; + do { tb_size_t output_size = LZ4F_compressFrameBound(size, tb_null); tb_assert_and_check_break(output_size); - output_data = output_size <= sizeof(buffer)? buffer : (tb_byte_t*)tb_malloc(output_size); + output_data = output_size <= sizeof(buffer) ? buffer : (tb_byte_t *)tb_malloc(output_size); tb_assert_and_check_break(output_data); tb_size_t real_or_errs = LZ4F_compressFrame(output_data, output_size, data, size, tb_null); - if (LZ4F_isError(real_or_errs)) - { + if (LZ4F_isError(real_or_errs)) { error = LZ4F_getErrorName(real_or_errs); break; } - lua_pushlstring(lua, (tb_char_t const*)output_data, real_or_errs); + lua_pushlstring(lua, (tb_char_t const *)output_data, real_or_errs); ok = tb_true; } while (0); - if (output_data && output_data != buffer) - { + if (output_data && output_data != buffer) { tb_free(output_data); output_data = tb_null; } - if (!ok) - { + if (!ok) { lua_pushnil(lua); - lua_pushstring(lua, error? error : "unknown"); + lua_pushstring(lua, error ? error : "unknown"); return 2; } return 1; diff --git a/core/src/xmake/lz4/compress_file.c b/core/src/xmake/lz4/compress_file.c index 662213864..552ece99d 100644 --- a/core/src/xmake/lz4/compress_file.c +++ b/core/src/xmake/lz4/compress_file.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compress_file" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compress_file" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,72 +33,65 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_compress_file(lua_State* lua) -{ - // check +tb_int_t xm_lz4_compress_file(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the file paths - tb_char_t const* srcpath = luaL_checkstring(lua, 1); - tb_char_t const* dstpath = luaL_checkstring(lua, 2); + tb_char_t const *srcpath = luaL_checkstring(lua, 1); + tb_char_t const *dstpath = luaL_checkstring(lua, 2); tb_check_return_val(srcpath && dstpath, 0); // init lz4 stream - xm_lz4_cstream_t* stream_lz4 = xm_lz4_cstream_init(); + xm_lz4_cstream_t *stream_lz4 = xm_lz4_cstream_init(); tb_check_return_val(stream_lz4, 0); // do compress - tb_bool_t ok = tb_false; + tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(srcpath, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(dstpath, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - if (istream && ostream && tb_stream_open(istream) && tb_stream_open(ostream)) - { + tb_stream_ref_t ostream = tb_stream_init_from_file(dstpath, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + if (istream && ostream && tb_stream_open(istream) && tb_stream_open(ostream)) { tb_bool_t write_ok = tb_false; tb_byte_t idata[TB_STREAM_BLOCK_MAXN]; tb_byte_t odata[TB_STREAM_BLOCK_MAXN]; - while (!tb_stream_beof(istream)) - { - write_ok = tb_false; + while (!tb_stream_beof(istream)) { + write_ok = tb_false; tb_long_t ireal = (tb_long_t)tb_stream_read(istream, idata, sizeof(idata)); - if (ireal > 0) - { + if (ireal > 0) { tb_long_t r = xm_lz4_cstream_write(stream_lz4, idata, ireal, tb_stream_beof(istream)); tb_assert_and_check_break(r >= 0); tb_check_continue(r > 0); tb_long_t oreal; - while ((oreal = xm_lz4_cstream_read(stream_lz4, odata, sizeof(odata))) > 0) - { - if (!tb_stream_bwrit(ostream, odata, oreal)) - { + while ((oreal = xm_lz4_cstream_read(stream_lz4, odata, sizeof(odata))) > 0) { + if (!tb_stream_bwrit(ostream, odata, oreal)) { oreal = -1; break; } } tb_assert_and_check_break(oreal >= 0); + } else { + break; } - else break; write_ok = tb_true; } - if (tb_stream_beof(istream) && write_ok) + if (tb_stream_beof(istream) && write_ok) { ok = tb_true; + } } // exit stream - if (istream) - { + if (istream) { tb_stream_exit(istream); istream = tb_null; } - if (ostream) - { + if (ostream) { tb_stream_exit(ostream); ostream = tb_null; } xm_lz4_cstream_exit(stream_lz4); - // ok? lua_pushboolean(lua, ok); return 1; } diff --git a/core/src/xmake/lz4/compress_stream_close.c b/core/src/xmake/lz4/compress_stream_close.c index e4866c479..4bea381e9 100644 --- a/core/src/xmake/lz4/compress_stream_close.c +++ b/core/src/xmake/lz4/compress_stream_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compress_stream_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compress_stream_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_compress_stream_close(lua_State* lua) -{ - // check +tb_int_t xm_lz4_compress_stream_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 the stream - xm_lz4_cstream_t* stream = (xm_lz4_cstream_t*)xm_lua_topointer(lua, 1); + xm_lz4_cstream_t *stream = (xm_lz4_cstream_t *)xm_lua_topointer(lua, 1); tb_check_return_val(stream, 0); // exit stream @@ -53,4 +52,3 @@ tb_int_t xm_lz4_compress_stream_close(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/lz4/compress_stream_open.c b/core/src/xmake/lz4/compress_stream_open.c index e48201095..536e259e0 100644 --- a/core/src/xmake/lz4/compress_stream_open.c +++ b/core/src/xmake/lz4/compress_stream_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compress_stream_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compress_stream_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_compress_stream_open(lua_State* lua) -{ - // check +tb_int_t xm_lz4_compress_stream_open(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_lz4_cstream_t* stream = xm_lz4_cstream_init(); - if (stream) xm_lua_pushpointer(lua, (tb_pointer_t)stream); - else lua_pushnil(lua); + xm_lz4_cstream_t *stream = xm_lz4_cstream_init(); + if (stream) { + xm_lua_pushpointer(lua, (tb_pointer_t)stream); + } else { + lua_pushnil(lua); + } return 1; } - diff --git a/core/src/xmake/lz4/compress_stream_read.c b/core/src/xmake/lz4/compress_stream_read.c index 1418a797f..6d0515eb5 100644 --- a/core/src/xmake/lz4/compress_stream_read.c +++ b/core/src/xmake/lz4/compress_stream_read.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compress_stream_read" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compress_stream_read" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,29 +34,26 @@ * implementation */ -tb_int_t xm_lz4_compress_stream_read(lua_State* lua) -{ - // check +tb_int_t xm_lz4_compress_stream_read(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check handle - if (!xm_lua_ispointer(lua, 1)) - { + 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); + 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) - { + 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; @@ -65,9 +62,10 @@ tb_int_t xm_lz4_compress_stream_read(lua_State* lua) // get size tb_long_t size = 0; - if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3); - if (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; diff --git a/core/src/xmake/lz4/compress_stream_write.c b/core/src/xmake/lz4/compress_stream_write.c index a805cea5f..8a4276318 100644 --- a/core/src/xmake/lz4/compress_stream_write.c +++ b/core/src/xmake/lz4/compress_stream_write.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compress_stream_write" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compress_stream_write" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,30 +34,30 @@ * implementation */ -tb_int_t xm_lz4_compress_stream_write(lua_State* lua) -{ - // check +tb_int_t xm_lz4_compress_stream_write(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check handle - if (!xm_lua_ispointer(lua, 1)) - { + 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); + 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) - { + 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; @@ -66,7 +66,9 @@ tb_int_t xm_lz4_compress_stream_write(lua_State* lua) // is end? tb_bool_t end = tb_false; - if (lua_isboolean(lua, 4)) end = lua_toboolean(lua, 4); + if (lua_isboolean(lua, 4)) { + end = lua_toboolean(lua, 4); + } // write data tb_long_t real = xm_lz4_cstream_write(stream, data, size, end); diff --git a/core/src/xmake/lz4/decompress.c b/core/src/xmake/lz4/decompress.c index 1221cc042..500e932a1 100644 --- a/core/src/xmake/lz4/decompress.c +++ b/core/src/xmake/lz4/decompress.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "decompress" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "decompress" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_decompress(lua_State* lua) -{ - // check +tb_int_t xm_lz4_decompress(lua_State *lua) { 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) - { + 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; @@ -52,31 +53,32 @@ tb_int_t xm_lz4_decompress(lua_State* lua) tb_assert_static(sizeof(lua_Integer) >= sizeof(tb_pointer_t)); // do decompress - tb_bool_t ok = tb_false; - LZ4F_errorCode_t code; + tb_bool_t ok = tb_false; + LZ4F_errorCode_t code; LZ4F_decompressionContext_t ctx = tb_null; - tb_buffer_t result; - do - { + tb_buffer_t result; + do { tb_buffer_init(&result); code = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION); - if (LZ4F_isError(code)) break; + if (LZ4F_isError(code)) { + break; + } tb_byte_t buffer[8192]; tb_bool_t failed = tb_false; - while (1) - { - size_t advance = (size_t)size; + 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)) - { + code = LZ4F_decompress(ctx, buffer, &buffer_size, data, &advance, tb_null); + if (LZ4F_isError(code)) { failed = tb_true; break; } - if (buffer_size == 0) break; + if (buffer_size == 0) { + break; + } data += advance; size -= advance; @@ -84,24 +86,21 @@ tb_int_t xm_lz4_decompress(lua_State* lua) } 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)); + lua_pushlstring(lua, (tb_char_t const *)tb_buffer_data(&result), tb_buffer_size(&result)); ok = tb_true; } while (0); - if (ctx) - { + if (ctx) { LZ4F_freeDecompressionContext(ctx); ctx = tb_null; } tb_buffer_exit(&result); - if (!ok) - { - tb_char_t const* error = LZ4F_getErrorName(code); + if (!ok) { + tb_char_t const *error = LZ4F_getErrorName(code); lua_pushnil(lua); - lua_pushstring(lua, error? error : "unknown"); + lua_pushstring(lua, error ? error : "unknown"); return 2; } return 1; } - diff --git a/core/src/xmake/lz4/decompress_file.c b/core/src/xmake/lz4/decompress_file.c index 533d677c6..accc993f8 100644 --- a/core/src/xmake/lz4/decompress_file.c +++ b/core/src/xmake/lz4/decompress_file.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "dedecompress_file" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "dedecompress_file" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,73 +33,65 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_decompress_file(lua_State* lua) -{ - // check +tb_int_t xm_lz4_decompress_file(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the file paths - tb_char_t const* srcpath = luaL_checkstring(lua, 1); - tb_char_t const* dstpath = luaL_checkstring(lua, 2); + tb_char_t const *srcpath = luaL_checkstring(lua, 1); + tb_char_t const *dstpath = luaL_checkstring(lua, 2); tb_check_return_val(srcpath && dstpath, 0); // init lz4 stream - xm_lz4_dstream_t* stream_lz4 = xm_lz4_dstream_init(); + xm_lz4_dstream_t *stream_lz4 = xm_lz4_dstream_init(); tb_check_return_val(stream_lz4, 0); // do decompress - tb_bool_t ok = tb_false; + tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(srcpath, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(dstpath, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - if (istream && ostream && tb_stream_open(istream) && tb_stream_open(ostream)) - { + tb_stream_ref_t ostream = tb_stream_init_from_file(dstpath, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + if (istream && ostream && tb_stream_open(istream) && tb_stream_open(ostream)) { tb_bool_t write_ok = tb_false; tb_byte_t idata[TB_STREAM_BLOCK_MAXN]; tb_byte_t odata[TB_STREAM_BLOCK_MAXN]; - while (!tb_stream_beof(istream)) - { - write_ok = tb_false; + while (!tb_stream_beof(istream)) { + write_ok = tb_false; tb_long_t ireal = (tb_long_t)tb_stream_read(istream, idata, sizeof(idata)); - if (ireal > 0) - { + if (ireal > 0) { tb_long_t r = xm_lz4_dstream_write(stream_lz4, idata, ireal, tb_stream_beof(istream)); tb_assert_and_check_break(r >= 0); tb_check_continue(r > 0); tb_long_t oreal; - while ((oreal = xm_lz4_dstream_read(stream_lz4, odata, sizeof(odata))) > 0) - { - if (!tb_stream_bwrit(ostream, odata, oreal)) - { + while ((oreal = xm_lz4_dstream_read(stream_lz4, odata, sizeof(odata))) > 0) { + if (!tb_stream_bwrit(ostream, odata, oreal)) { oreal = -1; break; } } tb_assert_and_check_break(oreal >= 0); + } else { + break; } - else break; write_ok = tb_true; } - if (tb_stream_beof(istream) && write_ok) + if (tb_stream_beof(istream) && write_ok) { ok = tb_true; + } } // exit stream - if (istream) - { + if (istream) { tb_stream_exit(istream); istream = tb_null; } - if (ostream) - { + if (ostream) { tb_stream_exit(ostream); ostream = tb_null; } xm_lz4_dstream_exit(stream_lz4); - // ok? lua_pushboolean(lua, ok); return 1; } - diff --git a/core/src/xmake/lz4/decompress_stream_close.c b/core/src/xmake/lz4/decompress_stream_close.c index 031330e93..c50cd4268 100644 --- a/core/src/xmake/lz4/decompress_stream_close.c +++ b/core/src/xmake/lz4/decompress_stream_close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "decompress_stream_close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "decompress_stream_close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_decompress_stream_close(lua_State* lua) -{ - // check +tb_int_t xm_lz4_decompress_stream_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 the stream - xm_lz4_dstream_t* stream = (xm_lz4_dstream_t*)xm_lua_topointer(lua, 1); + xm_lz4_dstream_t *stream = (xm_lz4_dstream_t *)xm_lua_topointer(lua, 1); tb_check_return_val(stream, 0); // exit stream @@ -53,4 +52,3 @@ tb_int_t xm_lz4_decompress_stream_close(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/lz4/decompress_stream_open.c b/core/src/xmake/lz4/decompress_stream_open.c index e9e65b555..7a37f4378 100644 --- a/core/src/xmake/lz4/decompress_stream_open.c +++ b/core/src/xmake/lz4/decompress_stream_open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "decompress_stream_open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "decompress_stream_open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_lz4_decompress_stream_open(lua_State* lua) -{ - // check +tb_int_t xm_lz4_decompress_stream_open(lua_State *lua) { 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); + 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; } - diff --git a/core/src/xmake/lz4/decompress_stream_read.c b/core/src/xmake/lz4/decompress_stream_read.c index 310f639b8..eb095d4e0 100644 --- a/core/src/xmake/lz4/decompress_stream_read.c +++ b/core/src/xmake/lz4/decompress_stream_read.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "decompress_stream_read" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "decompress_stream_read" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,29 +34,26 @@ * implementation */ -tb_int_t xm_lz4_decompress_stream_read(lua_State* lua) -{ - // check +tb_int_t xm_lz4_decompress_stream_read(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check handle - if (!xm_lua_ispointer(lua, 1)) - { + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid handle!"); return 2; } // get stream - xm_lz4_dstream_t* stream = (xm_lz4_dstream_t*)xm_lua_topointer(lua, 1); + xm_lz4_dstream_t *stream = (xm_lz4_dstream_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) - { + 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; @@ -65,9 +62,10 @@ tb_int_t xm_lz4_decompress_stream_read(lua_State* lua) // get size tb_long_t size = 0; - if (xm_lua_isinteger(lua, 3)) size = (tb_long_t)lua_tointeger(lua, 3); - if (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; diff --git a/core/src/xmake/lz4/decompress_stream_write.c b/core/src/xmake/lz4/decompress_stream_write.c index 5f5856b8e..94be0533e 100644 --- a/core/src/xmake/lz4/decompress_stream_write.c +++ b/core/src/xmake/lz4/decompress_stream_write.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "decompress_stream_write" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "decompress_stream_write" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,30 +34,30 @@ * implementation */ -tb_int_t xm_lz4_decompress_stream_write(lua_State* lua) -{ - // check +tb_int_t xm_lz4_decompress_stream_write(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check handle - if (!xm_lua_ispointer(lua, 1)) - { + if (!xm_lua_ispointer(lua, 1)) { lua_pushinteger(lua, -1); lua_pushliteral(lua, "invalid handle!"); return 2; } // get stream - xm_lz4_dstream_t* stream = (xm_lz4_dstream_t*)xm_lua_topointer(lua, 1); + xm_lz4_dstream_t *stream = (xm_lz4_dstream_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) - { + 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; diff --git a/core/src/xmake/lz4/prefix.h b/core/src/xmake/lz4/prefix.h index 3b7fdb820..534e5361a 100644 --- a/core/src/xmake/lz4/prefix.h +++ b/core/src/xmake/lz4/prefix.h @@ -35,18 +35,18 @@ // we need to define LZ4_byte if < 1.9.3 #if defined(LZ4_VERSION_NUMBER) && LZ4_VERSION_NUMBER < (1 * 100 * 100 + 9 * 100 + 3) -# if defined(__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) -# include <stdint.h> - typedef int8_t LZ4_i8; - typedef uint8_t LZ4_byte; - typedef uint16_t LZ4_u16; - typedef uint32_t LZ4_u32; -# else - typedef signed char LZ4_i8; - typedef unsigned char LZ4_byte; - typedef unsigned short LZ4_u16; - typedef unsigned int LZ4_u32; -# endif +#if defined(__cplusplus) || (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) +#include <stdint.h> +typedef int8_t LZ4_i8; +typedef uint8_t LZ4_byte; +typedef uint16_t LZ4_u16; +typedef uint32_t LZ4_u32; +#else +typedef signed char LZ4_i8; +typedef unsigned char LZ4_byte; +typedef unsigned short LZ4_u16; +typedef unsigned int LZ4_u32; +#endif #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -54,43 +54,37 @@ */ // the lz4 compress stream type -typedef struct __xm_lz4_cstream_t -{ - LZ4F_cctx* cctx; - LZ4_byte* buffer; - tb_size_t buffer_size; - tb_size_t buffer_maxn; - tb_size_t write_maxn; - tb_size_t header_size; - LZ4_byte header[LZ4F_HEADER_SIZE_MAX]; -}xm_lz4_cstream_t; +typedef struct __xm_lz4_cstream_t { + LZ4F_cctx *cctx; + LZ4_byte *buffer; + tb_size_t buffer_size; + tb_size_t buffer_maxn; + tb_size_t write_maxn; + tb_size_t header_size; + LZ4_byte header[LZ4F_HEADER_SIZE_MAX]; +} xm_lz4_cstream_t; // the lz4 decompress stream type -typedef struct __xm_lz4_dstream_t -{ - LZ4F_dctx* dctx; - LZ4_byte* buffer; - tb_size_t buffer_size; - tb_size_t buffer_maxn; - tb_size_t header_size; - LZ4_byte header[LZ4F_HEADER_SIZE_MAX]; -}xm_lz4_dstream_t; +typedef struct __xm_lz4_dstream_t { + LZ4F_dctx *dctx; + LZ4_byte *buffer; + tb_size_t buffer_size; + tb_size_t buffer_maxn; + tb_size_t header_size; + LZ4_byte header[LZ4F_HEADER_SIZE_MAX]; +} xm_lz4_dstream_t; /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces */ -static __tb_inline__ tb_void_t xm_lz4_cstream_exit(xm_lz4_cstream_t* stream) -{ - if (stream) - { - if (stream->cctx) - { +static __tb_inline__ tb_void_t xm_lz4_cstream_exit(xm_lz4_cstream_t *stream) { + if (stream) { + if (stream->cctx) { LZ4F_freeCompressionContext(stream->cctx); stream->cctx = tb_null; } - if (stream->buffer) - { + if (stream->buffer) { tb_free(stream->buffer); stream->buffer = tb_null; } @@ -98,20 +92,18 @@ static __tb_inline__ tb_void_t xm_lz4_cstream_exit(xm_lz4_cstream_t* stream) } } -static __tb_inline__ xm_lz4_cstream_t* xm_lz4_cstream_init() -{ - tb_size_t ret; - tb_bool_t ok = tb_false; - xm_lz4_cstream_t* stream = tb_null; - LZ4F_preferences_t const* prefsPtr = tb_null; - do - { +static __tb_inline__ xm_lz4_cstream_t *xm_lz4_cstream_init() { + tb_size_t ret; + tb_bool_t ok = tb_false; + xm_lz4_cstream_t *stream = tb_null; + LZ4F_preferences_t const *prefsPtr = tb_null; + do { stream = tb_malloc0_type(xm_lz4_cstream_t); tb_assert_and_check_break(stream); - stream->write_maxn = 64 * 1024; + stream->write_maxn = 64 * 1024; stream->buffer_maxn = LZ4F_compressBound(stream->write_maxn, prefsPtr); - stream->buffer = (LZ4_byte*)tb_malloc(stream->buffer_maxn); + stream->buffer = (LZ4_byte *)tb_malloc(stream->buffer_maxn); tb_assert_and_check_break(stream->buffer); ret = LZ4F_createCompressionContext(&stream->cctx, LZ4F_getVersion()); @@ -128,31 +120,36 @@ static __tb_inline__ xm_lz4_cstream_t* xm_lz4_cstream_init() } while (0); - if (!ok && stream) - { + if (!ok && stream) { xm_lz4_cstream_exit(stream); stream = tb_null; } return stream; } -static __tb_inline__ tb_long_t xm_lz4_cstream_write(xm_lz4_cstream_t* stream, tb_byte_t const* idata, tb_size_t isize, tb_bool_t end) -{ - // check +static __tb_inline__ tb_long_t xm_lz4_cstream_write(xm_lz4_cstream_t *stream, + tb_byte_t const *idata, + tb_size_t isize, + tb_bool_t end) { tb_assert_and_check_return_val(stream && stream->cctx && idata && isize, -1); tb_assert_and_check_return_val(isize <= stream->write_maxn, -1); tb_assert_and_check_return_val(stream->buffer_size + isize < stream->buffer_maxn, -1); - tb_size_t real = LZ4F_compressUpdate(stream->cctx, stream->buffer + stream->buffer_size, - stream->buffer_maxn - stream->buffer_size, idata, isize, tb_null); + tb_size_t real = LZ4F_compressUpdate(stream->cctx, + stream->buffer + stream->buffer_size, + stream->buffer_maxn - stream->buffer_size, + idata, + isize, + tb_null); if (LZ4F_isError(real)) return -1; stream->buffer_size += real; - if (end) - { - tb_size_t ret = LZ4F_compressEnd(stream->cctx, stream->buffer + stream->buffer_size, - stream->buffer_maxn - stream->buffer_size, tb_null); + if (end) { + tb_size_t ret = LZ4F_compressEnd(stream->cctx, + stream->buffer + stream->buffer_size, + stream->buffer_maxn - stream->buffer_size, + tb_null); if (LZ4F_isError(ret)) return -1; @@ -163,15 +160,12 @@ static __tb_inline__ tb_long_t xm_lz4_cstream_write(xm_lz4_cstream_t* stream, tb return isize; } -static __tb_inline__ tb_long_t xm_lz4_cstream_read(xm_lz4_cstream_t* stream, tb_byte_t* odata, tb_size_t osize) -{ - // check +static __tb_inline__ tb_long_t xm_lz4_cstream_read(xm_lz4_cstream_t *stream, tb_byte_t *odata, tb_size_t osize) { tb_assert_and_check_return_val(stream && stream->cctx && odata && osize, -1); tb_assert_and_check_return_val(osize >= stream->header_size, -1); tb_size_t read = 0; - if (stream->header_size) - { + if (stream->header_size) { tb_memcpy(odata, stream->header, stream->header_size); read += stream->header_size; osize -= stream->header_size; @@ -187,17 +181,13 @@ static __tb_inline__ tb_long_t xm_lz4_cstream_read(xm_lz4_cstream_t* stream, tb_ return read; } -static __tb_inline__ tb_void_t xm_lz4_dstream_exit(xm_lz4_dstream_t* stream) -{ - if (stream) - { - if (stream->dctx) - { +static __tb_inline__ tb_void_t xm_lz4_dstream_exit(xm_lz4_dstream_t *stream) { + if (stream) { + if (stream->dctx) { LZ4F_freeDecompressionContext(stream->dctx); stream->dctx = tb_null; } - if (stream->buffer) - { + if (stream->buffer) { tb_free(stream->buffer); stream->buffer = tb_null; } @@ -205,13 +195,11 @@ static __tb_inline__ tb_void_t xm_lz4_dstream_exit(xm_lz4_dstream_t* stream) } } -static __tb_inline__ xm_lz4_dstream_t* xm_lz4_dstream_init() -{ - LZ4F_errorCode_t ret; - tb_bool_t ok = tb_false; - xm_lz4_dstream_t* stream = tb_null; - do - { +static __tb_inline__ xm_lz4_dstream_t *xm_lz4_dstream_init() { + LZ4F_errorCode_t ret; + tb_bool_t ok = tb_false; + xm_lz4_dstream_t *stream = tb_null; + do { stream = tb_malloc0_type(xm_lz4_dstream_t); tb_assert_and_check_break(stream); @@ -223,23 +211,22 @@ static __tb_inline__ xm_lz4_dstream_t* xm_lz4_dstream_init() } while (0); - if (!ok && stream) - { + if (!ok && stream) { xm_lz4_dstream_exit(stream); stream = tb_null; } return stream; } -static __tb_inline__ tb_long_t xm_lz4_dstream_write(xm_lz4_dstream_t* stream, tb_byte_t const* idata, tb_size_t isize, tb_bool_t end) -{ - // check +static __tb_inline__ tb_long_t xm_lz4_dstream_write(xm_lz4_dstream_t *stream, + tb_byte_t const *idata, + tb_size_t isize, + tb_bool_t end) { tb_assert_and_check_return_val(stream && stream->dctx && idata && isize, -1); // read header first const tb_size_t header_size = sizeof(stream->header); - if (stream->header_size < header_size) - { + if (stream->header_size < header_size) { tb_size_t size = tb_min(header_size - stream->header_size, isize); tb_memcpy(stream->header + stream->header_size, idata, size); stream->header_size += size; @@ -247,16 +234,14 @@ static __tb_inline__ tb_long_t xm_lz4_dstream_write(xm_lz4_dstream_t* stream, tb isize -= size; // get frame info if header is ok - if (stream->header_size == header_size) - { + if (stream->header_size == header_size) { LZ4F_frameInfo_t info; - size_t consumed_size = header_size; - LZ4F_errorCode_t ret = LZ4F_getFrameInfo(stream->dctx, &info, stream->header, &consumed_size); + size_t consumed_size = header_size; + LZ4F_errorCode_t ret = LZ4F_getFrameInfo(stream->dctx, &info, stream->header, &consumed_size); if (LZ4F_isError(ret)) return -1; - switch (info.blockSizeID) - { + switch (info.blockSizeID) { case LZ4F_default: case LZ4F_max64KB: stream->buffer_maxn = 64 * 1024; @@ -274,7 +259,7 @@ static __tb_inline__ tb_long_t xm_lz4_dstream_write(xm_lz4_dstream_t* stream, tb return -1; } - stream->buffer = (LZ4_byte*)tb_malloc(stream->buffer_maxn); + stream->buffer = (LZ4_byte *)tb_malloc(stream->buffer_maxn); tb_assert_and_check_return_val(stream->buffer, -1); stream->buffer_size = header_size - consumed_size; @@ -290,16 +275,14 @@ static __tb_inline__ tb_long_t xm_lz4_dstream_write(xm_lz4_dstream_t* stream, tb return isize; } -static __tb_inline__ tb_long_t xm_lz4_dstream_read(xm_lz4_dstream_t* stream, tb_byte_t* odata, tb_size_t osize) -{ - // check +static __tb_inline__ tb_long_t xm_lz4_dstream_read(xm_lz4_dstream_t *stream, tb_byte_t *odata, tb_size_t osize) { tb_assert_and_check_return_val(stream && stream->dctx && stream->buffer && odata && osize, -1); tb_check_return_val(stream->buffer_size, 0); // do decompress - size_t srcsize = stream->buffer_size; - size_t dstsize = osize; - tb_size_t ret = LZ4F_decompress(stream->dctx, odata, &dstsize, stream->buffer, &srcsize, tb_null); + size_t srcsize = stream->buffer_size; + size_t dstsize = osize; + tb_size_t ret = LZ4F_decompress(stream->dctx, odata, &dstsize, stream->buffer, &srcsize, tb_null); if (LZ4F_isError(ret)) return -1; @@ -311,5 +294,3 @@ static __tb_inline__ tb_long_t xm_lz4_dstream_read(xm_lz4_dstream_t* stream, tb_ } #endif - - diff --git a/core/src/xmake/os/args.c b/core/src/xmake/os/args.c index f93a8d0db..b48445c89 100644 --- a/core/src/xmake/os/args.c +++ b/core/src/xmake/os/args.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "os.args" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "os.args" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,55 +33,55 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr, tb_size_t size, tb_bool_t escape, tb_bool_t nowrap) -{ - // check +static tb_void_t tb_os_args_append( + tb_string_ref_t result, tb_char_t const *cstr, tb_size_t size, tb_bool_t escape, tb_bool_t nowrap) { tb_assert_and_check_return(size < TB_PATH_MAXN); // need wrap quote? - tb_char_t ch; - tb_char_t const* p = cstr; - tb_bool_t wrap_quote = tb_false; - if (!nowrap) - { - while ((ch = *p)) - { - if (ch == ' ') wrap_quote = tb_true; + tb_char_t ch; + tb_char_t const *p = cstr; + tb_bool_t wrap_quote = tb_false; + if (!nowrap) { + while ((ch = *p)) { + if (ch == ' ') { + wrap_quote = tb_true; + } p++; } } // wrap begin quote - if (wrap_quote) tb_string_chrcat(result, '\"'); + if (wrap_quote) { + tb_string_chrcat(result, '\"'); + } // escape characters p = cstr; - while ((ch = *p)) - { + while ((ch = *p)) { // escape '"' or '\\' - if (ch == '\"' || ((escape || wrap_quote) && ch == '\\')) + if (ch == '\"' || ((escape || wrap_quote) && ch == '\\')) { tb_string_chrcat(result, '\\'); + } tb_string_chrcat(result, ch); p++; } // wrap end quote - if (wrap_quote) tb_string_chrcat(result, '\"'); + if (wrap_quote) { + tb_string_chrcat(result, '\"'); + } } /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ // os.args({"xx", "yy"}, {escape = true}) -tb_int_t xm_os_args(lua_State* lua) -{ - // check +tb_int_t xm_os_args(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // escape '\\' characters in global? tb_bool_t escape = tb_false; - if (lua_istable(lua, 2)) - { + if (lua_istable(lua, 2)) { // is escape? lua_pushstring(lua, "escape"); lua_gettable(lua, 2); @@ -91,8 +91,7 @@ tb_int_t xm_os_args(lua_State* lua) // disable to wrap quote characters in global? tb_bool_t nowrap = tb_false; - if (lua_istable(lua, 2)) - { + if (lua_istable(lua, 2)) { // is nowrap? lua_pushstring(lua, "nowrap"); lua_gettable(lua, 2); @@ -105,50 +104,51 @@ tb_int_t xm_os_args(lua_State* lua) tb_string_init(&result); // make string from arguments list - if (lua_istable(lua, 1)) - { + if (lua_istable(lua, 1)) { tb_size_t i = 0; tb_size_t n = (tb_size_t)lua_objlen(lua, 1); - for (i = 1; i <= n; i++) - { + for (i = 1; i <= n; i++) { // add space - if (i != 1) tb_string_chrcat(&result, ' '); + if (i != 1) { + tb_string_chrcat(&result, ' '); + } // add argument lua_pushnumber(lua, (tb_int_t)i); lua_rawget(lua, 1); - if (lua_istable(lua, -1)) // is path instance? - { + if (lua_istable(lua, -1)) { // is path instance? lua_pushstring(lua, "_STR"); lua_gettable(lua, -2); - size_t size = 0; - tb_char_t const* cstr = luaL_checklstring(lua, -1, &size); - if (cstr && size) + size_t size = 0; + tb_char_t const *cstr = luaL_checklstring(lua, -1, &size); + if (cstr && size) { tb_os_args_append(&result, cstr, size, escape, nowrap); + } lua_pop(lua, 1); - } - else - { - size_t size = 0; - tb_char_t const* cstr = luaL_checklstring(lua, -1, &size); - if (cstr && size) + } else { + size_t size = 0; + tb_char_t const *cstr = luaL_checklstring(lua, -1, &size); + if (cstr && size) { tb_os_args_append(&result, cstr, size, escape, nowrap); + } } lua_pop(lua, 1); } - } - else - { - size_t size = 0; - tb_char_t const* cstr = luaL_checklstring(lua, 1, &size); - if (cstr && size) + } else { + size_t size = 0; + tb_char_t const *cstr = luaL_checklstring(lua, 1, &size); + if (cstr && size) { tb_os_args_append(&result, cstr, size, escape, nowrap); + } } // return result tb_size_t size = tb_string_size(&result); - if (size) lua_pushlstring(lua, tb_string_cstr(&result), size); - else lua_pushliteral(lua, ""); + if (size) { + lua_pushlstring(lua, tb_string_cstr(&result), size); + } else { + lua_pushliteral(lua, ""); + } tb_string_exit(&result); return 1; } diff --git a/core/src/xmake/os/argv.c b/core/src/xmake/os/argv.c index 023235e9a..12b1f88ec 100644 --- a/core/src/xmake/os/argv.c +++ b/core/src/xmake/os/argv.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "os.argv" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "os.argv" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_argv(lua_State* lua) -{ - // check +tb_int_t xm_os_argv(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the argument string - tb_char_t const* args = luaL_checkstring(lua, 1); + tb_char_t const *args = luaL_checkstring(lua, 1); tb_check_return_val(args, 0); // split only? do not escape tb_bool_t splitonly = tb_false; - if (lua_istable(lua, 2)) - { + if (lua_istable(lua, 2)) { lua_pushstring(lua, "splitonly"); lua_gettable(lua, 2); splitonly = lua_toboolean(lua, -1); @@ -54,39 +51,39 @@ tb_int_t xm_os_argv(lua_State* lua) // parse argument list tb_string_t arg; - do - { + do { // init table lua_newtable(lua); // init arg - if (!tb_string_init(&arg)) break; + if (!tb_string_init(&arg)) { + break; + } // parse command to the arguments - tb_int_t i = 1; - tb_int_t skip = 0; - tb_int_t escape = 0; - tb_char_t quote = 0; - tb_char_t ch = 0; - tb_char_t const* p = args; - while ((ch = *p)) - { + tb_int_t i = 1; + tb_int_t skip = 0; + tb_int_t escape = 0; + tb_char_t quote = 0; + tb_char_t ch = 0; + tb_char_t const *p = args; + while ((ch = *p)) { // no escape now? - if (!escape) - { + if (!escape) { // enter quote? - if (!quote && (ch == '\"' || ch == '\'')) { quote = ch; skip = 1; } - // leave quote? - else if (ch == quote) { quote = 0; skip = 1; } - // escape charactor? only escape \\, \" - else if (ch == '\\' && (p[1] == '\\' || p[1] == '\"')) { escape = 1; skip = 1; } - // is argument end with ' '? - else if (!quote && tb_isspace(ch)) - { + if (!quote && (ch == '\"' || ch == '\'')) { + quote = ch; + skip = 1; + } else if (ch == quote) { // leave quote? + quote = 0; + skip = 1; + } else if (ch == '\\' && (p[1] == '\\' || p[1] == '\"')) { // escape character? only escape \\ and \" + escape = 1; + skip = 1; + } else if (!quote && tb_isspace(ch)) { // is argument end with ' '? // save this argument tb_string_ltrim(&arg); - if (tb_string_size(&arg)) - { + if (tb_string_size(&arg)) { // save argument lua_pushstring(lua, tb_string_cstr(&arg)); lua_rawseti(lua, -2, i++); @@ -98,11 +95,16 @@ tb_int_t xm_os_argv(lua_State* lua) } // save this charactor to argument - if (splitonly || !skip) tb_string_chrcat(&arg, ch); + if (splitonly || !skip) { + tb_string_chrcat(&arg, ch); + } // step and cancel escape - if (escape == 1) escape++; - else if (escape == 2) escape = 0; + if (escape == 1) { + escape++; + } else if (escape == 2) { + escape = 0; + } // clear skip skip = 0; @@ -113,8 +115,7 @@ tb_int_t xm_os_argv(lua_State* lua) // save this argument tb_string_ltrim(&arg); - if (tb_string_size(&arg)) - { + if (tb_string_size(&arg)) { // save argument lua_pushstring(lua, tb_string_cstr(&arg)); lua_rawseti(lua, -2, i++); diff --git a/core/src/xmake/os/chdir.c b/core/src/xmake/os/chdir.c index b05e71414..73619122c 100644 --- a/core/src/xmake/os/chdir.c +++ b/core/src/xmake/os/chdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "chdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "chdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_chdir(lua_State* lua) -{ - // check +tb_int_t xm_os_chdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // done os.chdir(path) lua_pushboolean(lua, tb_directory_current_set(path)); - // ok return 1; } diff --git a/core/src/xmake/os/cpdir.c b/core/src/xmake/os/cpdir.c index cdae6f173..2ea0be1ed 100644 --- a/core/src/xmake/os/cpdir.c +++ b/core/src/xmake/os/cpdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "cpdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "cpdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,25 +33,25 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_cpdir(lua_State* lua) -{ - // check +tb_int_t xm_os_cpdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the source and destination - tb_char_t const* src = luaL_checkstring(lua, 1); - tb_char_t const* dst = luaL_checkstring(lua, 2); + tb_char_t const *src = luaL_checkstring(lua, 1); + tb_char_t const *dst = luaL_checkstring(lua, 2); tb_check_return_val(src && dst, 0); // init copy flags - tb_size_t flags = TB_FILE_COPY_NONE; + tb_size_t flags = TB_FILE_COPY_NONE; tb_bool_t is_symlink = lua_toboolean(lua, 3); - if (is_symlink) + if (is_symlink) { flags |= TB_FILE_COPY_LINK; + } tb_bool_t copy_if_different = lua_toboolean(lua, 4); - if (copy_if_different) + if (copy_if_different) { flags |= TB_FILE_COPY_IF_DIFFERENT; + } // do copy lua_pushboolean(lua, tb_directory_copy(src, dst, flags)); diff --git a/core/src/xmake/os/cpfile.c b/core/src/xmake/os/cpfile.c index 7c2c312ce..d3c62832e 100644 --- a/core/src/xmake/os/cpfile.c +++ b/core/src/xmake/os/cpfile.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "cpfile" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "cpfile" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,29 +33,30 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_cpfile(lua_State* lua) -{ - // check +tb_int_t xm_os_cpfile(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the source and destination - tb_char_t const* src = luaL_checkstring(lua, 1); - tb_char_t const* dst = luaL_checkstring(lua, 2); + tb_char_t const *src = luaL_checkstring(lua, 1); + tb_char_t const *dst = luaL_checkstring(lua, 2); tb_check_return_val(src && dst, 0); // init copy flags - tb_size_t flags = TB_FILE_COPY_NONE; + tb_size_t flags = TB_FILE_COPY_NONE; tb_bool_t is_symlink = lua_toboolean(lua, 3); - if (is_symlink) + if (is_symlink) { flags |= TB_FILE_COPY_LINK; + } tb_bool_t is_writeable = lua_toboolean(lua, 4); - if (is_writeable) + if (is_writeable) { flags |= TB_FILE_COPY_WRITEABLE; + } tb_bool_t copy_if_different = lua_toboolean(lua, 5); - if (copy_if_different) + if (copy_if_different) { flags |= TB_FILE_COPY_IF_DIFFERENT; + } // do copy lua_pushboolean(lua, tb_file_copy(src, dst, flags)); diff --git a/core/src/xmake/os/cpuinfo.c b/core/src/xmake/os/cpuinfo.c index a2ce4bf26..7be9172cc 100644 --- a/core/src/xmake/os/cpuinfo.c +++ b/core/src/xmake/os/cpuinfo.c @@ -22,37 +22,36 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "cpuinfo" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "cpuinfo" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #if defined(TB_CONFIG_OS_MACOSX) -# include <sys/sysctl.h> -# include <sys/types.h> -# include <mach/mach.h> -# include <mach/processor_info.h> -# include <mach/mach_host.h> +#include <sys/sysctl.h> +#include <sys/types.h> +#include <mach/mach.h> +#include <mach/processor_info.h> +#include <mach/mach_host.h> #elif defined(TB_CONFIG_OS_WINDOWS) -# include <windows.h> +#include <windows.h> #elif defined(TB_CONFIG_OS_LINUX) -# include <stdio.h> +#include <stdio.h> #elif defined(TB_CONFIG_OS_BSD) -# include <stdio.h> -# include <string.h> -# include <sys/types.h> -# include <sys/sysctl.h> -# include <unistd.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <unistd.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ #if defined(TB_CONFIG_OS_WINDOWS) -static tb_uint64_t xm_os_cpuinfo_subtract_times(FILETIME const* one, FILETIME const* two) -{ +static tb_uint64_t xm_os_cpuinfo_subtract_times(FILETIME const *one, FILETIME const *two) { LARGE_INTEGER a, b; a.LowPart = one->dwLowDateTime; a.HighPart = one->dwHighDateTime; @@ -64,84 +63,82 @@ static tb_uint64_t xm_os_cpuinfo_subtract_times(FILETIME const* one, FILETIME co } #endif -static tb_float_t xm_os_cpuinfo_usagerate() -{ +static tb_float_t xm_os_cpuinfo_usagerate() { #if defined(TB_CONFIG_OS_MACOSX) - tb_float_t usagerate = 0; - natural_t cpu_count = 0; + tb_float_t usagerate = 0; + natural_t cpu_count = 0; processor_info_array_t cpuinfo; mach_msg_type_number_t cpuinfo_count; - static tb_hong_t s_time = 0; - if (tb_mclock() - s_time > 1000 && host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, &cpuinfo, &cpuinfo_count) == KERN_SUCCESS) - { - static processor_info_array_t s_cpuinfo_prev = tb_null; + static tb_hong_t s_time = 0; + if (tb_mclock() - s_time > 1000 && + host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, &cpuinfo, &cpuinfo_count) == + KERN_SUCCESS) { + static processor_info_array_t s_cpuinfo_prev = tb_null; static mach_msg_type_number_t s_cpuinfo_count_prev = 0; - for (tb_int_t i = 0; i < cpu_count; ++i) - { + for (tb_int_t i = 0; i < cpu_count; ++i) { tb_int_t use, total; - if (s_cpuinfo_prev) - { - use = (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] - s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_USER]) - + (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] - s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM]) - + (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE] - s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_NICE]); - total = use + (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE] - s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_IDLE]); - } - else - { - use = cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] + cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] + cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE]; + if (s_cpuinfo_prev) { + use = (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] - + s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_USER]) + + (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] - + s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM]) + + (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE] - + s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_NICE]); + total = use + (cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE] - + s_cpuinfo_prev[(CPU_STATE_MAX * i) + CPU_STATE_IDLE]); + } else { + use = cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] + cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] + + cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE]; total = use + cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE]; } - usagerate += total > 0? ((tb_float_t)use / (tb_float_t)total) : 0; + usagerate += total > 0 ? ((tb_float_t)use / (tb_float_t)total) : 0; } - if (s_cpuinfo_prev) + if (s_cpuinfo_prev) { vm_deallocate(mach_task_self(), (vm_address_t)s_cpuinfo_prev, sizeof(integer_t) * s_cpuinfo_count_prev); - s_time = tb_mclock(); - s_cpuinfo_prev = cpuinfo; + } + s_time = tb_mclock(); + s_cpuinfo_prev = cpuinfo; s_cpuinfo_count_prev = cpuinfo_count; } - return cpu_count > 0? usagerate / cpu_count : 0; + return cpu_count > 0 ? usagerate / cpu_count : 0; #elif defined(TB_CONFIG_OS_WINDOWS) // kernel include idle_time tb_float_t usagerate = 0; - FILETIME idle, kernel, user; - if (GetSystemTimes(&idle, &kernel, &user)) - { - static FILETIME idle_prev = {0}; - static FILETIME kernel_prev = {0}; - static FILETIME user_prev = {0}; + FILETIME idle, kernel, user; + if (GetSystemTimes(&idle, &kernel, &user)) { + static FILETIME idle_prev = { 0 }; + static FILETIME kernel_prev = { 0 }; + static FILETIME user_prev = { 0 }; - if (idle_prev.dwLowDateTime != 0 && idle_prev.dwHighDateTime != 0) - { - tb_uint64_t idle_diff = xm_os_cpuinfo_subtract_times(&idle, &idle_prev); + if (idle_prev.dwLowDateTime != 0 && idle_prev.dwHighDateTime != 0) { + tb_uint64_t idle_diff = xm_os_cpuinfo_subtract_times(&idle, &idle_prev); tb_uint64_t kernel_diff = xm_os_cpuinfo_subtract_times(&kernel, &kernel_prev); - tb_uint64_t user_diff = xm_os_cpuinfo_subtract_times(&user, &user_prev); + tb_uint64_t user_diff = xm_os_cpuinfo_subtract_times(&user, &user_prev); // kernel_time - idle_time = kernel_time, because kernel include idle_time - tb_uint64_t sys_total = kernel_diff + user_diff; + tb_uint64_t sys_total = kernel_diff + user_diff; tb_uint64_t kernel_total = kernel_diff - idle_diff; // sometimes kernel_time > idle_time - if (sys_total > 0) + if (sys_total > 0) { usagerate = (tb_float_t)((tb_double_t)(kernel_total + user_diff) / sys_total); + } } - idle_prev = idle; + idle_prev = idle; kernel_prev = kernel; - user_prev = user; + user_prev = user; } return usagerate; #elif defined(TB_CONFIG_OS_LINUX) tb_float_t usagerate = 0; - if (tb_file_info("/proc/stat", tb_null)) - { - FILE* fp = fopen("/proc/stat", "r"); - if (fp) - { - tb_char_t line[8192]; - static tb_int64_t total_prev = 0; + if (tb_file_info("/proc/stat", tb_null)) { + FILE *fp = fopen("/proc/stat", "r"); + if (fp) { + tb_char_t line[8192]; + static tb_int64_t total_prev = 0; static tb_int64_t active_prev = 0; - while (!feof(fp)) - { + while (!feof(fp)) { /* cpu 548760 0 867417 102226682 12430 0 9089 0 0 0 * cpu0 136863 0 218110 25632388 2706 0 2328 0 0 0 * cpu1 148383 0 213941 25627686 3925 0 2129 0 0 0 @@ -159,22 +156,32 @@ static tb_float_t xm_os_cpuinfo_usagerate() * guest * guest_nice */ - if (fgets(line, sizeof(line), fp) && !tb_strncmp(line, "cpu ", 4)) - { + if (fgets(line, sizeof(line), fp) && !tb_strncmp(line, "cpu ", 4)) { long long user, nice, sys, idle, iowait, irq, softirq, steal, guest, guest_nice; - if (10 == sscanf(line, "cpu %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld", - &user, &nice, &sys, &idle, &iowait, &irq, &softirq, &steal, &guest, &guest_nice)) - { - tb_int64_t active = (tb_int64_t)(user + nice + sys + irq + softirq + steal + guest + guest_nice); - tb_int64_t total = (tb_int64_t)(user + nice + sys + idle + iowait + irq + softirq + steal + guest + guest_nice); - if (total_prev > 0 && active_prev > 0) - { - tb_int64_t total_diff = total - total_prev; + if (10 == sscanf(line, + "cpu %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld", + &user, + &nice, + &sys, + &idle, + &iowait, + &irq, + &softirq, + &steal, + &guest, + &guest_nice)) { + tb_int64_t active = (tb_int64_t)(user + nice + sys + irq + softirq + steal + guest + + guest_nice); + tb_int64_t total = (tb_int64_t)(user + nice + sys + idle + iowait + irq + softirq + steal + + guest + guest_nice); + if (total_prev > 0 && active_prev > 0) { + tb_int64_t total_diff = total - total_prev; tb_int64_t active_diff = active - active_prev; - if (total_diff > 0) + if (total_diff > 0) { usagerate = (tb_float_t)((tb_double_t)active_diff / total_diff); + } } - total_prev = total; + total_prev = total; active_prev = active; } break; @@ -185,37 +192,36 @@ static tb_float_t xm_os_cpuinfo_usagerate() } return usagerate; #elif defined(TB_CONFIG_OS_BSD) && !defined(__OpenBSD__) -# define CP_USER 0 -# define CP_NICE 1 -# define CP_SYS 2 -# define CP_INTR 3 -# define CP_IDLE 4 -# define CPUSTATES 5 +#define CP_USER 0 +#define CP_NICE 1 +#define CP_SYS 2 +#define CP_INTR 3 +#define CP_IDLE 4 +#define CPUSTATES 5 - static tb_int64_t total_prev = 0; + static tb_int64_t total_prev = 0; static tb_int64_t active_prev = 0; - tb_float_t usagerate = 0; - long states[CPUSTATES] = {0}; - size_t states_size = sizeof(states); - if (sysctlbyname("kern.cp_time", &states, &states_size, tb_null, 0) == 0) - { + tb_float_t usagerate = 0; + long states[CPUSTATES] = { 0 }; + size_t states_size = sizeof(states); + if (sysctlbyname("kern.cp_time", &states, &states_size, tb_null, 0) == 0) { tb_long_t user = states[CP_USER]; tb_long_t nice = states[CP_NICE]; - tb_long_t sys = states[CP_SYS]; + tb_long_t sys = states[CP_SYS]; tb_long_t intr = states[CP_INTR]; tb_long_t idle = states[CP_IDLE]; tb_int64_t active = user + nice + sys + intr; - tb_int64_t total = user + nice + sys + idle + intr; - if (total_prev > 0 && active_prev > 0) - { - tb_int64_t total_diff = total - total_prev; + tb_int64_t total = user + nice + sys + idle + intr; + if (total_prev > 0 && active_prev > 0) { + tb_int64_t total_diff = total - total_prev; tb_int64_t active_diff = active - active_prev; - if (total_diff > 0) + if (total_diff > 0) { usagerate = (tb_float_t)((tb_double_t)active_diff / total_diff); + } } - total_prev = total; + total_prev = total; active_prev = active; } return usagerate; @@ -234,9 +240,7 @@ static tb_float_t xm_os_cpuinfo_usagerate() * ... * } */ -tb_int_t xm_os_cpuinfo(lua_State* lua) -{ - // check +tb_int_t xm_os_cpuinfo(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // init table @@ -245,13 +249,12 @@ tb_int_t xm_os_cpuinfo(lua_State* lua) // get cpu number tb_int_t ncpu = (tb_int_t)tb_cpu_count(); lua_pushstring(lua, "ncpu"); - lua_pushinteger(lua, ncpu > 0? ncpu : 1); + lua_pushinteger(lua, ncpu > 0 ? ncpu : 1); lua_settable(lua, -3); // get cpu usage rate tb_float_t usagerate = xm_os_cpuinfo_usagerate(); - if (usagerate >= 0) - { + if (usagerate >= 0) { lua_pushstring(lua, "usagerate"); lua_pushnumber(lua, usagerate); lua_settable(lua, -3); diff --git a/core/src/xmake/os/curdir.c b/core/src/xmake/os/curdir.c index c42f4e0d6..987aa60c9 100644 --- a/core/src/xmake/os/curdir.c +++ b/core/src/xmake/os/curdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "curdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "curdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_curdir(lua_State* lua) -{ - // check +tb_int_t xm_os_curdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // os.curdir() tb_char_t path[TB_PATH_MAXN]; - if (tb_directory_current(path, sizeof(path))) lua_pushstring(lua, path); - else lua_pushnil(lua); + if (tb_directory_current(path, sizeof(path))) { + lua_pushstring(lua, path); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/os/emptydir.c b/core/src/xmake/os/emptydir.c index 409a1445b..aab016d28 100644 --- a/core/src/xmake/os/emptydir.c +++ b/core/src/xmake/os/emptydir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "emptydir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "emptydir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,15 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_long_t xm_os_emptydir_walk(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) -{ - // check - tb_bool_t* is_emptydir = (tb_bool_t*)priv; +static tb_long_t xm_os_emptydir_walk(tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv) { + tb_bool_t *is_emptydir = (tb_bool_t *)priv; tb_assert_and_check_return_val(path && info && is_emptydir, TB_DIRECTORY_WALK_CODE_END); // is emptydir? - if (info->type == TB_FILE_TYPE_FILE || info->type == TB_FILE_TYPE_DIRECTORY) - { + if (info->type == TB_FILE_TYPE_FILE || info->type == TB_FILE_TYPE_DIRECTORY) { *is_emptydir = tb_false; return tb_false; } @@ -51,13 +48,11 @@ static tb_long_t xm_os_emptydir_walk(tb_char_t const* path, tb_file_info_t const /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_emptydir(lua_State* lua) -{ - // check +tb_int_t xm_os_emptydir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the directory - tb_char_t const* dir = luaL_checkstring(lua, 1); + tb_char_t const *dir = luaL_checkstring(lua, 1); tb_check_return_val(dir, 0); // os.emptydir(dir) diff --git a/core/src/xmake/os/exists.c b/core/src/xmake/os/exists.c index d09115cda..9bb244510 100644 --- a/core/src/xmake/os/exists.c +++ b/core/src/xmake/os/exists.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "exists" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "exists" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,13 +33,11 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_exists(lua_State* lua) -{ - // check +tb_int_t xm_os_exists(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // os.exists(path) diff --git a/core/src/xmake/os/filesize.c b/core/src/xmake/os/filesize.c index 0c1c52953..c746d96ed 100644 --- a/core/src/xmake/os/filesize.c +++ b/core/src/xmake/os/filesize.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "filesize" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "filesize" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_filesize(lua_State* lua) -{ - // check +tb_int_t xm_os_filesize(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // os.filesize(path) - tb_file_info_t info = {0}; - if (tb_file_info(path, &info) && (info.type == TB_FILE_TYPE_FILE)) + tb_file_info_t info = { 0 }; + if (tb_file_info(path, &info) && (info.type == TB_FILE_TYPE_FILE)) { lua_pushinteger(lua, (lua_Integer)info.size); - else lua_pushinteger(lua, 0); + } else { + lua_pushinteger(lua, 0); + } return 1; } diff --git a/core/src/xmake/os/find.c b/core/src/xmake/os/find.c index f1e11952b..144ab4daa 100644 --- a/core/src/xmake/os/find.c +++ b/core/src/xmake/os/find.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "find" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "find" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,89 +33,84 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_long_t xm_os_find_walk(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) -{ - // check +static tb_long_t xm_os_find_walk(tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv) { tb_value_ref_t tuple = (tb_value_ref_t)priv; tb_assert_and_check_return_val(path && info && tuple, TB_DIRECTORY_WALK_CODE_END); // the lua - lua_State* lua = (lua_State*)tuple[0].ptr; + lua_State *lua = (lua_State *)tuple[0].ptr; tb_assert_and_check_return_val(lua, TB_DIRECTORY_WALK_CODE_END); // the pattern - tb_char_t const* pattern = (tb_char_t const*)tuple[1].cstr; + tb_char_t const *pattern = (tb_char_t const *)tuple[1].cstr; tb_assert_and_check_return_val(pattern, TB_DIRECTORY_WALK_CODE_END); // remove ./ for path - if (path[0] == '.' && (path[1] == '/' || path[1] == '\\')) + if (path[0] == '.' && (path[1] == '/' || path[1] == '\\')) { path = path + 2; + } // the match mode tb_long_t mode = tuple[2].l; // the count - tb_size_t* pcount = &(tuple[3].ul); + tb_size_t *pcount = &(tuple[3].ul); - // trace - tb_trace_d("path[%c]: %s", info->type == TB_FILE_TYPE_DIRECTORY? 'd' : 'f', path); + tb_trace_d("path[%c]: %s", info->type == TB_FILE_TYPE_DIRECTORY ? 'd' : 'f', path); // we can ignore it directly if this path is file, but we need directory - tb_size_t needtype = (mode == 1)? TB_FILE_TYPE_DIRECTORY : ((mode == 0)? TB_FILE_TYPE_FILE : (TB_FILE_TYPE_FILE | TB_FILE_TYPE_DIRECTORY)); - if (info->type == TB_FILE_TYPE_FILE && needtype == TB_FILE_TYPE_DIRECTORY) + tb_size_t needtype = (mode == 1) ? TB_FILE_TYPE_DIRECTORY + : ((mode == 0) ? TB_FILE_TYPE_FILE : (TB_FILE_TYPE_FILE | TB_FILE_TYPE_DIRECTORY)); + if (info->type == TB_FILE_TYPE_FILE && needtype == TB_FILE_TYPE_DIRECTORY) { return TB_DIRECTORY_WALK_CODE_CONTINUE; + } // do path:match(pattern) lua_getfield(lua, -1, "match"); lua_pushstring(lua, path); lua_pushstring(lua, pattern); - if (lua_pcall(lua, 2, 1, 0)) - { - // trace + if (lua_pcall(lua, 2, 1, 0)) { tb_printf("error: call string.match(%s, %s) failed: %s!\n", path, pattern, lua_tostring(lua, -1)); return TB_DIRECTORY_WALK_CODE_END; } // match ok? - tb_bool_t matched = tb_false; + tb_bool_t matched = tb_false; tb_bool_t skip_recursion = tb_false; - if (lua_isstring(lua, -1) && !tb_strcmp(path, lua_tostring(lua, -1))) - { + if (lua_isstring(lua, -1) && !tb_strcmp(path, lua_tostring(lua, -1))) { // exists excludes? tb_bool_t excluded = tb_false; - if (lua_istable(lua, 5)) - { + if (lua_istable(lua, 5)) { // the root directory - size_t rootlen = 0; - tb_char_t const* rootdir = luaL_checklstring(lua, 1, &rootlen); + size_t rootlen = 0; + tb_char_t const *rootdir = luaL_checklstring(lua, 1, &rootlen); tb_assert_and_check_return_val(rootdir && rootlen, TB_DIRECTORY_WALK_CODE_END); - // check tb_assert(!tb_strncmp(path, rootdir, rootlen)); tb_assert(rootlen + 1 <= tb_strlen(path)); // skip the rootdir if not "." - if (tb_strcmp(rootdir, ".")) + if (tb_strcmp(rootdir, ".")) { path += rootlen + 1; + } // exclude paths - tb_int_t i = 0; + tb_int_t i = 0; tb_int_t count = (tb_int_t)lua_objlen(lua, 5); - for (i = 0; i < count && !excluded; i++) - { + for (i = 0; i < count && !excluded; i++) { // get exclude lua_rawgeti(lua, 5, i + 1); - tb_char_t const* exclude = lua_tostring(lua, -1); - if (exclude) - { + tb_char_t const *exclude = lua_tostring(lua, -1); + if (exclude) { // do path:match(exclude) lua_getfield(lua, -3, "match"); lua_pushstring(lua, path); lua_pushstring(lua, exclude); - if (lua_pcall(lua, 2, 1, 0)) - { - // trace - tb_printf("error: call string.match(%s, %s) failed: %s!\n", path, exclude, lua_tostring(lua, -1)); + if (lua_pcall(lua, 2, 1, 0)) { + tb_printf("error: call string.match(%s, %s) failed: %s!\n", + path, + exclude, + lua_tostring(lua, -1)); } // matched? @@ -131,17 +126,14 @@ static tb_long_t xm_os_find_walk(tb_char_t const* path, tb_file_info_t const* in } // does not exclude this path? - if (!excluded) - { + if (!excluded) { // match file or directory? - if (info->type & needtype) - { + if (info->type & needtype) { // save it lua_rawseti(lua, -3, (tb_int_t)(++*pcount)); // do callback function - if (lua_isfunction(lua, 6)) - { + if (lua_isfunction(lua, 6)) { // do callback(path, isdir) lua_pushvalue(lua, 6); lua_pushstring(lua, path); @@ -151,31 +143,35 @@ static tb_long_t xm_os_find_walk(tb_char_t const* path, tb_file_info_t const* in // is continue? tb_bool_t is_continue = lua_toboolean(lua, -1); lua_pop(lua, 1); - if (!is_continue) return TB_DIRECTORY_WALK_CODE_END; + if (!is_continue) { + return TB_DIRECTORY_WALK_CODE_END; + } } matched = tb_true; } } // we do not recurse sub-directories if this path has been excluded and it's directory - else skip_recursion = tb_true; + else { + skip_recursion = tb_true; + } + } + if (!matched) { + lua_pop(lua, 1); } - if (!matched) lua_pop(lua, 1); - return skip_recursion? TB_DIRECTORY_WALK_CODE_SKIP_RECURSION : TB_DIRECTORY_WALK_CODE_CONTINUE; + return skip_recursion ? TB_DIRECTORY_WALK_CODE_SKIP_RECURSION : TB_DIRECTORY_WALK_CODE_CONTINUE; } /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_find(lua_State* lua) -{ - // check +tb_int_t xm_os_find(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the root directory - tb_char_t const* rootdir = luaL_checkstring(lua, 1); + tb_char_t const *rootdir = luaL_checkstring(lua, 1); tb_check_return_val(rootdir, 0); // get the pattern - tb_char_t const* pattern = luaL_checkstring(lua, 2); + tb_char_t const *pattern = luaL_checkstring(lua, 2); tb_check_return_val(pattern, 0); // the recursion level @@ -192,10 +188,10 @@ tb_int_t xm_os_find(lua_State* lua) // do os.find(root, name) tb_value_t tuple[4]; - tuple[0].ptr = lua; - tuple[1].cstr = pattern; - tuple[2].l = mode; - tuple[3].ul = 0; + tuple[0].ptr = lua; + tuple[1].cstr = pattern; + tuple[2].l = mode; + tuple[3].ul = 0; tb_directory_walk(rootdir, recursion, tb_true, xm_os_find_walk, tuple); // pop string package diff --git a/core/src/xmake/os/fscase.c b/core/src/xmake/os/fscase.c index f7682f87a..b24a36e50 100644 --- a/core/src/xmake/os/fscase.c +++ b/core/src/xmake/os/fscase.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "fscase" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "fscase" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,13 +33,11 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_fscase(lua_State* lua) -{ - // check +tb_int_t xm_os_fscase(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); #if ((TB_VERSION_MAJOR * 100) + (TB_VERSION_MINOR * 10) + TB_VERSION_ALTER) >= 174 - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); lua_pushinteger(lua, (tb_int_t)tb_file_fscase(path)); diff --git a/core/src/xmake/os/getenv.c b/core/src/xmake/os/getenv.c index f9eaf7f18..4d82e0524 100644 --- a/core/src/xmake/os/getenv.c +++ b/core/src/xmake/os/getenv.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "getenv" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "getenv" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,41 +36,41 @@ // the separator #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) -# define XM_OS_ENV_SEP ';' +#define XM_OS_ENV_SEP ';' #else -# define XM_OS_ENV_SEP ':' +#define XM_OS_ENV_SEP ':' #endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_getenv(lua_State* lua) -{ - // check +tb_int_t xm_os_getenv(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the name - tb_char_t const* name = luaL_checkstring(lua, 1); + tb_char_t const *name = luaL_checkstring(lua, 1); tb_check_return_val(name, 0); // init values tb_string_t values; - if (!tb_string_init(&values)) return 0; + if (!tb_string_init(&values)) { + return 0; + } // init environment tb_environment_ref_t environment = tb_environment_init(); - if (environment) - { + if (environment) { // load variable - if (tb_environment_load(environment, name)) - { + if (tb_environment_load(environment, name)) { // make values tb_bool_t is_first = tb_true; - tb_for_all_if (tb_char_t const*, value, environment, value) - { + tb_for_all_if(tb_char_t const *, value, environment, value) { // append separator - if (!is_first) tb_string_chrcat(&values, XM_OS_ENV_SEP); - else is_first = tb_false; + if (!is_first) { + tb_string_chrcat(&values, XM_OS_ENV_SEP); + } else { + is_first = tb_false; + } // append value tb_string_cstrcat(&values, value); @@ -82,12 +82,14 @@ tb_int_t xm_os_getenv(lua_State* lua) } // save result - if (tb_string_size(&values)) lua_pushstring(lua, tb_string_cstr(&values)); - else lua_pushnil(lua); + if (tb_string_size(&values)) { + lua_pushstring(lua, tb_string_cstr(&values)); + } else { + lua_pushnil(lua); + } // exit values tb_string_exit(&values); - // ok return 1; } diff --git a/core/src/xmake/os/getenvs.c b/core/src/xmake/os/getenvs.c index 7e6d611d6..1576c101d 100644 --- a/core/src/xmake/os/getenvs.c +++ b/core/src/xmake/os/getenvs.c @@ -22,15 +22,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "getenvs" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "getenvs" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) -# include <windows.h> +#include <windows.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -39,9 +39,9 @@ // the separator #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) -# define XM_OS_ENV_SEP ';' +#define XM_OS_ENV_SEP ';' #else -# define XM_OS_ENV_SEP ':' +#define XM_OS_ENV_SEP ':' #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -50,72 +50,71 @@ // the user environment #if !defined(TB_CONFIG_OS_WINDOWS) || defined(TB_COMPILER_LIKE_UNIX) -extern tb_char_t** environ; +extern tb_char_t **environ; #endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -static tb_void_t xm_os_getenvs_trim(tb_char_t const** sstr, tb_char_t const** estr) -{ - // check +static tb_void_t xm_os_getenvs_trim(tb_char_t const **sstr, tb_char_t const **estr) { tb_assert(sstr && estr && *sstr && *estr); - tb_char_t const* p = *sstr; - tb_char_t const* e = *estr; + tb_char_t const *p = *sstr; + tb_char_t const *e = *estr; // trim left - while (p < e && tb_isspace(*p)) + while (p < e && tb_isspace(*p)) { p++; + } // trim right - while (e > p && tb_isspace(*(e - 1))) + while (e > p && tb_isspace(*(e - 1))) { e--; + } // save trimmed string *sstr = p; *estr = e; } -static tb_void_t xm_os_getenvs_process_line(lua_State* lua, tb_char_t const* line) -{ - // check +static tb_void_t xm_os_getenvs_process_line(lua_State *lua, tb_char_t const *line) { tb_assert_and_check_return(lua && line); tb_size_t n = tb_strlen(line); tb_check_return(n > 0); // find '=' separator - tb_char_t const* p = tb_strchr(line, '='); + tb_char_t const *p = tb_strchr(line, '='); tb_check_return(p); // get key and value parts - tb_char_t const* key_start = line; - tb_char_t const* key_end = p; - tb_char_t const* value_start = p + 1; - tb_char_t const* value_end = line + n; + tb_char_t const *key_start = line; + tb_char_t const *key_end = p; + tb_char_t const *value_start = p + 1; + tb_char_t const *value_end = line + n; // trim key xm_os_getenvs_trim(&key_start, &key_end); - if (key_start >= key_end) return; + if (key_start >= key_end) { + return; + } // trim value xm_os_getenvs_trim(&value_start, &value_end); // get key and value lengths - tb_size_t key_len = key_end - key_start; + tb_size_t key_len = key_end - key_start; tb_size_t value_len = value_end > value_start ? value_end - value_start : 0; // handle Windows-specific PATH conversion - tb_char_t const* final_key_start = key_start; - tb_size_t final_key_len = key_len; + tb_char_t const *final_key_start = key_start; + tb_size_t final_key_len = key_len; #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) - if (key_len == 4 && tb_strnicmp(key_start, "path", 4) == 0) - { + if (key_len == 4 && tb_strnicmp(key_start, "path", 4) == 0) { // use "PATH" instead of "path" static tb_char_t const PATH_UPPER[] = "PATH"; - final_key_start = PATH_UPPER; - final_key_len = 4; + final_key_start = PATH_UPPER; + final_key_len = 4; } #endif @@ -125,58 +124,50 @@ static tb_void_t xm_os_getenvs_process_line(lua_State* lua, tb_char_t const* lin lua_rawset(lua, -3); } -tb_int_t xm_os_getenvs(lua_State* lua) -{ - // check +tb_int_t xm_os_getenvs(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // init table lua_newtable(lua); #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) - tb_wchar_t const* p = (tb_wchar_t const*)GetEnvironmentStringsW(); - if (p) - { - tb_char_t* data = tb_null; - tb_size_t maxn = 0; - tb_char_t line[TB_PATH_MAXN]; - tb_size_t n = 0; - while (*p) - { + tb_wchar_t const *p = (tb_wchar_t const *)GetEnvironmentStringsW(); + if (p) { + tb_char_t *data = tb_null; + tb_size_t maxn = 0; + tb_char_t line[TB_PATH_MAXN]; + tb_size_t n = 0; + while (*p) { n = tb_wcslen(p); - if (n + 1 < tb_arrayn(line)) - { - if (tb_wtoa(line, p, tb_arrayn(line)) >= 0) + if (n + 1 < tb_arrayn(line)) { + if (tb_wtoa(line, p, tb_arrayn(line)) >= 0) { xm_os_getenvs_process_line(lua, line); - } - else - { - if (!data) - { - maxn = n + 1; - data = (tb_char_t*)tb_malloc(maxn); } - else if (n >= maxn) - { + } else { + if (!data) { + maxn = n + 1; + data = (tb_char_t *)tb_malloc(maxn); + } else if (n >= maxn) { maxn = n + TB_PATH_MAXN + 1; - data = (tb_char_t*)tb_ralloc(data, maxn); + data = (tb_char_t *)tb_ralloc(data, maxn); } tb_assert_and_check_break(data); - if (tb_wtoa(data, p, maxn) >= 0) + if (tb_wtoa(data, p, maxn) >= 0) { xm_os_getenvs_process_line(lua, data); + } } p += n + 1; } - if (data && data != line) tb_free(data); + if (data && data != line) { + tb_free(data); + } data = tb_null; } #else - tb_char_t const** p = (tb_char_t const**)environ; - if (p) - { - while (*p) - { + tb_char_t const **p = (tb_char_t const **)environ; + if (p) { + while (*p) { xm_os_getenvs_process_line(lua, *p); p++; } diff --git a/core/src/xmake/os/getown.c b/core/src/xmake/os/getown.c index d032a44b8..3d002cb96 100644 --- a/core/src/xmake/os/getown.c +++ b/core/src/xmake/os/getown.c @@ -22,16 +22,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "getown" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "getown" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #ifndef TB_CONFIG_OS_WINDOWS -# include <unistd.h> -# include <sys/stat.h> +#include <unistd.h> +#include <sys/stat.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -41,19 +41,18 @@ #ifndef TB_CONFIG_OS_WINDOWS // get owner by a given path -tb_int_t xm_os_getown(lua_State* lua) -{ - // check +tb_int_t xm_os_getown(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the pathname - tb_char_t const* pathname = luaL_checkstring(lua, 1); + tb_char_t const *pathname = luaL_checkstring(lua, 1); tb_check_return_val(pathname, 0); // get stat struct stat sts; - if (stat(pathname, &sts) != 0) + if (stat(pathname, &sts) != 0) { return 0; + } // push lua_newtable(lua); @@ -64,7 +63,6 @@ tb_int_t xm_os_getown(lua_State* lua) lua_pushinteger(lua, sts.st_gid); lua_settable(lua, -3); - // ok return 1; } diff --git a/core/src/xmake/os/getpid.c b/core/src/xmake/os/getpid.c index aeb0ace14..ad1a6f880 100644 --- a/core/src/xmake/os/getpid.c +++ b/core/src/xmake/os/getpid.c @@ -22,26 +22,24 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "getpid" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "getpid" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #ifdef TB_CONFIG_OS_WINDOWS -# include <windows.h> +#include <windows.h> #else -# include <unistd.h> -# include <errno.h> +#include <unistd.h> +#include <errno.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_getpid(lua_State* lua) -{ - // check +tb_int_t xm_os_getpid(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); #ifdef TB_CONFIG_OS_WINDOWS @@ -51,4 +49,3 @@ tb_int_t xm_os_getpid(lua_State* lua) #endif return 1; } - diff --git a/core/src/xmake/os/getwinsize.c b/core/src/xmake/os/getwinsize.c index 919bfa9bd..6faef97d0 100644 --- a/core/src/xmake/os/getwinsize.c +++ b/core/src/xmake/os/getwinsize.c @@ -22,19 +22,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "getwinsize" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "getwinsize" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) -# include <windows.h> +#include <windows.h> #else -# include <sys/ioctl.h> -# include <errno.h> // for errno -# include <unistd.h> // for STDOUT_FILENO +#include <sys/ioctl.h> +#include <errno.h> // for errno +#include <unistd.h> // for STDOUT_FILENO #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -42,9 +42,7 @@ */ // get console window size -tb_int_t xm_os_getwinsize(lua_State* lua) -{ - // check +tb_int_t xm_os_getwinsize(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // init default window size (we will not consider winsize limit if cannot get it) @@ -53,15 +51,13 @@ tb_int_t xm_os_getwinsize(lua_State* lua) // get winsize #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) CONSOLE_SCREEN_BUFFER_INFO csbi; - if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) - { + if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) { w = (tb_int_t)csbi.dwSize.X; h = (tb_int_t)csbi.dwSize.Y; } #else struct winsize size; - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) - { + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) == 0) { w = (tb_int_t)size.ws_col; h = (tb_int_t)size.ws_row; } @@ -83,6 +79,5 @@ tb_int_t xm_os_getwinsize(lua_State* lua) lua_pushinteger(lua, h); lua_settable(lua, -3); - // ok return 1; } diff --git a/core/src/xmake/os/gid.c b/core/src/xmake/os/gid.c index 739bfd64a..feb13791b 100644 --- a/core/src/xmake/os/gid.c +++ b/core/src/xmake/os/gid.c @@ -22,16 +22,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "gid" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "gid" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #ifndef TB_CONFIG_OS_WINDOWS -# include <unistd.h> -# include <errno.h> +#include <unistd.h> +#include <errno.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -40,25 +40,19 @@ #ifndef TB_CONFIG_OS_WINDOWS // get & set gid -tb_int_t xm_os_gid(lua_State* lua) -{ - // check +tb_int_t xm_os_gid(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_int_t rgidset = -1; tb_int_t egidset = -1; - tb_int_t argc = lua_gettop(lua); - if (argc == 1) - { - if (lua_istable(lua, 1)) - { + tb_int_t argc = lua_gettop(lua); + if (argc == 1) { + if (lua_istable(lua, 1)) { // os.gid({["rgid"] = rgid, ["egid"] = egid}) lua_getfield(lua, 1, "rgid"); lua_getfield(lua, 1, "egid"); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { + if (!lua_isnil(lua, -1)) { + if (!lua_isnumber(lua, -1)) { lua_pushfstring(lua, "invalid field type(%s) in `egid` for os.gid", luaL_typename(lua, -1)); lua_error(lua); return 0; @@ -66,10 +60,8 @@ tb_int_t xm_os_gid(lua_State* lua) egidset = (tb_int_t)lua_tonumber(lua, -1); } lua_pop(lua, 1); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { + if (!lua_isnil(lua, -1)) { + if (!lua_isnumber(lua, -1)) { lua_pushfstring(lua, "invalid field type(%s) in `rgid` for os.gid", luaL_typename(lua, -1)); lua_error(lua); return 0; @@ -77,45 +69,33 @@ tb_int_t xm_os_gid(lua_State* lua) rgidset = (tb_int_t)lua_tonumber(lua, -1); } lua_pop(lua, 1); - } - else if (lua_isnumber(lua, 1)) - { + } else if (lua_isnumber(lua, 1)) { // os.gid(gid) rgidset = egidset = (tb_int_t)lua_tonumber(lua, 1); - } - else - { + } else { lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 1)); lua_error(lua); return 0; } - } - else if (argc == 2) - { + } else if (argc == 2) { // os.gid(rgid, egid) - if (!lua_isnil(lua, 1)) - { - if (!lua_isnumber(lua, 1)) - { + if (!lua_isnil(lua, 1)) { + if (!lua_isnumber(lua, 1)) { lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 1)); lua_error(lua); return 0; } rgidset = (tb_int_t)lua_tonumber(lua, 1); } - if (!lua_isnil(lua, 2)) - { - if (!lua_isnumber(lua, 2)) - { + if (!lua_isnil(lua, 2)) { + if (!lua_isnumber(lua, 2)) { lua_pushfstring(lua, "invalid argument type(%s) for os.gid", luaL_typename(lua, 2)); lua_error(lua); return 0; } egidset = (tb_int_t)lua_tonumber(lua, 2); } - } - else if (argc != 0) - { + } else if (argc != 0) { lua_pushstring(lua, "invalid argument count for os.gid"); lua_error(lua); return 0; @@ -124,8 +104,7 @@ tb_int_t xm_os_gid(lua_State* lua) // store return value lua_newtable(lua); - if (rgidset != -1 || egidset != -1) - { + if (rgidset != -1 || egidset != -1) { // set rgid & egid lua_pushstring(lua, "errno"); lua_pushinteger(lua, setregid(rgidset, egidset) != 0 ? errno : 0); @@ -144,7 +123,6 @@ tb_int_t xm_os_gid(lua_State* lua) lua_pushinteger(lua, egid); lua_settable(lua, -3); - // ok return 1; } diff --git a/core/src/xmake/os/isdir.c b/core/src/xmake/os/isdir.c index 531988486..f182e029d 100644 --- a/core/src/xmake/os/isdir.c +++ b/core/src/xmake/os/isdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "isdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "isdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_isdir(lua_State* lua) -{ - // check +tb_int_t xm_os_isdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // is directory? - tb_file_info_t info = {0}; + tb_file_info_t info = { 0 }; lua_pushboolean(lua, tb_file_info(path, &info) && (info.type == TB_FILE_TYPE_DIRECTORY)); return 1; } diff --git a/core/src/xmake/os/isfile.c b/core/src/xmake/os/isfile.c index 293692a98..d7b1d66f2 100644 --- a/core/src/xmake/os/isfile.c +++ b/core/src/xmake/os/isfile.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "isfile" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "isfile" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_isfile(lua_State* lua) -{ - // check +tb_int_t xm_os_isfile(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // is file? - tb_file_info_t info = {0}; + tb_file_info_t info = { 0 }; lua_pushboolean(lua, tb_file_info(path, &info) && (info.type == TB_FILE_TYPE_FILE)); return 1; } diff --git a/core/src/xmake/os/islink.c b/core/src/xmake/os/islink.c index c2b70b74d..51181ecd2 100644 --- a/core/src/xmake/os/islink.c +++ b/core/src/xmake/os/islink.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "islink" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "islink" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_islink(lua_State* lua) -{ - // check +tb_int_t xm_os_islink(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // is link? - tb_file_info_t info = {0}; + tb_file_info_t info = { 0 }; lua_pushboolean(lua, tb_file_info(path, &info) && (info.flags & TB_FILE_FLAG_LINK)); return 1; } diff --git a/core/src/xmake/os/link.c b/core/src/xmake/os/link.c index 8c15dfa41..cead7f88c 100644 --- a/core/src/xmake/os/link.c +++ b/core/src/xmake/os/link.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "link" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "link" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_link(lua_State* lua) -{ - // check +tb_int_t xm_os_link(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the source and destination - tb_char_t const* src = luaL_checkstring(lua, 1); - tb_char_t const* dst = luaL_checkstring(lua, 2); + tb_char_t const *src = luaL_checkstring(lua, 1); + tb_char_t const *dst = luaL_checkstring(lua, 2); tb_check_return_val(src && dst, 0); // do os.link(src, dst) lua_pushboolean(lua, tb_file_link(src, dst)); - // ok return 1; } diff --git a/core/src/xmake/os/mclock.c b/core/src/xmake/os/mclock.c index 43e0e3f92..5fa440db6 100644 --- a/core/src/xmake/os/mclock.c +++ b/core/src/xmake/os/mclock.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "mclock" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "mclock" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // os.mclock() -tb_int_t xm_os_mclock(lua_State* lua) -{ - // check +tb_int_t xm_os_mclock(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // save result lua_pushnumber(lua, (lua_Number)tb_mclock()); - // ok return 1; } diff --git a/core/src/xmake/os/meminfo.c b/core/src/xmake/os/meminfo.c index 293359350..44fbb7770 100644 --- a/core/src/xmake/os/meminfo.c +++ b/core/src/xmake/os/meminfo.c @@ -22,55 +22,54 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "meminfo" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "meminfo" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #if defined(TB_CONFIG_OS_MACOSX) -# include <mach/mach.h> -# include <mach/machine.h> -# include <mach/vm_statistics.h> -# include <mach-o/dyld.h> -# include <mach-o/nlist.h> -# include <AvailabilityMacros.h> +#include <mach/mach.h> +#include <mach/machine.h> +#include <mach/vm_statistics.h> +#include <mach-o/dyld.h> +#include <mach-o/nlist.h> +#include <AvailabilityMacros.h> #elif defined(TB_CONFIG_OS_LINUX) -# include <stdio.h> -# include <sys/sysinfo.h> +#include <stdio.h> +#include <sys/sysinfo.h> #elif defined(TB_CONFIG_OS_WINDOWS) -# include <windows.h> +#include <windows.h> #elif defined(TB_CONFIG_OS_BSD) -# include <sys/types.h> -# include <sys/sysctl.h> +#include <sys/types.h> +#include <sys/sysctl.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ #ifdef TB_CONFIG_OS_LINUX -static tb_int64_t xm_os_meminfo_get_value(tb_char_t const* buffer, tb_char_t const* name) -{ - tb_char_t const* p = tb_strstr(buffer, name); - return p? tb_stoi64(p + tb_strlen(name)) : 0; +static tb_int64_t xm_os_meminfo_get_value(tb_char_t const *buffer, tb_char_t const *name) { + tb_char_t const *p = tb_strstr(buffer, name); + return p ? tb_stoi64(p + tb_strlen(name)) : 0; } #endif // get the used memory size (MB) -static tb_bool_t xm_os_meminfo_stats(tb_int_t* ptotalsize, tb_int_t* pavailsize) -{ +static tb_bool_t xm_os_meminfo_stats(tb_int_t *ptotalsize, tb_int_t *pavailsize) { #if defined(TB_CONFIG_OS_MACOSX) vm_statistics64_data_t vmstat; mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; - if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t) &vmstat, &count) == KERN_SUCCESS) - { - tb_int_t pagesize = (tb_int_t)tb_page_size(); - tb_int64_t totalsize = (tb_int64_t)(vmstat.inactive_count + vmstat.free_count + vmstat.active_count + vmstat.wire_count + if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t)&vmstat, &count) == KERN_SUCCESS) { + tb_int_t pagesize = (tb_int_t)tb_page_size(); + tb_int64_t totalsize = (tb_int64_t)(vmstat.inactive_count + vmstat.free_count + vmstat.active_count + + vmstat.wire_count #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 - + vmstat.compressor_page_count + + vmstat.compressor_page_count #endif - ) * pagesize; + ) * + pagesize; /* * NB: speculative pages are already accounted for in "free_count", * so "speculative_count" is the number of "free" pages that are @@ -78,7 +77,8 @@ static tb_bool_t xm_os_meminfo_stats(tb_int_t* ptotalsize, tb_int_t* pavailsize) * haven't actually been used by anyone so far. * */ - tb_int64_t availsize = (tb_int64_t)(vmstat.inactive_count + vmstat.free_count - vmstat.speculative_count) * pagesize; + tb_int64_t availsize = (tb_int64_t)(vmstat.inactive_count + vmstat.free_count - vmstat.speculative_count) * + pagesize; *ptotalsize = (tb_int_t)(totalsize / (1024 * 1024)); *pavailsize = (tb_int_t)(availsize / (1024 * 1024)); return tb_true; @@ -88,74 +88,69 @@ static tb_bool_t xm_os_meminfo_stats(tb_int_t* ptotalsize, tb_int_t* pavailsize) * * @see https://github.com/rfjakob/earlyoom/blob/cba1d599e4a7484c45ac017aa7702ff879f15846/meminfo.c#L52 */ - if (tb_file_info("/proc/meminfo", tb_null)) - { + if (tb_file_info("/proc/meminfo", tb_null)) { tb_bool_t ok = tb_false; - FILE* fp = fopen("/proc/meminfo", "r"); - if (fp) - { + FILE *fp = fopen("/proc/meminfo", "r"); + if (fp) { // 8192 should be enough for the foreseeable future. tb_char_t buffer[8192]; - size_t len = fread(buffer, 1, sizeof(buffer) - 1, fp); - if (!ferror(fp) && len) - { + size_t len = fread(buffer, 1, sizeof(buffer) - 1, fp); + if (!ferror(fp) && len) { tb_int64_t totalsize = xm_os_meminfo_get_value(buffer, "MemTotal:"); tb_int64_t availsize = xm_os_meminfo_get_value(buffer, "MemAvailable:"); - if (availsize <= 0) - { + if (availsize <= 0) { tb_int64_t cachesize = xm_os_meminfo_get_value(buffer, "Cached:"); tb_int64_t freesize = xm_os_meminfo_get_value(buffer, "MemFree:"); tb_int64_t buffersize = xm_os_meminfo_get_value(buffer, "Buffers:"); tb_int64_t shmemsize = xm_os_meminfo_get_value(buffer, "Shmem:"); - if (cachesize >= 0 && freesize >= 0 && buffersize >= 0 && shmemsize >= 0) + if (cachesize >= 0 && freesize >= 0 && buffersize >= 0 && shmemsize >= 0) { availsize = freesize + buffersize + cachesize - shmemsize; + } } - if (totalsize > 0 && availsize >= 0) - { + if (totalsize > 0 && availsize >= 0) { *ptotalsize = (tb_int_t)(totalsize / 1024); *pavailsize = (tb_int_t)(availsize / 1024); - ok = tb_true; + ok = tb_true; } } fclose(fp); } return ok; - } - else - { - struct sysinfo info = {0}; - if (sysinfo(&info) == 0) - { + } else { + struct sysinfo info = { 0 }; + if (sysinfo(&info) == 0) { *ptotalsize = (tb_int_t)(info.totalram / (1024 * 1024)); - *pavailsize = (tb_int_t)((info.freeram + info.bufferram/* + cache size */) / (1024 * 1024)); + *pavailsize = (tb_int_t)((info.freeram + info.bufferram /* + cache size */) / (1024 * 1024)); return tb_true; } } #elif defined(TB_CONFIG_OS_WINDOWS) MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); - if (GlobalMemoryStatusEx(&statex)) - { + if (GlobalMemoryStatusEx(&statex)) { *ptotalsize = (tb_int_t)(statex.ullTotalPhys / (1024 * 1024)); *pavailsize = (tb_int_t)(statex.ullAvailPhys / (1024 * 1024)); return tb_true; } #elif defined(TB_CONFIG_OS_BSD) && !defined(__OpenBSD__) unsigned long totalsize; - size_t size = sizeof(totalsize); - if (sysctlbyname("hw.physmem", &totalsize, &size, tb_null, 0) != 0) + size_t size = sizeof(totalsize); + if (sysctlbyname("hw.physmem", &totalsize, &size, tb_null, 0) != 0) { return tb_false; + } // http://web.mit.edu/freebsd/head/usr.bin/systat/vmstat.c tb_uint32_t v_free_count; size = sizeof(v_free_count); - if (sysctlbyname("vm.stats.vm.v_free_count", &v_free_count, &size, tb_null, 0) != 0) + if (sysctlbyname("vm.stats.vm.v_free_count", &v_free_count, &size, tb_null, 0) != 0) { return tb_false; + } tb_uint32_t v_inactive_count; size = sizeof(v_inactive_count); - if (sysctlbyname("vm.stats.vm.v_inactive_count", &v_inactive_count, &size, tb_null, 0) != 0) + if (sysctlbyname("vm.stats.vm.v_inactive_count", &v_inactive_count, &size, tb_null, 0) != 0) { return tb_false; + } *ptotalsize = (tb_int_t)(totalsize / (1024 * 1024)); *pavailsize = (tb_int_t)(((tb_int64_t)(v_free_count + v_inactive_count) * tb_page_size()) / (1024 * 1024)); @@ -167,9 +162,7 @@ static tb_bool_t xm_os_meminfo_stats(tb_int_t* ptotalsize, tb_int_t* pavailsize) /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_meminfo(lua_State* lua) -{ - // check +tb_int_t xm_os_meminfo(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // init table @@ -184,8 +177,7 @@ tb_int_t xm_os_meminfo(lua_State* lua) // get the memory size (MB) tb_int_t availsize = 0; tb_int_t totalsize = 0; - if (xm_os_meminfo_stats(&totalsize, &availsize)) - { + if (xm_os_meminfo_stats(&totalsize, &availsize)) { lua_pushstring(lua, "totalsize"); lua_pushinteger(lua, totalsize); lua_settable(lua, -3); diff --git a/core/src/xmake/os/mkdir.c b/core/src/xmake/os/mkdir.c index 6528ad71f..9f8b3399d 100644 --- a/core/src/xmake/os/mkdir.c +++ b/core/src/xmake/os/mkdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "mkdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "mkdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,20 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_mkdir(lua_State* lua) -{ - // check +tb_int_t xm_os_mkdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // os.mkdir(path) - tb_file_info_t info = {0}; - if (!tb_file_info(path, &info) || (info.type != TB_FILE_TYPE_DIRECTORY)) + tb_file_info_t info = { 0 }; + if (!tb_file_info(path, &info) || (info.type != TB_FILE_TYPE_DIRECTORY)) { lua_pushboolean(lua, tb_directory_create(path)); - else lua_pushboolean(lua, tb_true); + } else { + lua_pushboolean(lua, tb_true); + } - // ok return 1; } diff --git a/core/src/xmake/os/mtime.c b/core/src/xmake/os/mtime.c index 09c988caa..0dd10c7ca 100644 --- a/core/src/xmake/os/mtime.c +++ b/core/src/xmake/os/mtime.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "mtime" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "mtime" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_mtime(lua_State* lua) -{ - // check +tb_int_t xm_os_mtime(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // os.mtime(path) - tb_file_info_t info = {0}; - if (tb_file_info(path, &info)) + tb_file_info_t info = { 0 }; + if (tb_file_info(path, &info)) { lua_pushinteger(lua, (lua_Integer)info.mtime); - else lua_pushinteger(lua, 0); + } else { + lua_pushinteger(lua, 0); + } return 1; } diff --git a/core/src/xmake/os/prefix.h b/core/src/xmake/os/prefix.h index adbe78c7d..f98808a6b 100644 --- a/core/src/xmake/os/prefix.h +++ b/core/src/xmake/os/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/os/readlink.c b/core/src/xmake/os/readlink.c index 0877a10da..c1a45cf4b 100644 --- a/core/src/xmake/os/readlink.c +++ b/core/src/xmake/os/readlink.c @@ -22,27 +22,25 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "readlink" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "readlink" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #ifndef TB_CONFIG_OS_WINDOWS -# include <unistd.h> +#include <unistd.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_readlink(lua_State* lua) -{ - // check +tb_int_t xm_os_readlink(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // is link? @@ -51,30 +49,26 @@ tb_int_t xm_os_readlink(lua_State* lua) #else tb_char_t srcpath[TB_PATH_MAXN]; tb_long_t size = readlink(path, srcpath, TB_PATH_MAXN); - if (size == TB_PATH_MAXN) - { + if (size == TB_PATH_MAXN) { tb_size_t maxn = TB_PATH_MAXN * 2; - tb_char_t* data = (tb_char_t*)tb_malloc(maxn); - if (data) - { + tb_char_t *data = (tb_char_t *)tb_malloc(maxn); + if (data) { tb_long_t size = readlink(path, data, maxn); - if (size > 0 && size < maxn) - { + if (size > 0 && size < maxn) { data[size] = '\0'; lua_pushstring(lua, data); + } else { + lua_pushnil(lua); } - else lua_pushnil(lua); tb_free(data); } - } - else if (size >= 0 && size < TB_PATH_MAXN) - { + } else if (size >= 0 && size < TB_PATH_MAXN) { srcpath[size] = '\0'; lua_pushstring(lua, srcpath); + } else { + lua_pushnil(lua); } - else lua_pushnil(lua); #endif - // ok return 1; } diff --git a/core/src/xmake/os/rename.c b/core/src/xmake/os/rename.c index 35a24ecb4..0cc469ded 100644 --- a/core/src/xmake/os/rename.c +++ b/core/src/xmake/os/rename.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "rename" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "rename" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_rename(lua_State* lua) -{ - // check +tb_int_t xm_os_rename(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the source and destination - tb_char_t const* src = luaL_checkstring(lua, 1); - tb_char_t const* dst = luaL_checkstring(lua, 2); + tb_char_t const *src = luaL_checkstring(lua, 1); + tb_char_t const *dst = luaL_checkstring(lua, 2); tb_check_return_val(src && dst, 0); // do rename diff --git a/core/src/xmake/os/rmdir.c b/core/src/xmake/os/rmdir.c index 28ef1d251..21b7005a8 100644 --- a/core/src/xmake/os/rmdir.c +++ b/core/src/xmake/os/rmdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "rmdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "rmdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,38 +33,33 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_long_t xm_os_rmdir_empty(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) -{ - // check - tb_bool_t* is_emptydir = (tb_bool_t*)priv; +static tb_long_t xm_os_rmdir_empty(tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv) { + tb_bool_t *is_emptydir = (tb_bool_t *)priv; tb_assert_and_check_return_val(path && info && is_emptydir, TB_DIRECTORY_WALK_CODE_END); // is emptydir? - if (info->type == TB_FILE_TYPE_DIRECTORY || info->type == TB_FILE_TYPE_FILE) - { + if (info->type == TB_FILE_TYPE_DIRECTORY || info->type == TB_FILE_TYPE_FILE) { // not emptydir *is_emptydir = tb_false; return TB_DIRECTORY_WALK_CODE_END; } return TB_DIRECTORY_WALK_CODE_CONTINUE; } -static tb_long_t xm_os_rmdir_remove(tb_char_t const* path, tb_file_info_t const* info, tb_cpointer_t priv) -{ - // check +static tb_long_t xm_os_rmdir_remove(tb_char_t const *path, tb_file_info_t const *info, tb_cpointer_t priv) { tb_assert_and_check_return_val(path, TB_DIRECTORY_WALK_CODE_END); // is directory? - if (info->type == TB_FILE_TYPE_DIRECTORY) - { + if (info->type == TB_FILE_TYPE_DIRECTORY) { // is emptydir? tb_bool_t is_emptydir = tb_true; tb_directory_walk(path, tb_false, tb_true, xm_os_rmdir_empty, &is_emptydir); - // trace tb_trace_d("path: %s, emptydir: %u", path, is_emptydir); // remove empty directory - if (is_emptydir) tb_directory_remove(path); + if (is_emptydir) { + tb_directory_remove(path); + } } return TB_DIRECTORY_WALK_CODE_CONTINUE; } @@ -72,35 +67,30 @@ static tb_long_t xm_os_rmdir_remove(tb_char_t const* path, tb_file_info_t const* /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_rmdir(lua_State* lua) -{ - // check +tb_int_t xm_os_rmdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // only remove empty directory? tb_bool_t rmempty = lua_toboolean(lua, 2); - if (rmempty) - { + if (rmempty) { // remove all empty directories tb_directory_walk(path, tb_true, tb_false, xm_os_rmdir_remove, tb_null); // remove empty root directory tb_bool_t is_emptydir = tb_true; tb_directory_walk(path, tb_false, tb_true, xm_os_rmdir_empty, &is_emptydir); - if (is_emptydir) tb_directory_remove(path); + if (is_emptydir) { + tb_directory_remove(path); + } - // trace tb_trace_d("path: %s, emptydir: %u", path, is_emptydir); - // ok? lua_pushboolean(lua, !tb_file_info(path, tb_null)); - } - else - { + } else { // os.rmdir(path) lua_pushboolean(lua, tb_directory_remove(path)); } diff --git a/core/src/xmake/os/rmfile.c b/core/src/xmake/os/rmfile.c index 1fd29e7c9..2032d4ee2 100644 --- a/core/src/xmake/os/rmfile.c +++ b/core/src/xmake/os/rmfile.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "rmfile" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "rmfile" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,13 +33,11 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_rmfile(lua_State* lua) -{ - // check +tb_int_t xm_os_rmfile(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // do remove diff --git a/core/src/xmake/os/setenv.c b/core/src/xmake/os/setenv.c index b2f7042d7..be375b2f4 100644 --- a/core/src/xmake/os/setenv.c +++ b/core/src/xmake/os/setenv.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "setenv" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "setenv" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,18 +34,16 @@ * implementation */ // ok = os.setenv(name, value) -tb_int_t xm_os_setenv(lua_State* lua) -{ - // check +tb_int_t xm_os_setenv(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the name and value - size_t value_size = 0; - tb_char_t const* name = luaL_checkstring(lua, 1); - tb_char_t const* value = luaL_checklstring(lua, 2, &value_size); + size_t value_size = 0; + tb_char_t const *name = luaL_checkstring(lua, 1); + tb_char_t const *value = luaL_checklstring(lua, 2, &value_size); tb_check_return_val(name, 0); // set it - lua_pushboolean(lua, value? tb_environment_set(name, value) : tb_false); + lua_pushboolean(lua, value ? tb_environment_set(name, value) : tb_false); return 1; } diff --git a/core/src/xmake/os/signal.c b/core/src/xmake/os/signal.c index 86ae2aa00..64f68762a 100644 --- a/core/src/xmake/os/signal.c +++ b/core/src/xmake/os/signal.c @@ -22,24 +22,25 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "signal" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "signal" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #if defined(TB_CONFIG_OS_MACOSX) || defined(TB_CONFIG_OS_IOS) -# include <unistd.h> -# include <signal.h> -#elif defined(TB_CONFIG_OS_LINUX) || defined(TB_CONFIG_OS_BSD) || defined(TB_CONFIG_OS_ANDROID) || defined(TB_CONFIG_OS_HAIKU) -# include <unistd.h> -# include <signal.h> +#include <unistd.h> +#include <signal.h> +#elif defined(TB_CONFIG_OS_LINUX) || defined(TB_CONFIG_OS_BSD) || defined(TB_CONFIG_OS_ANDROID) || \ + defined(TB_CONFIG_OS_HAIKU) +#include <unistd.h> +#include <signal.h> #endif #ifdef TB_CONFIG_OS_BSD -# include <sys/types.h> -# include <sys/sysctl.h> -# include <signal.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <signal.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -47,29 +48,27 @@ */ typedef enum __xm_os_signal_e { XM_OS_SIGINT = 2 -}xm_os_signal_e; +} xm_os_signal_e; typedef enum __xm_os_signal_handler_e { XM_OS_SIGFUN = 0, XM_OS_SIGDFL = 1, XM_OS_SIGIGN = 2, -}xm_os_signal_handler_e; +} xm_os_signal_handler_e; /* ////////////////////////////////////////////////////////////////////////////////////// * globals */ -static lua_State* g_lua = tb_null; +static lua_State *g_lua = tb_null; /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t xm_os_signal_handler_impl(tb_int_t signo) -{ +static tb_void_t xm_os_signal_handler_impl(tb_int_t signo) { // do callback(signo) - lua_State* lua = g_lua; - if (lua) - { - tb_char_t name[64] = {0}; + lua_State *lua = g_lua; + if (lua) { + tb_char_t name[64] = { 0 }; tb_snprintf(name, sizeof(name), "_SIGNAL_HANDLER_%d", signo); lua_getglobal(lua, name); lua_pushinteger(lua, signo); @@ -78,76 +77,72 @@ static tb_void_t xm_os_signal_handler_impl(tb_int_t signo) } #if defined(TB_CONFIG_OS_WINDOWS) -static BOOL WINAPI xm_os_signal_handler(DWORD ctrl_type) -{ - if (ctrl_type == CTRL_C_EVENT) +static BOOL WINAPI xm_os_signal_handler(DWORD ctrl_type) { + if (ctrl_type == CTRL_C_EVENT) { xm_os_signal_handler_impl(XM_OS_SIGINT); + } return TRUE; } #elif defined(SIGINT) -static tb_void_t xm_os_signal_handler(tb_int_t signo_native) -{ +static tb_void_t xm_os_signal_handler(tb_int_t signo_native) { tb_int_t signo = -1; - switch (signo_native) - { + switch (signo_native) { case SIGINT: signo = XM_OS_SIGINT; break; default: break; } - if (signo >= 0) + if (signo >= 0) { xm_os_signal_handler_impl(signo); + } } #endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_signal(lua_State* lua) -{ - // check +tb_int_t xm_os_signal(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - g_lua = lua; + g_lua = lua; tb_int_t handler = XM_OS_SIGFUN; // check signal handler - if (lua_isnumber(lua, 2)) - handler = (tb_int_t) luaL_checkinteger(lua, 2); - else if (!lua_isfunction(lua, 2)) + if (lua_isnumber(lua, 2)) { + handler = (tb_int_t)luaL_checkinteger(lua, 2); + } else if (!lua_isfunction(lua, 2)) { return 0; + } // save signal handler tb_int_t signo = (tb_int_t)luaL_checkinteger(lua, 1); - if (handler == XM_OS_SIGFUN) - { - tb_char_t name[64] = {0}; + if (handler == XM_OS_SIGFUN) { + tb_char_t name[64] = { 0 }; tb_snprintf(name, sizeof(name), "_SIGNAL_HANDLER_%d", signo); lua_pushvalue(lua, 2); lua_setglobal(lua, name); } #if defined(TB_CONFIG_OS_WINDOWS) - if (signo != XM_OS_SIGINT) + if (signo != XM_OS_SIGINT) { return 0; + } - switch (handler) - { - case XM_OS_SIGFUN: - SetConsoleCtrlHandler(xm_os_signal_handler, TRUE); - break; - case XM_OS_SIGDFL: - SetConsoleCtrlHandler(NULL, FALSE); - break; - case XM_OS_SIGIGN: - SetConsoleCtrlHandler(NULL, TRUE); - break; - default: - break; + switch (handler) { + case XM_OS_SIGFUN: + SetConsoleCtrlHandler(xm_os_signal_handler, TRUE); + break; + case XM_OS_SIGDFL: + SetConsoleCtrlHandler(NULL, FALSE); + break; + case XM_OS_SIGIGN: + SetConsoleCtrlHandler(NULL, TRUE); + break; + default: + break; } #elif defined(SIGINT) - switch (signo) - { + switch (signo) { case XM_OS_SIGINT: signo = SIGINT; break; @@ -155,19 +150,18 @@ tb_int_t xm_os_signal(lua_State* lua) return 0; } - switch (handler) - { - case XM_OS_SIGFUN: - signal(signo, xm_os_signal_handler); - break; - case XM_OS_SIGDFL: - signal(signo, SIG_DFL); - break; - case XM_OS_SIGIGN: - signal(signo, SIG_IGN); - break; - default: - break; + switch (handler) { + case XM_OS_SIGFUN: + signal(signo, xm_os_signal_handler); + break; + case XM_OS_SIGDFL: + signal(signo, SIG_DFL); + break; + case XM_OS_SIGIGN: + signal(signo, SIG_IGN); + break; + default: + break; } #endif return 0; diff --git a/core/src/xmake/os/sleep.c b/core/src/xmake/os/sleep.c index 528279d45..0c8fa135e 100644 --- a/core/src/xmake/os/sleep.c +++ b/core/src/xmake/os/sleep.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "sleep" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "sleep" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,10 +33,11 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_sleep(lua_State* lua) -{ +tb_int_t xm_os_sleep(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_long_t interval = (tb_long_t)luaL_checklong(lua, 1); - if (interval >= 0) tb_msleep(interval); + if (interval >= 0) { + tb_msleep(interval); + } return 0; } diff --git a/core/src/xmake/os/strerror.c b/core/src/xmake/os/strerror.c index f801dc9c3..c1ae41039 100644 --- a/core/src/xmake/os/strerror.c +++ b/core/src/xmake/os/strerror.c @@ -22,35 +22,31 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "strerror" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "strerror" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) -# include <windows.h> +#include <windows.h> #else -# include <errno.h> -# include <string.h> +#include <errno.h> +#include <string.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_strerror(lua_State* lua) -{ - // check +tb_int_t xm_os_strerror(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get syserror state tb_size_t syserror = tb_syserror_state(); - if (syserror != TB_STATE_SYSERROR_UNKNOWN_ERROR) - { - tb_char_t const* strerr = "Unknown"; - switch (syserror) - { + if (syserror != TB_STATE_SYSERROR_UNKNOWN_ERROR) { + tb_char_t const *strerr = "Unknown"; + switch (syserror) { case TB_STATE_SYSERROR_NOT_PERM: strerr = "Permission denied"; break; @@ -66,11 +62,9 @@ tb_int_t xm_os_strerror(lua_State* lua) break; } lua_pushstring(lua, strerr); - } - else - { + } else { #if defined(TB_CONFIG_OS_WINDOWS) && !defined(TB_COMPILER_LIKE_UNIX) - tb_char_t strerr[128] = {0}; + tb_char_t strerr[128] = { 0 }; tb_snprintf(strerr, sizeof(strerr), "Unknown Error (%lu)", (tb_size_t)GetLastError()); lua_pushstring(lua, strerr); #else diff --git a/core/src/xmake/os/syserror.c b/core/src/xmake/os/syserror.c index edd367422..3ef2a5c52 100644 --- a/core/src/xmake/os/syserror.c +++ b/core/src/xmake/os/syserror.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "syserror" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "syserror" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,22 +33,27 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_syserror(lua_State* lua) -{ - // check +tb_int_t xm_os_syserror(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get syserror state - tb_int_t err = 0; + tb_int_t err = 0; tb_size_t syserror = tb_syserror_state(); - switch (syserror) - { - case TB_STATE_SYSERROR_NOT_PERM: err = 1; break; - case TB_STATE_SYSERROR_NOT_FILEDIR: err = 2; break; + switch (syserror) { + case TB_STATE_SYSERROR_NOT_PERM: + err = 1; + break; + case TB_STATE_SYSERROR_NOT_FILEDIR: + err = 2; + break; #if ((TB_VERSION_MAJOR * 100) + (TB_VERSION_MINOR * 10) + TB_VERSION_ALTER) >= 173 - case TB_STATE_SYSERROR_NOT_ACCESS: err = 3; break; + case TB_STATE_SYSERROR_NOT_ACCESS: + err = 3; + break; #endif - case TB_STATE_SYSERROR_UNKNOWN_ERROR: err = -1; break; + case TB_STATE_SYSERROR_UNKNOWN_ERROR: + err = -1; + break; } lua_pushinteger(lua, err); return 1; diff --git a/core/src/xmake/os/tmpdir.c b/core/src/xmake/os/tmpdir.c index b2c3a0c43..6d20fc546 100644 --- a/core/src/xmake/os/tmpdir.c +++ b/core/src/xmake/os/tmpdir.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "tmpdir" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "tmpdir" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_tmpdir(lua_State* lua) -{ - // check +tb_int_t xm_os_tmpdir(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // os.tmpdir() tb_char_t path[TB_PATH_MAXN]; - if (tb_directory_temporary(path, sizeof(path))) lua_pushstring(lua, path); - else lua_pushnil(lua); + if (tb_directory_temporary(path, sizeof(path))) { + lua_pushstring(lua, path); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/os/touch.c b/core/src/xmake/os/touch.c index 30f214f2b..5c470440b 100644 --- a/core/src/xmake/os/touch.c +++ b/core/src/xmake/os/touch.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "touch" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "touch" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,12 +33,10 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_os_touch(lua_State* lua) -{ - // check +tb_int_t xm_os_touch(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); tb_time_t atime = (tb_time_t)luaL_checknumber(lua, 2); diff --git a/core/src/xmake/os/uid.c b/core/src/xmake/os/uid.c index 246b17b9a..2785437d0 100644 --- a/core/src/xmake/os/uid.c +++ b/core/src/xmake/os/uid.c @@ -22,16 +22,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "uid" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "uid" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #ifndef TB_CONFIG_OS_WINDOWS -# include <unistd.h> -# include <errno.h> +#include <unistd.h> +#include <errno.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -40,25 +40,19 @@ #ifndef TB_CONFIG_OS_WINDOWS // get & set uid -tb_int_t xm_os_uid(lua_State* lua) -{ - // check +tb_int_t xm_os_uid(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_int_t ruidset = -1; tb_int_t euidset = -1; - tb_int_t argc = lua_gettop(lua); - if (argc == 1) - { - if (lua_istable(lua, 1)) - { + tb_int_t argc = lua_gettop(lua); + if (argc == 1) { + if (lua_istable(lua, 1)) { // os.uid({["ruid"] = ruid, ["euid"] = euid}) lua_getfield(lua, 1, "ruid"); lua_getfield(lua, 1, "euid"); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { + if (!lua_isnil(lua, -1)) { + if (!lua_isnumber(lua, -1)) { lua_pushfstring(lua, "invalid field type(%s) in `euid` for os.uid", luaL_typename(lua, -1)); lua_error(lua); return 0; @@ -66,10 +60,8 @@ tb_int_t xm_os_uid(lua_State* lua) euidset = (tb_int_t)lua_tonumber(lua, -1); } lua_pop(lua, 1); - if (!lua_isnil(lua, -1)) - { - if (!lua_isnumber(lua, -1)) - { + if (!lua_isnil(lua, -1)) { + if (!lua_isnumber(lua, -1)) { lua_pushfstring(lua, "invalid field type(%s) in `ruid` for os.uid", luaL_typename(lua, -1)); lua_error(lua); return 0; @@ -77,45 +69,33 @@ tb_int_t xm_os_uid(lua_State* lua) ruidset = (tb_int_t)lua_tonumber(lua, -1); } lua_pop(lua, 1); - } - else if (lua_isnumber(lua, 1)) - { + } else if (lua_isnumber(lua, 1)) { // os.uid(uid) ruidset = euidset = (tb_int_t)lua_tonumber(lua, 1); - } - else - { + } else { lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 1)); lua_error(lua); return 0; } - } - else if (argc == 2) - { + } else if (argc == 2) { // os.uid(ruid, euid) - if (!lua_isnil(lua, 1)) - { - if (!lua_isnumber(lua, 1)) - { + if (!lua_isnil(lua, 1)) { + if (!lua_isnumber(lua, 1)) { lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 1)); lua_error(lua); return 0; } ruidset = (tb_int_t)lua_tonumber(lua, 1); } - if (!lua_isnil(lua, 2)) - { - if (!lua_isnumber(lua, 2)) - { + if (!lua_isnil(lua, 2)) { + if (!lua_isnumber(lua, 2)) { lua_pushfstring(lua, "invalid argument type(%s) for os.uid", luaL_typename(lua, 2)); lua_error(lua); return 0; } euidset = (tb_int_t)lua_tonumber(lua, 2); } - } - else if (argc != 0) - { + } else if (argc != 0) { lua_pushstring(lua, "invalid argument count for os.uid"); lua_error(lua); return 0; @@ -124,8 +104,7 @@ tb_int_t xm_os_uid(lua_State* lua) // store return value lua_newtable(lua); - if (ruidset != -1 || euidset != -1) - { + if (ruidset != -1 || euidset != -1) { // set ruid & euid lua_pushstring(lua, "errno"); lua_pushinteger(lua, setreuid(ruidset, euidset) != 0 ? errno : 0); @@ -144,7 +123,6 @@ tb_int_t xm_os_uid(lua_State* lua) lua_pushinteger(lua, euid); lua_settable(lua, -3); - // ok return 1; } diff --git a/core/src/xmake/package/loadxmi.c b/core/src/xmake/package/loadxmi.c index 57c6a14d2..15a579f91 100644 --- a/core/src/xmake/package/loadxmi.c +++ b/core/src/xmake/package/loadxmi.c @@ -22,41 +22,38 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "loadxmi" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "loadxmi" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes */ #include "prefix.h" #ifdef USE_LUAJIT -# define XMI_USE_LUAJIT +#define XMI_USE_LUAJIT #endif #include "xmi.h" /* ////////////////////////////////////////////////////////////////////////////////////// * types */ -typedef int (*xm_setup_func_t)(xmi_lua_ops_t* ops); +typedef int (*xm_setup_func_t)(xmi_lua_ops_t *ops); /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_package_loadxmi(lua_State* lua) -{ - // check +tb_int_t xm_package_loadxmi(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); - tb_char_t const* name = luaL_checkstring(lua, 2); + tb_char_t const *name = luaL_checkstring(lua, 2); tb_check_return_val(name, 0); // load module library tb_dynamic_ref_t lib = tb_dynamic_init(path); - if (!lib) - { + if (!lib) { lua_pushnil(lua); lua_pushfstring(lua, "load %s failed", path); return 2; @@ -64,8 +61,7 @@ tb_int_t xm_package_loadxmi(lua_State* lua) // get xmiopen_xxx function lua_CFunction luaopen_func = (lua_CFunction)tb_dynamic_func(lib, name); - if (!luaopen_func) - { + if (!luaopen_func) { lua_pushnil(lua); lua_pushfstring(lua, "cannot get symbol %s failed", name); return 2; @@ -73,69 +69,67 @@ tb_int_t xm_package_loadxmi(lua_State* lua) // get xmisetup function xm_setup_func_t xmisetup_func = (xm_setup_func_t)tb_dynamic_func(lib, "xmisetup"); - if (!xmisetup_func) - { + if (!xmisetup_func) { lua_pushnil(lua); lua_pushfstring(lua, "cannot get symbol xmisetup failed"); return 2; } // setup lua interfaces - static xmi_lua_ops_t s_luaops = {0}; + static xmi_lua_ops_t s_luaops = { 0 }; static tb_bool_t s_luaops_inited = tb_false; - if (!s_luaops_inited) - { + if (!s_luaops_inited) { // get functions #ifdef XMI_USE_LUAJIT - s_luaops._lua_newuserdata = &lua_newuserdata; + s_luaops._lua_newuserdata = &lua_newuserdata; #else - s_luaops._lua_getglobal = &lua_getglobal; - s_luaops._lua_geti = &lua_geti; - s_luaops._lua_rawgetp = &lua_rawgetp; - s_luaops._lua_getiuservalue = &lua_getiuservalue; - s_luaops._lua_newuserdatauv = &lua_newuserdatauv; + s_luaops._lua_getglobal = &lua_getglobal; + s_luaops._lua_geti = &lua_geti; + s_luaops._lua_rawgetp = &lua_rawgetp; + s_luaops._lua_getiuservalue = &lua_getiuservalue; + s_luaops._lua_newuserdatauv = &lua_newuserdatauv; #endif - s_luaops._lua_gettable = &lua_gettable; - s_luaops._lua_getfield = &lua_getfield; - s_luaops._lua_rawget = &lua_rawget; - s_luaops._lua_rawgeti = &lua_rawgeti; - s_luaops._lua_createtable = &lua_createtable; - s_luaops._lua_getmetatable = &lua_getmetatable; + s_luaops._lua_gettable = &lua_gettable; + s_luaops._lua_getfield = &lua_getfield; + s_luaops._lua_rawget = &lua_rawget; + s_luaops._lua_rawgeti = &lua_rawgeti; + s_luaops._lua_createtable = &lua_createtable; + s_luaops._lua_getmetatable = &lua_getmetatable; // set functions #ifndef XMI_USE_LUAJIT - s_luaops._lua_setglobal = &lua_setglobal; - s_luaops._lua_seti = &lua_seti; - s_luaops._lua_rawsetp = &lua_rawsetp; - s_luaops._lua_setiuservalue = &lua_setiuservalue; + s_luaops._lua_setglobal = &lua_setglobal; + s_luaops._lua_seti = &lua_seti; + s_luaops._lua_rawsetp = &lua_rawsetp; + s_luaops._lua_setiuservalue = &lua_setiuservalue; #endif - s_luaops._lua_settable = &lua_settable; - s_luaops._lua_setfield = &lua_setfield; - s_luaops._lua_rawset = &lua_rawset; - s_luaops._lua_rawseti = &lua_rawseti; - s_luaops._lua_setmetatable = &lua_setmetatable; + s_luaops._lua_settable = &lua_settable; + s_luaops._lua_setfield = &lua_setfield; + s_luaops._lua_rawset = &lua_rawset; + s_luaops._lua_rawseti = &lua_rawseti; + s_luaops._lua_setmetatable = &lua_setmetatable; // access functions - s_luaops._lua_isnumber = &lua_isnumber; - s_luaops._lua_isstring = &lua_isstring; - s_luaops._lua_iscfunction = &lua_iscfunction; - s_luaops._lua_isuserdata = &lua_isuserdata; - s_luaops._lua_type = &lua_type; - s_luaops._lua_typename = &lua_typename; + s_luaops._lua_isnumber = &lua_isnumber; + s_luaops._lua_isstring = &lua_isstring; + s_luaops._lua_iscfunction = &lua_iscfunction; + s_luaops._lua_isuserdata = &lua_isuserdata; + s_luaops._lua_type = &lua_type; + s_luaops._lua_typename = &lua_typename; #ifndef XMI_USE_LUAJIT - s_luaops._lua_isinteger = &lua_isinteger; + s_luaops._lua_isinteger = &lua_isinteger; #endif - s_luaops._lua_tonumberx = &lua_tonumberx; - s_luaops._lua_tointegerx = &lua_tointegerx; - s_luaops._lua_toboolean = &lua_toboolean; - s_luaops._lua_tolstring = &lua_tolstring; - s_luaops._lua_tocfunction = &lua_tocfunction; - s_luaops._lua_touserdata = &lua_touserdata; - s_luaops._lua_tothread = &lua_tothread; - s_luaops._lua_topointer = &lua_topointer; + s_luaops._lua_tonumberx = &lua_tonumberx; + s_luaops._lua_tointegerx = &lua_tointegerx; + s_luaops._lua_toboolean = &lua_toboolean; + s_luaops._lua_tolstring = &lua_tolstring; + s_luaops._lua_tocfunction = &lua_tocfunction; + s_luaops._lua_touserdata = &lua_touserdata; + s_luaops._lua_tothread = &lua_tothread; + s_luaops._lua_topointer = &lua_topointer; #ifndef XMI_USE_LUAJIT - s_luaops._lua_rawlen = &lua_rawlen; + s_luaops._lua_rawlen = &lua_rawlen; #endif // push functions @@ -153,74 +147,74 @@ tb_int_t xm_package_loadxmi(lua_State* lua) // stack functions #ifdef XMI_USE_LUAJIT - s_luaops._lua_insert = &lua_insert; - s_luaops._lua_remove = &lua_remove; - s_luaops._lua_replace = &lua_replace; + s_luaops._lua_insert = &lua_insert; + s_luaops._lua_remove = &lua_remove; + s_luaops._lua_replace = &lua_replace; #else - s_luaops._lua_absindex = &lua_absindex; - s_luaops._lua_rotate = &lua_rotate; + s_luaops._lua_absindex = &lua_absindex; + s_luaops._lua_rotate = &lua_rotate; #endif - s_luaops._lua_gettop = &lua_gettop; - s_luaops._lua_settop = &lua_settop; - s_luaops._lua_pushvalue = &lua_pushvalue; - s_luaops._lua_copy = &lua_copy; - s_luaops._lua_checkstack = &lua_checkstack; - s_luaops._lua_xmove = &lua_xmove; + s_luaops._lua_gettop = &lua_gettop; + s_luaops._lua_settop = &lua_settop; + s_luaops._lua_pushvalue = &lua_pushvalue; + s_luaops._lua_copy = &lua_copy; + s_luaops._lua_checkstack = &lua_checkstack; + s_luaops._lua_xmove = &lua_xmove; // miscellaneous functions - s_luaops._lua_error = &lua_error; - s_luaops._lua_next = &lua_next; - s_luaops._lua_concat = &lua_concat; - s_luaops._lua_getallocf = &lua_getallocf; - s_luaops._lua_setallocf = &lua_setallocf; + s_luaops._lua_error = &lua_error; + s_luaops._lua_next = &lua_next; + s_luaops._lua_concat = &lua_concat; + s_luaops._lua_getallocf = &lua_getallocf; + s_luaops._lua_setallocf = &lua_setallocf; #ifndef XMI_USE_LUAJIT - s_luaops._lua_len = &lua_len; - s_luaops._lua_toclose = &lua_toclose; - s_luaops._lua_closeslot = &lua_closeslot; - s_luaops._lua_stringtonumber = &lua_stringtonumber; + s_luaops._lua_len = &lua_len; + s_luaops._lua_toclose = &lua_toclose; + s_luaops._lua_closeslot = &lua_closeslot; + s_luaops._lua_stringtonumber = &lua_stringtonumber; #endif // 'load' and 'call' functions #ifdef XMI_USE_LUAJIT - s_luaops._lua_call = &lua_call; - s_luaops._lua_pcall = &lua_pcall; + s_luaops._lua_call = &lua_call; + s_luaops._lua_pcall = &lua_pcall; #else - s_luaops._lua_callk = &lua_callk; - s_luaops._lua_pcallk = &lua_pcallk; + s_luaops._lua_callk = &lua_callk; + s_luaops._lua_pcallk = &lua_pcallk; #endif - s_luaops._lua_load = &lua_load; - s_luaops._lua_dump = &lua_dump; + s_luaops._lua_load = &lua_load; + s_luaops._lua_dump = &lua_dump; // luaL functions #ifndef XMI_USE_LUAJIT - s_luaops._luaL_tolstring = &luaL_tolstring; - s_luaops._luaL_typeerror = &luaL_typeerror; + s_luaops._luaL_tolstring = &luaL_tolstring; + s_luaops._luaL_typeerror = &luaL_typeerror; #endif - s_luaops._luaL_getmetafield = &luaL_getmetafield; - s_luaops._luaL_callmeta = &luaL_callmeta; - s_luaops._luaL_argerror = &luaL_argerror; - s_luaops._luaL_checklstring = &luaL_checklstring; - s_luaops._luaL_optlstring = &luaL_optlstring; - s_luaops._luaL_checknumber = &luaL_checknumber; - s_luaops._luaL_optnumber = &luaL_optnumber; - s_luaops._luaL_checkinteger = &luaL_checkinteger; - s_luaops._luaL_optinteger = &luaL_optinteger; - s_luaops._luaL_checkstack = &luaL_checkstack; - s_luaops._luaL_checktype = &luaL_checktype; - s_luaops._luaL_checkany = &luaL_checkany; - s_luaops._luaL_newmetatable = &luaL_newmetatable; - s_luaops._luaL_testudata = &luaL_testudata; - s_luaops._luaL_checkudata = &luaL_checkudata; - s_luaops._luaL_where = &luaL_where; - s_luaops._luaL_error = &luaL_error; - s_luaops._luaL_checkoption = &luaL_checkoption; - s_luaops._luaL_fileresult = &luaL_fileresult; - s_luaops._luaL_execresult = &luaL_execresult; - s_luaops._luaL_setfuncs = &luaL_setfuncs; - s_luaops._luaL_loadfilex = &luaL_loadfilex; - s_luaops._luaL_loadstring = &luaL_loadstring; + s_luaops._luaL_getmetafield = &luaL_getmetafield; + s_luaops._luaL_callmeta = &luaL_callmeta; + s_luaops._luaL_argerror = &luaL_argerror; + s_luaops._luaL_checklstring = &luaL_checklstring; + s_luaops._luaL_optlstring = &luaL_optlstring; + s_luaops._luaL_checknumber = &luaL_checknumber; + s_luaops._luaL_optnumber = &luaL_optnumber; + s_luaops._luaL_checkinteger = &luaL_checkinteger; + s_luaops._luaL_optinteger = &luaL_optinteger; + s_luaops._luaL_checkstack = &luaL_checkstack; + s_luaops._luaL_checktype = &luaL_checktype; + s_luaops._luaL_checkany = &luaL_checkany; + s_luaops._luaL_newmetatable = &luaL_newmetatable; + s_luaops._luaL_testudata = &luaL_testudata; + s_luaops._luaL_checkudata = &luaL_checkudata; + s_luaops._luaL_where = &luaL_where; + s_luaops._luaL_error = &luaL_error; + s_luaops._luaL_checkoption = &luaL_checkoption; + s_luaops._luaL_fileresult = &luaL_fileresult; + s_luaops._luaL_execresult = &luaL_execresult; + s_luaops._luaL_setfuncs = &luaL_setfuncs; + s_luaops._luaL_loadfilex = &luaL_loadfilex; + s_luaops._luaL_loadstring = &luaL_loadstring; - s_luaops_inited = tb_true; + s_luaops_inited = tb_true; } xmisetup_func(&s_luaops); diff --git a/core/src/xmake/package/prefix.h b/core/src/xmake/package/prefix.h index d6bed58b8..253140059 100644 --- a/core/src/xmake/package/prefix.h +++ b/core/src/xmake/package/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/path/absolute.c b/core/src/xmake/path/absolute.c index a780cdce8..9b05bc07d 100644 --- a/core/src/xmake/path/absolute.c +++ b/core/src/xmake/path/absolute.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "absolute" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "absolute" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_path_absolute(lua_State* lua) -{ - // check +tb_int_t xm_path_absolute(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // get the root - tb_char_t const* root = luaL_optstring(lua, 2, tb_null); + tb_char_t const *root = luaL_optstring(lua, 2, tb_null); // do path:absolute(root) tb_char_t data[TB_PATH_MAXN]; diff --git a/core/src/xmake/path/directory.c b/core/src/xmake/path/directory.c index 2af805d45..7a6b37863 100644 --- a/core/src/xmake/path/directory.c +++ b/core/src/xmake/path/directory.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "directory" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "directory" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,20 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_path_directory(lua_State* lua) -{ - // check +tb_int_t xm_path_directory(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // do path:directory() - tb_char_t data[TB_PATH_MAXN]; - tb_char_t const* dir = tb_path_directory(path, data, sizeof(data)); - if (dir) lua_pushstring(lua, dir); - else lua_pushnil(lua); + tb_char_t data[TB_PATH_MAXN]; + tb_char_t const *dir = tb_path_directory(path, data, sizeof(data)); + if (dir) { + lua_pushstring(lua, dir); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/path/is_absolute.c b/core/src/xmake/path/is_absolute.c index 7ced8d3fe..7ac489e26 100644 --- a/core/src/xmake/path/is_absolute.c +++ b/core/src/xmake/path/is_absolute.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "is_absolute" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "is_absolute" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,13 +33,11 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_path_is_absolute(lua_State* lua) -{ - // check +tb_int_t xm_path_is_absolute(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // path:is_absolute() diff --git a/core/src/xmake/path/prefix.h b/core/src/xmake/path/prefix.h index 3fe86b204..c87dc37bf 100644 --- a/core/src/xmake/path/prefix.h +++ b/core/src/xmake/path/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/path/relative.c b/core/src/xmake/path/relative.c index ec1dcc215..5d3beaaa5 100644 --- a/core/src/xmake/path/relative.c +++ b/core/src/xmake/path/relative.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "relative" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "relative" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_path_relative(lua_State* lua) -{ - // check +tb_int_t xm_path_relative(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path - tb_char_t const* path = luaL_checkstring(lua, 1); + tb_char_t const *path = luaL_checkstring(lua, 1); tb_check_return_val(path, 0); // get the root - tb_char_t const* root = luaL_optstring(lua, 2, tb_null); + tb_char_t const *root = luaL_optstring(lua, 2, tb_null); // path:relative(root) tb_char_t data[TB_PATH_MAXN]; diff --git a/core/src/xmake/path/translate.c b/core/src/xmake/path/translate.c index ecb545098..8d1debda0 100644 --- a/core/src/xmake/path/translate.c +++ b/core/src/xmake/path/translate.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "translate" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "translate" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,31 +33,32 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_path_translate(lua_State* lua) -{ - // check +tb_int_t xm_path_translate(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the path size_t path_size = 0; - tb_char_t const* path = luaL_checklstring(lua, 1, &path_size); + tb_char_t const *path = luaL_checklstring(lua, 1, &path_size); tb_check_return_val(path, 0); // get the option argument, e.g. {normalize = true} tb_bool_t normalize = tb_false; - if (lua_istable(lua, 2)) - { + if (lua_istable(lua, 2)) { lua_pushstring(lua, "normalize"); lua_gettable(lua, 2); - if (lua_toboolean(lua, -1)) + if (lua_toboolean(lua, -1)) { normalize = tb_true; + } lua_pop(lua, 1); } // do path:translate() tb_char_t data[TB_PATH_MAXN]; tb_size_t size = tb_path_translate_to(path, (tb_size_t)path_size, data, sizeof(data), normalize); - if (size) lua_pushlstring(lua, data, (size_t)size); - else lua_pushnil(lua); + if (size) { + lua_pushlstring(lua, data, (size_t)size); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/prefix.h b/core/src/xmake/prefix.h index 790b9797b..4b159741e 100644 --- a/core/src/xmake/prefix.h +++ b/core/src/xmake/prefix.h @@ -27,19 +27,19 @@ #include "prefix/prefix.h" #include "luaconf.h" #if defined(TB_CONFIG_OS_WINDOWS) && defined(__cplusplus) -# undef LUA_API -# undef LUALIB_API -# define LUA_API extern "C" -# define LUALIB_API LUA_API +#undef LUA_API +#undef LUALIB_API +#define LUA_API extern "C" +#define LUALIB_API LUA_API #endif #ifdef USE_LUAJIT -# include "luajit.h" -# include "lualib.h" -# include "lauxlib.h" +#include "luajit.h" +#include "lualib.h" +#include "lauxlib.h" #else -# include "lua.h" -# include "lualib.h" -# include "lauxlib.h" +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -47,7 +47,7 @@ */ // this issue has been fixed, @see https://github.com/LuaJIT/LuaJIT/commit/e9af1abec542e6f9851ff2368e7f196b6382a44c -#if 0//TB_CPU_BIT64 +#if 0 //TB_CPU_BIT64 /* we use this interface instead of lua_pushlightuserdata() to fix bad light userdata pointer bug * * @see https://github.com/xmake-io/xmake/issues/914 @@ -95,41 +95,33 @@ static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx) return xm_lua_topointer2(lua, idx, tb_null); } #else -static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State* lua, tb_pointer_t ptr) -{ +static __tb_inline__ tb_void_t xm_lua_pushpointer(lua_State *lua, tb_pointer_t ptr) { lua_pushlightuserdata(lua, ptr); } -static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State* lua, tb_int_t idx) -{ +static __tb_inline__ tb_bool_t xm_lua_ispointer(lua_State *lua, tb_int_t idx) { return lua_isuserdata(lua, idx); } -static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State* lua, tb_int_t idx, tb_char_t const** pstr) -{ - if (pstr) *pstr = tb_null; +static __tb_inline__ tb_pointer_t xm_lua_topointer2(lua_State *lua, tb_int_t idx, tb_char_t const **pstr) { + if (pstr) + *pstr = tb_null; return lua_touserdata(lua, idx); } -static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State* lua, tb_int_t idx) -{ +static __tb_inline__ tb_pointer_t xm_lua_topointer(lua_State *lua, tb_int_t idx) { return lua_touserdata(lua, idx); } #endif -static __tb_inline__ tb_void_t xm_lua_register(lua_State *lua, tb_char_t const* libname, luaL_Reg const* l) -{ +static __tb_inline__ tb_void_t xm_lua_register(lua_State *lua, tb_char_t const *libname, luaL_Reg const *l) { #if LUA_VERSION_NUM >= 504 - if (libname) - { + if (libname) { lua_getglobal(lua, libname); - if (lua_isnil(lua, -1)) - { + if (lua_isnil(lua, -1)) { lua_pop(lua, 1); lua_newtable(lua); } luaL_setfuncs(lua, l, 0); lua_setglobal(lua, libname); - } - else - { + } else { luaL_setfuncs(lua, l, 0); } #else @@ -137,8 +129,7 @@ static __tb_inline__ tb_void_t xm_lua_register(lua_State *lua, tb_char_t const* #endif } -static __tb_inline__ tb_int_t xm_lua_isinteger(lua_State* lua, int idx) -{ +static __tb_inline__ tb_int_t xm_lua_isinteger(lua_State *lua, int idx) { #ifdef USE_LUAJIT return lua_isnumber(lua, idx); #else @@ -147,5 +138,3 @@ static __tb_inline__ tb_int_t xm_lua_isinteger(lua_State* lua, int idx) } #endif - - diff --git a/core/src/xmake/prefix/config.h b/core/src/xmake/prefix/config.h index dbf9fb1cf..9ea5384f5 100644 --- a/core/src/xmake/prefix/config.h +++ b/core/src/xmake/prefix/config.h @@ -36,7 +36,7 @@ * small mode */ #if XM_CONFIG_SMALL -# define __xm_small__ +#define __xm_small__ #endif /*! @def __xm_debug__ @@ -44,7 +44,7 @@ * debug mode */ #ifdef __tb_debug__ -# define __xm_debug__ +#define __xm_debug__ #endif /* unix/gcc on msys/cygwin? do not support it now! @@ -64,9 +64,7 @@ * $ make install prefix=/mingw64 */ #if defined(TB_CONFIG_OS_WINDOWS) && defined(TB_COMPILER_LIKE_UNIX) -# error "do not support gcc on msys/cygwin, please uses mingw-w64-[i686|x86_64]-gcc" +#error "do not support gcc on msys/cygwin, please uses mingw-w64-[i686|x86_64]-gcc" #endif #endif - - diff --git a/core/src/xmake/prefix/prefix.h b/core/src/xmake/prefix/prefix.h index ee288bf96..954fca4c7 100644 --- a/core/src/xmake/prefix/prefix.h +++ b/core/src/xmake/prefix/prefix.h @@ -27,7 +27,4 @@ #include "config.h" #include "version.h" - #endif - - diff --git a/core/src/xmake/prefix/version.h b/core/src/xmake/prefix/version.h index 35f60ddf1..c5a3465e2 100644 --- a/core/src/xmake/prefix/version.h +++ b/core/src/xmake/prefix/version.h @@ -31,29 +31,37 @@ */ /// the major version -#define XM_VERSION_MAJOR XM_CONFIG_VERSION_MAJOR +#define XM_VERSION_MAJOR XM_CONFIG_VERSION_MAJOR /// the minor version -#define XM_VERSION_MINOR XM_CONFIG_VERSION_MINOR +#define XM_VERSION_MINOR XM_CONFIG_VERSION_MINOR /// the alter version -#define XM_VERSION_ALTER XM_CONFIG_VERSION_ALTER +#define XM_VERSION_ALTER XM_CONFIG_VERSION_ALTER /// the build version #ifndef XM_CONFIG_VERSION_BUILD -# define XM_CONFIG_VERSION_BUILD 0 +#define XM_CONFIG_VERSION_BUILD 0 #endif -#define XM_VERSION_BUILD XM_CONFIG_VERSION_BUILD +#define XM_VERSION_BUILD XM_CONFIG_VERSION_BUILD /// the build version string -#define XM_VERSION_BUILD_STRING __tb_mstring_ex__(XM_CONFIG_VERSION_BUILD) +#define XM_VERSION_BUILD_STRING __tb_mstring_ex__(XM_CONFIG_VERSION_BUILD) /// the version string -#define XM_VERSION_STRING __tb_mstrcat6__("xmake_", __tb_mstring_ex__(__tb_mconcat8_ex__(v, XM_VERSION_MAJOR, _, XM_VERSION_MINOR, _, XM_VERSION_ALTER, _, XM_CONFIG_VERSION_BUILD)), "_", TB_ARCH_VERSION_STRING, " by ", TB_COMPILER_VERSION_STRING) +#define XM_VERSION_STRING \ + __tb_mstrcat6__("xmake_", \ + __tb_mstring_ex__(__tb_mconcat8_ex__( \ + v, XM_VERSION_MAJOR, _, XM_VERSION_MINOR, _, XM_VERSION_ALTER, _, XM_CONFIG_VERSION_BUILD)), \ + "_", \ + TB_ARCH_VERSION_STRING, \ + " by ", \ + TB_COMPILER_VERSION_STRING) /// the short version string -#define XM_VERSION_SHORT_STRING __tb_mstrcat__("xmake_", __tb_mstring_ex__(__tb_mconcat8_ex__(v, XM_VERSION_MAJOR, _, XM_VERSION_MINOR, _, XM_VERSION_ALTER, _, XM_CONFIG_VERSION_BUILD))) +#define XM_VERSION_SHORT_STRING \ + __tb_mstrcat__("xmake_", \ + __tb_mstring_ex__(__tb_mconcat8_ex__( \ + v, XM_VERSION_MAJOR, _, XM_VERSION_MINOR, _, XM_VERSION_ALTER, _, XM_CONFIG_VERSION_BUILD))) #endif - - diff --git a/core/src/xmake/process/close.c b/core/src/xmake/process/close.c index 71485caac..bd39d49ee 100644 --- a/core/src/xmake/process/close.c +++ b/core/src/xmake/process/close.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "process.close" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "process.close" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // process.close(p) -tb_int_t xm_process_close(lua_State* lua) -{ - // check +tb_int_t xm_process_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 the process tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1); diff --git a/core/src/xmake/process/kill.c b/core/src/xmake/process/kill.c index ad153953a..21cc68149 100644 --- a/core/src/xmake/process/kill.c +++ b/core/src/xmake/process/kill.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "process.kill" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "process.kill" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,13 @@ */ // process.kill(p) -tb_int_t xm_process_kill(lua_State* lua) -{ - // check +tb_int_t xm_process_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 the process tb_process_ref_t process = (tb_process_ref_t)xm_lua_topointer(lua, 1); diff --git a/core/src/xmake/process/open.c b/core/src/xmake/process/open.c index 48d85c1f3..669e90ff1 100644 --- a/core/src/xmake/process/open.c +++ b/core/src/xmake/process/open.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "process.open" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "process.open" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -41,38 +41,36 @@ * infile = "", inpipe = "", inpipe = "", * envs = {"PATH=xxx", "XXX=yyy"}}) */ -tb_int_t xm_process_open(lua_State* lua) -{ - // check +tb_int_t xm_process_open(lua_State *lua) { 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); + 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}; + 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)) - { + 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)) + if (lua_toboolean(lua, -1)) { attr.flags |= TB_PROCESS_FLAG_DETACH; + } lua_pop(lua, 1); // get curdir @@ -100,35 +98,31 @@ tb_int_t xm_process_open(lua_State* lua) lua_pop(lua, 1); // get infile - if (!inpath) - { + if (!inpath) { lua_pushstring(lua, "infile"); lua_gettable(lua, 3); - infile = (xm_io_file_t*)lua_touserdata(lua, -1); + infile = (xm_io_file_t *)lua_touserdata(lua, -1); lua_pop(lua, 1); } // get outfile - if (!outpath) - { + if (!outpath) { lua_pushstring(lua, "outfile"); lua_gettable(lua, 3); - outfile = (xm_io_file_t*)lua_touserdata(lua, -1); + outfile = (xm_io_file_t *)lua_touserdata(lua, -1); lua_pop(lua, 1); } // get errfile - if (!errpath) - { + if (!errpath) { lua_pushstring(lua, "errfile"); lua_gettable(lua, 3); - errfile = (xm_io_file_t*)lua_touserdata(lua, -1); + errfile = (xm_io_file_t *)lua_touserdata(lua, -1); lua_pop(lua, 1); } // get inpipe - if (!inpath && !infile) - { + if (!inpath && !infile) { lua_pushstring(lua, "inpipe"); lua_gettable(lua, 3); inpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1); @@ -136,8 +130,7 @@ tb_int_t xm_process_open(lua_State* lua) } // get outpipe - if (!outpath && !outfile) - { + if (!outpath && !outfile) { lua_pushstring(lua, "outpipe"); lua_gettable(lua, 3); outpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1); @@ -145,8 +138,7 @@ tb_int_t xm_process_open(lua_State* lua) } // get errpipe - if (!errpath && !errfile) - { + if (!errpath && !errfile) { lua_pushstring(lua, "errpipe"); lua_gettable(lua, 3); errpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1); @@ -156,36 +148,36 @@ tb_int_t xm_process_open(lua_State* lua) // get environments lua_pushstring(lua, "envs"); lua_gettable(lua, 2); - if (lua_istable(lua, -1)) - { + 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++) - { + 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)) - { + if (lua_isstring(lua, -1)) { // add this environment value - if (envn + 1 < tb_arrayn(envs)) + if (envn + 1 < tb_arrayn(envs)) { envs[envn++] = lua_tostring(lua, -1); - else - { + } else { // error - lua_pushfstring(lua, "envs is too large(%d > %d) for process.openv", (tb_int_t)envn, tb_arrayn(envs) - 1); + lua_pushfstring(lua, + "envs is too large(%d > %d) for process.openv", + (tb_int_t)envn, + tb_arrayn(envs) - 1); lua_error(lua); } - } - else - { + } else { // error - lua_pushfstring(lua, "invalid envs[%d] type(%s) for process.openv", (tb_int_t)i, luaL_typename(lua, -1)); + lua_pushfstring(lua, + "invalid envs[%d] type(%s) for process.openv", + (tb_int_t)i, + luaL_typename(lua, -1)); lua_error(lua); } @@ -197,81 +189,67 @@ tb_int_t xm_process_open(lua_State* lua) } // redirect stdin? - if (inpath) - { + 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)) - { + } 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) - { + 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) - { + } else if (inpipe) { attr.in.pipe = inpipe; attr.intype = TB_PROCESS_REDIRECT_TYPE_PIPE; } - // redirect stdout? - if (outpath) - { + 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)) - { + } 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) - { + 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) - { + } else if (outpipe) { attr.out.pipe = outpipe; attr.outtype = TB_PROCESS_REDIRECT_TYPE_PIPE; } // redirect stderr? - if (errpath) - { + 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)) - { + } 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) - { + 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) - { + } else if (errpipe) { attr.err.pipe = errpipe; attr.errtype = TB_PROCESS_REDIRECT_TYPE_PIPE; } // set the new environments - if (envn > 0) attr.envp = envs; + 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); + if (process) { + xm_lua_pushpointer(lua, (tb_pointer_t)process); + } else { + lua_pushnil(lua); + } return 1; } diff --git a/core/src/xmake/process/openv.c b/core/src/xmake/process/openv.c index 24a8cc112..1b95c45fe 100644 --- a/core/src/xmake/process/openv.c +++ b/core/src/xmake/process/openv.c @@ -22,16 +22,17 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "process.openv" -#define TB_TRACE_MODULE_DEBUG (0) +#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> +#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 /* ////////////////////////////////////////////////////////////////////////////////////// @@ -44,14 +45,11 @@ * infile = "", inpipe = "", inpipe = "", * envs = {"PATH=xxx", "XXX=yyy"}}) */ -tb_int_t xm_process_openv(lua_State* lua) -{ - // check +tb_int_t xm_process_openv(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // check argv - if (!lua_istable(lua, 2)) - { + if (!lua_istable(lua, 2)) { // error lua_pushfstring(lua, "invalid argv type(%s) for process.openv", luaL_typename(lua, 2)); lua_error(lua); @@ -59,7 +57,7 @@ tb_int_t xm_process_openv(lua_State* lua) } // get shellname - tb_char_t const* shellname = lua_tostring(lua, 1); + tb_char_t const *shellname = lua_tostring(lua, 1); tb_check_return_val(shellname, 0); // get the arguments count @@ -67,34 +65,29 @@ tb_int_t xm_process_openv(lua_State* lua) 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_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++) - { + 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)) - { + if (lua_isstring(lua, -1)) { // pass this argument argv[1 + argi] = lua_tostring(lua, -1); } // is path instance? - else if (lua_istable(lua, -1)) - { + 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 - { + } else { // error lua_pushfstring(lua, "invalid argv[%d] type(%s) for process.openv", (tb_int_t)argi, luaL_typename(lua, -1)); lua_error(lua); @@ -105,35 +98,36 @@ tb_int_t xm_process_openv(lua_State* lua) } // init attributes - tb_process_attr_t attr = {0}; + 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)) - { + 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)) + 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)) + if (lua_toboolean(lua, -1)) { exclusive = tb_true; + } lua_pop(lua, 1); // get curdir @@ -161,35 +155,31 @@ tb_int_t xm_process_openv(lua_State* lua) lua_pop(lua, 1); // get infile - if (!inpath) - { + if (!inpath) { lua_pushstring(lua, "infile"); lua_gettable(lua, 3); - infile = (xm_io_file_t*)lua_touserdata(lua, -1); + infile = (xm_io_file_t *)lua_touserdata(lua, -1); lua_pop(lua, 1); } // get outfile - if (!outpath) - { + if (!outpath) { lua_pushstring(lua, "outfile"); lua_gettable(lua, 3); - outfile = (xm_io_file_t*)lua_touserdata(lua, -1); + outfile = (xm_io_file_t *)lua_touserdata(lua, -1); lua_pop(lua, 1); } // get errfile - if (!errpath) - { + if (!errpath) { lua_pushstring(lua, "errfile"); lua_gettable(lua, 3); - errfile = (xm_io_file_t*)lua_touserdata(lua, -1); + errfile = (xm_io_file_t *)lua_touserdata(lua, -1); lua_pop(lua, 1); } // get inpipe - if (!inpath && !infile) - { + if (!inpath && !infile) { lua_pushstring(lua, "inpipe"); lua_gettable(lua, 3); inpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1); @@ -197,8 +187,7 @@ tb_int_t xm_process_openv(lua_State* lua) } // get outpipe - if (!outpath && !outfile) - { + if (!outpath && !outfile) { lua_pushstring(lua, "outpipe"); lua_gettable(lua, 3); outpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1); @@ -206,8 +195,7 @@ tb_int_t xm_process_openv(lua_State* lua) } // get errpipe - if (!errpath && !errfile) - { + if (!errpath && !errfile) { lua_pushstring(lua, "errpipe"); lua_gettable(lua, 3); errpipe = (tb_pipe_file_ref_t)lua_touserdata(lua, -1); @@ -217,36 +205,36 @@ tb_int_t xm_process_openv(lua_State* lua) // get environments lua_pushstring(lua, "envs"); lua_gettable(lua, 3); - if (lua_istable(lua, -1)) - { + 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++) - { + 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)) - { + if (lua_isstring(lua, -1)) { // add this environment value - if (envn + 1 < tb_arrayn(envs)) + if (envn + 1 < tb_arrayn(envs)) { envs[envn++] = lua_tostring(lua, -1); - else - { + } else { // error - lua_pushfstring(lua, "envs is too large(%d > %d) for process.openv", (tb_int_t)envn, tb_arrayn(envs) - 1); + lua_pushfstring(lua, + "envs is too large(%d > %d) for process.openv", + (tb_int_t)envn, + tb_arrayn(envs) - 1); lua_error(lua); } - } - else - { + } else { // error - lua_pushfstring(lua, "invalid envs[%d] type(%s) for process.openv", (tb_int_t)i, luaL_typename(lua, -1)); + lua_pushfstring(lua, + "invalid envs[%d] type(%s) for process.openv", + (tb_int_t)i, + luaL_typename(lua, -1)); lua_error(lua); } @@ -258,96 +246,88 @@ tb_int_t xm_process_openv(lua_State* lua) } // redirect stdin? - if (inpath) - { + 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)) - { + } 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) - { + 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) - { + } else if (inpipe) { attr.in.pipe = inpipe; attr.intype = TB_PROCESS_REDIRECT_TYPE_PIPE; } // redirect stdout? - if (outpath) - { + 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)) - { + } 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) - { + 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) - { + } else if (outpipe) { attr.out.pipe = outpipe; attr.outtype = TB_PROCESS_REDIRECT_TYPE_PIPE; } // redirect stderr? - if (errpath) - { + 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)) - { + } 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) - { + 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) - { + } else if (errpipe) { attr.err.pipe = errpipe; attr.errtype = TB_PROCESS_REDIRECT_TYPE_PIPE; } // set the new environments - if (envn > 0) attr.envp = envs; + 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); + if (exclusive) { + signal(SIGINT, SIG_IGN); + } #endif #if defined(SIGQUIT) - if (exclusive) signal(SIGQUIT, SIG_IGN); + 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); + if (process) { + xm_lua_pushpointer(lua, (tb_pointer_t)process); + } else { + lua_pushnil(lua); + } // exit argv - if (argv) tb_free(argv); + if (argv) { + tb_free(argv); + } argv = tb_null; - // ok return 1; } diff --git a/core/src/xmake/process/prefix.h b/core/src/xmake/process/prefix.h index 8389ccc65..74fb665d4 100644 --- a/core/src/xmake/process/prefix.h +++ b/core/src/xmake/process/prefix.h @@ -27,5 +27,3 @@ #include "../prefix.h" #endif - - diff --git a/core/src/xmake/process/wait.c b/core/src/xmake/process/wait.c index 055ae4fab..84d3b7079 100644 --- a/core/src/xmake/process/wait.c +++ b/core/src/xmake/process/wait.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "process.wait" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "process.wait" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,14 +35,11 @@ */ // ok, status = process.wait(proc, timeout) -tb_int_t xm_process_wait(lua_State* lua) -{ - // check +tb_int_t xm_process_wait(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)) { // error lua_pushfstring(lua, "invalid argument type(%s) for process.wait", luaL_typename(lua, 1)); lua_error(lua); @@ -58,12 +55,11 @@ tb_int_t xm_process_wait(lua_State* lua) // wait it tb_long_t status = 0; - tb_long_t ok = tb_process_wait(process, &status, timeout); + tb_long_t ok = tb_process_wait(process, &status, timeout); // save result lua_pushinteger(lua, ok); lua_pushinteger(lua, status); - // ok return 2; } diff --git a/core/src/xmake/readline/add_history.c b/core/src/xmake/readline/add_history.c index bac1aae24..8f0c1cf4d 100644 --- a/core/src/xmake/readline/add_history.c +++ b/core/src/xmake/readline/add_history.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "add_history" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "add_history" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,19 +36,16 @@ #ifdef XM_CONFIG_API_HAVE_READLINE // add_history wrapper -tb_int_t xm_readline_add_history(lua_State* lua) -{ - // check +tb_int_t xm_readline_add_history(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get history - tb_char_t const* history = luaL_checkstring(lua, 1); + 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 diff --git a/core/src/xmake/readline/clear_history.c b/core/src/xmake/readline/clear_history.c index 8582736f5..d29690bea 100644 --- a/core/src/xmake/readline/clear_history.c +++ b/core/src/xmake/readline/clear_history.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "clear_history" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "clear_history" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,20 +36,18 @@ #ifdef XM_CONFIG_API_HAVE_READLINE // clear_history wrapper -tb_int_t xm_readline_clear_history(lua_State* lua) -{ - // check +tb_int_t xm_readline_clear_history(lua_State *lua) { 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) + for (tb_int_t i = history_length - 1; i >= 0; --i) { remove_history(i); + } #else clear_history(); #endif - // ok return 0; } diff --git a/core/src/xmake/readline/history_list.c b/core/src/xmake/readline/history_list.c index c072cbd8d..45267edd5 100644 --- a/core/src/xmake/readline/history_list.c +++ b/core/src/xmake/readline/history_list.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "history_list" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "history_list" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -36,17 +36,14 @@ #ifdef XM_CONFIG_API_HAVE_READLINE // history_list wrapper -tb_int_t xm_readline_history_list(lua_State* lua) -{ - // check +tb_int_t xm_readline_history_list(lua_State *lua) { 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) - { + for (tb_int_t i = 1; i <= history_length; ++i) { lua_newtable(lua); // field line @@ -59,8 +56,7 @@ tb_int_t xm_readline_history_list(lua_State* lua) } #else tb_int_t i = 1; - for (HIST_ENTRY **p = history_list(); *p; ++p, ++i) - { + for (HIST_ENTRY **p = history_list(); *p; ++p, ++i) { lua_newtable(lua); // field line @@ -73,7 +69,6 @@ tb_int_t xm_readline_history_list(lua_State* lua) } #endif - // ok return 1; } diff --git a/core/src/xmake/readline/prefix.h b/core/src/xmake/readline/prefix.h index 0a67aad27..959ea17f3 100644 --- a/core/src/xmake/readline/prefix.h +++ b/core/src/xmake/readline/prefix.h @@ -26,12 +26,9 @@ */ #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> +#include <stdio.h> // on some OS (like centos) required +#include <readline/readline.h> +#include <readline/history.h> #endif - #endif - - diff --git a/core/src/xmake/readline/readline.c b/core/src/xmake/readline/readline.c index 1e1fdc240..fa3e631cc 100644 --- a/core/src/xmake/readline/readline.c +++ b/core/src/xmake/readline/readline.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "readline" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "readline" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -37,27 +37,24 @@ */ // readline wrapper -tb_int_t xm_readline_readline(lua_State* lua) -{ - // check +tb_int_t xm_readline_readline(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the prompt - tb_char_t const* prompt = luaL_optstring(lua, 1, tb_null); + tb_char_t const *prompt = luaL_optstring(lua, 1, tb_null); // call readline - tb_char_t* line = readline(prompt); - if (line) - { + tb_char_t *line = readline(prompt); + if (line) { // return line lua_pushstring(lua, line); // free it tb_free(line); + } else { + lua_pushnil(lua); } - else lua_pushnil(lua); - // ok return 1; } diff --git a/core/src/xmake/sandbox/interactive.c b/core/src/xmake/sandbox/interactive.c index 7c83ca06e..ac3566577 100644 --- a/core/src/xmake/sandbox/interactive.c +++ b/core/src/xmake/sandbox/interactive.c @@ -38,10 +38,10 @@ #include "prefix.h" #include "../winos/ansi.h" #ifdef XM_CONFIG_API_HAVE_READLINE -# include <readline/history.h> -# include <readline/readline.h> -# include <stdlib.h> -# include <stdio.h> // on some OS (like centos) required +#include <readline/history.h> +#include <readline/readline.h> +#include <stdlib.h> +#include <stdio.h> // on some OS (like centos) required #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -53,7 +53,7 @@ // for lua5.4 #ifndef LUA_QL -# define LUA_QL(x) "'" x "'" +#define LUA_QL(x) "'" x "'" #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -61,13 +61,13 @@ */ // report results -static tb_void_t xm_sandbox_report(lua_State *lua) -{ - if (!lua_isnil(lua, -1)) - { +static tb_void_t xm_sandbox_report(lua_State *lua) { + if (!lua_isnil(lua, -1)) { // get message - tb_char_t const* msg = lua_tostring(lua, -1); - if (!msg) msg = "(error object is not a string)"; + tb_char_t const *msg = lua_tostring(lua, -1); + if (!msg) { + msg = "(error object is not a string)"; + } // print it tb_printl(msg); @@ -79,13 +79,12 @@ static tb_void_t xm_sandbox_report(lua_State *lua) } // the traceback function -static tb_int_t xm_sandbox_traceback(lua_State *lua) -{ - if (!lua_isstring(lua, 1)) - { +static tb_int_t xm_sandbox_traceback(lua_State *lua) { + if (!lua_isstring(lua, 1)) { // non-string error object? try metamethod. - if (lua_isnoneornil(lua, 1) || !luaL_callmeta(lua, 1, "__tostring") || !lua_isstring(lua, -1)) + if (lua_isnoneornil(lua, 1) || !luaL_callmeta(lua, 1, "__tostring") || !lua_isstring(lua, -1)) { return 1; // return non-string error object. + } // replace object by result of __tostring metamethod. lua_remove(lua, 1); @@ -97,8 +96,7 @@ static tb_int_t xm_sandbox_traceback(lua_State *lua) } // execute codes -static tb_int_t xm_sandbox_docall(lua_State* lua, tb_int_t narg, tb_int_t clear) -{ +static tb_int_t xm_sandbox_docall(lua_State *lua, tb_int_t narg, tb_int_t clear) { /* get error function index * * stack: arg1(sandbox_scope) scriptfunc(top) -> ... @@ -122,23 +120,21 @@ static tb_int_t xm_sandbox_docall(lua_State* lua, tb_int_t narg, tb_int_t clear) lua_remove(lua, errfunc); // force a complete garbage collection in case of errors - if (status != 0) lua_gc(lua, LUA_GCCOLLECT, 0); + if (status != 0) { + lua_gc(lua, LUA_GCCOLLECT, 0); + } - // ok? return status; } // this line is incomplete? -static tb_int_t xm_sandbox_incomplete(lua_State *lua, tb_int_t status) -{ +static tb_int_t xm_sandbox_incomplete(lua_State *lua, tb_int_t status) { // syntax error? - if (status == LUA_ERRSYNTAX) - { + if (status == LUA_ERRSYNTAX) { size_t lmsg; - tb_char_t const* msg = lua_tolstring(lua, -1, &lmsg); - tb_char_t const* tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1); - if (tb_strstr(msg, LUA_QL("<eof>")) == tp) - { + tb_char_t const *msg = lua_tolstring(lua, -1, &lmsg); + tb_char_t const *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1); + if (tb_strstr(msg, LUA_QL("<eof>")) == tp) { lua_pop(lua, 1); return 1; } @@ -146,13 +142,11 @@ static tb_int_t xm_sandbox_incomplete(lua_State *lua, tb_int_t status) return 0; } // read line -static tb_size_t xm_sandbox_readline(tb_char_t* data, tb_size_t maxn, tb_char_t const* prompt) -{ +static tb_size_t xm_sandbox_readline(tb_char_t *data, tb_size_t maxn, tb_char_t const *prompt) { #ifdef XM_CONFIG_API_HAVE_READLINE // get line - tb_char_t const* line = readline(prompt); - if (line) - { + tb_char_t const *line = readline(prompt); + if (line) { // add line to history add_history(line); @@ -160,12 +154,13 @@ static tb_size_t xm_sandbox_readline(tb_char_t* data, tb_size_t maxn, tb_char_t tb_size_t size = tb_strlcpy(data, line, maxn); // free line - free((void*)line); + free((void *)line); // truncated? - if (size >= maxn) return 0; + if (size >= maxn) { + return 0; + } - // ok return size; } #else @@ -175,8 +170,9 @@ static tb_size_t xm_sandbox_readline(tb_char_t* data, tb_size_t maxn, tb_char_t tb_print_sync(); // get input buffer - if (tb_stdfile_gets(tb_stdfile_input(), data, maxn)) + if (tb_stdfile_gets(tb_stdfile_input(), data, maxn)) { return tb_strlen(data); + } #endif // no more input @@ -184,21 +180,19 @@ static tb_size_t xm_sandbox_readline(tb_char_t* data, tb_size_t maxn, tb_char_t } // read and push input line -static tb_int_t xm_sandbox_pushline(lua_State* lua, tb_char_t const* prompt2) -{ +static tb_int_t xm_sandbox_pushline(lua_State *lua, tb_char_t const *prompt2) { // read line tb_char_t data[LUA_PROMPT_BUFSIZE]; tb_size_t size = xm_sandbox_readline(data, sizeof(data), prompt2); - if (size) - { + if (size) { // split line '\0' - if (data[size - 1] == '\n') + if (data[size - 1] == '\n') { data[size - 1] = '\0'; + } // push line lua_pushstring(lua, data); - // ok return 1; } @@ -207,31 +201,30 @@ static tb_int_t xm_sandbox_pushline(lua_State* lua, tb_char_t const* prompt2) } // load code line -static tb_int_t xm_sandbox_loadline(lua_State* lua, tb_int_t top) -{ +static tb_int_t xm_sandbox_loadline(lua_State *lua, tb_int_t top) { // clear stack lua_settop(lua, top); // get prompt strings from arg1(sandbox_scope) lua_pushstring(lua, "$interactive_prompt"); lua_gettable(lua, 1); - tb_char_t const* prompt = lua_tostring(lua, -1); + tb_char_t const *prompt = lua_tostring(lua, -1); lua_pop(lua, 1); lua_pushstring(lua, "$interactive_prompt2"); lua_gettable(lua, 1); - tb_char_t const* prompt2 = lua_tostring(lua, -1); + tb_char_t const *prompt2 = lua_tostring(lua, -1); lua_pop(lua, 1); // read first line tb_int_t status; tb_char_t data[LUA_PROMPT_BUFSIZE]; tb_size_t size = xm_sandbox_readline(data + 7, sizeof(data) - 7, prompt); - if (size) - { + if (size) { // split line '\0' - if (data[size - 1] == '\n') + if (data[size - 1] == '\n') { data[--size] = '\0'; + } // patch "return " tb_memcpy(data, "return ", 7); @@ -239,20 +232,20 @@ static tb_int_t xm_sandbox_loadline(lua_State* lua, tb_int_t top) // attempt to load "return ..." status = luaL_loadbuffer(lua, data, size + 7, "=stdin"); - // ok? - if (status != LUA_ERRSYNTAX) return status; + if (status != LUA_ERRSYNTAX) { + return status; + } // pop error msg lua_pop(lua, 1); // push line to load it again lua_pushstring(lua, data + 7); - } - else return -1; + } else + return -1; // load input line - while (1) - { + while (1) { /* repeat until gets a complete line * * stack: arg1(sandbox_scope) scriptbuffer(top) -> ... @@ -261,14 +254,17 @@ static tb_int_t xm_sandbox_loadline(lua_State* lua, tb_int_t top) status = luaL_loadbuffer(lua, lua_tostring(lua, -1), (size_t)lua_strlen(lua, -1), "=stdin"); // complete? - if (!xm_sandbox_incomplete(lua, status)) break; + if (!xm_sandbox_incomplete(lua, status)) { + break; + } // get more input - if (!xm_sandbox_pushline(lua, prompt2)) return -1; + if (!xm_sandbox_pushline(lua, prompt2)) { + return -1; + } // cancel multi-line input? - if (!tb_strcmp(lua_tostring(lua, -1), "q")) - { + if (!tb_strcmp(lua_tostring(lua, -1), "q")) { lua_pop(lua, 2); lua_pushstring(lua, "return "); continue; @@ -301,9 +297,7 @@ static tb_int_t xm_sandbox_loadline(lua_State* lua, tb_int_t top) */ // sandbox.interactive() -tb_int_t xm_sandbox_interactive(lua_State* lua) -{ - // check +tb_int_t xm_sandbox_interactive(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); /* get init stack top @@ -314,11 +308,9 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) // enter interactive tb_int_t status; - while ((status = xm_sandbox_loadline(lua, top)) != -1) - { + while ((status = xm_sandbox_loadline(lua, top)) != -1) { // execute codes - if (status == 0) - { + if (status == 0) { /* bind sandbox * * stack: arg1(top) scriptfunc arg1(sandbox_scope) -> ... @@ -331,8 +323,11 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) lua_getfield(lua, 1, "$interactive_setfenv"); lua_pushvalue(lua, -2); lua_pushvalue(lua, 1); - if (lua_pcall(lua, 2, 0, 0) != 0) - tb_printl(lua_pushfstring(lua, "error calling " LUA_QL("$interactive_setfenv") " (%s)", lua_tostring(lua, -1))); + if (lua_pcall(lua, 2, 0, 0) != 0) { + tb_printl(lua_pushfstring(lua, + "error calling " LUA_QL("$interactive_setfenv") " (%s)", + lua_tostring(lua, -1))); + } #endif /* run script @@ -343,12 +338,12 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) } // report errors - if (status) + if (status) { xm_sandbox_report(lua); + } // print any results - if (status == 0 && lua_gettop(lua) > top) - { + if (status == 0 && lua_gettop(lua) > top) { // get results count tb_int_t count = lua_gettop(lua) - top; @@ -359,8 +354,10 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) */ lua_getfield(lua, 1, "$interactive_dump"); // load $interactive_dump() from sandbox_scope lua_insert(lua, -(count + 1)); - if (lua_pcall(lua, count, 0, 0) != 0) - tb_printl(lua_pushfstring(lua, "error calling " LUA_QL("$interactive_dump") " (%s)", lua_tostring(lua, -1))); + if (lua_pcall(lua, count, 0, 0) != 0) { + tb_printl( + lua_pushfstring(lua, "error calling " LUA_QL("$interactive_dump") " (%s)", lua_tostring(lua, -1))); + } } } @@ -369,6 +366,5 @@ tb_int_t xm_sandbox_interactive(lua_State* lua) tb_printl(""); tb_print_sync(); - // end return 0; } diff --git a/core/src/xmake/sandbox/prefix.h b/core/src/xmake/sandbox/prefix.h index 5643b16f3..5c183bb7c 100644 --- a/core/src/xmake/sandbox/prefix.h +++ b/core/src/xmake/sandbox/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/semver/compare.c b/core/src/xmake/semver/compare.c index 56139b934..8310b73fc 100644 --- a/core/src/xmake/semver/compare.c +++ b/core/src/xmake/semver/compare.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "compare" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "compare" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,33 +35,28 @@ */ // semver.compare("v1.0.1-beta", "1.2") > 0? -tb_int_t xm_semver_compare(lua_State* lua) -{ - // check +tb_int_t xm_semver_compare(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the version1 string - tb_char_t const* version1_str = luaL_checkstring(lua, 1); + 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_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))) - { + 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))) - { + 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; @@ -70,7 +65,6 @@ tb_int_t xm_semver_compare(lua_State* lua) // do compare lua_pushinteger(lua, semver_pcmp(&semver1, &semver2)); - // end semver_dtor(&semver1); semver_dtor(&semver2); return 1; diff --git a/core/src/xmake/semver/parse.c b/core/src/xmake/semver/parse.c index 1f458462d..63bd47f93 100644 --- a/core/src/xmake/semver/parse.c +++ b/core/src/xmake/semver/parse.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "parse" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "parse" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -54,25 +54,21 @@ } * */ -tb_int_t xm_semver_parse(lua_State* lua) -{ - // check +tb_int_t xm_semver_parse(lua_State *lua) { 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 *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))) - { + 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; diff --git a/core/src/xmake/semver/prefix.h b/core/src/xmake/semver/prefix.h index 2c365a73c..5d9f15b1d 100644 --- a/core/src/xmake/semver/prefix.h +++ b/core/src/xmake/semver/prefix.h @@ -42,7 +42,8 @@ __tb_extern_c_enter__ * @param semver the semver struct * */ -tb_void_t lua_pushsemver(lua_State *lua, semver_t const* semver); +tb_void_t +lua_pushsemver(lua_State *lua, semver_t const *semver); /* ////////////////////////////////////////////////////////////////////////////////////// * leave diff --git a/core/src/xmake/semver/satisfies.c b/core/src/xmake/semver/satisfies.c index 2c815e4dd..a7c47f4fa 100644 --- a/core/src/xmake/semver/satisfies.c +++ b/core/src/xmake/semver/satisfies.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "satisfies" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "satisfies" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -38,48 +38,38 @@ * * 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_int_t xm_semver_satisfies(lua_State *lua) { 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_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))) - { + 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)) - { + 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))) - { + 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 - { + } else { semver_dtor(&range_semver); lua_pushnil(lua); lua_pushfstring(lua, "unable to parse semver '%s'", version_str); return 2; } - } - else - { + } else { lua_pushnil(lua); lua_pushfstring(lua, "unable to parse semver range '%s'", range_str); return 2; @@ -87,9 +77,8 @@ tb_int_t xm_semver_satisfies(lua_State* lua) } // try to parse the version string - semver_t semver = {0}; - if (semver_tryn(&semver, version_str, tb_strlen(version_str))) - { + 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; diff --git a/core/src/xmake/semver/select.c b/core/src/xmake/semver/select.c index 762fd848f..de3904288 100644 --- a/core/src/xmake/semver/select.c +++ b/core/src/xmake/semver/select.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "select" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "select" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,24 +33,25 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * 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) -{ +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) - { + 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); + 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); } @@ -69,53 +70,50 @@ static tb_bool_t xm_semver_select_from_versions_tags1(lua_State* lua, tb_int_t f lua_pushstring(lua, top.raw); lua_setfield(lua, -2, "version"); - lua_pushstring(lua, fromidx == 2? "version" : "tag"); + 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) -{ +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) - { + 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); + 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) - { + 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_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) -{ +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) - { + 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); + 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) - { + 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"); @@ -126,22 +124,24 @@ static tb_bool_t xm_semver_select_from_branches(lua_State* lua, tb_int_t fromidx } 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) -{ +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) - { + 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) + 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); } @@ -158,14 +158,13 @@ static tb_bool_t xm_semver_select_latest_from_versions_tags(lua_State* lua, tb_i lua_pushstring(lua, top.raw); lua_setfield(lua, -2, "version"); - lua_pushstring(lua, fromidx == 2? "version" : "tag"); + lua_pushstring(lua, fromidx == 2 ? "version" : "tag"); lua_setfield(lua, -2, "source"); semver_dtor(&top); return tb_true; } - /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ @@ -174,20 +173,17 @@ static tb_bool_t xm_semver_select_latest_from_versions_tags(lua_State* lua, tb_i * * 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_int_t xm_semver_select(lua_State *lua) { 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 - { + 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); @@ -197,59 +193,48 @@ tb_int_t xm_semver_select(lua_State* lua) // parse the version range string is_range = semver_rangen(&range, range_str, range_len) == 0; - if (is_range) - { + if (is_range) { // attempt to select version from the versions list first - if (xm_semver_select_from_versions_tags1(lua, 2, &semver, &range, &matches)) - { + 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)) - { + if (xm_semver_select_from_versions_tags1(lua, 3, &semver, &range, &matches)) { ok = tb_true; break; } - } - else - { + } else { // attempt to select version from the versions list first - if (xm_semver_select_from_versions_tags2(lua, 2, &semver, range_str, range_len)) - { + 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)) - { + 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)) - { + 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")) - { + 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)) - { + 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)) - { + if (xm_semver_select_latest_from_versions_tags(lua, 3, &semver, &matches)) { ok = tb_true; break; } @@ -264,10 +249,8 @@ tb_int_t xm_semver_select(lua_State* lua) semver_range_dtor(&range); // failed? - if (!ok) - { - if (!is_range) - { + if (!ok) { + if (!is_range) { lua_pushnil(lua); lua_pushfstring(lua, "unable to parse semver range '%s'", range_str); } @@ -277,6 +260,5 @@ tb_int_t xm_semver_select(lua_State* lua) return 2; } - // ok return 1; } diff --git a/core/src/xmake/semver/semver.c b/core/src/xmake/semver/semver.c index dcc1b9302..201f83435 100644 --- a/core/src/xmake/semver/semver.c +++ b/core/src/xmake/semver/semver.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "semver" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "semver" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,9 +33,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_void_t lua_pushsemver(lua_State *lua, semver_t const* semver) -{ - // check +tb_void_t lua_pushsemver(lua_State *lua, semver_t const *semver) { tb_assert(lua && semver); // return a semver table @@ -60,12 +58,14 @@ tb_void_t lua_pushsemver(lua_State *lua, semver_t const* semver) 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); + 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); } @@ -76,10 +76,12 @@ tb_void_t lua_pushsemver(lua_State *lua, semver_t const* semver) 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); + 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); } diff --git a/core/src/xmake/string/convert.c b/core/src/xmake/string/convert.c index 0001b7f9c..b3adaf8b8 100644 --- a/core/src/xmake/string/convert.c +++ b/core/src/xmake/string/convert.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "convert" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "convert" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,54 +35,52 @@ */ // the charset entry type -typedef struct __xm_charset_entry_t -{ +typedef struct __xm_charset_entry_t { // the charset type - tb_size_t type; + tb_size_t type; // the charset name - tb_char_t const* name; - -}xm_charset_entry_t, *xm_charset_entry_ref_t; + 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" } +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 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) -{ +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_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)) + if (itor != tb_iterator_tail(iterator)) { return (xm_charset_entry_ref_t)tb_iterator_item(iterator, itor); - else return tb_null; + } else { + return tb_null; + } } /* ////////////////////////////////////////////////////////////////////////////////////// @@ -100,16 +98,14 @@ static xm_charset_entry_ref_t xm_string_charset_find_by_name(tb_char_t const* na * local result = string.convert(str, "utf8", "gb2312") * @endcode */ -tb_int_t xm_string_convert(lua_State* lua) -{ - // check +tb_int_t xm_string_convert(lua_State *lua) { 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_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 @@ -120,21 +116,27 @@ tb_int_t xm_string_convert(lua_State* lua) tb_check_return_val(fcharset && tcharset, 0); // empty string? - if (!src_size) lua_pushstring(lua, ""); - else - { + 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) - { + 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); } - else lua_pushnil(lua); tb_free(dst_data); } - // ok return 1; } diff --git a/core/src/xmake/string/endswith.c b/core/src/xmake/string/endswith.c index e6bb8b9c4..f7e5e1805 100644 --- a/core/src/xmake/string/endswith.c +++ b/core/src/xmake/string/endswith.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "endswith" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "endswith" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,18 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_string_endswith(lua_State* lua) -{ - // check +tb_int_t xm_string_endswith(lua_State *lua) { 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); + 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; } diff --git a/core/src/xmake/string/lastof.c b/core/src/xmake/string/lastof.c index 8e6f42300..8785e3987 100644 --- a/core/src/xmake/string/lastof.c +++ b/core/src/xmake/string/lastof.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "string_lastof" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "string_lastof" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,16 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * 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) -{ +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 - { + 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) - { + if (next) { curr = next; next += nsubstr; } @@ -50,14 +48,19 @@ static tb_void_t xm_string_lastof_str(lua_State* lua, tb_char_t const* cstr, tb_ } while (!next); // found? - if (curr) lua_pushinteger(lua, curr - cstr + 1); - else lua_pushnil(lua); + 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); +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); + } } /* ////////////////////////////////////////////////////////////////////////////////////// @@ -69,22 +72,23 @@ static tb_void_t xm_string_lastof_chr(lua_State* lua, tb_char_t const* cstr, tb_ * @param str the string * @param substr the substring */ -tb_int_t xm_string_lastof(lua_State* lua) -{ - // check +tb_int_t xm_string_lastof(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get string size_t nstr = 0; - tb_char_t const* cstr = luaL_checklstring(lua, 1, &nstr); + 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); + 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); + 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; } diff --git a/core/src/xmake/string/prefix.h b/core/src/xmake/string/prefix.h index 8f9e53d94..890fc368c 100644 --- a/core/src/xmake/string/prefix.h +++ b/core/src/xmake/string/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/string/split.c b/core/src/xmake/string/split.c index c035a5fab..5ca0c92bb 100644 --- a/core/src/xmake/string/split.c +++ b/core/src/xmake/string/split.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "string_split" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "string_split" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,62 +33,59 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * 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) +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); + pos = tb_strstr(cstr, cdls); } - if (cstr < end) - { + if (cstr < end) { lua_pushlstring(lua, cstr, end - cstr); lua_rawseti(lua, -2, ++num); - } - else if (strict && (limit < 0 || num < limit) && cstr == end) - { + } 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) +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); + pos = tb_strchr(cstr, ch); } - if (cstr < end) - { + if (cstr < end) { lua_pushlstring(lua, cstr, end - cstr); lua_rawseti(lua, -2, ++num); - } - else if (strict && (limit < 0 || num < limit) && cstr == end) - { + } else if (strict && (limit < 0 || num < limit) && cstr == end) { lua_pushliteral(lua, ""); lua_rawseti(lua, -2, ++num); } @@ -105,28 +102,29 @@ static tb_void_t xm_string_split_chr(lua_State* lua, tb_char_t const* cstr, tb_s * @param strict is strict? * @param limit the limit count */ -tb_int_t xm_string_split(lua_State* lua) -{ - // check +tb_int_t xm_string_split(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get string size_t nstr = 0; - tb_char_t const* cstr = luaL_checklstring(lua, 1, &nstr); + 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); + tb_char_t const *cdls = luaL_checklstring(lua, 2, &ndls); // is strict? - tb_bool_t const strict = (tb_bool_t)lua_toboolean(lua, 3); + 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); + 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); + 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; } diff --git a/core/src/xmake/string/startswith.c b/core/src/xmake/string/startswith.c index d904de3cb..04b53ffa7 100644 --- a/core/src/xmake/string/startswith.c +++ b/core/src/xmake/string/startswith.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "startswith" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "startswith" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,20 +33,17 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_string_startswith(lua_State* lua) -{ - // check +tb_int_t xm_string_startswith(lua_State *lua) { 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); + 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; } diff --git a/core/src/xmake/string/trim.c b/core/src/xmake/string/trim.c index 42a4b846c..54e6e4b5a 100644 --- a/core/src/xmake/string/trim.c +++ b/core/src/xmake/string/trim.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "string_trim" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "string_trim" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,25 +33,25 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * 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 +static tb_void_t xm_string_trim_space(tb_char_t const **psstr, tb_char_t const **pestr, tb_int_t mode) { tb_assert(psstr && pestr && *psstr && *pestr); - tb_char_t const* p = *psstr; - tb_char_t const* e = *pestr; + tb_char_t const *p = *psstr; + tb_char_t const *e = *pestr; // trim left? - if (mode <= 0) - while (p < e && tb_isspace(*p)) + if (mode <= 0) { + while (p < e && tb_isspace(*p)) { p++; + } + } // trim right - if (mode >= 0) - { + if (mode >= 0) { e--; - while (e >= p && tb_isspace(*e)) + while (e >= p && tb_isspace(*e)) { e--; + } e++; } @@ -60,25 +60,29 @@ static tb_void_t xm_string_trim_space(tb_char_t const** psstr, tb_char_t const** *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 +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) { tb_assert(sstr && estr && ctrim); - tb_char_t const* p = sstr; - while (p < estr && tb_strnchr(ctrim, ntrim, *p)) + 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 +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) { tb_assert(sstr && estr && ctrim); - tb_char_t const* p = estr - 1; - while (p >= sstr && tb_strnchr(ctrim, ntrim, *p)) + tb_char_t const *p = estr - 1; + while (p >= sstr && tb_strnchr(ctrim, ntrim, *p)) { p--; + } return p + 1; } @@ -97,36 +101,35 @@ static tb_char_t const* xm_string_rtrim(tb_char_t const* sstr, tb_char_t const* * local result = string.trim(str, "(", -1) * @endcode */ -tb_int_t xm_string_trim(lua_State* lua) -{ - // check +tb_int_t xm_string_trim(lua_State *lua) { 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_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 - { + 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) + 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 - { + } 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); + 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; diff --git a/core/src/xmake/thread/event_exit.c b/core/src/xmake/thread/event_exit.c index 54e977068..a9b16db6c 100644 --- a/core/src/xmake/thread/event_exit.c +++ b/core/src/xmake/thread/event_exit.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_event_exit.c + * @file event_exit.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_event" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_event_exit(lua_State* lua) -{ +tb_int_t xm_thread_event_exit(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + xm_thread_event_t *thread_event = xm_thread_event_get(lua, 1); tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); - if (tb_atomic_fetch_and_sub(&thread_event->refn, 1) == 1) - { - if (thread_event->handle) - { + if (tb_atomic_fetch_and_sub(&thread_event->refn, 1) == 1) { + if (thread_event->handle) { tb_event_exit(thread_event->handle); thread_event->handle = tb_null; } @@ -52,4 +49,3 @@ tb_int_t xm_thread_event_exit(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/event_incref.c b/core/src/xmake/thread/event_incref.c index ff8b658d9..9a2b6163a 100644 --- a/core/src/xmake/thread/event_incref.c +++ b/core/src/xmake/thread/event_incref.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_event_incref.c + * @file event_incref.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_event" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_event_incref(lua_State* lua) -{ +tb_int_t xm_thread_event_incref(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + xm_thread_event_t *thread_event = xm_thread_event_get(lua, 1); tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_event->refn, 1) >= 1); return 1; } - diff --git a/core/src/xmake/thread/event_init.c b/core/src/xmake/thread/event_init.c index 942537718..d661939b0 100644 --- a/core/src/xmake/thread/event_init.c +++ b/core/src/xmake/thread/event_init.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_event" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_event_init(lua_State* lua) -{ +tb_int_t xm_thread_event_init(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_bool_t ok = tb_false; - xm_thread_event_t* thread_event = tb_null; - do - { + tb_bool_t ok = tb_false; + xm_thread_event_t *thread_event = tb_null; + do { thread_event = tb_malloc0_type(xm_thread_event_t); tb_assert_and_check_break(thread_event); - thread_event->refn = 1; + thread_event->refn = 1; thread_event->handle = tb_event_init(); tb_assert_and_check_break(thread_event->handle); @@ -53,12 +51,9 @@ tb_int_t xm_thread_event_init(lua_State* lua) } while (0); - if (!ok) - { - if (thread_event) - { - if (thread_event->handle) - { + if (!ok) { + if (thread_event) { + if (thread_event->handle) { tb_event_exit(thread_event->handle); thread_event->handle = tb_null; } diff --git a/core/src/xmake/thread/event_post.c b/core/src/xmake/thread/event_post.c index 0b8e5dabd..fea3fdd82 100644 --- a/core/src/xmake/thread/event_post.c +++ b/core/src/xmake/thread/event_post.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_event_lock.c + * @file event_post.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_event" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_event_post(lua_State* lua) -{ +tb_int_t xm_thread_event_post(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + xm_thread_event_t *thread_event = xm_thread_event_get(lua, 1); tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); lua_pushboolean(lua, tb_event_post(thread_event->handle)); return 1; } - diff --git a/core/src/xmake/thread/event_wait.c b/core/src/xmake/thread/event_wait.c index d6d628311..ae4453d5e 100644 --- a/core/src/xmake/thread/event_wait.c +++ b/core/src/xmake/thread/event_wait.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_event_wait.c + * @file event_wait.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_event" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_event" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,15 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_event_wait(lua_State* lua) -{ +tb_int_t xm_thread_event_wait(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_event_t* thread_event = xm_thread_event_get(lua, 1); + xm_thread_event_t *thread_event = xm_thread_event_get(lua, 1); tb_assert_and_check_return_val(thread_event && thread_event->handle, 0); tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 2); lua_pushinteger(lua, tb_event_wait(thread_event->handle, timeout)); return 1; } - diff --git a/core/src/xmake/thread/mutex_exit.c b/core/src/xmake/thread/mutex_exit.c index 39d69ab8a..6d79e6197 100644 --- a/core/src/xmake/thread/mutex_exit.c +++ b/core/src/xmake/thread/mutex_exit.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_mutex_exit.c + * @file mutex_exit.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_mutex" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_mutex_exit(lua_State* lua) -{ +tb_int_t xm_thread_mutex_exit(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_mutex_t* thread_mutex = xm_thread_mutex_get(lua, 1); + xm_thread_mutex_t *thread_mutex = xm_thread_mutex_get(lua, 1); tb_assert_and_check_return_val(thread_mutex && thread_mutex->handle, 0); - if (tb_atomic_fetch_and_sub(&thread_mutex->refn, 1) == 1) - { - if (thread_mutex->handle) - { + if (tb_atomic_fetch_and_sub(&thread_mutex->refn, 1) == 1) { + if (thread_mutex->handle) { tb_mutex_exit(thread_mutex->handle); thread_mutex->handle = tb_null; } @@ -52,4 +49,3 @@ tb_int_t xm_thread_mutex_exit(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/mutex_incref.c b/core/src/xmake/thread/mutex_incref.c index 34a234757..d76f49daf 100644 --- a/core/src/xmake/thread/mutex_incref.c +++ b/core/src/xmake/thread/mutex_incref.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_mutex_incref.c + * @file mutex_incref.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_mutex" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_mutex_incref(lua_State* lua) -{ +tb_int_t xm_thread_mutex_incref(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_mutex_t* thread_mutex = xm_thread_mutex_get(lua, 1); + xm_thread_mutex_t *thread_mutex = xm_thread_mutex_get(lua, 1); tb_assert_and_check_return_val(thread_mutex && thread_mutex->handle, 0); lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_mutex->refn, 1) >= 1); return 1; } - diff --git a/core/src/xmake/thread/mutex_init.c b/core/src/xmake/thread/mutex_init.c index ff69e82ad..bca78a134 100644 --- a/core/src/xmake/thread/mutex_init.c +++ b/core/src/xmake/thread/mutex_init.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_mutex" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_mutex_init(lua_State* lua) -{ +tb_int_t xm_thread_mutex_init(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_bool_t ok = tb_false; - xm_thread_mutex_t* thread_mutex = tb_null; - do - { + tb_bool_t ok = tb_false; + xm_thread_mutex_t *thread_mutex = tb_null; + do { thread_mutex = tb_malloc0_type(xm_thread_mutex_t); tb_assert_and_check_break(thread_mutex); - thread_mutex->refn = 1; + thread_mutex->refn = 1; thread_mutex->handle = tb_mutex_init(); tb_assert_and_check_break(thread_mutex->handle); @@ -53,12 +51,9 @@ tb_int_t xm_thread_mutex_init(lua_State* lua) } while (0); - if (!ok) - { - if (thread_mutex) - { - if (thread_mutex->handle) - { + if (!ok) { + if (thread_mutex) { + if (thread_mutex->handle) { tb_mutex_exit(thread_mutex->handle); thread_mutex->handle = tb_null; } diff --git a/core/src/xmake/thread/mutex_lock.c b/core/src/xmake/thread/mutex_lock.c index 380301da1..711c3a07b 100644 --- a/core/src/xmake/thread/mutex_lock.c +++ b/core/src/xmake/thread/mutex_lock.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_mutex_lock.c + * @file mutex_lock.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_mutex" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_mutex_lock(lua_State* lua) -{ +tb_int_t xm_thread_mutex_lock(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_mutex_t* thread_mutex = xm_thread_mutex_get(lua, 1); + xm_thread_mutex_t *thread_mutex = xm_thread_mutex_get(lua, 1); tb_assert_and_check_return_val(thread_mutex && thread_mutex->handle, 0); lua_pushboolean(lua, tb_mutex_enter(thread_mutex->handle)); return 1; } - diff --git a/core/src/xmake/thread/mutex_trylock.c b/core/src/xmake/thread/mutex_trylock.c index 7b83f3a90..7149a2b62 100644 --- a/core/src/xmake/thread/mutex_trylock.c +++ b/core/src/xmake/thread/mutex_trylock.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_mutex_trylock.c + * @file mutex_trylock.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_mutex" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_mutex_trylock(lua_State* lua) -{ +tb_int_t xm_thread_mutex_trylock(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_mutex_t* thread_mutex = xm_thread_mutex_get(lua, 1); + xm_thread_mutex_t *thread_mutex = xm_thread_mutex_get(lua, 1); tb_assert_and_check_return_val(thread_mutex && thread_mutex->handle, 0); lua_pushboolean(lua, tb_mutex_enter_try(thread_mutex->handle)); return 1; } - diff --git a/core/src/xmake/thread/mutex_unlock.c b/core/src/xmake/thread/mutex_unlock.c index beb0c9e0a..a73d04dc1 100644 --- a/core/src/xmake/thread/mutex_unlock.c +++ b/core/src/xmake/thread/mutex_unlock.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_mutex_unlock.c + * @file mutex_unlock.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_mutex" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_mutex" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_mutex_unlock(lua_State* lua) -{ +tb_int_t xm_thread_mutex_unlock(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_mutex_t* thread_mutex = xm_thread_mutex_get(lua, 1); + xm_thread_mutex_t *thread_mutex = xm_thread_mutex_get(lua, 1); tb_assert_and_check_return_val(thread_mutex && thread_mutex->handle, 0); lua_pushboolean(lua, tb_mutex_leave(thread_mutex->handle)); return 1; } - diff --git a/core/src/xmake/thread/prefix.h b/core/src/xmake/thread/prefix.h index 2b841e6a7..2ddd14adf 100644 --- a/core/src/xmake/thread/prefix.h +++ b/core/src/xmake/thread/prefix.h @@ -31,126 +31,117 @@ */ // the thread type -typedef struct __xm_thread_t -{ +typedef struct __xm_thread_t { tb_thread_ref_t handle; tb_string_t callback; tb_string_t callinfo; - -}xm_thread_t; +} xm_thread_t; // the thread value kind -typedef enum __xm_thread_value_kind_e -{ +typedef enum __xm_thread_value_kind_e { XM_THREAD_VALUE_NIL = 0, XM_THREAD_VALUE_BOOL = 1, XM_THREAD_VALUE_INT = 2, XM_THREAD_VALUE_NUM = 3, XM_THREAD_VALUE_STR = 4 - -}xm_thread_value_kind_e; +} xm_thread_value_kind_e; // the thread value type -typedef struct __xm_thread_value_t -{ - tb_uint32_t kind : 3; - tb_uint32_t size : 29; - union - { - tb_char_t* string; +typedef struct __xm_thread_value_t { + tb_uint32_t kind : 3; + tb_uint32_t size : 29; + union { + tb_char_t *string; tb_bool_t boolean; lua_Integer integer; lua_Number number; } u; - -}xm_thread_value_t; +} xm_thread_value_t; // the thread event type -typedef struct __xm_thread_event_t -{ - tb_event_ref_t handle; - tb_atomic_t refn; - -}xm_thread_event_t; +typedef struct __xm_thread_event_t { + tb_event_ref_t handle; + tb_atomic_t refn; +} xm_thread_event_t; // the thread mutex type -typedef struct __xm_thread_mutex_t -{ - tb_mutex_ref_t handle; - tb_atomic_t refn; - -}xm_thread_mutex_t; +typedef struct __xm_thread_mutex_t { + tb_mutex_ref_t handle; + tb_atomic_t refn; +} xm_thread_mutex_t; // the thread semaphore type -typedef struct __xm_thread_semaphore_t -{ +typedef struct __xm_thread_semaphore_t { tb_semaphore_ref_t handle; tb_atomic_t refn; - -}xm_thread_semaphore_t; +} xm_thread_semaphore_t; // the thread queue type -typedef struct __xm_thread_queue_t -{ - tb_queue_ref_t handle; - tb_atomic_t refn; - -}xm_thread_queue_t; +typedef struct __xm_thread_queue_t { + tb_queue_ref_t handle; + tb_atomic_t refn; +} xm_thread_queue_t; // the thread sharedata type -typedef struct __xm_thread_sharedata_t -{ +typedef struct __xm_thread_sharedata_t { xm_thread_value_t value; tb_buffer_t buffer; tb_atomic_t refn; - -}xm_thread_sharedata_t; +} xm_thread_sharedata_t; // get the thread event from arguments -static __tb_inline__ xm_thread_event_t* xm_thread_event_get(lua_State* lua, tb_int_t index) -{ - xm_thread_event_t* thread_event = tb_null; - if (xm_lua_isinteger(lua, index)) thread_event = (xm_thread_event_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); - else if (xm_lua_ispointer(lua, index)) thread_event = (xm_thread_event_t*)xm_lua_topointer(lua, index); +static __tb_inline__ xm_thread_event_t *xm_thread_event_get(lua_State *lua, tb_int_t index) { + xm_thread_event_t *thread_event = tb_null; + if (xm_lua_isinteger(lua, index)) { + thread_event = (xm_thread_event_t *)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + } else if (xm_lua_ispointer(lua, index)) { + thread_event = (xm_thread_event_t *)xm_lua_topointer(lua, index); + } return thread_event; } // get the thread mutex from arguments -static __tb_inline__ xm_thread_mutex_t* xm_thread_mutex_get(lua_State* lua, tb_int_t index) -{ - xm_thread_mutex_t* thread_mutex = tb_null; - if (xm_lua_isinteger(lua, index)) thread_mutex = (xm_thread_mutex_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); - else if (xm_lua_ispointer(lua, index)) thread_mutex = (xm_thread_mutex_t*)xm_lua_topointer(lua, index); +static __tb_inline__ xm_thread_mutex_t *xm_thread_mutex_get(lua_State *lua, tb_int_t index) { + xm_thread_mutex_t *thread_mutex = tb_null; + if (xm_lua_isinteger(lua, index)) { + thread_mutex = (xm_thread_mutex_t *)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + } else if (xm_lua_ispointer(lua, index)) { + thread_mutex = (xm_thread_mutex_t *)xm_lua_topointer(lua, index); + } return thread_mutex; } // get the thread semaphore from arguments -static __tb_inline__ xm_thread_semaphore_t* xm_thread_semaphore_get(lua_State* lua, tb_int_t index) -{ - xm_thread_semaphore_t* thread_semaphore = tb_null; - if (xm_lua_isinteger(lua, index)) thread_semaphore = (xm_thread_semaphore_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); - else if (xm_lua_ispointer(lua, index)) thread_semaphore = (xm_thread_semaphore_t*)xm_lua_topointer(lua, index); +static __tb_inline__ xm_thread_semaphore_t *xm_thread_semaphore_get(lua_State *lua, tb_int_t index) { + xm_thread_semaphore_t *thread_semaphore = tb_null; + if (xm_lua_isinteger(lua, index)) { + thread_semaphore = (xm_thread_semaphore_t *)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + } else if (xm_lua_ispointer(lua, index)) { + thread_semaphore = (xm_thread_semaphore_t *)xm_lua_topointer(lua, index); + } return thread_semaphore; } // get the thread queue from arguments -static __tb_inline__ xm_thread_queue_t* xm_thread_queue_get(lua_State* lua, tb_int_t index) -{ - xm_thread_queue_t* thread_queue = tb_null; - if (xm_lua_isinteger(lua, index)) thread_queue = (xm_thread_queue_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); - else if (xm_lua_ispointer(lua, index)) thread_queue = (xm_thread_queue_t*)xm_lua_topointer(lua, index); +static __tb_inline__ xm_thread_queue_t *xm_thread_queue_get(lua_State *lua, tb_int_t index) { + xm_thread_queue_t *thread_queue = tb_null; + if (xm_lua_isinteger(lua, index)) { + thread_queue = (xm_thread_queue_t *)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + } else if (xm_lua_ispointer(lua, index)) { + thread_queue = (xm_thread_queue_t *)xm_lua_topointer(lua, index); + } return thread_queue; } // get the thread sharedata from arguments -static __tb_inline__ xm_thread_sharedata_t* xm_thread_sharedata_get(lua_State* lua, tb_int_t index) -{ - xm_thread_sharedata_t* thread_sharedata = tb_null; - if (xm_lua_isinteger(lua, index)) thread_sharedata = (xm_thread_sharedata_t*)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); - else if (xm_lua_ispointer(lua, index)) thread_sharedata = (xm_thread_sharedata_t*)xm_lua_topointer(lua, index); +static __tb_inline__ xm_thread_sharedata_t *xm_thread_sharedata_get(lua_State *lua, tb_int_t index) { + xm_thread_sharedata_t *thread_sharedata = tb_null; + if (xm_lua_isinteger(lua, index)) { + thread_sharedata = (xm_thread_sharedata_t *)(tb_size_t)(tb_long_t)lua_tointeger(lua, index); + } else if (xm_lua_ispointer(lua, index)) { + thread_sharedata = (xm_thread_sharedata_t *)xm_lua_topointer(lua, index); + } return thread_sharedata; } #endif - - diff --git a/core/src/xmake/thread/queue_clear.c b/core/src/xmake/thread/queue_clear.c index 4888babe2..533c035ed 100644 --- a/core/src/xmake/thread/queue_clear.c +++ b/core/src/xmake/thread/queue_clear.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_queue_clear.c + * @file queue_clear.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,15 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_clear(lua_State* lua) -{ +tb_int_t xm_thread_queue_clear(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + xm_thread_queue_t *thread_queue = xm_thread_queue_get(lua, 1); tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); tb_queue_clear(thread_queue->handle); lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/queue_exit.c b/core/src/xmake/thread/queue_exit.c index 8178ea22f..de051250e 100644 --- a/core/src/xmake/thread/queue_exit.c +++ b/core/src/xmake/thread/queue_exit.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_queue_exit.c + * @file queue_exit.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_exit(lua_State* lua) -{ +tb_int_t xm_thread_queue_exit(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + xm_thread_queue_t *thread_queue = xm_thread_queue_get(lua, 1); tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); - if (tb_atomic_fetch_and_sub(&thread_queue->refn, 1) == 1) - { - if (thread_queue->handle) - { + if (tb_atomic_fetch_and_sub(&thread_queue->refn, 1) == 1) { + if (thread_queue->handle) { tb_queue_exit(thread_queue->handle); thread_queue->handle = tb_null; } @@ -52,4 +49,3 @@ tb_int_t xm_thread_queue_exit(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/queue_incref.c b/core/src/xmake/thread/queue_incref.c index 4f06807c4..2322ce241 100644 --- a/core/src/xmake/thread/queue_incref.c +++ b/core/src/xmake/thread/queue_incref.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_queue_incref.c + * @file queue_incref.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_incref(lua_State* lua) -{ +tb_int_t xm_thread_queue_incref(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + xm_thread_queue_t *thread_queue = xm_thread_queue_get(lua, 1); tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_queue->refn, 1) >= 1); return 1; } - diff --git a/core/src/xmake/thread/queue_init.c b/core/src/xmake/thread/queue_init.c index 0c8af884d..a0d683dd3 100644 --- a/core/src/xmake/thread/queue_init.c +++ b/core/src/xmake/thread/queue_init.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t xm_thread_value_free(tb_element_ref_t element, tb_pointer_t buff) -{ - xm_thread_value_t* item = (xm_thread_value_t*)buff; - if (item) - { - if (item->kind == XM_THREAD_VALUE_STR) - { - if (item->u.string) tb_free((tb_pointer_t)item->u.string); +static tb_void_t xm_thread_value_free(tb_element_ref_t element, tb_pointer_t buff) { + xm_thread_value_t *item = (xm_thread_value_t *)buff; + if (item) { + if (item->kind == XM_THREAD_VALUE_STR) { + if (item->u.string) { + tb_free((tb_pointer_t)item->u.string); + } item->u.string = tb_null; } item->size = 0; @@ -50,19 +49,18 @@ static tb_void_t xm_thread_value_free(tb_element_ref_t element, tb_pointer_t buf /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_init(lua_State* lua) -{ +tb_int_t xm_thread_queue_init(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_bool_t ok = tb_false; - xm_thread_queue_t* thread_queue = tb_null; - do - { + tb_bool_t ok = tb_false; + xm_thread_queue_t *thread_queue = tb_null; + do { thread_queue = tb_malloc0_type(xm_thread_queue_t); tb_assert_and_check_break(thread_queue); - thread_queue->refn = 1; - thread_queue->handle = tb_queue_init(0, tb_element_mem(sizeof(xm_thread_value_t), xm_thread_value_free, tb_null)); + thread_queue->refn = 1; + thread_queue->handle = tb_queue_init(0, + tb_element_mem(sizeof(xm_thread_value_t), xm_thread_value_free, tb_null)); tb_assert_and_check_break(thread_queue->handle); xm_lua_pushpointer(lua, (tb_pointer_t)thread_queue); @@ -70,12 +68,9 @@ tb_int_t xm_thread_queue_init(lua_State* lua) } while (0); - if (!ok) - { - if (thread_queue) - { - if (thread_queue->handle) - { + if (!ok) { + if (thread_queue) { + if (thread_queue->handle) { tb_queue_exit(thread_queue->handle); thread_queue->handle = tb_null; } diff --git a/core/src/xmake/thread/queue_pop.c b/core/src/xmake/thread/queue_pop.c index c666a85dd..256cb151e 100644 --- a/core/src/xmake/thread/queue_pop.c +++ b/core/src/xmake/thread/queue_pop.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_queue_pop.c + * @file queue_pop.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,30 +33,29 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_pop(lua_State* lua) -{ +tb_int_t xm_thread_queue_pop(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + xm_thread_queue_t *thread_queue = xm_thread_queue_get(lua, 1); tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); - if (tb_queue_null(thread_queue->handle)) - { + if (tb_queue_null(thread_queue->handle)) { lua_pushnil(lua); lua_pushliteral(lua, "the thread queue is empty"); return 2; } - xm_thread_value_t* item = (xm_thread_value_t*)tb_queue_get(thread_queue->handle); + xm_thread_value_t *item = (xm_thread_value_t *)tb_queue_get(thread_queue->handle); tb_assert_and_check_return_val(item, 0); tb_bool_t ok = tb_false; - switch (item->kind) - { + switch (item->kind) { case XM_THREAD_VALUE_STR: - if (item->size) + if (item->size) { lua_pushlstring(lua, item->u.string, item->size); - else lua_pushliteral(lua, ""); + } else { + lua_pushliteral(lua, ""); + } ok = tb_true; break; case XM_THREAD_VALUE_INT: @@ -79,8 +78,7 @@ tb_int_t xm_thread_queue_pop(lua_State* lua) break; } - if (!ok) - { + if (!ok) { lua_pushnil(lua); lua_pushliteral(lua, "invalid thread queue item"); return 2; @@ -89,4 +87,3 @@ tb_int_t xm_thread_queue_pop(lua_State* lua) tb_queue_pop(thread_queue->handle); return 1; } - diff --git a/core/src/xmake/thread/queue_push.c b/core/src/xmake/thread/queue_push.c index 8125f2b9a..0a9ff63e1 100644 --- a/core/src/xmake/thread/queue_push.c +++ b/core/src/xmake/thread/queue_push.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_queue_push.c + * @file queue_push.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,57 +33,43 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_push(lua_State* lua) -{ +tb_int_t xm_thread_queue_push(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + xm_thread_queue_t *thread_queue = xm_thread_queue_get(lua, 1); tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); - if (tb_queue_full(thread_queue->handle)) - { + if (tb_queue_full(thread_queue->handle)) { lua_pushboolean(lua, tb_false); lua_pushliteral(lua, "the thread queue is full"); return 2; } xm_thread_value_t item; - if (lua_isstring(lua, 2)) - { - size_t data_size = 0; - tb_char_t const* data = luaL_checklstring(lua, 2, &data_size); + if (lua_isstring(lua, 2)) { + size_t data_size = 0; + tb_char_t const *data = luaL_checklstring(lua, 2, &data_size); tb_assert_and_check_return_val(data, 0); item.kind = (tb_uint32_t)XM_THREAD_VALUE_STR; item.size = (tb_uint32_t)data_size; - if (data_size) - { + if (data_size) { item.u.string = tb_malloc_cstr(data_size); tb_assert_and_check_return_val(item.u.string, 0); tb_memcpy(item.u.string, data, data_size); } - } - else if (xm_lua_isinteger(lua, 2)) - { - item.kind = (tb_uint32_t)XM_THREAD_VALUE_INT; + } else if (xm_lua_isinteger(lua, 2)) { + item.kind = (tb_uint32_t)XM_THREAD_VALUE_INT; item.u.integer = lua_tointeger(lua, 2); - } - else if (lua_isnumber(lua, 2)) - { - item.kind = (tb_uint32_t)XM_THREAD_VALUE_NUM; + } else if (lua_isnumber(lua, 2)) { + item.kind = (tb_uint32_t)XM_THREAD_VALUE_NUM; item.u.number = lua_tonumber(lua, 2); - } - else if (lua_isboolean(lua, 2)) - { - item.kind = (tb_uint32_t)XM_THREAD_VALUE_BOOL; + } else if (lua_isboolean(lua, 2)) { + item.kind = (tb_uint32_t)XM_THREAD_VALUE_BOOL; item.u.boolean = lua_toboolean(lua, 2); - } - else if (lua_isnil(lua, 2)) - { + } else if (lua_isnil(lua, 2)) { item.kind = (tb_uint32_t)XM_THREAD_VALUE_NIL; - } - else - { + } else { lua_pushboolean(lua, tb_false); lua_pushliteral(lua, "unsupported thread queue item"); return 2; @@ -93,4 +79,3 @@ tb_int_t xm_thread_queue_push(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/queue_size.c b/core/src/xmake/thread/queue_size.c index 559a1dceb..9adfbe028 100644 --- a/core/src/xmake/thread/queue_size.c +++ b/core/src/xmake/thread/queue_size.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_queue_lock.c + * @file queue_size.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_queue" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_queue" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_queue_size(lua_State* lua) -{ +tb_int_t xm_thread_queue_size(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_queue_t* thread_queue = xm_thread_queue_get(lua, 1); + xm_thread_queue_t *thread_queue = xm_thread_queue_get(lua, 1); tb_assert_and_check_return_val(thread_queue && thread_queue->handle, 0); lua_pushinteger(lua, (tb_int_t)tb_queue_size(thread_queue->handle)); return 1; } - diff --git a/core/src/xmake/thread/semaphore_exit.c b/core/src/xmake/thread/semaphore_exit.c index cffffa249..ca6386757 100644 --- a/core/src/xmake/thread/semaphore_exit.c +++ b/core/src/xmake/thread/semaphore_exit.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_semaphore_exit.c + * @file semaphore_exit.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_semaphore" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,14 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_semaphore_exit(lua_State* lua) -{ +tb_int_t xm_thread_semaphore_exit(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + xm_thread_semaphore_t *thread_semaphore = xm_thread_semaphore_get(lua, 1); tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); - if (tb_atomic_fetch_and_sub(&thread_semaphore->refn, 1) == 1) - { - if (thread_semaphore->handle) - { + if (tb_atomic_fetch_and_sub(&thread_semaphore->refn, 1) == 1) { + if (thread_semaphore->handle) { tb_semaphore_exit(thread_semaphore->handle); thread_semaphore->handle = tb_null; } @@ -52,4 +49,3 @@ tb_int_t xm_thread_semaphore_exit(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/semaphore_incref.c b/core/src/xmake/thread/semaphore_incref.c index 84c2118d3..315332981 100644 --- a/core/src/xmake/thread/semaphore_incref.c +++ b/core/src/xmake/thread/semaphore_incref.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_semaphore_incref.c + * @file semaphore_incref.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_semaphore" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_semaphore_incref(lua_State* lua) -{ +tb_int_t xm_thread_semaphore_incref(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + xm_thread_semaphore_t *thread_semaphore = xm_thread_semaphore_get(lua, 1); tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_semaphore->refn, 1) >= 1); return 1; } - diff --git a/core/src/xmake/thread/semaphore_init.c b/core/src/xmake/thread/semaphore_init.c index 976b48de9..a4c7a80fe 100644 --- a/core/src/xmake/thread/semaphore_init.c +++ b/core/src/xmake/thread/semaphore_init.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_semaphore" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,20 +33,18 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_semaphore_init(lua_State* lua) -{ +tb_int_t xm_thread_semaphore_init(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_bool_t ok = tb_false; - xm_thread_semaphore_t* thread_semaphore = tb_null; - do - { + tb_bool_t ok = tb_false; + xm_thread_semaphore_t *thread_semaphore = tb_null; + do { tb_long_t value = (tb_long_t)luaL_checknumber(lua, 1); thread_semaphore = tb_malloc0_type(xm_thread_semaphore_t); tb_assert_and_check_break(thread_semaphore); - thread_semaphore->refn = 1; + thread_semaphore->refn = 1; thread_semaphore->handle = tb_semaphore_init(value); tb_assert_and_check_break(thread_semaphore->handle); @@ -55,12 +53,9 @@ tb_int_t xm_thread_semaphore_init(lua_State* lua) } while (0); - if (!ok) - { - if (thread_semaphore) - { - if (thread_semaphore->handle) - { + if (!ok) { + if (thread_semaphore) { + if (thread_semaphore->handle) { tb_semaphore_exit(thread_semaphore->handle); thread_semaphore->handle = tb_null; } diff --git a/core/src/xmake/thread/semaphore_post.c b/core/src/xmake/thread/semaphore_post.c index 9ca94bda5..20aea4dc0 100644 --- a/core/src/xmake/thread/semaphore_post.c +++ b/core/src/xmake/thread/semaphore_post.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_semaphore_lock.c + * @file semaphore_post.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_semaphore" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,15 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_semaphore_post(lua_State* lua) -{ +tb_int_t xm_thread_semaphore_post(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + xm_thread_semaphore_t *thread_semaphore = xm_thread_semaphore_get(lua, 1); tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); tb_long_t value = (tb_long_t)luaL_checknumber(lua, 2); lua_pushboolean(lua, tb_semaphore_post(thread_semaphore->handle, value)); return 1; } - diff --git a/core/src/xmake/thread/semaphore_wait.c b/core/src/xmake/thread/semaphore_wait.c index c8e4d1491..1c886d1bc 100644 --- a/core/src/xmake/thread/semaphore_wait.c +++ b/core/src/xmake/thread/semaphore_wait.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_semaphore_unlock.c + * @file semaphore_wait.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_semaphore" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_semaphore" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,15 +33,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_semaphore_wait(lua_State* lua) -{ +tb_int_t xm_thread_semaphore_wait(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_semaphore_t* thread_semaphore = xm_thread_semaphore_get(lua, 1); + xm_thread_semaphore_t *thread_semaphore = xm_thread_semaphore_get(lua, 1); tb_assert_and_check_return_val(thread_semaphore && thread_semaphore->handle, 0); tb_long_t timeout = (tb_long_t)luaL_checknumber(lua, 2); lua_pushinteger(lua, tb_semaphore_wait(thread_semaphore->handle, timeout)); return 1; } - diff --git a/core/src/xmake/thread/sharedata_clear.c b/core/src/xmake/thread/sharedata_clear.c index 0c8b4afcf..e55d5b059 100644 --- a/core/src/xmake/thread/sharedata_clear.c +++ b/core/src/xmake/thread/sharedata_clear.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_sharedata_clear.c + * @file sharedata_clear.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_sharedata" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_sharedata" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,11 +33,10 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_sharedata_clear(lua_State* lua) -{ +tb_int_t xm_thread_sharedata_clear(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_sharedata_t* thread_sharedata = xm_thread_sharedata_get(lua, 1); + xm_thread_sharedata_t *thread_sharedata = xm_thread_sharedata_get(lua, 1); tb_assert_and_check_return_val(thread_sharedata, 0); thread_sharedata->value.kind = XM_THREAD_VALUE_NIL; @@ -45,4 +44,3 @@ tb_int_t xm_thread_sharedata_clear(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/sharedata_exit.c b/core/src/xmake/thread/sharedata_exit.c index ad4a9f33f..419718f0f 100644 --- a/core/src/xmake/thread/sharedata_exit.c +++ b/core/src/xmake/thread/sharedata_exit.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_sharedata_exit.c + * @file sharedata_exit.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_sharedata" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_sharedata" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,19 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_sharedata_exit(lua_State* lua) -{ +tb_int_t xm_thread_sharedata_exit(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_sharedata_t* thread_sharedata = xm_thread_sharedata_get(lua, 1); + xm_thread_sharedata_t *thread_sharedata = xm_thread_sharedata_get(lua, 1); tb_assert_and_check_return_val(thread_sharedata, 0); - if (tb_atomic_fetch_and_sub(&thread_sharedata->refn, 1) == 1) - { + if (tb_atomic_fetch_and_sub(&thread_sharedata->refn, 1) == 1) { tb_buffer_exit(&thread_sharedata->buffer); tb_free(thread_sharedata); } lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/sharedata_get.c b/core/src/xmake/thread/sharedata_get.c index abd020d3b..6699a7500 100644 --- a/core/src/xmake/thread/sharedata_get.c +++ b/core/src/xmake/thread/sharedata_get.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_sharedata_get.c + * @file sharedata_get.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_sharedata" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_sharedata" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,20 +33,22 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_sharedata_get_(lua_State* lua) -{ +tb_int_t xm_thread_sharedata_get_(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_sharedata_t* thread_sharedata = xm_thread_sharedata_get(lua, 1); + xm_thread_sharedata_t *thread_sharedata = xm_thread_sharedata_get(lua, 1); tb_assert_and_check_return_val(thread_sharedata, 0); tb_bool_t ok = tb_false; - switch (thread_sharedata->value.kind) - { + switch (thread_sharedata->value.kind) { case XM_THREAD_VALUE_STR: - if (tb_buffer_size(&thread_sharedata->buffer) > 0) - lua_pushlstring(lua, (tb_char_t*)tb_buffer_data(&thread_sharedata->buffer), tb_buffer_size(&thread_sharedata->buffer)); - else lua_pushliteral(lua, ""); + if (tb_buffer_size(&thread_sharedata->buffer) > 0) { + lua_pushlstring(lua, + (tb_char_t *)tb_buffer_data(&thread_sharedata->buffer), + tb_buffer_size(&thread_sharedata->buffer)); + } else { + lua_pushliteral(lua, ""); + } ok = tb_true; break; case XM_THREAD_VALUE_INT: @@ -69,8 +71,7 @@ tb_int_t xm_thread_sharedata_get_(lua_State* lua) break; } - if (!ok) - { + if (!ok) { lua_pushnil(lua); lua_pushliteral(lua, "invalid thread sharedata thread_sharedata"); return 2; @@ -78,5 +79,3 @@ tb_int_t xm_thread_sharedata_get_(lua_State* lua) return 1; } - - diff --git a/core/src/xmake/thread/sharedata_incref.c b/core/src/xmake/thread/sharedata_incref.c index e0d32c600..8706c00a3 100644 --- a/core/src/xmake/thread/sharedata_incref.c +++ b/core/src/xmake/thread/sharedata_incref.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_sharedata_incref.c + * @file sharedata_incref.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_sharedata" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_sharedata" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,14 +33,12 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_sharedata_incref(lua_State* lua) -{ +tb_int_t xm_thread_sharedata_incref(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_sharedata_t* thread_sharedata = xm_thread_sharedata_get(lua, 1); + xm_thread_sharedata_t *thread_sharedata = xm_thread_sharedata_get(lua, 1); tb_assert_and_check_return_val(thread_sharedata, 0); lua_pushboolean(lua, tb_atomic_fetch_and_add(&thread_sharedata->refn, 1) >= 1); return 1; } - diff --git a/core/src/xmake/thread/sharedata_init.c b/core/src/xmake/thread/sharedata_init.c index b38fc217a..ac712e5f3 100644 --- a/core/src/xmake/thread/sharedata_init.c +++ b/core/src/xmake/thread/sharedata_init.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_sharedata" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_sharedata" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,18 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_sharedata_init(lua_State* lua) -{ +tb_int_t xm_thread_sharedata_init(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_bool_t ok = tb_false; - xm_thread_sharedata_t* thread_sharedata = tb_null; - do - { + tb_bool_t ok = tb_false; + xm_thread_sharedata_t *thread_sharedata = tb_null; + do { thread_sharedata = tb_malloc0_type(xm_thread_sharedata_t); tb_assert_and_check_break(thread_sharedata); - thread_sharedata->refn = 1; + thread_sharedata->refn = 1; thread_sharedata->value.kind = XM_THREAD_VALUE_NIL; tb_buffer_init(&thread_sharedata->buffer); @@ -53,10 +51,8 @@ tb_int_t xm_thread_sharedata_init(lua_State* lua) } while (0); - if (!ok) - { - if (thread_sharedata) - { + if (!ok) { + if (thread_sharedata) { tb_buffer_exit(&thread_sharedata->buffer); tb_free(thread_sharedata); } diff --git a/core/src/xmake/thread/sharedata_set.c b/core/src/xmake/thread/sharedata_set.c index 08e661f20..2bd66d1f9 100644 --- a/core/src/xmake/thread/sharedata_set.c +++ b/core/src/xmake/thread/sharedata_set.c @@ -15,15 +15,15 @@ * Copyright (C) 2015-present, Xmake Open Source Community. * * @author ruki - * @file thread_sharedata_set.c + * @file sharedata_set.c * */ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread_sharedata" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread_sharedata" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,45 +33,35 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_sharedata_set(lua_State* lua) -{ +tb_int_t xm_thread_sharedata_set(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - xm_thread_sharedata_t* thread_sharedata = xm_thread_sharedata_get(lua, 1); + xm_thread_sharedata_t *thread_sharedata = xm_thread_sharedata_get(lua, 1); tb_assert_and_check_return_val(thread_sharedata, 0); - if (lua_isstring(lua, 2)) - { - size_t data_size = 0; - tb_char_t const* data = luaL_checklstring(lua, 2, &data_size); + if (lua_isstring(lua, 2)) { + size_t data_size = 0; + tb_char_t const *data = luaL_checklstring(lua, 2, &data_size); tb_assert_and_check_return_val(data, 0); thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_STR; - if (data_size) - tb_buffer_memncpy(&thread_sharedata->buffer, (tb_byte_t const*)data, data_size); - else tb_buffer_clear(&thread_sharedata->buffer); - } - else if (xm_lua_isinteger(lua, 2)) - { - thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_INT; + if (data_size) { + tb_buffer_memncpy(&thread_sharedata->buffer, (tb_byte_t const *)data, data_size); + } else { + tb_buffer_clear(&thread_sharedata->buffer); + } + } else if (xm_lua_isinteger(lua, 2)) { + thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_INT; thread_sharedata->value.u.integer = lua_tointeger(lua, 2); - } - else if (lua_isnumber(lua, 2)) - { - thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_NUM; + } else if (lua_isnumber(lua, 2)) { + thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_NUM; thread_sharedata->value.u.number = lua_tonumber(lua, 2); - } - else if (lua_isboolean(lua, 2)) - { - thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_BOOL; + } else if (lua_isboolean(lua, 2)) { + thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_BOOL; thread_sharedata->value.u.boolean = lua_toboolean(lua, 2); - } - else if (lua_isnil(lua, 2)) - { + } else if (lua_isnil(lua, 2)) { thread_sharedata->value.kind = (tb_uint32_t)XM_THREAD_VALUE_NIL; - } - else - { + } else { lua_pushboolean(lua, tb_false); lua_pushliteral(lua, "unsupported thread sharedata item"); return 2; @@ -80,5 +70,3 @@ tb_int_t xm_thread_sharedata_set(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - - diff --git a/core/src/xmake/thread/thread_exit.c b/core/src/xmake/thread/thread_exit.c index 9d2c8e908..ca495d4a5 100644 --- a/core/src/xmake/thread/thread_exit.c +++ b/core/src/xmake/thread/thread_exit.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,26 +33,23 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_exit(lua_State* lua) -{ - // check +tb_int_t xm_thread_exit(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 thread - xm_thread_t* thread = (xm_thread_t*)xm_lua_topointer(lua, 1); + xm_thread_t *thread = (xm_thread_t *)xm_lua_topointer(lua, 1); tb_check_return_val(thread, 0); // exit thread - if (thread) - { + if (thread) { tb_string_exit(&thread->callback); tb_string_exit(&thread->callinfo); - if (thread->handle) - { + if (thread->handle) { tb_thread_exit(thread->handle); thread->handle = tb_null; } @@ -61,4 +58,3 @@ tb_int_t xm_thread_exit(lua_State* lua) lua_pushboolean(lua, tb_true); return 1; } - diff --git a/core/src/xmake/thread/thread_init.c b/core/src/xmake/thread/thread_init.c index 6fc44267a..2d3e067f7 100644 --- a/core/src/xmake/thread/thread_init.c +++ b/core/src/xmake/thread/thread_init.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,47 +35,46 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * macros */ -#define XM_THREAD_ENGINE_NAME "xmake" +#define XM_THREAD_ENGINE_NAME "xmake" /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_int_t xm_thread_func(tb_cpointer_t priv) -{ - xm_thread_t* thread = (xm_thread_t*)priv; +static tb_int_t xm_thread_func(tb_cpointer_t priv) { + xm_thread_t *thread = (xm_thread_t *)priv; tb_assert_and_check_return_val(thread, 0); xm_engine_ref_t engine = xm_engine_pool_alloc(xm_engine_pool()); - if (!engine) engine = xm_engine_init(XM_THREAD_ENGINE_NAME, tb_null); - if (engine) - { - lua_State* lua = xm_engine_lua(engine); + if (!engine) { + engine = xm_engine_init(XM_THREAD_ENGINE_NAME, tb_null); + } + if (engine) { + lua_State *lua = xm_engine_lua(engine); tb_assert(lua); // pass callback - tb_char_t const* callback_data = tb_string_cstr(&thread->callback); + tb_char_t const *callback_data = tb_string_cstr(&thread->callback); tb_size_t callback_size = tb_string_size(&thread->callback); - if (callback_data && callback_size) - { + if (callback_data && callback_size) { lua_pushlstring(lua, callback_data, callback_size); lua_setglobal(lua, "_THREAD_CALLBACK"); } // pass callinfo - tb_char_t const* callinfo_data = tb_string_cstr(&thread->callinfo); + tb_char_t const *callinfo_data = tb_string_cstr(&thread->callinfo); tb_size_t callinfo_size = tb_string_size(&thread->callinfo); - if (callinfo_data && callinfo_size) - { + if (callinfo_data && callinfo_size) { lua_pushlstring(lua, callinfo_data, callinfo_size); lua_setglobal(lua, "_THREAD_CALLINFO"); } // start engine - tb_char_t* argv[] = {XM_THREAD_ENGINE_NAME, tb_null}; + tb_char_t *argv[] = { XM_THREAD_ENGINE_NAME, tb_null }; xm_engine_main(engine, 1, argv, tb_null); - if (!xm_engine_pool_free(xm_engine_pool(), engine)) + if (!xm_engine_pool_free(xm_engine_pool(), engine)) { xm_engine_exit(engine); + } } return 0; } @@ -83,26 +82,23 @@ static tb_int_t xm_thread_func(tb_cpointer_t priv) /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_init(lua_State* lua) -{ - // check +tb_int_t xm_thread_init(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); - tb_bool_t ok = tb_false; - xm_thread_t* thread = tb_null; - do - { + tb_bool_t ok = tb_false; + xm_thread_t *thread = tb_null; + do { // get thread name - tb_char_t const* name = luaL_checkstring(lua, 1); + tb_char_t const *name = luaL_checkstring(lua, 1); // get callback - size_t callback_size = 0; - tb_char_t const* callback_data = luaL_checklstring(lua, 2, &callback_size); + size_t callback_size = 0; + tb_char_t const *callback_data = luaL_checklstring(lua, 2, &callback_size); tb_assert_and_check_break(callback_data && callback_size); // get callinfo - size_t callinfo_size = 0; - tb_char_t const* callinfo_data = luaL_checklstring(lua, 3, &callinfo_size); + size_t callinfo_size = 0; + tb_char_t const *callinfo_data = luaL_checklstring(lua, 3, &callinfo_size); tb_assert_and_check_break(callinfo_data && callinfo_size); // get stack size @@ -127,14 +123,11 @@ tb_int_t xm_thread_init(lua_State* lua) } while (0); - if (!ok) - { - if (thread) - { + if (!ok) { + if (thread) { tb_string_exit(&thread->callback); tb_string_exit(&thread->callinfo); - if (thread->handle) - { + if (thread->handle) { tb_thread_exit(thread->handle); thread->handle = tb_null; } diff --git a/core/src/xmake/thread/thread_resume.c b/core/src/xmake/thread/thread_resume.c index 9843ac2d7..d9ab421cf 100644 --- a/core/src/xmake/thread/thread_resume.c +++ b/core/src/xmake/thread/thread_resume.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_resume(lua_State* lua) -{ - // check +tb_int_t xm_thread_resume(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 thread - xm_thread_t* thread = (xm_thread_t*)xm_lua_topointer(lua, 1); + xm_thread_t *thread = (xm_thread_t *)xm_lua_topointer(lua, 1); tb_check_return_val(thread && thread->handle, 0); // resume thread lua_pushboolean(lua, tb_thread_resume(thread->handle)); return 1; } - diff --git a/core/src/xmake/thread/thread_suspend.c b/core/src/xmake/thread/thread_suspend.c index e21509413..7eb6a6a36 100644 --- a/core/src/xmake/thread/thread_suspend.c +++ b/core/src/xmake/thread/thread_suspend.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,21 +33,19 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_suspend(lua_State* lua) -{ - // check +tb_int_t xm_thread_suspend(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 thread - xm_thread_t* thread = (xm_thread_t*)xm_lua_topointer(lua, 1); + xm_thread_t *thread = (xm_thread_t *)xm_lua_topointer(lua, 1); tb_check_return_val(thread && thread->handle, 0); // suspend thread lua_pushboolean(lua, tb_thread_suspend(thread->handle)); return 1; } - diff --git a/core/src/xmake/thread/thread_wait.c b/core/src/xmake/thread/thread_wait.c index f2d7199ec..8c9ffab18 100644 --- a/core/src/xmake/thread/thread_wait.c +++ b/core/src/xmake/thread/thread_wait.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "thread" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "thread" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,17 +33,16 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t xm_thread_wait(lua_State* lua) -{ - // check +tb_int_t xm_thread_wait(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 thread - xm_thread_t* thread = (xm_thread_t*)xm_lua_topointer(lua, 1); + xm_thread_t *thread = (xm_thread_t *)xm_lua_topointer(lua, 1); tb_check_return_val(thread->handle, 0); // get timeout @@ -54,4 +53,3 @@ tb_int_t xm_thread_wait(lua_State* lua) lua_pushinteger(lua, (tb_int_t)tb_thread_wait(thread->handle, timeout, &retval)); return 1; } - diff --git a/core/src/xmake/tty/prefix.h b/core/src/xmake/tty/prefix.h index 6fb63c5f6..1afa646e3 100644 --- a/core/src/xmake/tty/prefix.h +++ b/core/src/xmake/tty/prefix.h @@ -26,7 +26,4 @@ */ #include "../prefix.h" - #endif - - diff --git a/core/src/xmake/tty/term_mode.c b/core/src/xmake/tty/term_mode.c index ec5f55ae9..df5af5b4c 100644 --- a/core/src/xmake/tty/term_mode.c +++ b/core/src/xmake/tty/term_mode.c @@ -22,15 +22,15 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "term_mode" -#define TB_TRACE_MODULE_DEBUG (0) +#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> +#include <windows.h> #endif /* ////////////////////////////////////////////////////////////////////////////////////// @@ -40,9 +40,7 @@ /* 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_int_t xm_tty_term_mode(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); #ifdef TB_CONFIG_OS_WINDOWS @@ -51,20 +49,25 @@ tb_int_t xm_tty_term_mode(lua_State* lua) tb_int_t stdtype = (tb_int_t)luaL_checkinteger(lua, 1); // get and set terminal mode - DWORD mode = 0; + 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; + 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)) - { + if (lua_isnumber(lua, 2)) { tb_int_t newmode = (tb_int_t)lua_tointeger(lua, 2); - if (console_handle != INVALID_HANDLE_VALUE) + if (console_handle != INVALID_HANDLE_VALUE) { SetConsoleMode(console_handle, (DWORD)newmode); + } } #else tb_int_t mode = 0; diff --git a/core/src/xmake/utils/bin2c.c b/core/src/xmake/utils/bin2c.c index aeb80efac..61befeaa9 100644 --- a/core/src/xmake/utils/bin2c.c +++ b/core/src/xmake/utils/bin2c.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "bin2c" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "bin2c" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -33,39 +33,37 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static __tb_inline__ tb_size_t xm_utils_bin2c_hex2str(tb_char_t str[5], tb_byte_t value) -{ - static tb_char_t const* digits_table = "0123456789ABCDEF"; - str[0] = ' '; - str[1] = '0'; - str[2] = 'x'; - str[3] = digits_table[(value >> 4) & 15]; - str[4] = digits_table[value & 15]; +static __tb_inline__ tb_size_t xm_utils_bin2c_hex2str(tb_char_t str[5], tb_byte_t value) { + static tb_char_t const *digits_table = "0123456789ABCDEF"; + str[0] = ' '; + str[1] = '0'; + str[2] = 'x'; + str[3] = digits_table[(value >> 4) & 15]; + str[4] = digits_table[value & 15]; return 5; } -static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t ostream, tb_int_t linewidth, tb_bool_t nozeroend) -{ +static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, + tb_stream_ref_t ostream, + tb_int_t linewidth, + tb_bool_t nozeroend) { tb_bool_t first = tb_true; - tb_hong_t i = 0; - tb_hong_t left = 0; + tb_hong_t i = 0; + tb_hong_t left = 0; tb_char_t line[4096]; tb_byte_t data[512]; tb_size_t linesize = 0; - tb_size_t need = 0; + tb_size_t need = 0; tb_assert_and_check_return_val(linewidth < sizeof(data), tb_false); - while (!tb_stream_beof(istream)) - { + while (!tb_stream_beof(istream)) { linesize = 0; - left = tb_stream_left(istream); - need = (tb_size_t)tb_min(left, linewidth); - if (need) - { + left = tb_stream_left(istream); + need = (tb_size_t)tb_min(left, linewidth); + if (need) { if (!tb_stream_bread(istream, data, need)) break; - if (!nozeroend && tb_stream_beof(istream)) - { + if (!nozeroend && tb_stream_beof(istream)) { tb_assert_and_check_break(need + 1 < sizeof(data)); data[need++] = '\0'; } @@ -73,16 +71,15 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t os tb_assert_and_check_break(linesize + 6 * need < sizeof(line)); i = 0; - if (first) - { - first = tb_false; + if (first) { + first = tb_false; line[linesize++] = ' '; + } else { + line[linesize++] = ','; } - else line[linesize++] = ','; linesize += xm_utils_bin2c_hex2str(line + linesize, data[i]); - for (i = 1; i < need; i++) - { + for (i = 1; i < need; i++) { line[linesize++] = ','; linesize += xm_utils_bin2c_hex2str(line + linesize, data[i]); } @@ -104,17 +101,15 @@ static tb_bool_t xm_utils_bin2c_dump(tb_stream_ref_t istream, tb_stream_ref_t os * * local ok, errors = utils.bin2c(binaryfile, outputfile, linewidth, nozeroend) */ -tb_int_t xm_utils_bin2c(lua_State* lua) -{ - // check +tb_int_t xm_utils_bin2c(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the binaryfile - tb_char_t const* binaryfile = luaL_checkstring(lua, 1); + tb_char_t const *binaryfile = luaL_checkstring(lua, 1); tb_check_return_val(binaryfile, 0); // get the outputfile - tb_char_t const* outputfile = luaL_checkstring(lua, 2); + tb_char_t const *outputfile = luaL_checkstring(lua, 2); tb_check_return_val(outputfile, 0); // get line width @@ -124,27 +119,24 @@ tb_int_t xm_utils_bin2c(lua_State* lua) tb_bool_t nozeroend = (tb_bool_t)lua_toboolean(lua, 4); // do dump - tb_bool_t ok = tb_false; + tb_bool_t ok = tb_false; tb_stream_ref_t istream = tb_stream_init_from_file(binaryfile, TB_FILE_MODE_RO); - tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); - do - { - if (!tb_stream_open(istream)) - { + tb_stream_ref_t ostream = tb_stream_init_from_file(outputfile, + TB_FILE_MODE_RW | TB_FILE_MODE_CREAT | TB_FILE_MODE_TRUNC); + do { + if (!tb_stream_open(istream)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2c: open %s failed", binaryfile); break; } - if (!tb_stream_open(ostream)) - { + if (!tb_stream_open(ostream)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2c: open %s failed", outputfile); break; } - if (!xm_utils_bin2c_dump(istream, ostream, linewidth, nozeroend)) - { + if (!xm_utils_bin2c_dump(istream, ostream, linewidth, nozeroend)) { lua_pushboolean(lua, tb_false); lua_pushfstring(lua, "bin2c: dump data failed"); break; @@ -155,11 +147,13 @@ tb_int_t xm_utils_bin2c(lua_State* lua) } while (0); - if (istream) tb_stream_clos(istream); + if (istream) + tb_stream_clos(istream); istream = tb_null; - if (ostream) tb_stream_clos(ostream); + if (ostream) + tb_stream_clos(ostream); ostream = tb_null; - return ok? 1 : 2; + return ok ? 1 : 2; } diff --git a/core/src/xmake/utils/prefix.h b/core/src/xmake/utils/prefix.h index 287e6bb45..a9f509e72 100644 --- a/core/src/xmake/utils/prefix.h +++ b/core/src/xmake/utils/prefix.h @@ -27,5 +27,3 @@ #include "../prefix.h" #endif - - diff --git a/core/src/xmake/winos/ansi.c b/core/src/xmake/winos/ansi.c index e4780d134..9646db59b 100644 --- a/core/src/xmake/winos/ansi.c +++ b/core/src/xmake/winos/ansi.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "ansi" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "ansi" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,65 +35,55 @@ * implementation */ -static tb_int_t xm_expand_cp(tb_int_t cp) -{ - // check +static tb_int_t xm_expand_cp(tb_int_t cp) { tb_assert_and_check_return_val(cp >= 0 && cp <= 65535, 0); - if (cp == CP_OEMCP) return GetOEMCP(); - if (cp == CP_ACP) return GetACP(); + if (cp == CP_OEMCP) { + return GetOEMCP(); + } + if (cp == CP_ACP) { + return GetACP(); + } return cp; } -tb_int_t xm_winos_console_cp(lua_State* lua) -{ - // check +tb_int_t xm_winos_console_cp(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_int_t n = lua_gettop(lua); - if (n >= 1) - { + if (n >= 1) { lua_Integer cp = luaL_checkinteger(lua, 1); luaL_argcheck(lua, cp >= 0 && cp < 65536, 1, "invalid code page"); cp = xm_expand_cp((tb_uint_t)cp); luaL_argcheck(lua, SetConsoleCP((tb_uint_t)cp), 1, "failed to set code page"); lua_pushinteger(lua, cp); return 1; - } - else - { + } else { tb_uint_t cp = GetConsoleCP(); lua_pushinteger(lua, (lua_Integer)cp); return 1; } } -tb_int_t xm_winos_console_output_cp(lua_State* lua) -{ - // check +tb_int_t xm_winos_console_output_cp(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_int_t n = lua_gettop(lua); - if (n >= 1) - { + if (n >= 1) { lua_Integer cp = luaL_checkinteger(lua, 1); luaL_argcheck(lua, cp >= 0 && cp < 65536, 1, "invalid code page"); cp = xm_expand_cp((tb_uint_t)cp); luaL_argcheck(lua, SetConsoleOutputCP((tb_uint_t)cp), 1, "failed to set code page"); lua_pushinteger(lua, cp); return 1; - } - else - { + } else { tb_uint_t cp = GetConsoleOutputCP(); lua_pushinteger(lua, (lua_Integer)cp); return 1; } } -tb_int_t xm_winos_cp_info(lua_State* lua) -{ - // check +tb_int_t xm_winos_cp_info(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); lua_Integer cp = luaL_checkinteger(lua, 1); @@ -104,7 +94,7 @@ tb_int_t xm_winos_cp_info(lua_State* lua) lua_newtable(lua); lua_pushliteral(lua, "name"); - tb_char_t* namebuf = tb_malloc_cstr(sizeof(cp_info.CodePageName) * 2); + tb_char_t *namebuf = tb_malloc_cstr(sizeof(cp_info.CodePageName) * 2); tb_assert_and_check_return_val(namebuf, 0); tb_size_t namelen = tb_wtoa(namebuf, (const tb_wchar_t *)cp_info.CodePageName, sizeof(cp_info.CodePageName) * 2); tb_assert_and_check_return_val(namelen < sizeof(cp_info.CodePageName) * 2, 0); @@ -121,13 +111,12 @@ tb_int_t xm_winos_cp_info(lua_State* lua) lua_settable(lua, -3); lua_pushliteral(lua, "default_char"); - lua_pushstring(lua, (tb_char_t const*)cp_info.DefaultChar); + lua_pushstring(lua, (tb_char_t const *)cp_info.DefaultChar); lua_settable(lua, -3); lua_pushliteral(lua, "lead_byte"); lua_createtable(lua, MAX_LEADBYTES / 2, 0); - for (tb_size_t i = 0; i < MAX_LEADBYTES && cp_info.LeadByte[i] != 0 && cp_info.LeadByte[i + 1] != 0; i += 2) - { + for (tb_size_t i = 0; i < MAX_LEADBYTES && cp_info.LeadByte[i] != 0 && cp_info.LeadByte[i + 1] != 0; i += 2) { lua_pushinteger(lua, (i / 2) + 1); lua_createtable(lua, 0, 2); @@ -146,9 +135,7 @@ tb_int_t xm_winos_cp_info(lua_State* lua) return 1; } -tb_int_t xm_winos_ansi_cp(lua_State* lua) -{ - // check +tb_int_t xm_winos_ansi_cp(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_uint_t cp = GetACP(); @@ -156,13 +143,10 @@ tb_int_t xm_winos_ansi_cp(lua_State* lua) return 1; } -tb_int_t xm_winos_oem_cp(lua_State* lua) -{ - // check +tb_int_t xm_winos_oem_cp(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); tb_uint_t cp = GetOEMCP(); lua_pushinteger(lua, (lua_Integer)cp); return 1; } - diff --git a/core/src/xmake/winos/logical_drives.c b/core/src/xmake/winos/logical_drives.c index f10e4e09b..9f6df60ea 100644 --- a/core/src/xmake/winos/logical_drives.c +++ b/core/src/xmake/winos/logical_drives.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "logical_drives" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "logical_drives" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,21 +35,19 @@ */ // get the logical drives -tb_int_t xm_winos_logical_drives(lua_State* lua) -{ +tb_int_t xm_winos_logical_drives(lua_State *lua) { // init table lua_newtable(lua); // get logical drives - tb_char_t* data = tb_null; - do - { + tb_char_t *data = tb_null; + do { // get buffer size DWORD size = GetLogicalDriveStringsA(0, tb_null); tb_assert_and_check_break(size); // make data buffer - data = (tb_char_t*)tb_malloc0(size + 1); + data = (tb_char_t *)tb_malloc0(size + 1); tb_assert_and_check_break(data); // get logical drives @@ -57,10 +55,9 @@ tb_int_t xm_winos_logical_drives(lua_State* lua) tb_assert_and_check_break(size); // parse logical drives - tb_size_t i = 1; - tb_char_t const* p = data; - while (*p) - { + tb_size_t i = 1; + tb_char_t const *p = data; + while (*p) { // save drive lua_pushinteger(lua, i++); lua_pushstring(lua, p); @@ -72,9 +69,10 @@ tb_int_t xm_winos_logical_drives(lua_State* lua) } while (0); // exit data - if (data) tb_free(data); + if (data) { + tb_free(data); + } data = tb_null; - // ok return 1; } diff --git a/core/src/xmake/winos/prefix.h b/core/src/xmake/winos/prefix.h index 3c7e103dc..6543b112d 100644 --- a/core/src/xmake/winos/prefix.h +++ b/core/src/xmake/winos/prefix.h @@ -26,10 +26,7 @@ */ #include "../prefix.h" #ifdef TB_CONFIG_OS_WINDOWS -# include <windows.h> +#include <windows.h> #endif - #endif - - diff --git a/core/src/xmake/winos/registry_keys.c b/core/src/xmake/winos/registry_keys.c index 807a094a1..49ddf613f 100644 --- a/core/src/xmake/winos/registry_keys.c +++ b/core/src/xmake/winos/registry_keys.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "registry_keys" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "registry_keys" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -35,75 +35,79 @@ */ // the enum info type -typedef struct __xm_winos_registry_enum_info_t -{ - lua_State* lua; - HKEY key; - tb_int_t ok; - tb_char_t const* error; - tb_int_t count; - tb_wchar_t key_name[1024]; - tb_char_t key_path_a[TB_PATH_MAXN]; - -}xm_winos_registry_enum_info_t; +typedef struct __xm_winos_registry_enum_info_t { + lua_State *lua; + HKEY key; + tb_int_t ok; + tb_char_t const *error; + tb_int_t count; + tb_wchar_t key_name[1024]; + tb_char_t key_path_a[TB_PATH_MAXN]; +} xm_winos_registry_enum_info_t; /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t xm_winos_registry_enum_keys(xm_winos_registry_enum_info_t* info, tb_wchar_t const* rootdir, tb_long_t recursion) -{ +static tb_void_t xm_winos_registry_enum_keys(xm_winos_registry_enum_info_t *info, + tb_wchar_t const *rootdir, + tb_long_t recursion) { // enum keys - HKEY keynew = tb_null; - lua_State* lua = info->lua; - tb_wchar_t* key_path = tb_null; + HKEY keynew = tb_null; + lua_State *lua = info->lua; + tb_wchar_t *key_path = tb_null; tb_size_t key_path_maxn = TB_PATH_MAXN; - do - { + do { // open registry key - if (RegOpenKeyExW(info->key, rootdir, 0, KEY_READ, &keynew) != ERROR_SUCCESS && keynew) - { - info->ok = -1; + if (RegOpenKeyExW(info->key, rootdir, 0, KEY_READ, &keynew) != ERROR_SUCCESS && keynew) { + info->ok = -1; info->error = "open registry key failed"; break; } // query keys - DWORD key_name_num = 0; + DWORD key_name_num = 0; DWORD key_name_maxn = 0; - if (RegQueryInfoKeyW(keynew, tb_null, tb_null, tb_null, &key_name_num, &key_name_maxn, tb_null, tb_null, tb_null, tb_null, tb_null, tb_null) != ERROR_SUCCESS) - { + if (RegQueryInfoKeyW(keynew, + tb_null, + tb_null, + tb_null, + &key_name_num, + &key_name_maxn, + tb_null, + tb_null, + tb_null, + tb_null, + tb_null, + tb_null) != ERROR_SUCCESS) { // cannot query this key, we ignore it break; } key_name_maxn++; // add `\0` // ensure enough key path buffer - if (key_name_maxn > tb_arrayn(info->key_name)) - { - info->ok = -1; + if (key_name_maxn > tb_arrayn(info->key_name)) { + info->ok = -1; info->error = "no enough key path buffer"; break; } // init key path - key_path = (tb_wchar_t*)tb_malloc(key_path_maxn * sizeof(tb_wchar_t)); - if (!key_path) - { - info->ok = -1; + key_path = (tb_wchar_t *)tb_malloc(key_path_maxn * sizeof(tb_wchar_t)); + if (!key_path) { + info->ok = -1; info->error = "no enough key path buffer"; break; } // get all keys DWORD i = 0; - for (i = 0; i < key_name_num && info->ok > 0; i++) - { + for (i = 0; i < key_name_num && info->ok > 0; i++) { // get key name - info->key_name[0] = L'\0'; + info->key_name[0] = L'\0'; DWORD key_name_size = tb_arrayn(info->key_name); - if (RegEnumKeyExW(keynew, i, info->key_name, &key_name_size, tb_null, tb_null, tb_null, tb_null) != ERROR_SUCCESS) - { - info->ok = -1; + if (RegEnumKeyExW(keynew, i, info->key_name, &key_name_size, tb_null, tb_null, tb_null, tb_null) != + ERROR_SUCCESS) { + info->ok = -1; info->error = "get registry key failed"; break; } @@ -113,9 +117,8 @@ static tb_void_t xm_winos_registry_enum_keys(xm_winos_registry_enum_info_t* info // get key path (mbs) tb_size_t key_path_a_size = tb_wtoa(info->key_path_a, key_path, sizeof(info->key_path_a)); - if (key_path_a_size == -1) - { - info->ok = -1; + if (key_path_a_size == -1) { + info->ok = -1; info->error = "convert registry key path failed"; break; } @@ -129,26 +132,29 @@ static tb_void_t xm_winos_registry_enum_keys(xm_winos_registry_enum_info_t* info // is continue? tb_bool_t is_continue = lua_toboolean(lua, -1); lua_pop(lua, 1); - if (!is_continue) - { + if (!is_continue) { info->ok = 0; break; } // enum all subkeys - if (recursion > 0 || recursion < 0) - xm_winos_registry_enum_keys(info, key_path, recursion > 0? recursion - 1 : recursion); + if (recursion > 0 || recursion < 0) { + xm_winos_registry_enum_keys(info, key_path, recursion > 0 ? recursion - 1 : recursion); + } } } while (0); // exit registry key - if (keynew) + if (keynew) { RegCloseKey(keynew); + } keynew = tb_null; // free key path - if (key_path) tb_free(key_path); + if (key_path) { + tb_free(key_path); + } key_path = tb_null; } @@ -164,36 +170,41 @@ static tb_void_t xm_winos_registry_enum_keys(xm_winos_registry_enum_info_t* info * return true -- continue or break * end) */ -tb_int_t xm_winos_registry_keys(lua_State* lua) -{ - // check +tb_int_t xm_winos_registry_keys(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the arguments - tb_char_t const* rootkey = luaL_checkstring(lua, 1); - tb_char_t const* rootdir = luaL_checkstring(lua, 2); - tb_long_t recursion = (tb_long_t)lua_tointeger(lua, 3); - tb_bool_t is_function = lua_isfunction(lua, 4); + tb_char_t const *rootkey = luaL_checkstring(lua, 1); + tb_char_t const *rootdir = luaL_checkstring(lua, 2); + tb_long_t recursion = (tb_long_t)lua_tointeger(lua, 3); + tb_bool_t is_function = lua_isfunction(lua, 4); tb_check_return_val(rootkey && rootdir && is_function, 0); // enum keys - tb_bool_t ok = tb_false; - tb_int_t count = 0; - HKEY key = tb_null; - do - { + tb_bool_t ok = tb_false; + tb_int_t count = 0; + HKEY key = tb_null; + do { // get registry rootkey - if (!tb_strcmp(rootkey, "HKEY_CLASSES_ROOT")) key = HKEY_CLASSES_ROOT; - else if (!tb_strcmp(rootkey, "HKCR")) key = HKEY_CLASSES_ROOT; - else if (!tb_strcmp(rootkey, "HKEY_CURRENT_CONFIG")) key = HKEY_CURRENT_CONFIG; - else if (!tb_strcmp(rootkey, "HKCC")) key = HKEY_CURRENT_CONFIG; - else if (!tb_strcmp(rootkey, "HKEY_CURRENT_USER")) key = HKEY_CURRENT_USER; - else if (!tb_strcmp(rootkey, "HKCU")) key = HKEY_CURRENT_USER; - else if (!tb_strcmp(rootkey, "HKEY_LOCAL_MACHINE")) key = HKEY_LOCAL_MACHINE; - else if (!tb_strcmp(rootkey, "HKLM")) key = HKEY_LOCAL_MACHINE; - else if (!tb_strcmp(rootkey, "HKEY_USERS")) key = HKEY_USERS; - else - { + if (!tb_strcmp(rootkey, "HKEY_CLASSES_ROOT")) { + key = HKEY_CLASSES_ROOT; + } else if (!tb_strcmp(rootkey, "HKCR")) { + key = HKEY_CLASSES_ROOT; + } else if (!tb_strcmp(rootkey, "HKEY_CURRENT_CONFIG")) { + key = HKEY_CURRENT_CONFIG; + } else if (!tb_strcmp(rootkey, "HKCC")) { + key = HKEY_CURRENT_CONFIG; + } else if (!tb_strcmp(rootkey, "HKEY_CURRENT_USER")) { + key = HKEY_CURRENT_USER; + } else if (!tb_strcmp(rootkey, "HKCU")) { + key = HKEY_CURRENT_USER; + } else if (!tb_strcmp(rootkey, "HKEY_LOCAL_MACHINE")) { + key = HKEY_LOCAL_MACHINE; + } else if (!tb_strcmp(rootkey, "HKLM")) { + key = HKEY_LOCAL_MACHINE; + } else if (!tb_strcmp(rootkey, "HKEY_USERS")) { + key = HKEY_USERS; + } else { lua_pushnil(lua); lua_pushfstring(lua, "invalid registry rootkey: %s", rootkey); break; @@ -201,8 +212,7 @@ tb_int_t xm_winos_registry_keys(lua_State* lua) // do enum tb_wchar_t rootdir_w[TB_PATH_MAXN]; - if (tb_atow(rootdir_w, rootdir, TB_PATH_MAXN) == -1) - { + if (tb_atow(rootdir_w, rootdir, TB_PATH_MAXN) == -1) { lua_pushnil(lua); lua_pushfstring(lua, "rootdir is too long: %s", rootdir); break; @@ -216,20 +226,17 @@ tb_int_t xm_winos_registry_keys(lua_State* lua) xm_winos_registry_enum_keys(&info, rootdir_w, recursion); count = info.count; ok = info.ok >= 0; - if (!ok) - { + if (!ok) { lua_pushnil(lua); - lua_pushfstring(lua, "%s: %s\\%s", info.error? info.error : "enum registry keys failed", rootkey, rootdir); + lua_pushfstring(lua, "%s: %s\\%s", info.error ? info.error : "enum registry keys failed", rootkey, rootdir); } } while (0); - // ok? - if (ok) - { + if (ok) { lua_pushinteger(lua, count); return 1; + } else { + return 2; } - else return 2; } - diff --git a/core/src/xmake/winos/registry_query.c b/core/src/xmake/winos/registry_query.c index d494a6db9..bfaebe744 100644 --- a/core/src/xmake/winos/registry_query.c +++ b/core/src/xmake/winos/registry_query.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "registry_query" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "registry_query" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -34,7 +34,8 @@ * types */ // the RegGetValueW func type -typedef BOOL (WINAPI* xm_RegGetValueW_t)(HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpValue, DWORD dwFlags, LPDWORD pdwType, PVOID pvData, LPDWORD pcbData); +typedef BOOL(WINAPI *xm_RegGetValueW_t)( + HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lpValue, DWORD dwFlags, LPDWORD pdwType, PVOID pvData, LPDWORD pcbData); /* ////////////////////////////////////////////////////////////////////////////////////// * implementation @@ -44,38 +45,43 @@ typedef BOOL (WINAPI* xm_RegGetValueW_t)(HKEY hkey, LPCWSTR lpSubKey, LPCWSTR lp * * local value, errors = winos.registry_query("HKEY_LOCAL_MACHINE", "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", "Debugger") */ -tb_int_t xm_winos_registry_query(lua_State* lua) -{ - // check +tb_int_t xm_winos_registry_query(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the arguments - tb_char_t const* rootkey = luaL_checkstring(lua, 1); - tb_char_t const* rootdir = luaL_checkstring(lua, 2); - tb_char_t const* valuename = luaL_checkstring(lua, 3); + tb_char_t const *rootkey = luaL_checkstring(lua, 1); + tb_char_t const *rootdir = luaL_checkstring(lua, 2); + tb_char_t const *valuename = luaL_checkstring(lua, 3); tb_check_return_val(rootkey && rootdir && valuename, 0); // query key-value - tb_bool_t ok = tb_false; - HKEY key = tb_null; - HKEY keynew = tb_null; - tb_char_t* value = tb_null; - tb_wchar_t* value_w = tb_null; + tb_bool_t ok = tb_false; + HKEY key = tb_null; + HKEY keynew = tb_null; + tb_char_t *value = tb_null; + tb_wchar_t *value_w = tb_null; tb_size_t value_n = (tb_size_t)-1; - do - { + do { // get registry rootkey - if (!tb_strcmp(rootkey, "HKEY_CLASSES_ROOT")) key = HKEY_CLASSES_ROOT; - else if (!tb_strcmp(rootkey, "HKCR")) key = HKEY_CLASSES_ROOT; - else if (!tb_strcmp(rootkey, "HKEY_CURRENT_CONFIG")) key = HKEY_CURRENT_CONFIG; - else if (!tb_strcmp(rootkey, "HKCC")) key = HKEY_CURRENT_CONFIG; - else if (!tb_strcmp(rootkey, "HKEY_CURRENT_USER")) key = HKEY_CURRENT_USER; - else if (!tb_strcmp(rootkey, "HKCU")) key = HKEY_CURRENT_USER; - else if (!tb_strcmp(rootkey, "HKEY_LOCAL_MACHINE")) key = HKEY_LOCAL_MACHINE; - else if (!tb_strcmp(rootkey, "HKLM")) key = HKEY_LOCAL_MACHINE; - else if (!tb_strcmp(rootkey, "HKEY_USERS")) key = HKEY_USERS; - else - { + if (!tb_strcmp(rootkey, "HKEY_CLASSES_ROOT")) { + key = HKEY_CLASSES_ROOT; + } else if (!tb_strcmp(rootkey, "HKCR")) { + key = HKEY_CLASSES_ROOT; + } else if (!tb_strcmp(rootkey, "HKEY_CURRENT_CONFIG")) { + key = HKEY_CURRENT_CONFIG; + } else if (!tb_strcmp(rootkey, "HKCC")) { + key = HKEY_CURRENT_CONFIG; + } else if (!tb_strcmp(rootkey, "HKEY_CURRENT_USER")) { + key = HKEY_CURRENT_USER; + } else if (!tb_strcmp(rootkey, "HKCU")) { + key = HKEY_CURRENT_USER; + } else if (!tb_strcmp(rootkey, "HKEY_LOCAL_MACHINE")) { + key = HKEY_LOCAL_MACHINE; + } else if (!tb_strcmp(rootkey, "HKLM")) { + key = HKEY_LOCAL_MACHINE; + } else if (!tb_strcmp(rootkey, "HKEY_USERS")) { + key = HKEY_USERS; + } else { lua_pushnil(lua); lua_pushfstring(lua, "invalid registry rootkey: %s", rootkey); break; @@ -83,8 +89,7 @@ tb_int_t xm_winos_registry_query(lua_State* lua) // convert rootdir to wide characters tb_wchar_t rootdir_w[TB_PATH_MAXN]; - if (tb_atow(rootdir_w, rootdir, TB_PATH_MAXN) == (tb_size_t)-1) - { + if (tb_atow(rootdir_w, rootdir, TB_PATH_MAXN) == (tb_size_t)-1) { lua_pushnil(lua); lua_pushfstring(lua, "invalid registry rootkey:: %s", rootdir); break; @@ -92,8 +97,7 @@ tb_int_t xm_winos_registry_query(lua_State* lua) // convert valuename to wide characters tb_wchar_t valuename_w[TB_PATH_MAXN]; - if (tb_atow(valuename_w, valuename, TB_PATH_MAXN) == (tb_size_t)-1) - { + if (tb_atow(valuename_w, valuename, TB_PATH_MAXN) == (tb_size_t)-1) { lua_pushnil(lua); lua_pushfstring(lua, "invalid registry valuename: %s", valuename); break; @@ -101,22 +105,21 @@ tb_int_t xm_winos_registry_query(lua_State* lua) // attempt to load RegGetValueW static xm_RegGetValueW_t s_RegGetValueW = tb_null; - if (!s_RegGetValueW) - { + if (!s_RegGetValueW) { // load the advapi32 module tb_dynamic_ref_t module = (tb_dynamic_ref_t)GetModuleHandleA("advapi32.dll"); - if (!module) module = tb_dynamic_init("advapi32.dll"); - if (module) s_RegGetValueW = (xm_RegGetValueW_t)tb_dynamic_func(module, "RegGetValueW"); + if (!module) + module = tb_dynamic_init("advapi32.dll"); + if (module) + s_RegGetValueW = (xm_RegGetValueW_t)tb_dynamic_func(module, "RegGetValueW"); } // get registry value DWORD type = 0; - if (s_RegGetValueW) - { + if (s_RegGetValueW) { // get registry value size DWORD valuesize_w = 0; - if (s_RegGetValueW(key, rootdir_w, valuename_w, RRF_RT_ANY, 0, tb_null, &valuesize_w) != ERROR_SUCCESS) - { + if (s_RegGetValueW(key, rootdir_w, valuename_w, RRF_RT_ANY, 0, tb_null, &valuesize_w) != ERROR_SUCCESS) { lua_pushnil(lua); lua_pushfstring(lua, "get registry value size failed: %s\\%s;%s", rootkey, rootdir, valuename); break; @@ -124,34 +127,32 @@ tb_int_t xm_winos_registry_query(lua_State* lua) // make value buffer DWORD valuesize = valuesize_w * 2; - value = (tb_char_t*)tb_malloc0(valuesize); + value = (tb_char_t *)tb_malloc0(valuesize); tb_assert_and_check_break(value); - value_w = (tb_wchar_t*)tb_malloc0(valuesize_w); + value_w = (tb_wchar_t *)tb_malloc0(valuesize_w); tb_assert_and_check_break(value_w); // get value result, we attempt to do not expand value if get failed type = 0; - if (s_RegGetValueW(key, rootdir_w, valuename_w, RRF_RT_ANY, &type, (PVOID)value_w, &valuesize_w) != ERROR_SUCCESS && - s_RegGetValueW(key, rootdir_w, valuename_w, RRF_RT_ANY | RRF_NOEXPAND, &type, (PVOID)value_w, &valuesize_w) != ERROR_SUCCESS) - { + if (s_RegGetValueW(key, rootdir_w, valuename_w, RRF_RT_ANY, &type, (PVOID)value_w, &valuesize_w) != + ERROR_SUCCESS && + s_RegGetValueW( + key, rootdir_w, valuename_w, RRF_RT_ANY | RRF_NOEXPAND, &type, (PVOID)value_w, &valuesize_w) != + ERROR_SUCCESS) { lua_pushnil(lua); lua_pushfstring(lua, "get registry value failed: %s\\%s;%s", rootkey, rootdir, valuename); break; } - + value_n = tb_wtoa(value, value_w, valuesize); - if (value_n == (tb_size_t)-1) - { + if (value_n == (tb_size_t)-1) { lua_pushnil(lua); lua_pushfstring(lua, "wtoa registry value failed: %s\\%s;%s", rootkey, rootdir, valuename); break; } - } - else - { + } else { // open registry key - if (RegOpenKeyExW(key, rootdir_w, 0, KEY_QUERY_VALUE, &keynew) != ERROR_SUCCESS && keynew) - { + if (RegOpenKeyExW(key, rootdir_w, 0, KEY_QUERY_VALUE, &keynew) != ERROR_SUCCESS && keynew) { lua_pushnil(lua); lua_pushfstring(lua, "open registry key failed: %s\\%s", rootkey, rootdir); break; @@ -159,8 +160,7 @@ tb_int_t xm_winos_registry_query(lua_State* lua) // get registry value size DWORD valuesize_w = 0; - if (RegQueryValueExW(keynew, valuename_w, tb_null, tb_null, tb_null, &valuesize_w) != ERROR_SUCCESS) - { + if (RegQueryValueExW(keynew, valuename_w, tb_null, tb_null, tb_null, &valuesize_w) != ERROR_SUCCESS) { lua_pushnil(lua); lua_pushfstring(lua, "get registry value size failed: %s\\%s;%s", rootkey, rootdir, valuename); break; @@ -168,23 +168,21 @@ tb_int_t xm_winos_registry_query(lua_State* lua) // make value buffer DWORD valuesize = valuesize_w * 2; - value = (tb_char_t*)tb_malloc0(valuesize); + value = (tb_char_t *)tb_malloc0(valuesize); tb_assert_and_check_break(value); - value_w = (tb_wchar_t*)tb_malloc0(valuesize_w); + value_w = (tb_wchar_t *)tb_malloc0(valuesize_w); tb_assert_and_check_break(value_w); // get value result type = 0; - if (RegQueryValueExW(keynew, valuename_w, tb_null, &type, (LPBYTE)value_w, &valuesize_w) != ERROR_SUCCESS) - { + if (RegQueryValueExW(keynew, valuename_w, tb_null, &type, (LPBYTE)value_w, &valuesize_w) != ERROR_SUCCESS) { lua_pushnil(lua); lua_pushfstring(lua, "get registry value failed: %s\\%s;%s", rootkey, rootdir, valuename); break; } value_n = tb_wtoa(value, value_w, valuesize); - if (value_n == (tb_size_t)-1) - { + if (value_n == (tb_size_t)-1) { lua_pushnil(lua); lua_pushfstring(lua, "wtoa registry value failed: %s\\%s;%s", rootkey, rootdir, valuename); break; @@ -192,19 +190,18 @@ tb_int_t xm_winos_registry_query(lua_State* lua) } // save result - switch (type) - { + switch (type) { case REG_SZ: case REG_EXPAND_SZ: lua_pushlstring(lua, value, value_n); ok = tb_true; break; case REG_DWORD: - lua_pushfstring(lua, "%d", *((tb_int_t*)value)); + lua_pushfstring(lua, "%d", *((tb_int_t *)value)); ok = tb_true; break; case REG_QWORD: - lua_pushfstring(lua, "%lld", *((tb_int64_t*)value)); + lua_pushfstring(lua, "%lld", *((tb_int64_t *)value)); ok = tb_true; break; default: @@ -216,16 +213,20 @@ tb_int_t xm_winos_registry_query(lua_State* lua) } while (0); // exit registry key - if (keynew) + if (keynew) { RegCloseKey(keynew); + } keynew = tb_null; // exit value - if (value) tb_free(value); + if (value) { + tb_free(value); + } value = tb_null; - if (value_w) tb_free(value_w); + if (value_w) { + tb_free(value_w); + } value_w = tb_null; - // ok? - return ok? 1 : 2; + return ok ? 1 : 2; } diff --git a/core/src/xmake/winos/registry_values.c b/core/src/xmake/winos/registry_values.c index 7ce8ada10..543665853 100644 --- a/core/src/xmake/winos/registry_values.c +++ b/core/src/xmake/winos/registry_values.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "registry_values" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "registry_values" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -42,54 +42,68 @@ * return true -- continue or break * end) */ -tb_int_t xm_winos_registry_values(lua_State* lua) -{ - // check +tb_int_t xm_winos_registry_values(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the arguments - tb_char_t const* rootkey = luaL_checkstring(lua, 1); - tb_char_t const* rootdir = luaL_checkstring(lua, 2); - tb_bool_t is_function = lua_isfunction(lua, 3); + tb_char_t const *rootkey = luaL_checkstring(lua, 1); + tb_char_t const *rootdir = luaL_checkstring(lua, 2); + tb_bool_t is_function = lua_isfunction(lua, 3); tb_check_return_val(rootkey && rootdir && is_function, 0); // enum values - tb_bool_t ok = tb_false; - tb_int_t count = 0; - HKEY key = tb_null; - HKEY keynew = tb_null; - do - { + tb_bool_t ok = tb_false; + tb_int_t count = 0; + HKEY key = tb_null; + HKEY keynew = tb_null; + do { // get registry rootkey - if (!tb_strcmp(rootkey, "HKEY_CLASSES_ROOT")) key = HKEY_CLASSES_ROOT; - else if (!tb_strcmp(rootkey, "HKCR")) key = HKEY_CLASSES_ROOT; - else if (!tb_strcmp(rootkey, "HKEY_CURRENT_CONFIG")) key = HKEY_CURRENT_CONFIG; - else if (!tb_strcmp(rootkey, "HKCC")) key = HKEY_CURRENT_CONFIG; - else if (!tb_strcmp(rootkey, "HKEY_CURRENT_USER")) key = HKEY_CURRENT_USER; - else if (!tb_strcmp(rootkey, "HKCU")) key = HKEY_CURRENT_USER; - else if (!tb_strcmp(rootkey, "HKEY_LOCAL_MACHINE")) key = HKEY_LOCAL_MACHINE; - else if (!tb_strcmp(rootkey, "HKLM")) key = HKEY_LOCAL_MACHINE; - else if (!tb_strcmp(rootkey, "HKEY_USERS")) key = HKEY_USERS; - else - { + if (!tb_strcmp(rootkey, "HKEY_CLASSES_ROOT")) { + key = HKEY_CLASSES_ROOT; + } else if (!tb_strcmp(rootkey, "HKCR")) { + key = HKEY_CLASSES_ROOT; + } else if (!tb_strcmp(rootkey, "HKEY_CURRENT_CONFIG")) { + key = HKEY_CURRENT_CONFIG; + } else if (!tb_strcmp(rootkey, "HKCC")) { + key = HKEY_CURRENT_CONFIG; + } else if (!tb_strcmp(rootkey, "HKEY_CURRENT_USER")) { + key = HKEY_CURRENT_USER; + } else if (!tb_strcmp(rootkey, "HKCU")) { + key = HKEY_CURRENT_USER; + } else if (!tb_strcmp(rootkey, "HKEY_LOCAL_MACHINE")) { + key = HKEY_LOCAL_MACHINE; + } else if (!tb_strcmp(rootkey, "HKLM")) { + key = HKEY_LOCAL_MACHINE; + } else if (!tb_strcmp(rootkey, "HKEY_USERS")) { + key = HKEY_USERS; + } else { lua_pushnil(lua); lua_pushfstring(lua, "invalid registry rootkey: %s", rootkey); break; } // open registry key - if (RegOpenKeyExA(key, rootdir, 0, KEY_QUERY_VALUE, &keynew) != ERROR_SUCCESS && keynew) - { + if (RegOpenKeyExA(key, rootdir, 0, KEY_QUERY_VALUE, &keynew) != ERROR_SUCCESS && keynew) { lua_pushnil(lua); lua_pushfstring(lua, "open registry key failed: %s\\%s", rootkey, rootdir); break; } // query values - DWORD value_name_num = 0; + DWORD value_name_num = 0; DWORD value_name_maxn = 0; - if (RegQueryInfoKeyW(keynew, tb_null, tb_null, tb_null, tb_null, tb_null, tb_null, &value_name_num, &value_name_maxn, tb_null, tb_null, tb_null) != ERROR_SUCCESS) - { + if (RegQueryInfoKeyW(keynew, + tb_null, + tb_null, + tb_null, + tb_null, + tb_null, + tb_null, + &value_name_num, + &value_name_maxn, + tb_null, + tb_null, + tb_null) != ERROR_SUCCESS) { lua_pushnil(lua); lua_pushfstring(lua, "query registry info failed: %s\\%s", rootkey, rootdir); break; @@ -98,23 +112,21 @@ tb_int_t xm_winos_registry_values(lua_State* lua) // ensure enough value name buffer tb_wchar_t value_name[1024]; - if (value_name_maxn > tb_arrayn(value_name)) - { + if (value_name_maxn > tb_arrayn(value_name)) { lua_pushnil(lua); lua_pushfstring(lua, "no enough value name buffer: %s\\%s", rootkey, rootdir); break; } // get all values - DWORD i = 0; + DWORD i = 0; tb_char_t value_name_a[1024]; - for (i = 0; i < value_name_num; i++) - { + for (i = 0; i < value_name_num; i++) { // get value name - value_name[0] = L'\0'; + value_name[0] = L'\0'; DWORD value_name_size = tb_arrayn(value_name); - if (RegEnumValueW(keynew, i, value_name, &value_name_size, tb_null, tb_null, tb_null, tb_null) != ERROR_SUCCESS) - { + if (RegEnumValueW(keynew, i, value_name, &value_name_size, tb_null, tb_null, tb_null, tb_null) != + ERROR_SUCCESS) { lua_pushnil(lua); lua_pushfstring(lua, "get registry value name(%d) failed: %s\\%s", i, rootkey, rootdir); break; @@ -122,8 +134,7 @@ tb_int_t xm_winos_registry_values(lua_State* lua) // get value name (mbs) tb_size_t value_name_a_size = tb_wtoa(value_name_a, value_name, sizeof(value_name_a)); - if (value_name_a_size == -1) - { + if (value_name_a_size == -1) { lua_pushnil(lua); lua_pushfstring(lua, "convert registry value name(%d) failed: %s\\%s", i, rootkey, rootdir); break; @@ -138,29 +149,27 @@ tb_int_t xm_winos_registry_values(lua_State* lua) // is continue? tb_bool_t is_continue = lua_toboolean(lua, -1); lua_pop(lua, 1); - if (!is_continue) - { + if (!is_continue) { ok = tb_true; break; } } - // ok if (i == value_name_num) ok = tb_true; } while (0); // exit registry key - if (keynew) + if (keynew) { RegCloseKey(keynew); + } keynew = tb_null; - // ok? - if (ok) - { + if (ok) { lua_pushinteger(lua, count); return 1; + } else { + return 2; } - else return 2; } diff --git a/core/src/xmake/winos/short_path.c b/core/src/xmake/winos/short_path.c index 5812a20da..9c2192787 100644 --- a/core/src/xmake/winos/short_path.c +++ b/core/src/xmake/winos/short_path.c @@ -22,8 +22,8 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * trace */ -#define TB_TRACE_MODULE_NAME "short_path" -#define TB_TRACE_MODULE_DEBUG (0) +#define TB_TRACE_MODULE_NAME "short_path" +#define TB_TRACE_MODULE_DEBUG (0) /* ////////////////////////////////////////////////////////////////////////////////////// * includes @@ -38,19 +38,16 @@ * * local short_path, errors = winos.short_path(long_path) */ -tb_int_t xm_winos_short_path(lua_State* lua) -{ - // check +tb_int_t xm_winos_short_path(lua_State *lua) { tb_assert_and_check_return_val(lua, 0); // get the arguments - tb_char_t const* long_path = luaL_checkstring(lua, 1); + tb_char_t const *long_path = luaL_checkstring(lua, 1); tb_check_return_val(long_path, 0); // convert long path to wide characters tb_wchar_t long_path_w[TB_PATH_MAXN]; - if (tb_atow(long_path_w, long_path, TB_PATH_MAXN) == (tb_size_t)-1) - { + if (tb_atow(long_path_w, long_path, TB_PATH_MAXN) == (tb_size_t)-1) { lua_pushnil(lua); lua_pushfstring(lua, "invalid long path: %s", long_path); return 2; @@ -58,18 +55,16 @@ tb_int_t xm_winos_short_path(lua_State* lua) // get short path tb_wchar_t short_path_w[TB_PATH_MAXN]; - if (GetShortPathNameW(long_path_w, short_path_w, TB_PATH_MAXN) == 0) - { + if (GetShortPathNameW(long_path_w, short_path_w, TB_PATH_MAXN) == 0) { lua_pushnil(lua); lua_pushfstring(lua, "cannot get short path from: %s", long_path); return 2; } // return result - tb_char_t* short_path_a = (tb_char_t*)long_path_w; + tb_char_t *short_path_a = (tb_char_t *)long_path_w; tb_size_t short_path_n = tb_wtoa(short_path_a, short_path_w, TB_PATH_MAXN); - if (short_path_n == (tb_size_t)-1) - { + if (short_path_n == (tb_size_t)-1) { lua_pushnil(lua); lua_pushfstring(lua, "invalid short path from %s!", long_path); return 2; diff --git a/core/src/xmake/xmake.c b/core/src/xmake/xmake.c index 3e7fee7fb..c673d776a 100644 --- a/core/src/xmake/xmake.c +++ b/core/src/xmake/xmake.c @@ -27,52 +27,42 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static __tb_inline__ tb_bool_t xm_check_mode(tb_size_t mode) -{ +static __tb_inline__ tb_bool_t xm_check_mode(tb_size_t mode) { #ifdef __xm_debug__ - if (!(mode & TB_MODE_DEBUG)) - { + if (!(mode & TB_MODE_DEBUG)) { tb_trace_e("libxmake.a has __tb_debug__ but xmake/xmake.h not"); return tb_false; } #else - if (mode & TB_MODE_DEBUG) - { + if (mode & TB_MODE_DEBUG) { tb_trace_e("xmake/xmake.h has __tb_debug__ but libxmake.a not"); return tb_false; } #endif #ifdef __xm_small__ - if (!(mode & TB_MODE_SMALL)) - { + if (!(mode & TB_MODE_SMALL)) { tb_trace_e("libxmake.a has __tb_small__ but xmake/xmake.h not"); return tb_false; } #else - if (mode & TB_MODE_SMALL) - { + if (mode & TB_MODE_SMALL) { tb_trace_e("xmake/xmake.h has __tb_small__ but libxmake.a not"); return tb_false; } #endif - // ok return tb_true; } -static __tb_inline__ tb_bool_t xm_version_check(tb_hize_t build) -{ +static __tb_inline__ tb_bool_t xm_version_check(tb_hize_t build) { // the version oly for link the static vtag string - tb_version_t const* version = xm_version(); tb_used(version); + tb_version_t const *version = xm_version(); + tb_used(version); - // ok - if ((build / 100) == (XM_VERSION_BUILD / 100)) - { + if ((build / 100) == (XM_VERSION_BUILD / 100)) { tb_trace_d("version: %s", XM_VERSION_STRING); return tb_true; - } - else - { + } else { tb_trace_w("version: %s != %llu", XM_VERSION_STRING, build); } @@ -83,13 +73,13 @@ static __tb_inline__ tb_bool_t xm_version_check(tb_hize_t build) /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_bool_t xm_init_(tb_size_t mode, tb_hize_t build) -{ - // trace +tb_bool_t xm_init_(tb_size_t mode, tb_hize_t build) { tb_trace_d("init: .."); // check mode - if (!xm_check_mode(mode)) return tb_false; + if (!xm_check_mode(mode)) { + return tb_false; + } // check version xm_version_check(build); @@ -99,29 +89,27 @@ 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)) return tb_false; + if (!tb_init(tb_null, tb_null)) { + return tb_false; + } #endif - // trace tb_trace_d("init: ok"); - // ok return tb_true; } -tb_void_t xm_exit() -{ +tb_void_t xm_exit() { // exit tbox tb_exit(); } -tb_version_t const* xm_version() -{ +tb_version_t const *xm_version() { // init version tag for binary search - static __tb_volatile__ tb_char_t const* s_vtag = "[xmake]: [vtag]: " XM_VERSION_STRING; tb_used(s_vtag); + static __tb_volatile__ tb_char_t const *s_vtag = "[xmake]: [vtag]: " XM_VERSION_STRING; + tb_used(s_vtag); // init version - static tb_version_t s_version = {0}; - if (!s_version.major) - { + static tb_version_t s_version = { 0 }; + if (!s_version.major) { s_version.major = XM_VERSION_MAJOR; s_version.minor = XM_VERSION_MINOR; s_version.alter = XM_VERSION_ALTER; @@ -130,4 +118,3 @@ tb_version_t const* xm_version() return &s_version; } - diff --git a/core/src/xmake/xmake.h b/core/src/xmake/xmake.h index 0d4c5bfcd..bdca229f7 100644 --- a/core/src/xmake/xmake.h +++ b/core/src/xmake/xmake.h @@ -37,15 +37,15 @@ __tb_extern_c_enter__ */ #ifdef __xm_debug__ -# define __xm_mode_debug__ TB_MODE_DEBUG +#define __xm_mode_debug__ TB_MODE_DEBUG #else -# define __xm_mode_debug__ (0) +#define __xm_mode_debug__ (0) #endif #ifdef __xm_small__ -# define __xm_mode_small__ TB_MODE_SMALL +#define __xm_mode_small__ TB_MODE_SMALL #else -# define __xm_mode_small__ (0) +#define __xm_mode_small__ (0) #endif /*! init xmake @@ -67,7 +67,7 @@ __tb_extern_c_enter__ } * @endcode */ -#define xm_init() xm_init_((tb_size_t)(__xm_mode_debug__ | __xm_mode_small__), XM_VERSION_BUILD) +#define xm_init() xm_init_((tb_size_t)(__xm_mode_debug__ | __xm_mode_small__), XM_VERSION_BUILD) /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces @@ -80,16 +80,17 @@ __tb_extern_c_enter__ * * @return tb_true or tb_false */ -tb_bool_t xm_init_(tb_size_t mode, tb_hize_t build); +tb_bool_t +xm_init_(tb_size_t mode, tb_hize_t build); /// exit the xmake library -tb_void_t xm_exit(tb_noarg_t); +tb_void_t xm_exit(tb_noarg_t); /*! the xmake version * * @return the xmake version */ -tb_version_t const* xm_version(tb_noarg_t); +tb_version_t const *xm_version(tb_noarg_t); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/tests/apis/add_allowedxxx/src/main.cpp b/tests/apis/add_allowedxxx/src/main.cpp index 7c435d251..55d2f380a 100644 --- a/tests/apis/add_allowedxxx/src/main.cpp +++ b/tests/apis/add_allowedxxx/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/apis/add_configfiles/main.c b/tests/apis/add_configfiles/main.c index 59d3c5669..139483db0 100644 --- a/tests/apis/add_configfiles/main.c +++ b/tests/apis/add_configfiles/main.c @@ -2,8 +2,7 @@ #include <stdlib.h> #include "config.h" -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { printf("hello %s\n", HELLO); return 0; } diff --git a/tests/apis/add_configfiles/main2.c b/tests/apis/add_configfiles/main2.c index 2bd25b908..c961d4b4e 100644 --- a/tests/apis/add_configfiles/main2.c +++ b/tests/apis/add_configfiles/main2.c @@ -2,8 +2,7 @@ #include <stdlib.h> #include "config2.h" -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { printf("hello2 %s\n", HELLO2); return 0; } diff --git a/tests/apis/add_defines/src/main.cpp b/tests/apis/add_defines/src/main.cpp index d16b296dc..b92ee9f34 100644 --- a/tests/apis/add_defines/src/main.cpp +++ b/tests/apis/add_defines/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << TEST1 << endl; cout << TEST2 << endl; cout << TEST3 << endl; diff --git a/tests/apis/add_deps/src/interface.c b/tests/apis/add_deps/src/interface.c index 598d07560..00d3b28d6 100644 --- a/tests/apis/add_deps/src/interface.c +++ b/tests/apis/add_deps/src/interface.c @@ -1,6 +1,5 @@ #include "interface.h" -int add(int a, int b) -{ +int add(int a, int b) { return a + b; } diff --git a/tests/apis/add_deps/src/interface.h b/tests/apis/add_deps/src/interface.h index ef24ba3ce..b1e1216f9 100644 --- a/tests/apis/add_deps/src/interface.h +++ b/tests/apis/add_deps/src/interface.h @@ -3,11 +3,11 @@ extern "C" { #endif #if defined(_WIN32) -# define __export __declspec(dllexport) +#define __export __declspec(dllexport) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) -# define __export __attribute__((visibility("default"))) +#define __export __attribute__((visibility("default"))) #else -# define __export +#define __export #endif /*! calculate add(a, b) @@ -17,7 +17,7 @@ extern "C" { * * @return the result */ -__export int add(int a, int b); +__export int add(int a, int b); #ifdef __cplusplus } diff --git a/tests/apis/add_deps/src/main.c b/tests/apis/add_deps/src/main.c index 113930e1a..ccf4b2a77 100644 --- a/tests/apis/add_deps/src/main.c +++ b/tests/apis/add_deps/src/main.c @@ -1,6 +1,6 @@ #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("hello world!\n"); return 0; } diff --git a/tests/apis/check_xxx/foo.c b/tests/apis/check_xxx/foo.c index c055f5886..df779f2e6 100644 --- a/tests/apis/check_xxx/foo.c +++ b/tests/apis/check_xxx/foo.c @@ -1,6 +1,5 @@ #include "config.h" -int foo() -{ +int foo() { return 0; } diff --git a/tests/apis/check_xxx/main.c b/tests/apis/check_xxx/main.c index 9b130982d..11b7fad8e 100644 --- a/tests/apis/check_xxx/main.c +++ b/tests/apis/check_xxx/main.c @@ -1,3 +1,3 @@ -int main(int argc, char** argv) { +int main(int argc, char **argv) { return 0; } diff --git a/tests/apis/clone_target/src/main.cpp b/tests/apis/clone_target/src/main.cpp index db2361659..55d2f380a 100644 --- a/tests/apis/clone_target/src/main.cpp +++ b/tests/apis/clone_target/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ - cout << "hello world!" << endl; +int main(int argc, char **argv) { + cout << "hello world!" << endl; return 0; } diff --git a/tests/apis/custom_scopeapis/src/main.cpp b/tests/apis/custom_scopeapis/src/main.cpp index 7c435d251..55d2f380a 100644 --- a/tests/apis/custom_scopeapis/src/main.cpp +++ b/tests/apis/custom_scopeapis/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/apis/custom_toolchain/src/test.cpp b/tests/apis/custom_toolchain/src/test.cpp index 7cdb7d93f..66905a94c 100644 --- a/tests/apis/custom_toolchain/src/test.cpp +++ b/tests/apis/custom_toolchain/src/test.cpp @@ -1,5 +1,5 @@ #include "foo.h" -int main(int argc, char** argv) { +int main(int argc, char **argv) { return add(1, 2); } diff --git a/tests/apis/namespace/basic/src/main.cpp b/tests/apis/namespace/basic/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/basic/src/main.cpp +++ b/tests/apis/namespace/basic/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/includes/src/main.cpp b/tests/apis/namespace/includes/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/includes/src/main.cpp +++ b/tests/apis/namespace/includes/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/inner/src/main.cpp b/tests/apis/namespace/inner/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/inner/src/main.cpp +++ b/tests/apis/namespace/inner/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/nested/src/main.cpp b/tests/apis/namespace/nested/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/nested/src/main.cpp +++ b/tests/apis/namespace/nested/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/option/src/main.cpp b/tests/apis/namespace/option/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/option/src/main.cpp +++ b/tests/apis/namespace/option/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/package/src/main.cpp b/tests/apis/namespace/package/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/package/src/main.cpp +++ b/tests/apis/namespace/package/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/root/src/main.cpp b/tests/apis/namespace/root/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/root/src/main.cpp +++ b/tests/apis/namespace/root/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/rule/src/main.cpp b/tests/apis/namespace/rule/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/rule/src/main.cpp +++ b/tests/apis/namespace/rule/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/namespace/toolchain/src/main.cpp b/tests/apis/namespace/toolchain/src/main.cpp index 4cf7b5e62..b2c84c4bd 100644 --- a/tests/apis/namespace/toolchain/src/main.cpp +++ b/tests/apis/namespace/toolchain/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; std::cout << "sub(2, 1) = " << sub(2, 1) << std::endl; return 0; diff --git a/tests/apis/rules/src/main.c b/tests/apis/rules/src/main.c index b86c9ad8e..14d033f7a 100644 --- a/tests/apis/rules/src/main.c +++ b/tests/apis/rules/src/main.c @@ -1,8 +1,7 @@ #include <stdio.h> extern int test(int a, int b); -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { printf("1 + 1 = %d\n", test(1, 1)); return 0; } diff --git a/tests/apis/rules/src/main2.c b/tests/apis/rules/src/main2.c index 9a8264b1f..14d033f7a 100644 --- a/tests/apis/rules/src/main2.c +++ b/tests/apis/rules/src/main2.c @@ -1,7 +1,7 @@ #include <stdio.h> extern int test(int a, int b); -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("1 + 1 = %d\n", test(1, 1)); return 0; } diff --git a/tests/apis/rules_inject_deps/src/main.cpp b/tests/apis/rules_inject_deps/src/main.cpp index db2361659..55d2f380a 100644 --- a/tests/apis/rules_inject_deps/src/main.cpp +++ b/tests/apis/rules_inject_deps/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ - cout << "hello world!" << endl; +int main(int argc, char **argv) { + cout << "hello world!" << endl; return 0; } diff --git a/tests/apis/rules_order/src/main.c b/tests/apis/rules_order/src/main.c index d612d8782..e6173ef04 100644 --- a/tests/apis/rules_order/src/main.c +++ b/tests/apis/rules_order/src/main.c @@ -1,6 +1,5 @@ #include <stdio.h> -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { return 0; } diff --git a/tests/apis/set_toolchains/src/test.cpp b/tests/apis/set_toolchains/src/test.cpp index ed1924789..41e494481 100644 --- a/tests/apis/set_toolchains/src/test.cpp +++ b/tests/apis/set_toolchains/src/test.cpp @@ -3,7 +3,7 @@ using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << "add(1, 2) = " << add(1, 2) << endl; return 0; } diff --git a/tests/apis/target_get_from/src/main.cpp b/tests/apis/target_get_from/src/main.cpp index ed1924789..41e494481 100644 --- a/tests/apis/target_get_from/src/main.cpp +++ b/tests/apis/target_get_from/src/main.cpp @@ -3,7 +3,7 @@ using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << "add(1, 2) = " << add(1, 2) << endl; return 0; } diff --git a/tests/plugins/macro/src/main.cpp b/tests/plugins/macro/src/main.cpp index 7c775d288..9b6abffea 100644 --- a/tests/plugins/macro/src/main.cpp +++ b/tests/plugins/macro/src/main.cpp @@ -1,6 +1,6 @@ #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "hello world!" << std::endl; return 0; } diff --git a/tests/plugins/pack/src/main.cpp b/tests/plugins/pack/src/main.cpp index e64ad9bc2..55d2f380a 100644 --- a/tests/plugins/pack/src/main.cpp +++ b/tests/plugins/pack/src/main.cpp @@ -2,7 +2,7 @@ using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/c++/console_zig_cxx/src/main.cpp b/tests/projects/c++/console_zig_cxx/src/main.cpp index 7c435d251..55d2f380a 100644 --- a/tests/projects/c++/console_zig_cxx/src/main.cpp +++ b/tests/projects/c++/console_zig_cxx/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/c++/doctest/src/main.cpp b/tests/projects/c++/doctest/src/main.cpp index 7c435d251..55d2f380a 100644 --- a/tests/projects/c++/doctest/src/main.cpp +++ b/tests/projects/c++/doctest/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/c++/manifest/src/main.cpp b/tests/projects/c++/manifest/src/main.cpp index db2361659..55d2f380a 100644 --- a/tests/projects/c++/manifest/src/main.cpp +++ b/tests/projects/c++/manifest/src/main.cpp @@ -2,8 +2,7 @@ using namespace std; -int main(int argc, char** argv) -{ - cout << "hello world!" << endl; +int main(int argc, char **argv) { + cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/c++/modules/class/src/hello_impl.cpp b/tests/projects/c++/modules/class/src/hello_impl.cpp index 5dd555a7a..b09f4b96c 100644 --- a/tests/projects/c++/modules/class/src/hello_impl.cpp +++ b/tests/projects/c++/modules/class/src/hello_impl.cpp @@ -5,10 +5,9 @@ module hello; using namespace std; namespace hello { - say::say(int data) : data_(data) { - - } - void say::hello() { - cout << "hello, say class: " << data_ << endl; - } +say::say(int data) : data_(data) { +} +void say::hello() { + cout << "hello, say class: " << data_ << endl; } +} // namespace hello diff --git a/tests/projects/c++/modules/class_cmake/src/hello_impl.cpp b/tests/projects/c++/modules/class_cmake/src/hello_impl.cpp index 5dd555a7a..b09f4b96c 100644 --- a/tests/projects/c++/modules/class_cmake/src/hello_impl.cpp +++ b/tests/projects/c++/modules/class_cmake/src/hello_impl.cpp @@ -5,10 +5,9 @@ module hello; using namespace std; namespace hello { - say::say(int data) : data_(data) { - - } - void say::hello() { - cout << "hello, say class: " << data_ << endl; - } +say::say(int data) : data_(data) { +} +void say::hello() { + cout << "hello, say class: " << data_ << endl; } +} // namespace hello diff --git a/tests/projects/c++/modules/class_cmake/src/main.cpp b/tests/projects/c++/modules/class_cmake/src/main.cpp index b96807bda..87b8b92f7 100644 --- a/tests/projects/c++/modules/class_cmake/src/main.cpp +++ b/tests/projects/c++/modules/class_cmake/src/main.cpp @@ -1,7 +1,7 @@ import hello; int main() { - hello::say s(sizeof(hello::say)); - s.hello(); - return 0; + hello::say s(sizeof(hello::say)); + s.hello(); + return 0; } diff --git a/tests/projects/c++/modules/dependence/src/hello_impl.cpp b/tests/projects/c++/modules/dependence/src/hello_impl.cpp index 064afebad..b7f9baaa3 100644 --- a/tests/projects/c++/modules/dependence/src/hello_impl.cpp +++ b/tests/projects/c++/modules/dependence/src/hello_impl.cpp @@ -5,19 +5,20 @@ module hello; import mod; void inner() { - std::cout << "hello world! data: " - << mod::foo() << std::endl; + std::cout << "hello world! data: " << mod::foo() << std::endl; } namespace hello { - int data__; - void say_hello() { ::inner(); } +int data__; +void say_hello() { + ::inner(); +} - say::say(int data) : data_{data} { - } +say::say(int data) : data_{ data } { +} - void say::hello() { - hello::data__ = data_; - ::inner(); - } +void say::hello() { + hello::data__ = data_; + ::inner(); } +} // namespace hello diff --git a/tests/projects/c++/modules/dependence/src/main.cpp b/tests/projects/c++/modules/dependence/src/main.cpp index 93cc427a4..470c69ebc 100644 --- a/tests/projects/c++/modules/dependence/src/main.cpp +++ b/tests/projects/c++/modules/dependence/src/main.cpp @@ -6,4 +6,3 @@ int main() { hello::say(sizeof(hello::say)).hello(); return 0; } - diff --git a/tests/projects/c++/modules/dependence/src/mod_impl.cpp b/tests/projects/c++/modules/dependence/src/mod_impl.cpp index 1adf6570e..aabac7648 100644 --- a/tests/projects/c++/modules/dependence/src/mod_impl.cpp +++ b/tests/projects/c++/modules/dependence/src/mod_impl.cpp @@ -2,7 +2,7 @@ module mod; import hello; namespace mod { - int foo() { - return hello::data__; - } +int foo() { + return hello::data__; } +} // namespace mod diff --git a/tests/projects/c++/modules/dependence2/src/main.cpp b/tests/projects/c++/modules/dependence2/src/main.cpp index b5c04f1b2..a74024e11 100644 --- a/tests/projects/c++/modules/dependence2/src/main.cpp +++ b/tests/projects/c++/modules/dependence2/src/main.cpp @@ -7,4 +7,3 @@ int main() { printf("sub(1, 2): %d\n", foo::sub(1, 2)); return 0; } - diff --git a/tests/projects/c++/modules/dependency_flag_update2/src/main.cpp b/tests/projects/c++/modules/dependency_flag_update2/src/main.cpp index d34cba080..8629450ac 100644 --- a/tests/projects/c++/modules/dependency_flag_update2/src/main.cpp +++ b/tests/projects/c++/modules/dependency_flag_update2/src/main.cpp @@ -1,6 +1,6 @@ import foobar; int main() { - hello(); - return 0; + hello(); + return 0; } diff --git a/tests/projects/c++/modules/headerunits_person/src/test.cpp b/tests/projects/c++/modules/headerunits_person/src/test.cpp index d12206ef7..17a7fb34d 100644 --- a/tests/projects/c++/modules/headerunits_person/src/test.cpp +++ b/tests/projects/c++/modules/headerunits_person/src/test.cpp @@ -1,11 +1,10 @@ import person;
import <iostream>;
-import <string>; // For operator<< for std::string
+import <string>; // For operator<< for std::string
using namespace std;
-int main()
-{
+int main() {
Person person{ "Kole", "Webb" };
cout << person.getLastName() << ", " << person.getFirstName() << endl;
}
diff --git a/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp b/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp index 1d385dcfa..a3447b492 100644 --- a/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp +++ b/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp @@ -2,9 +2,8 @@ import foo; -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("add(1, 2): %d\n", foo::add(1, 2)); printf("sub(1, 2): %d\n", foo::sub(1, 2)); return 0; } - diff --git a/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp b/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp index eccfed5d3..fa17673ab 100644 --- a/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp +++ b/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp @@ -5,15 +5,15 @@ module hello; using namespace std; namespace hello { - void say_hi() { - cout << "hello hi!" << endl; - } +void say_hi() { + cout << "hello hi!" << endl; +} - void say_hello() { - cout << "hello world!" << endl; - } +void say_hello() { + cout << "hello world!" << endl; +} - void say_xz() { - cout << "hello xz!" << endl; - } +void say_xz() { + cout << "hello xz!" << endl; } +} // namespace hello diff --git a/tests/projects/c++/modules/moduleonly_private_dep/src/main.cpp b/tests/projects/c++/modules/moduleonly_private_dep/src/main.cpp index 0fdfc6d11..1ad0c5d8e 100644 --- a/tests/projects/c++/modules/moduleonly_private_dep/src/main.cpp +++ b/tests/projects/c++/modules/moduleonly_private_dep/src/main.cpp @@ -1,5 +1,5 @@ import B; int main() { - return func(); + return func(); } diff --git a/tests/projects/c++/modules/moduleonly_private_dep/src/modB.cpp b/tests/projects/c++/modules/moduleonly_private_dep/src/modB.cpp index 6e7a8de49..fb3c7a97a 100644 --- a/tests/projects/c++/modules/moduleonly_private_dep/src/modB.cpp +++ b/tests/projects/c++/modules/moduleonly_private_dep/src/modB.cpp @@ -3,5 +3,5 @@ module B; import A; int func() { - return foo; + return foo; } diff --git a/tests/projects/c++/modules/namespace/src/hello_impl.cpp b/tests/projects/c++/modules/namespace/src/hello_impl.cpp index 064afebad..b7f9baaa3 100644 --- a/tests/projects/c++/modules/namespace/src/hello_impl.cpp +++ b/tests/projects/c++/modules/namespace/src/hello_impl.cpp @@ -5,19 +5,20 @@ module hello; import mod; void inner() { - std::cout << "hello world! data: " - << mod::foo() << std::endl; + std::cout << "hello world! data: " << mod::foo() << std::endl; } namespace hello { - int data__; - void say_hello() { ::inner(); } +int data__; +void say_hello() { + ::inner(); +} - say::say(int data) : data_{data} { - } +say::say(int data) : data_{ data } { +} - void say::hello() { - hello::data__ = data_; - ::inner(); - } +void say::hello() { + hello::data__ = data_; + ::inner(); } +} // namespace hello diff --git a/tests/projects/c++/modules/namespace/src/main.cpp b/tests/projects/c++/modules/namespace/src/main.cpp index 93cc427a4..470c69ebc 100644 --- a/tests/projects/c++/modules/namespace/src/main.cpp +++ b/tests/projects/c++/modules/namespace/src/main.cpp @@ -6,4 +6,3 @@ int main() { hello::say(sizeof(hello::say)).hello(); return 0; } - diff --git a/tests/projects/c++/modules/namespace/src/mod_impl.cpp b/tests/projects/c++/modules/namespace/src/mod_impl.cpp index 1adf6570e..aabac7648 100644 --- a/tests/projects/c++/modules/namespace/src/mod_impl.cpp +++ b/tests/projects/c++/modules/namespace/src/mod_impl.cpp @@ -2,7 +2,7 @@ module mod; import hello; namespace mod { - int foo() { - return hello::data__; - } +int foo() { + return hello::data__; } +} // namespace mod diff --git a/tests/projects/c++/modules/namespace2/src/hello_impl.cpp b/tests/projects/c++/modules/namespace2/src/hello_impl.cpp index 064afebad..b7f9baaa3 100644 --- a/tests/projects/c++/modules/namespace2/src/hello_impl.cpp +++ b/tests/projects/c++/modules/namespace2/src/hello_impl.cpp @@ -5,19 +5,20 @@ module hello; import mod; void inner() { - std::cout << "hello world! data: " - << mod::foo() << std::endl; + std::cout << "hello world! data: " << mod::foo() << std::endl; } namespace hello { - int data__; - void say_hello() { ::inner(); } +int data__; +void say_hello() { + ::inner(); +} - say::say(int data) : data_{data} { - } +say::say(int data) : data_{ data } { +} - void say::hello() { - hello::data__ = data_; - ::inner(); - } +void say::hello() { + hello::data__ = data_; + ::inner(); } +} // namespace hello diff --git a/tests/projects/c++/modules/namespace2/src/main.cpp b/tests/projects/c++/modules/namespace2/src/main.cpp index 93cc427a4..470c69ebc 100644 --- a/tests/projects/c++/modules/namespace2/src/main.cpp +++ b/tests/projects/c++/modules/namespace2/src/main.cpp @@ -6,4 +6,3 @@ int main() { hello::say(sizeof(hello::say)).hello(); return 0; } - diff --git a/tests/projects/c++/modules/namespace2/src/mod_impl.cpp b/tests/projects/c++/modules/namespace2/src/mod_impl.cpp index 1adf6570e..aabac7648 100644 --- a/tests/projects/c++/modules/namespace2/src/mod_impl.cpp +++ b/tests/projects/c++/modules/namespace2/src/mod_impl.cpp @@ -2,7 +2,7 @@ module mod; import hello; namespace mod { - int foo() { - return hello::data__; - } +int foo() { + return hello::data__; } +} // namespace mod diff --git a/tests/projects/c++/modules/packages-subtarget/my-repo/packages/f/foo/src/foo.cpp b/tests/projects/c++/modules/packages-subtarget/my-repo/packages/f/foo/src/foo.cpp index e5565a01a..22b4cec59 100644 --- a/tests/projects/c++/modules/packages-subtarget/my-repo/packages/f/foo/src/foo.cpp +++ b/tests/projects/c++/modules/packages-subtarget/my-repo/packages/f/foo/src/foo.cpp @@ -1,6 +1,6 @@ module foo; namespace foo { - void say(const char *msg) { - } +void say(const char *msg) { } +} // namespace foo diff --git a/tests/projects/c++/modules/packages/my-repo/packages/f/foo/src/foo.cpp b/tests/projects/c++/modules/packages/my-repo/packages/f/foo/src/foo.cpp index e5565a01a..22b4cec59 100644 --- a/tests/projects/c++/modules/packages/my-repo/packages/f/foo/src/foo.cpp +++ b/tests/projects/c++/modules/packages/my-repo/packages/f/foo/src/foo.cpp @@ -1,6 +1,6 @@ module foo; namespace foo { - void say(const char *msg) { - } +void say(const char *msg) { } +} // namespace foo diff --git a/tests/projects/c++/modules/partitions/src/main.cpp b/tests/projects/c++/modules/partitions/src/main.cpp index 058dfd522..898d28058 100644 --- a/tests/projects/c++/modules/partitions/src/main.cpp +++ b/tests/projects/c++/modules/partitions/src/main.cpp @@ -3,7 +3,6 @@ import math; int main() { - std::cout << std::endl; std::cout << "add(3, 4): " << add(3, 4) << std::endl; diff --git a/tests/projects/c++/modules/partitions_implunit/src/foo.cpp b/tests/projects/c++/modules/partitions_implunit/src/foo.cpp index cc4c0c3fe..85fcc3b63 100644 --- a/tests/projects/c++/modules/partitions_implunit/src/foo.cpp +++ b/tests/projects/c++/modules/partitions_implunit/src/foo.cpp @@ -3,4 +3,6 @@ module Foo; // Impl is implictly imported here
-void foo() { bar(); }
+void foo() {
+ bar();
+}
diff --git a/tests/projects/c++/modules/partitions_implunit/src/main.cpp b/tests/projects/c++/modules/partitions_implunit/src/main.cpp index acef780c2..9d38b891e 100644 --- a/tests/projects/c++/modules/partitions_implunit/src/main.cpp +++ b/tests/projects/c++/modules/partitions_implunit/src/main.cpp @@ -1,6 +1,6 @@ import Foo;
-int main(int argc, char** argv) {
+int main(int argc, char **argv) {
foo();
return 0;
}
diff --git a/tests/projects/c++/modules/partitions_implunit2/src/main.cpp b/tests/projects/c++/modules/partitions_implunit2/src/main.cpp index acef780c2..9d38b891e 100644 --- a/tests/projects/c++/modules/partitions_implunit2/src/main.cpp +++ b/tests/projects/c++/modules/partitions_implunit2/src/main.cpp @@ -1,6 +1,6 @@ import Foo;
-int main(int argc, char** argv) {
+int main(int argc, char **argv) {
foo();
return 0;
}
diff --git a/tests/projects/c++/modules/phony/src/hello.cpp b/tests/projects/c++/modules/phony/src/hello.cpp index 38ecf20e3..fe5943e3c 100644 --- a/tests/projects/c++/modules/phony/src/hello.cpp +++ b/tests/projects/c++/modules/phony/src/hello.cpp @@ -7,11 +7,10 @@ module hello; using namespace std; namespace hello { - say::say(int data) : data_(data) { - - } +say::say(int data) : data_(data) { +} - void say::hello() { - cout << "hello, say class: " << data_ << endl; - } +void say::hello() { + cout << "hello, say class: " << data_ << endl; +} } // namespace hello diff --git a/tests/projects/c++/modules/phony2/include/foo.h b/tests/projects/c++/modules/phony2/include/foo.h index e66099202..025d24b7c 100644 --- a/tests/projects/c++/modules/phony2/include/foo.h +++ b/tests/projects/c++/modules/phony2/include/foo.h @@ -1 +1,3 @@ -inline int foo() { return 5; } +inline int foo() { + return 5; +} diff --git a/tests/projects/c++/modules/phony2/src/hello.cpp b/tests/projects/c++/modules/phony2/src/hello.cpp index 38ecf20e3..fe5943e3c 100644 --- a/tests/projects/c++/modules/phony2/src/hello.cpp +++ b/tests/projects/c++/modules/phony2/src/hello.cpp @@ -7,11 +7,10 @@ module hello; using namespace std; namespace hello { - say::say(int data) : data_(data) { - - } +say::say(int data) : data_(data) { +} - void say::hello() { - cout << "hello, say class: " << data_ << endl; - } +void say::hello() { + cout << "hello, say class: " << data_ << endl; +} } // namespace hello diff --git a/tests/projects/c++/modules/staticlib/src/main.cpp b/tests/projects/c++/modules/staticlib/src/main.cpp index af2c328eb..ea199d05c 100644 --- a/tests/projects/c++/modules/staticlib/src/main.cpp +++ b/tests/projects/c++/modules/staticlib/src/main.cpp @@ -5,4 +5,3 @@ int main() { printf("%d\n", mod::foo()); return 0; } - diff --git a/tests/projects/c++/modules/staticlib/src/mod.cpp b/tests/projects/c++/modules/staticlib/src/mod.cpp index 9c7cf5290..34e0d0872 100644 --- a/tests/projects/c++/modules/staticlib/src/mod.cpp +++ b/tests/projects/c++/modules/staticlib/src/mod.cpp @@ -1,7 +1,7 @@ module mod; namespace mod { - int foo() { - return 2; - } +int foo() { + return 2; } +} // namespace mod diff --git a/tests/projects/c++/modules/staticlib2/src/bar.cpp b/tests/projects/c++/modules/staticlib2/src/bar.cpp index 0b5385096..4d62c82be 100644 --- a/tests/projects/c++/modules/staticlib2/src/bar.cpp +++ b/tests/projects/c++/modules/staticlib2/src/bar.cpp @@ -1,8 +1,7 @@ module bar; namespace bar { - int hello() { - return 2; - } +int hello() { + return 2; } - +} // namespace bar diff --git a/tests/projects/c++/modules/staticlib2/src/foo.cpp b/tests/projects/c++/modules/staticlib2/src/foo.cpp index 254371283..a72dfe3ca 100644 --- a/tests/projects/c++/modules/staticlib2/src/foo.cpp +++ b/tests/projects/c++/modules/staticlib2/src/foo.cpp @@ -3,8 +3,7 @@ module foo; import bar; namespace foo { - int hello() { - return bar::hello(); - } +int hello() { + return bar::hello(); } - +} // namespace foo diff --git a/tests/projects/c++/modules/staticlib2/src/main.cpp b/tests/projects/c++/modules/staticlib2/src/main.cpp index 69e91b126..0c8bebf35 100644 --- a/tests/projects/c++/modules/staticlib2/src/main.cpp +++ b/tests/projects/c++/modules/staticlib2/src/main.cpp @@ -5,4 +5,3 @@ int main() { printf("%d\n", foo::hello()); return 0; } - diff --git a/tests/projects/c++/modules/stdmodules/src/my_module.cpp b/tests/projects/c++/modules/stdmodules/src/my_module.cpp index ab3119785..786ae3a9d 100644 --- a/tests/projects/c++/modules/stdmodules/src/my_module.cpp +++ b/tests/projects/c++/modules/stdmodules/src/my_module.cpp @@ -2,4 +2,6 @@ module my_module; import std; -auto my_sum(std::size_t a, std::size_t b) -> std::size_t { return a + b; } +auto my_sum(std::size_t a, std::size_t b) -> std::size_t { + return a + b; +} diff --git a/tests/projects/c++/modules/stdmodules/test/test.cpp b/tests/projects/c++/modules/stdmodules/test/test.cpp index 7c6f7b4df..960b9c3f6 100644 --- a/tests/projects/c++/modules/stdmodules/test/test.cpp +++ b/tests/projects/c++/modules/stdmodules/test/test.cpp @@ -4,6 +4,6 @@ import my_module; using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << my_sum(1, 1) << endl; } diff --git a/tests/projects/c++/modules/stdmodules_cpp_only/src/main.cpp b/tests/projects/c++/modules/stdmodules_cpp_only/src/main.cpp index 477d7c742..b4a8e6c26 100644 --- a/tests/projects/c++/modules/stdmodules_cpp_only/src/main.cpp +++ b/tests/projects/c++/modules/stdmodules_cpp_only/src/main.cpp @@ -2,8 +2,7 @@ import std; using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/c++/modules/stdmodules_deps/src/foo.cpp b/tests/projects/c++/modules/stdmodules_deps/src/foo.cpp index c1e34dec6..59fd74153 100644 --- a/tests/projects/c++/modules/stdmodules_deps/src/foo.cpp +++ b/tests/projects/c++/modules/stdmodules_deps/src/foo.cpp @@ -1,3 +1,5 @@ module foo; -auto my_sum(std::size_t a, std::size_t b) -> std::size_t { return a + b; } +auto my_sum(std::size_t a, std::size_t b) -> std::size_t { + return a + b; +} diff --git a/tests/projects/c++/modules/stdmodules_deps/src/main.cpp b/tests/projects/c++/modules/stdmodules_deps/src/main.cpp index e40a369fa..5806de710 100644 --- a/tests/projects/c++/modules/stdmodules_deps/src/main.cpp +++ b/tests/projects/c++/modules/stdmodules_deps/src/main.cpp @@ -4,6 +4,6 @@ import bar; import std; int main() { - std::cout << my_sum(1, 2) << ":" << my_sum2(1, 2) << std::endl; - return 0; + std::cout << my_sum(1, 2) << ":" << my_sum2(1, 2) << std::endl; + return 0; } diff --git a/tests/projects/c++/modules/stdmodules_multiple_targets/src/my_module.cpp b/tests/projects/c++/modules/stdmodules_multiple_targets/src/my_module.cpp index ab3119785..786ae3a9d 100644 --- a/tests/projects/c++/modules/stdmodules_multiple_targets/src/my_module.cpp +++ b/tests/projects/c++/modules/stdmodules_multiple_targets/src/my_module.cpp @@ -2,4 +2,6 @@ module my_module; import std; -auto my_sum(std::size_t a, std::size_t b) -> std::size_t { return a + b; } +auto my_sum(std::size_t a, std::size_t b) -> std::size_t { + return a + b; +} diff --git a/tests/projects/c++/modules/submodules/src/main.cpp b/tests/projects/c++/modules/submodules/src/main.cpp index 5ff18701f..2ba0a3212 100644 --- a/tests/projects/c++/modules/submodules/src/main.cpp +++ b/tests/projects/c++/modules/submodules/src/main.cpp @@ -2,7 +2,6 @@ import math; int main() { - std::cout << std::endl; std::cout << "add(3, 4): " << add(3, 4) << std::endl; diff --git a/tests/projects/c++/modules/submodules2/src/main.cpp b/tests/projects/c++/modules/submodules2/src/main.cpp index ae58a08a3..db98c4d81 100644 --- a/tests/projects/c++/modules/submodules2/src/main.cpp +++ b/tests/projects/c++/modules/submodules2/src/main.cpp @@ -1,6 +1,6 @@ import main_module; -int main(int argc, char** argv) { +int main(int argc, char **argv) { hello(); return 0; } diff --git a/tests/projects/c++/modules/submodules2/src/module2.cpp b/tests/projects/c++/modules/submodules2/src/module2.cpp index 6ce9f9da9..09e195d39 100644 --- a/tests/projects/c++/modules/submodules2/src/module2.cpp +++ b/tests/projects/c++/modules/submodules2/src/module2.cpp @@ -6,4 +6,3 @@ module main_module.module2; void hello2(void) { printf("hello2\n"); } - diff --git a/tests/projects/c++/modules/xmake_tests1/src/main.cpp b/tests/projects/c++/modules/xmake_tests1/src/main.cpp index 7a7488a3b..a7fb45f9e 100644 --- a/tests/projects/c++/modules/xmake_tests1/src/main.cpp +++ b/tests/projects/c++/modules/xmake_tests1/src/main.cpp @@ -1,3 +1,5 @@ import Foo; -int main() { return foo(); } +int main() { + return foo(); +} diff --git a/tests/projects/c++/modules/xmake_tests2/src/main.cpp b/tests/projects/c++/modules/xmake_tests2/src/main.cpp index 7a7488a3b..a7fb45f9e 100644 --- a/tests/projects/c++/modules/xmake_tests2/src/main.cpp +++ b/tests/projects/c++/modules/xmake_tests2/src/main.cpp @@ -1,3 +1,5 @@ import Foo; -int main() { return foo(); } +int main() { + return foo(); +} diff --git a/tests/projects/c++/precompiled_header/src/header.h b/tests/projects/c++/precompiled_header/src/header.h index affda55ca..910e73544 100644 --- a/tests/projects/c++/precompiled_header/src/header.h +++ b/tests/projects/c++/precompiled_header/src/header.h @@ -1,7 +1,7 @@ // header.h #ifndef HEADER_H #define HEADER_H - + #include <algorithm> #include <deque> #include <iostream> @@ -24,4 +24,3 @@ #include <iomanip> #endif - diff --git a/tests/projects/c++/precompiled_header/src/main.cpp b/tests/projects/c++/precompiled_header/src/main.cpp index 27d41ee50..bfce631e2 100644 --- a/tests/projects/c++/precompiled_header/src/main.cpp +++ b/tests/projects/c++/precompiled_header/src/main.cpp @@ -1,7 +1,6 @@ #include "header.h" -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { std::string s("xmake"); printf("hello %s!\n", s.c_str()); return 0; diff --git a/tests/projects/c++/precompiled_header/src/test.c b/tests/projects/c++/precompiled_header/src/test.c index 7f0aa2bf4..6b15d3d8a 100644 --- a/tests/projects/c++/precompiled_header/src/test.c +++ b/tests/projects/c++/precompiled_header/src/test.c @@ -1,4 +1,3 @@ -void test_c(void) -{ +void test_c(void) { } diff --git a/tests/projects/c++/precompiled_header/src/test.cpp b/tests/projects/c++/precompiled_header/src/test.cpp index e9d6c83d9..4a526251e 100644 --- a/tests/projects/c++/precompiled_header/src/test.cpp +++ b/tests/projects/c++/precompiled_header/src/test.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test() -{ +int test() { return 0; } diff --git a/tests/projects/c++/precompiled_header/src/test2.cpp b/tests/projects/c++/precompiled_header/src/test2.cpp index 596359cd2..0d9d9240f 100644 --- a/tests/projects/c++/precompiled_header/src/test2.cpp +++ b/tests/projects/c++/precompiled_header/src/test2.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test2() -{ +int test2() { return 0; } diff --git a/tests/projects/c++/precompiled_header/src/test4.cpp b/tests/projects/c++/precompiled_header/src/test4.cpp index a92803eb7..ebcbe51e0 100644 --- a/tests/projects/c++/precompiled_header/src/test4.cpp +++ b/tests/projects/c++/precompiled_header/src/test4.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test4() -{ +int test4() { return 0; } diff --git a/tests/projects/c++/precompiled_header/src/test5.cpp b/tests/projects/c++/precompiled_header/src/test5.cpp index 2a3e7066c..c2193ac84 100644 --- a/tests/projects/c++/precompiled_header/src/test5.cpp +++ b/tests/projects/c++/precompiled_header/src/test5.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test5() -{ +int test5() { return 0; } diff --git a/tests/projects/c++/precompiled_header/src/test6.cpp b/tests/projects/c++/precompiled_header/src/test6.cpp index efbd7c6c1..03dd19a28 100644 --- a/tests/projects/c++/precompiled_header/src/test6.cpp +++ b/tests/projects/c++/precompiled_header/src/test6.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test6() -{ +int test6() { return 0; } diff --git a/tests/projects/c++/precompiled_header/src/test7.cpp b/tests/projects/c++/precompiled_header/src/test7.cpp index 24aeff619..d0e0e9445 100644 --- a/tests/projects/c++/precompiled_header/src/test7.cpp +++ b/tests/projects/c++/precompiled_header/src/test7.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test7() -{ +int test7() { return 0; } diff --git a/tests/projects/c++/precompiled_header/src/test8.cpp b/tests/projects/c++/precompiled_header/src/test8.cpp index 6ad5680a6..048f2952d 100644 --- a/tests/projects/c++/precompiled_header/src/test8.cpp +++ b/tests/projects/c++/precompiled_header/src/test8.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test8() -{ +int test8() { return 0; } diff --git a/tests/projects/c++/precompiled_header/test3.cpp b/tests/projects/c++/precompiled_header/test3.cpp index 7daf91dd7..c914e1dff 100644 --- a/tests/projects/c++/precompiled_header/test3.cpp +++ b/tests/projects/c++/precompiled_header/test3.cpp @@ -2,7 +2,6 @@ #include "src/header.h" #include "src/header2.h" -int test3() -{ +int test3() { return 0; } diff --git a/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp index dc06458ae..03ede9550 100644 --- a/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp +++ b/tests/projects/c++/protobuf_grpc_cpp_plugin/src/main.cpp @@ -4,8 +4,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << "hello world!" << endl; return 0; } diff --git a/tests/projects/c++/shared_library/src/foo.h b/tests/projects/c++/shared_library/src/foo.h index 20df0baa4..bd496c784 100644 --- a/tests/projects/c++/shared_library/src/foo.h +++ b/tests/projects/c++/shared_library/src/foo.h @@ -3,11 +3,11 @@ extern "C" { #endif #if defined(_WIN32) -# define __export __declspec(dllexport) +#define __export __declspec(dllexport) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) -# define __export __attribute__((visibility("default"))) +#define __export __attribute__((visibility("default"))) #else -# define __export +#define __export #endif __export int add(int a, int b); diff --git a/tests/projects/c++/shared_library/src/main.cpp b/tests/projects/c++/shared_library/src/main.cpp index ed1924789..41e494481 100644 --- a/tests/projects/c++/shared_library/src/main.cpp +++ b/tests/projects/c++/shared_library/src/main.cpp @@ -3,7 +3,7 @@ using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << "add(1, 2) = " << add(1, 2) << endl; return 0; } diff --git a/tests/projects/c++/shared_library_export_all/src/bar.h b/tests/projects/c++/shared_library_export_all/src/bar.h index 687386e0a..5abca5a30 100644 --- a/tests/projects/c++/shared_library_export_all/src/bar.h +++ b/tests/projects/c++/shared_library_export_all/src/bar.h @@ -1,4 +1,4 @@ class bar { -public: + public: static int add(int a, int b); }; diff --git a/tests/projects/c++/shared_library_export_all/src/foo.h b/tests/projects/c++/shared_library_export_all/src/foo.h index e27636b94..9ec469d23 100644 --- a/tests/projects/c++/shared_library_export_all/src/foo.h +++ b/tests/projects/c++/shared_library_export_all/src/foo.h @@ -1,4 +1,4 @@ class foo { -public: + public: static int add(int a, int b); }; diff --git a/tests/projects/c++/shared_library_export_all/src/main.cpp b/tests/projects/c++/shared_library_export_all/src/main.cpp index cc95897ee..d731d7590 100644 --- a/tests/projects/c++/shared_library_export_all/src/main.cpp +++ b/tests/projects/c++/shared_library_export_all/src/main.cpp @@ -2,7 +2,7 @@ #include "bar.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "foo::add(1, 2) = " << foo::add(1, 2) << std::endl; std::cout << "bar::add(1, 2) = " << bar::add(1, 2) << std::endl; return 0; diff --git a/tests/projects/c++/shared_library_with_soname/src/foo.h b/tests/projects/c++/shared_library_with_soname/src/foo.h index 20df0baa4..bd496c784 100644 --- a/tests/projects/c++/shared_library_with_soname/src/foo.h +++ b/tests/projects/c++/shared_library_with_soname/src/foo.h @@ -3,11 +3,11 @@ extern "C" { #endif #if defined(_WIN32) -# define __export __declspec(dllexport) +#define __export __declspec(dllexport) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) -# define __export __attribute__((visibility("default"))) +#define __export __attribute__((visibility("default"))) #else -# define __export +#define __export #endif __export int add(int a, int b); diff --git a/tests/projects/c++/shared_library_with_soname/src/main.cpp b/tests/projects/c++/shared_library_with_soname/src/main.cpp index ed1924789..41e494481 100644 --- a/tests/projects/c++/shared_library_with_soname/src/main.cpp +++ b/tests/projects/c++/shared_library_with_soname/src/main.cpp @@ -3,7 +3,7 @@ using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << "add(1, 2) = " << add(1, 2) << endl; return 0; } diff --git a/tests/projects/c++/static_library/src/main.cpp b/tests/projects/c++/static_library/src/main.cpp index ed1924789..41e494481 100644 --- a/tests/projects/c++/static_library/src/main.cpp +++ b/tests/projects/c++/static_library/src/main.cpp @@ -3,7 +3,7 @@ using namespace std; -int main(int argc, char** argv) { +int main(int argc, char **argv) { cout << "add(1, 2) = " << add(1, 2) << endl; return 0; } diff --git a/tests/projects/c++/test(brackets)/src/main.cpp b/tests/projects/c++/test(brackets)/src/main.cpp index a20aef120..134c3d51a 100644 --- a/tests/projects/c++/test(brackets)/src/main.cpp +++ b/tests/projects/c++/test(brackets)/src/main.cpp @@ -3,8 +3,7 @@ using namespace std; -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { cout << TEST " " WORD "!" << endl; return 0; } diff --git a/tests/projects/c++/unity_build/src/bar/test4.cpp b/tests/projects/c++/unity_build/src/bar/test4.cpp index 8fe732964..5012c2235 100644 --- a/tests/projects/c++/unity_build/src/bar/test4.cpp +++ b/tests/projects/c++/unity_build/src/bar/test4.cpp @@ -3,10 +3,9 @@ #include "header.h" namespace MY_UNITY_ID { - int i = 42; +int i = 42; } -int test4() -{ +int test4() { return MY_UNITY_ID::i; } diff --git a/tests/projects/c++/unity_build/src/bar/test5.cpp b/tests/projects/c++/unity_build/src/bar/test5.cpp index afff0b46c..80beed9a4 100644 --- a/tests/projects/c++/unity_build/src/bar/test5.cpp +++ b/tests/projects/c++/unity_build/src/bar/test5.cpp @@ -3,11 +3,9 @@ #include "header.h" namespace MY_UNITY_ID { - int i = 42; +int i = 42; } -int test5() -{ +int test5() { return MY_UNITY_ID::i; } - diff --git a/tests/projects/c++/unity_build/src/bar/test6.cpp b/tests/projects/c++/unity_build/src/bar/test6.cpp index efbd7c6c1..03dd19a28 100644 --- a/tests/projects/c++/unity_build/src/bar/test6.cpp +++ b/tests/projects/c++/unity_build/src/bar/test6.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test6() -{ +int test6() { return 0; } diff --git a/tests/projects/c++/unity_build/src/foo/test.cpp b/tests/projects/c++/unity_build/src/foo/test.cpp index e9d6c83d9..4a526251e 100644 --- a/tests/projects/c++/unity_build/src/foo/test.cpp +++ b/tests/projects/c++/unity_build/src/foo/test.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test() -{ +int test() { return 0; } diff --git a/tests/projects/c++/unity_build/src/foo/test2.cpp b/tests/projects/c++/unity_build/src/foo/test2.cpp index 596359cd2..0d9d9240f 100644 --- a/tests/projects/c++/unity_build/src/foo/test2.cpp +++ b/tests/projects/c++/unity_build/src/foo/test2.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test2() -{ +int test2() { return 0; } diff --git a/tests/projects/c++/unity_build/src/header.h b/tests/projects/c++/unity_build/src/header.h index affda55ca..910e73544 100644 --- a/tests/projects/c++/unity_build/src/header.h +++ b/tests/projects/c++/unity_build/src/header.h @@ -1,7 +1,7 @@ // header.h #ifndef HEADER_H #define HEADER_H - + #include <algorithm> #include <deque> #include <iostream> @@ -24,4 +24,3 @@ #include <iomanip> #endif - diff --git a/tests/projects/c++/unity_build/src/main.cpp b/tests/projects/c++/unity_build/src/main.cpp index 27d41ee50..bfce631e2 100644 --- a/tests/projects/c++/unity_build/src/main.cpp +++ b/tests/projects/c++/unity_build/src/main.cpp @@ -1,7 +1,6 @@ #include "header.h" -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { std::string s("xmake"); printf("hello %s!\n", s.c_str()); return 0; diff --git a/tests/projects/c++/unity_build/src/test.c b/tests/projects/c++/unity_build/src/test.c index 7f0aa2bf4..6b15d3d8a 100644 --- a/tests/projects/c++/unity_build/src/test.c +++ b/tests/projects/c++/unity_build/src/test.c @@ -1,4 +1,3 @@ -void test_c(void) -{ +void test_c(void) { } diff --git a/tests/projects/c++/unity_build/src/test7.cpp b/tests/projects/c++/unity_build/src/test7.cpp index 24aeff619..d0e0e9445 100644 --- a/tests/projects/c++/unity_build/src/test7.cpp +++ b/tests/projects/c++/unity_build/src/test7.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test7() -{ +int test7() { return 0; } diff --git a/tests/projects/c++/unity_build/src/test8.cpp b/tests/projects/c++/unity_build/src/test8.cpp index 6ad5680a6..048f2952d 100644 --- a/tests/projects/c++/unity_build/src/test8.cpp +++ b/tests/projects/c++/unity_build/src/test8.cpp @@ -2,7 +2,6 @@ // main.cpp #include "header.h" -int test8() -{ +int test8() { return 0; } diff --git a/tests/projects/pascal/console_with_c/src/foo.c b/tests/projects/pascal/console_with_c/src/foo.c index 66ca7cfb4..0d56f5f09 100644 --- a/tests/projects/pascal/console_with_c/src/foo.c +++ b/tests/projects/pascal/console_with_c/src/foo.c @@ -1,7 +1,5 @@ #include <stdint.h> -extern int64_t fib(int64_t n) -{ +extern int64_t fib(int64_t n) { return n > 1 ? fib(n - 1) + fib(n - 2) : 1; } - diff --git a/xmake/templates/c++/console/project/src/main.cpp b/xmake/templates/c++/console/project/src/main.cpp index 7c775d288..9b6abffea 100644 --- a/xmake/templates/c++/console/project/src/main.cpp +++ b/xmake/templates/c++/console/project/src/main.cpp @@ -1,6 +1,6 @@ #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "hello world!" << std::endl; return 0; } diff --git a/xmake/templates/c++/module.binary/project/modules/binary/bar/src/add.cpp b/xmake/templates/c++/module.binary/project/modules/binary/bar/src/add.cpp index e19d07e12..93fdf0ad8 100644 --- a/xmake/templates/c++/module.binary/project/modules/binary/bar/src/add.cpp +++ b/xmake/templates/c++/module.binary/project/modules/binary/bar/src/add.cpp @@ -1,7 +1,7 @@ #include <stdio.h> #include <stdlib.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { int a = atoi(argv[1]); int b = atoi(argv[2]); printf("%d", a + b); diff --git a/xmake/templates/c++/module.binary/project/modules/binary/bar/src/sub.cpp b/xmake/templates/c++/module.binary/project/modules/binary/bar/src/sub.cpp index 980289d1a..ae7b6d1f2 100644 --- a/xmake/templates/c++/module.binary/project/modules/binary/bar/src/sub.cpp +++ b/xmake/templates/c++/module.binary/project/modules/binary/bar/src/sub.cpp @@ -1,10 +1,9 @@ #include <stdio.h> #include <stdlib.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { int a = atoi(argv[1]); int b = atoi(argv[2]); printf("%d", a - b); return 0; } - diff --git a/xmake/templates/c++/module.binary/project/src/main.cpp b/xmake/templates/c++/module.binary/project/src/main.cpp index 7c775d288..9b6abffea 100644 --- a/xmake/templates/c++/module.binary/project/src/main.cpp +++ b/xmake/templates/c++/module.binary/project/src/main.cpp @@ -1,6 +1,6 @@ #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "hello world!" << std::endl; return 0; } diff --git a/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp b/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp index a98baca02..adde214ca 100644 --- a/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp +++ b/xmake/templates/c++/module.shared/project/modules/shared/foo/src/foo.cpp @@ -1,27 +1,22 @@ #include <xmi.h> -static int add(lua_State* lua) { +static int add(lua_State *lua) { int a = lua_tointeger(lua, 1); int b = lua_tointeger(lua, 2); lua_pushinteger(lua, a + b); return 1; } -static int sub(lua_State* lua) { +static int sub(lua_State *lua) { int a = lua_tointeger(lua, 1); int b = lua_tointeger(lua, 2); lua_pushinteger(lua, a - b); return 1; } -int luaopen(foo, lua_State* lua) { - static const luaL_Reg funcs[] = { - {"add", add}, - {"sub", sub}, - {NULL, NULL} - }; +int luaopen(foo, lua_State *lua) { + static const luaL_Reg funcs[] = { { "add", add }, { "sub", sub }, { NULL, NULL } }; lua_newtable(lua); luaL_setfuncs(lua, funcs, 0); return 1; } - diff --git a/xmake/templates/c++/module.shared/project/src/main.cpp b/xmake/templates/c++/module.shared/project/src/main.cpp index 7c775d288..9b6abffea 100644 --- a/xmake/templates/c++/module.shared/project/src/main.cpp +++ b/xmake/templates/c++/module.shared/project/src/main.cpp @@ -1,6 +1,6 @@ #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "hello world!" << std::endl; return 0; } diff --git a/xmake/templates/c++/qt.quickapp_static/project/src/main.cpp b/xmake/templates/c++/qt.quickapp_static/project/src/main.cpp index 2ec4b880c..7d9cb80df 100644 --- a/xmake/templates/c++/qt.quickapp_static/project/src/main.cpp +++ b/xmake/templates/c++/qt.quickapp_static/project/src/main.cpp @@ -1,8 +1,7 @@ #include <QGuiApplication> #include <QQmlApplicationEngine> -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { #if QT_VERSION >= 0x50601 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); #endif diff --git a/xmake/templates/c++/qt.shared/project/src/demo.cpp b/xmake/templates/c++/qt.shared/project/src/demo.cpp index bd791c271..576c861f0 100644 --- a/xmake/templates/c++/qt.shared/project/src/demo.cpp +++ b/xmake/templates/c++/qt.shared/project/src/demo.cpp @@ -1,6 +1,4 @@ #include "demo.h" - -QtDemo::QtDemo() -{ +QtDemo::QtDemo() { } diff --git a/xmake/templates/c++/qt.shared/project/src/demo.h b/xmake/templates/c++/qt.shared/project/src/demo.h index a0c8d28c6..89f29ec46 100644 --- a/xmake/templates/c++/qt.shared/project/src/demo.h +++ b/xmake/templates/c++/qt.shared/project/src/demo.h @@ -3,10 +3,8 @@ #include "demo_global.h" -class QT_DEMOSHARED_EXPORT QtDemo -{ - -public: +class QT_DEMOSHARED_EXPORT QtDemo { + public: QtDemo(); }; diff --git a/xmake/templates/c++/qt.shared/project/src/demo_global.h b/xmake/templates/c++/qt.shared/project/src/demo_global.h index a1daff02a..d6b1ddf0b 100644 --- a/xmake/templates/c++/qt.shared/project/src/demo_global.h +++ b/xmake/templates/c++/qt.shared/project/src/demo_global.h @@ -4,9 +4,9 @@ #include <QtCore/qglobal.h> #if defined(QT_DEMO_LIBRARY) -# define QT_DEMOSHARED_EXPORT Q_DECL_EXPORT +#define QT_DEMOSHARED_EXPORT Q_DECL_EXPORT #else -# define QT_DEMOSHARED_EXPORT Q_DECL_IMPORT +#define QT_DEMOSHARED_EXPORT Q_DECL_IMPORT #endif #endif // QT_DEMO_GLOBAL_H diff --git a/xmake/templates/c++/qt.static/project/src/demo.cpp b/xmake/templates/c++/qt.static/project/src/demo.cpp index a702ad253..576c861f0 100644 --- a/xmake/templates/c++/qt.static/project/src/demo.cpp +++ b/xmake/templates/c++/qt.static/project/src/demo.cpp @@ -1,5 +1,4 @@ #include "demo.h" -QtDemo::QtDemo() -{ +QtDemo::QtDemo() { } diff --git a/xmake/templates/c++/qt.static/project/src/demo.h b/xmake/templates/c++/qt.static/project/src/demo.h index 51eb9be55..24730c9d8 100644 --- a/xmake/templates/c++/qt.static/project/src/demo.h +++ b/xmake/templates/c++/qt.static/project/src/demo.h @@ -1,10 +1,8 @@ #ifndef QT_DEMO_H #define QT_DEMO_H -class QtDemo -{ - -public: +class QtDemo { + public: QtDemo(); }; diff --git a/xmake/templates/c++/qt.widgetapp/project/src/main.cpp b/xmake/templates/c++/qt.widgetapp/project/src/main.cpp index eb4e26c90..191ae18d2 100644 --- a/xmake/templates/c++/qt.widgetapp/project/src/main.cpp +++ b/xmake/templates/c++/qt.widgetapp/project/src/main.cpp @@ -3,7 +3,7 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; + MainWindow w; w.show(); return a.exec(); } diff --git a/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.cpp b/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.cpp index 57ab27f7d..c9c6a0d98 100644 --- a/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.cpp +++ b/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.cpp @@ -1,9 +1,7 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), - ui(new Ui::MainWindow) { +MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } diff --git a/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.h b/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.h index a3948a917..a4245aa42 100644 --- a/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.h +++ b/xmake/templates/c++/qt.widgetapp/project/src/mainwindow.h @@ -7,15 +7,14 @@ namespace Ui { class MainWindow; } -class MainWindow : public QMainWindow -{ +class MainWindow : public QMainWindow { Q_OBJECT -public: + public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); -private: + private: Ui::MainWindow *ui; }; diff --git a/xmake/templates/c++/qt.widgetapp_static/project/src/main.cpp b/xmake/templates/c++/qt.widgetapp_static/project/src/main.cpp index b48f94ec8..db5b0cce8 100644 --- a/xmake/templates/c++/qt.widgetapp_static/project/src/main.cpp +++ b/xmake/templates/c++/qt.widgetapp_static/project/src/main.cpp @@ -1,10 +1,9 @@ #include "mainwindow.h" #include <QApplication> -int main(int argc, char *argv[]) -{ +int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; + MainWindow w; w.show(); return a.exec(); diff --git a/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.cpp b/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.cpp index 49d64fce7..c9c6a0d98 100644 --- a/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.cpp +++ b/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.cpp @@ -1,14 +1,10 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) -{ +MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } -MainWindow::~MainWindow() -{ +MainWindow::~MainWindow() { delete ui; } diff --git a/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.h b/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.h index a3948a917..a4245aa42 100644 --- a/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.h +++ b/xmake/templates/c++/qt.widgetapp_static/project/src/mainwindow.h @@ -7,15 +7,14 @@ namespace Ui { class MainWindow; } -class MainWindow : public QMainWindow -{ +class MainWindow : public QMainWindow { Q_OBJECT -public: + public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); -private: + private: Ui::MainWindow *ui; }; diff --git a/xmake/templates/c++/shared/project/src/foo.h b/xmake/templates/c++/shared/project/src/foo.h index 20df0baa4..bd496c784 100644 --- a/xmake/templates/c++/shared/project/src/foo.h +++ b/xmake/templates/c++/shared/project/src/foo.h @@ -3,11 +3,11 @@ extern "C" { #endif #if defined(_WIN32) -# define __export __declspec(dllexport) +#define __export __declspec(dllexport) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) -# define __export __attribute__((visibility("default"))) +#define __export __attribute__((visibility("default"))) #else -# define __export +#define __export #endif __export int add(int a, int b); diff --git a/xmake/templates/c++/shared/project/src/main.cpp b/xmake/templates/c++/shared/project/src/main.cpp index d55d4d342..3834ee4d3 100644 --- a/xmake/templates/c++/shared/project/src/main.cpp +++ b/xmake/templates/c++/shared/project/src/main.cpp @@ -1,7 +1,7 @@ #include "foo.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; return 0; } diff --git a/xmake/templates/c++/static/project/src/main.cpp b/xmake/templates/c++/static/project/src/main.cpp index d55d4d342..3834ee4d3 100644 --- a/xmake/templates/c++/static/project/src/main.cpp +++ b/xmake/templates/c++/static/project/src/main.cpp @@ -1,7 +1,7 @@ #include "foo.h" #include <iostream> -int main(int argc, char** argv) { +int main(int argc, char **argv) { std::cout << "add(1, 2) = " << add(1, 2) << std::endl; return 0; } diff --git a/xmake/templates/c++/tbox.console/project/src/main.cpp b/xmake/templates/c++/tbox.console/project/src/main.cpp index 8cad00d22..2c7596b33 100644 --- a/xmake/templates/c++/tbox.console/project/src/main.cpp +++ b/xmake/templates/c++/tbox.console/project/src/main.cpp @@ -6,7 +6,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * main */ -tb_int_t main(tb_int_t argc, tb_char_t** argv) { +tb_int_t main(tb_int_t argc, tb_char_t **argv) { if (tb_init(tb_null, tb_null)) { tb_trace_i("hello tbox!"); tb_exit(); diff --git a/xmake/templates/c++/tbox.shared/project/src/_demo/main.cpp b/xmake/templates/c++/tbox.shared/project/src/_demo/main.cpp index 84dad3f2b..b1e763989 100644 --- a/xmake/templates/c++/tbox.shared/project/src/_demo/main.cpp +++ b/xmake/templates/c++/tbox.shared/project/src/_demo/main.cpp @@ -6,7 +6,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * main */ -tb_int_t main(tb_int_t argc, tb_char_t** argv) { +tb_int_t main(tb_int_t argc, tb_char_t **argv) { if (tb_init(tb_null, tb_null)) { tb_trace_i("hello tbox!"); tb_trace_i("add(1 + 1) = %d", add(1, 1)); diff --git a/xmake/templates/c++/tbox.shared/project/src/_library/interface.h b/xmake/templates/c++/tbox.shared/project/src/_library/interface.h index 398ad5d04..843ef1c98 100644 --- a/xmake/templates/c++/tbox.shared/project/src/_library/interface.h +++ b/xmake/templates/c++/tbox.shared/project/src/_library/interface.h @@ -11,7 +11,8 @@ */ __tb_extern_c_enter__ -__tb_export__ tb_int_t add(tb_int_t a, tb_int_t b); + __tb_export__ tb_int_t + add(tb_int_t a, tb_int_t b); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/xmake/templates/c++/tbox.static/project/src/_demo/main.cpp b/xmake/templates/c++/tbox.static/project/src/_demo/main.cpp index 84dad3f2b..b1e763989 100644 --- a/xmake/templates/c++/tbox.static/project/src/_demo/main.cpp +++ b/xmake/templates/c++/tbox.static/project/src/_demo/main.cpp @@ -6,7 +6,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * main */ -tb_int_t main(tb_int_t argc, tb_char_t** argv) { +tb_int_t main(tb_int_t argc, tb_char_t **argv) { if (tb_init(tb_null, tb_null)) { tb_trace_i("hello tbox!"); tb_trace_i("add(1 + 1) = %d", add(1, 1)); diff --git a/xmake/templates/c++/tbox.static/project/src/_library/interface.h b/xmake/templates/c++/tbox.static/project/src/_library/interface.h index 33d088314..caddb8394 100644 --- a/xmake/templates/c++/tbox.static/project/src/_library/interface.h +++ b/xmake/templates/c++/tbox.static/project/src/_library/interface.h @@ -11,7 +11,8 @@ */ __tb_extern_c_enter__ -tb_int_t add(tb_int_t a, tb_int_t b); + tb_int_t + add(tb_int_t a, tb_int_t b); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/xmake/templates/c++/wxwidgets/project/src/main.cpp b/xmake/templates/c++/wxwidgets/project/src/main.cpp index fbf187aa9..4b43fe6e3 100644 --- a/xmake/templates/c++/wxwidgets/project/src/main.cpp +++ b/xmake/templates/c++/wxwidgets/project/src/main.cpp @@ -1,44 +1,48 @@ #include <wx/wx.h> namespace Examples { - class Frame : public wxFrame { +class Frame : public wxFrame { enum wxOwnedID { - ID_Hello = 2 + ID_Hello = 2 }; public: - Frame(): wxFrame(nullptr, wxID_ANY, "Hello World") { - auto menuFile = new wxMenu(); - menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item"); - menuFile->AppendSeparator(); - menuFile->Append(wxID_EXIT); + Frame() : wxFrame(nullptr, wxID_ANY, "Hello World") { + auto menuFile = new wxMenu(); + menuFile->Append(ID_Hello, "&Hello...\tCtrl-H", "Help string shown in status bar for this menu item"); + menuFile->AppendSeparator(); + menuFile->Append(wxID_EXIT); - auto menuHelp = new wxMenu(); - menuHelp->Append(wxID_ABOUT); - auto menuBar = new wxMenuBar(); + auto menuHelp = new wxMenu(); + menuHelp->Append(wxID_ABOUT); + auto menuBar = new wxMenuBar(); - menuBar->Append(menuFile, "&File"); - menuBar->Append(menuHelp, "&Help"); - SetMenuBar(menuBar); + menuBar->Append(menuFile, "&File"); + menuBar->Append(menuHelp, "&Help"); + SetMenuBar(menuBar); - CreateStatusBar(); - SetStatusText("Welcome to wxWidgets!"); + CreateStatusBar(); + SetStatusText("Welcome to wxWidgets!"); - menuBar->Bind(wxEVT_MENU, [&](wxCommandEvent& event) { - if (event.GetId() == ID_Hello) wxLogMessage("Hello world from wxWidgets!"); - else if (event.GetId() == wxID_ABOUT) wxMessageBox("This is a wxWidgets Hello World example", "About Hello World", wxOK|wxICON_INFORMATION); - else if (event.GetId() == wxID_EXIT) Close(true); - else event.Skip(); - }); + menuBar->Bind(wxEVT_MENU, [&](wxCommandEvent &event) { + if (event.GetId() == ID_Hello) + wxLogMessage("Hello world from wxWidgets!"); + else if (event.GetId() == wxID_ABOUT) + wxMessageBox("This is a wxWidgets Hello World example", "About Hello World", wxOK | wxICON_INFORMATION); + else if (event.GetId() == wxID_EXIT) + Close(true); + else + event.Skip(); + }); } - }; +}; - class Application : public wxApp { +class Application : public wxApp { bool OnInit() override { - (new Frame())->Show(); - return true; + (new Frame())->Show(); + return true; } - }; -} +}; +} // namespace Examples wxIMPLEMENT_APP(Examples::Application);
\ No newline at end of file diff --git a/xmake/templates/c++/xmake.cli/project/src/lni/main.cpp b/xmake/templates/c++/xmake.cli/project/src/lni/main.cpp index 80237da16..e8955e074 100644 --- a/xmake/templates/c++/xmake.cli/project/src/lni/main.cpp +++ b/xmake/templates/c++/xmake.cli/project/src/lni/main.cpp @@ -3,26 +3,21 @@ extern "C" { } static tb_byte_t const g_luafiles_data[] = { - #include "luafiles.xmz.h" +#include "luafiles.xmz.h" }; -static tb_int_t lni_test_hello(lua_State* lua) { +static tb_int_t lni_test_hello(lua_State *lua) { lua_pushliteral(lua, "hello xmake!"); return 1; } -static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua) { - static luaL_Reg const lni_test_funcs[] = { - {"hello", lni_test_hello} - , {tb_null, tb_null} - }; +static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State *lua) { + static luaL_Reg const lni_test_funcs[] = { { "hello", lni_test_hello }, { tb_null, tb_null } }; xm_engine_register(engine, "test", lni_test_funcs); xm_engine_add_embedfiles(engine, g_luafiles_data, sizeof(g_luafiles_data)); } -tb_int_t main(tb_int_t argc, tb_char_t** argv) { - tb_char_t* taskargv[] = {"lua", "-D", "lua.main", tb_null}; +tb_int_t main(tb_int_t argc, tb_char_t **argv) { + tb_char_t *taskargv[] = { "lua", "-D", "lua.main", tb_null }; return xm_engine_run("${TARGETNAME}", argc, argv, taskargv, lni_initalizer); } - - diff --git a/xmake/templates/c/console/project/src/main.c b/xmake/templates/c/console/project/src/main.c index 113930e1a..ccf4b2a77 100644 --- a/xmake/templates/c/console/project/src/main.c +++ b/xmake/templates/c/console/project/src/main.c @@ -1,6 +1,6 @@ #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("hello world!\n"); return 0; } diff --git a/xmake/templates/c/module.binary/project/modules/binary/bar/src/add.c b/xmake/templates/c/module.binary/project/modules/binary/bar/src/add.c index e19d07e12..93fdf0ad8 100644 --- a/xmake/templates/c/module.binary/project/modules/binary/bar/src/add.c +++ b/xmake/templates/c/module.binary/project/modules/binary/bar/src/add.c @@ -1,7 +1,7 @@ #include <stdio.h> #include <stdlib.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { int a = atoi(argv[1]); int b = atoi(argv[2]); printf("%d", a + b); diff --git a/xmake/templates/c/module.binary/project/modules/binary/bar/src/sub.c b/xmake/templates/c/module.binary/project/modules/binary/bar/src/sub.c index 980289d1a..ae7b6d1f2 100644 --- a/xmake/templates/c/module.binary/project/modules/binary/bar/src/sub.c +++ b/xmake/templates/c/module.binary/project/modules/binary/bar/src/sub.c @@ -1,10 +1,9 @@ #include <stdio.h> #include <stdlib.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { int a = atoi(argv[1]); int b = atoi(argv[2]); printf("%d", a - b); return 0; } - diff --git a/xmake/templates/c/module.binary/project/src/main.c b/xmake/templates/c/module.binary/project/src/main.c index 113930e1a..ccf4b2a77 100644 --- a/xmake/templates/c/module.binary/project/src/main.c +++ b/xmake/templates/c/module.binary/project/src/main.c @@ -1,6 +1,6 @@ #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("hello world!\n"); return 0; } diff --git a/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c index a98baca02..adde214ca 100644 --- a/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c +++ b/xmake/templates/c/module.shared/project/modules/shared/foo/src/foo.c @@ -1,27 +1,22 @@ #include <xmi.h> -static int add(lua_State* lua) { +static int add(lua_State *lua) { int a = lua_tointeger(lua, 1); int b = lua_tointeger(lua, 2); lua_pushinteger(lua, a + b); return 1; } -static int sub(lua_State* lua) { +static int sub(lua_State *lua) { int a = lua_tointeger(lua, 1); int b = lua_tointeger(lua, 2); lua_pushinteger(lua, a - b); return 1; } -int luaopen(foo, lua_State* lua) { - static const luaL_Reg funcs[] = { - {"add", add}, - {"sub", sub}, - {NULL, NULL} - }; +int luaopen(foo, lua_State *lua) { + static const luaL_Reg funcs[] = { { "add", add }, { "sub", sub }, { NULL, NULL } }; lua_newtable(lua); luaL_setfuncs(lua, funcs, 0); return 1; } - diff --git a/xmake/templates/c/module.shared/project/src/main.c b/xmake/templates/c/module.shared/project/src/main.c index 113930e1a..ccf4b2a77 100644 --- a/xmake/templates/c/module.shared/project/src/main.c +++ b/xmake/templates/c/module.shared/project/src/main.c @@ -1,6 +1,6 @@ #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("hello world!\n"); return 0; } diff --git a/xmake/templates/c/shared/project/src/foo.h b/xmake/templates/c/shared/project/src/foo.h index b014f28e3..e15925829 100644 --- a/xmake/templates/c/shared/project/src/foo.h +++ b/xmake/templates/c/shared/project/src/foo.h @@ -1,11 +1,9 @@ #if defined(_WIN32) -# define __export __declspec(dllexport) +#define __export __declspec(dllexport) #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) -# define __export __attribute__((visibility("default"))) +#define __export __attribute__((visibility("default"))) #else -# define __export +#define __export #endif __export int add(int a, int b); - - diff --git a/xmake/templates/c/shared/project/src/main.c b/xmake/templates/c/shared/project/src/main.c index a4c769408..f1c29d751 100644 --- a/xmake/templates/c/shared/project/src/main.c +++ b/xmake/templates/c/shared/project/src/main.c @@ -1,7 +1,7 @@ #include "foo.h" #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("add(1, 2) = %d\n", add(1, 2)); return 0; } diff --git a/xmake/templates/c/static/project/src/main.c b/xmake/templates/c/static/project/src/main.c index a4c769408..f1c29d751 100644 --- a/xmake/templates/c/static/project/src/main.c +++ b/xmake/templates/c/static/project/src/main.c @@ -1,7 +1,7 @@ #include "foo.h" #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("add(1, 2) = %d\n", add(1, 2)); return 0; } diff --git a/xmake/templates/c/tbox.console/project/src/main.c b/xmake/templates/c/tbox.console/project/src/main.c index 8cad00d22..2c7596b33 100644 --- a/xmake/templates/c/tbox.console/project/src/main.c +++ b/xmake/templates/c/tbox.console/project/src/main.c @@ -6,7 +6,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * main */ -tb_int_t main(tb_int_t argc, tb_char_t** argv) { +tb_int_t main(tb_int_t argc, tb_char_t **argv) { if (tb_init(tb_null, tb_null)) { tb_trace_i("hello tbox!"); tb_exit(); diff --git a/xmake/templates/c/tbox.shared/project/src/_demo/main.c b/xmake/templates/c/tbox.shared/project/src/_demo/main.c index 84dad3f2b..b1e763989 100644 --- a/xmake/templates/c/tbox.shared/project/src/_demo/main.c +++ b/xmake/templates/c/tbox.shared/project/src/_demo/main.c @@ -6,7 +6,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * main */ -tb_int_t main(tb_int_t argc, tb_char_t** argv) { +tb_int_t main(tb_int_t argc, tb_char_t **argv) { if (tb_init(tb_null, tb_null)) { tb_trace_i("hello tbox!"); tb_trace_i("add(1 + 1) = %d", add(1, 1)); diff --git a/xmake/templates/c/tbox.shared/project/src/_library/interface.h b/xmake/templates/c/tbox.shared/project/src/_library/interface.h index 398ad5d04..843ef1c98 100644 --- a/xmake/templates/c/tbox.shared/project/src/_library/interface.h +++ b/xmake/templates/c/tbox.shared/project/src/_library/interface.h @@ -11,7 +11,8 @@ */ __tb_extern_c_enter__ -__tb_export__ tb_int_t add(tb_int_t a, tb_int_t b); + __tb_export__ tb_int_t + add(tb_int_t a, tb_int_t b); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/xmake/templates/c/tbox.static/project/src/_demo/main.c b/xmake/templates/c/tbox.static/project/src/_demo/main.c index 84dad3f2b..b1e763989 100644 --- a/xmake/templates/c/tbox.static/project/src/_demo/main.c +++ b/xmake/templates/c/tbox.static/project/src/_demo/main.c @@ -6,7 +6,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * main */ -tb_int_t main(tb_int_t argc, tb_char_t** argv) { +tb_int_t main(tb_int_t argc, tb_char_t **argv) { if (tb_init(tb_null, tb_null)) { tb_trace_i("hello tbox!"); tb_trace_i("add(1 + 1) = %d", add(1, 1)); diff --git a/xmake/templates/c/tbox.static/project/src/_library/interface.c b/xmake/templates/c/tbox.static/project/src/_library/interface.c index a0ccb007b..ab0a826a6 100644 --- a/xmake/templates/c/tbox.static/project/src/_library/interface.c +++ b/xmake/templates/c/tbox.static/project/src/_library/interface.c @@ -6,7 +6,6 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * implementation */ -tb_int_t add(tb_int_t a, tb_int_t b) -{ +tb_int_t add(tb_int_t a, tb_int_t b) { return a + b; } diff --git a/xmake/templates/c/tbox.static/project/src/_library/interface.h b/xmake/templates/c/tbox.static/project/src/_library/interface.h index 33d088314..caddb8394 100644 --- a/xmake/templates/c/tbox.static/project/src/_library/interface.h +++ b/xmake/templates/c/tbox.static/project/src/_library/interface.h @@ -11,7 +11,8 @@ */ __tb_extern_c_enter__ -tb_int_t add(tb_int_t a, tb_int_t b); + tb_int_t + add(tb_int_t a, tb_int_t b); /* ////////////////////////////////////////////////////////////////////////////////////// * extern diff --git a/xmake/templates/c/xmake.cli/project/src/lni/main.c b/xmake/templates/c/xmake.cli/project/src/lni/main.c index 98a4c19ce..a4c6499ee 100644 --- a/xmake/templates/c/xmake.cli/project/src/lni/main.c +++ b/xmake/templates/c/xmake.cli/project/src/lni/main.c @@ -1,25 +1,21 @@ #include <xmake/xmake.h> static tb_byte_t const g_luafiles_data[] = { - #include "luafiles.xmz.h" +#include "luafiles.xmz.h" }; -static tb_int_t lni_test_hello(lua_State* lua) { +static tb_int_t lni_test_hello(lua_State *lua) { lua_pushliteral(lua, "hello xmake!"); return 1; } -static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua) { - static luaL_Reg const lni_test_funcs[] = { - {"hello", lni_test_hello} - , {tb_null, tb_null} - }; +static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State *lua) { + static luaL_Reg const lni_test_funcs[] = { { "hello", lni_test_hello }, { tb_null, tb_null } }; xm_engine_register(engine, "test", lni_test_funcs); xm_engine_add_embedfiles(engine, g_luafiles_data, sizeof(g_luafiles_data)); } -tb_int_t main(tb_int_t argc, tb_char_t** argv) { - tb_char_t* taskargv[] = {"lua", "-D", "lua.main", tb_null}; +tb_int_t main(tb_int_t argc, tb_char_t **argv) { + tb_char_t *taskargv[] = { "lua", "-D", "lua.main", tb_null }; return xm_engine_run("${TARGETNAME}", argc, argv, taskargv, lni_initalizer); } - diff --git a/xmake/templates/kotlin/shared_library/project/src/main.c b/xmake/templates/kotlin/shared_library/project/src/main.c index 55e16305d..abb3f2c12 100644 --- a/xmake/templates/kotlin/shared_library/project/src/main.c +++ b/xmake/templates/kotlin/shared_library/project/src/main.c @@ -1,7 +1,7 @@ #include "libfoo_api.h" #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("add(1, 2) = %d\n", kotlin_add(1, 2)); return 0; } diff --git a/xmake/templates/kotlin/static_library/project/src/main.c b/xmake/templates/kotlin/static_library/project/src/main.c index 55e16305d..abb3f2c12 100644 --- a/xmake/templates/kotlin/static_library/project/src/main.c +++ b/xmake/templates/kotlin/static_library/project/src/main.c @@ -1,7 +1,7 @@ #include "libfoo_api.h" #include <stdio.h> -int main(int argc, char** argv) { +int main(int argc, char **argv) { printf("add(1, 2) = %d\n", kotlin_add(1, 2)); return 0; } |
