From 10194ed2bc005211feba0c6d54924782b69f7fc6 Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Tue, 2 Jul 2019 10:59:21 +0800 Subject: add file:__gc --- core/src/xmake/io/file_close.c | 76 -------------------------- core/src/xmake/io/file_close___gc.c | 103 ++++++++++++++++++++++++++++++++++++ core/src/xmake/machine.c | 3 +- 3 files changed, 105 insertions(+), 77 deletions(-) delete mode 100644 core/src/xmake/io/file_close.c create mode 100644 core/src/xmake/io/file_close___gc.c diff --git a/core/src/xmake/io/file_close.c b/core/src/xmake/io/file_close.c deleted file mode 100644 index e05f33861..000000000 --- a/core/src/xmake/io/file_close.c +++ /dev/null @@ -1,76 +0,0 @@ -/*!A cross-platform build utility based on Lua - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Copyright (C) 2015 - 2019, TBOOX Open Source Group. - * - * @author OpportunityLiu - * @file file_close.c - * - */ - -/* ////////////////////////////////////////////////////////////////////////////////////// - * trace - */ -#define TB_TRACE_MODULE_NAME "file_close" -#define TB_TRACE_MODULE_DEBUG (0) - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "file.h" -#include "prefix.h" - -/* ////////////////////////////////////////////////////////////////////////////////////// - * implementation - */ - -/* - * file:close() - */ -tb_int_t xm_io_file_close(lua_State* lua) -{ - // check - tb_assert_and_check_return_val(lua, 0); - - xm_io_file* file = xm_io_getfile(lua); - if (xm_io_file_is_closed(file)) - { - lua_pushboolean(lua, tb_true); - xm_io_file_success(); - } - if (xm_io_file_is_file(file)) - { - if (!tb_file_exit(file->file_ref)) xm_io_file_error(lua, "failed to close file"); - file->file_ref = tb_null; - if (file->path) - { - tb_free(file->path); - file->path = tb_null; - } - tb_strlcpy(file->name, "file: (closed file)", tb_arrayn(file->name)); - lua_pushboolean(lua, tb_true); - xm_io_file_success(); - } - else - { - // should we support close std files? - - // if (fclose(file->std_ref)) xm_io_file_error(lua, "failed to close file"); - // file->std_ref = tb_null; - // file->path = tb_null; - // tb_strcpy(file->name, "file: (closed file)"); - lua_pushboolean(lua, tb_true); - xm_io_file_success(); - } -} diff --git a/core/src/xmake/io/file_close___gc.c b/core/src/xmake/io/file_close___gc.c new file mode 100644 index 000000000..26f584b9c --- /dev/null +++ b/core/src/xmake/io/file_close___gc.c @@ -0,0 +1,103 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Copyright (C) 2015 - 2019, TBOOX Open Source Group. + * + * @author OpportunityLiu + * @file file_close___gc.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "file_close___gc" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "file.h" +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +static tb_int_t xm_io_file_close_impl(lua_State* lua, tb_bool_t allow_closed_file) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + xm_io_file* file = xm_io_getfile(lua); + if (xm_io_file_is_closed(file)) + { + if (allow_closed_file) + { + lua_pushboolean(lua, tb_true); + xm_io_file_success(); + }else + xm_io_file_error_closed(lua); + } + if (xm_io_file_is_file(file)) + { + if (!tb_file_exit(file->file_ref)) xm_io_file_error(lua, "failed to close file"); + file->file_ref = tb_null; + if (file->path) + { + tb_free(file->path); + file->path = tb_null; + } + tb_strlcpy(file->name, "file: (closed file)", tb_arrayn(file->name)); + lua_pushboolean(lua, tb_true); + xm_io_file_success(); + } + else + { + // should we support close std files? + + // if (fclose(file->std_ref)) xm_io_file_error(lua, "failed to close file"); + // file->std_ref = tb_null; + // file->path = tb_null; + // tb_strcpy(file->name, "file: (closed file)"); + lua_pushboolean(lua, tb_true); + xm_io_file_success(); + } +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* + * file:close() + */ +tb_int_t xm_io_file_close(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + return xm_io_file_close_impl(lua, tb_false); +} + +/* + * file:close() + */ +tb_int_t xm_io_file___gc(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + return xm_io_file_close_impl(lua, tb_true); +} diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 26734acfd..f09f941e1 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -105,6 +105,7 @@ tb_int_t xm_io_file_isatty(lua_State* lua); tb_int_t xm_io_file_path(lua_State* lua); tb_int_t xm_io_file___len(lua_State* lua); tb_int_t xm_io_file___tostring(lua_State* lua); +tb_int_t xm_io_file___gc(lua_State* lua); // the path functions tb_int_t xm_path_relative(lua_State* lua); @@ -243,7 +244,7 @@ static luaL_Reg const g_io_file_functions[] = , { "isatty", xm_io_file_isatty } , { "path", xm_io_file_path } , { "close", xm_io_file_close } -, { "__gc", xm_io_file_close } +, { "__gc", xm_io_file___gc } , { "__len", xm_io_file___len } , { "__tostring", xm_io_file___tostring } , { tb_null, tb_null } -- cgit v1.3.1