diff options
| author | OpportunityLiu <[email protected]> | 2019-07-02 10:12:56 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-02 10:12:56 +0800 |
| commit | 0261c0e50b59980f08cd8185bf2fb84e7248d104 (patch) | |
| tree | a7135a6534029e6fd89b4681fe9bf76c950f0775 /core/src | |
| parent | 35dbc87a2c7e36e8cd3d6e81acd84ff3d5542ba8 (diff) | |
add some check
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/global/prefix.h | 32 | ||||
| -rw-r--r-- | core/src/xmake/io/file.h | 27 | ||||
| -rw-r--r-- | core/src/xmake/io/file___len.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/io/file___tostring.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_close.c | 13 | ||||
| -rw-r--r-- | core/src/xmake/io/file_flush.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/io/file_isatty.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_path.c | 20 | ||||
| -rw-r--r-- | core/src/xmake/io/file_read.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/io/file_seek.c | 5 | ||||
| -rw-r--r-- | core/src/xmake/io/file_write.c | 32 | ||||
| -rw-r--r-- | core/src/xmake/io/open.c | 3 | ||||
| -rw-r--r-- | core/src/xmake/io/std.c | 2 |
13 files changed, 73 insertions, 79 deletions
diff --git a/core/src/xmake/global/prefix.h b/core/src/xmake/global/prefix.h deleted file mode 100644 index e8e7c8d5a..000000000 --- a/core/src/xmake/global/prefix.h +++ /dev/null @@ -1,32 +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 ruki - * @file prefix.h - * - */ -#ifndef XM_G_PREFIX_H -#define XM_G_PREFIX_H - -/* ////////////////////////////////////////////////////////////////////////////////////// - * includes - */ -#include "../prefix.h" - - -#endif - - diff --git a/core/src/xmake/io/file.h b/core/src/xmake/io/file.h index b14e13ad0..3d8312b31 100644 --- a/core/src/xmake/io/file.h +++ b/core/src/xmake/io/file.h @@ -28,13 +28,13 @@ typedef enum __xm_io_file_type_e { - XM_IO_FILE_TYPE_FILE = 0, //!< disk file + 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_FLAG_TTY = 0x10, //!< mark tty std stream } xm_io_file_type_e; typedef enum __xm_io_file_encoding_e @@ -42,13 +42,14 @@ typedef enum __xm_io_file_encoding_e XM_IO_FILE_ENCODING_BINARY = -1, XM_IO_FILE_ENCODING_UNKNOWN = -2, #ifdef TB_CONFIG_OS_WINDOWS - XM_IO_FILE_ENCODING_ANSI = -3, + XM_IO_FILE_ENCODING_ANSI = -3, #endif } xm_io_file_encoding_e; typedef struct __xm_io_file { - union { + union + { tb_file_ref_t file_ref; FILE* std_ref; }; @@ -61,7 +62,10 @@ typedef struct __xm_io_file } xm_io_file; #define xm_io_file_is_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE) -#define xm_io_file_is_closed_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE && fileref->file_ref == tb_null) +#define xm_io_file_is_closed_file(fileref) (fileref->type == XM_IO_FILE_TYPE_FILE && !(fileref->file_ref)) +#define xm_io_file_is_std(fileref) (fileref->type != XM_IO_FILE_TYPE_FILE) +#define xm_io_file_is_closed_std(fileref) (fileref->type != XM_IO_FILE_TYPE_FILE && !(fileref->file_ref)) +#define xm_io_file_is_closed(fileref) (xm_io_file_is_closed_file(fileref) || xm_io_file_is_closed_std(fileref)) #define xm_io_file_is_tty(fileref) (!!(fileref->type & XM_IO_FILE_FLAG_TTY)) #define xm_io_file_udata "XM_IO_FILE*" @@ -71,6 +75,7 @@ typedef struct __xm_io_file { \ return 1; \ } while (0) + #define xm_io_file_error(lua, reason) \ do \ { \ @@ -83,11 +88,23 @@ typedef struct __xm_io_file static inline xm_io_file* xm_io_newfile(lua_State* lua) { + tb_assert_and_check_return_val(lua, tb_null); + xm_io_file* file = (xm_io_file*)lua_newuserdata(lua, sizeof(xm_io_file)); + tb_assert(file); luaL_getmetatable(lua, xm_io_file_udata); lua_setmetatable(lua, -2); tb_memset(file, 0, sizeof(xm_io_file)); return file; } +static inline xm_io_file* xm_io_getfile(lua_State* lua) +{ + tb_assert_and_check_return_val(lua, tb_null); + + xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + tb_assert(file); + return file; +} + #endif diff --git a/core/src/xmake/io/file___len.c b/core/src/xmake/io/file___len.c index 31557b6d4..98a715a0e 100644 --- a/core/src/xmake/io/file___len.c +++ b/core/src/xmake/io/file___len.c @@ -43,8 +43,8 @@ tb_int_t xm_io_file___len(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); - if(xm_io_file_is_closed_file(file)) + xm_io_file* file = xm_io_getfile(lua); + if(xm_io_file_is_closed(file)) xm_io_file_error_closed(lua); else if (xm_io_file_is_file(file)) { diff --git a/core/src/xmake/io/file___tostring.c b/core/src/xmake/io/file___tostring.c index e92b3c61d..28817ad62 100644 --- a/core/src/xmake/io/file___tostring.c +++ b/core/src/xmake/io/file___tostring.c @@ -43,7 +43,7 @@ tb_int_t xm_io_file___tostring(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* file = xm_io_getfile(lua); lua_pushstring(lua, file->name); return 1; } diff --git a/core/src/xmake/io/file_close.c b/core/src/xmake/io/file_close.c index f384f4c11..e05f33861 100644 --- a/core/src/xmake/io/file_close.c +++ b/core/src/xmake/io/file_close.c @@ -43,8 +43,8 @@ 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_file*)luaL_checkudata(lua, 1, xm_io_file_udata); - if (xm_io_file_is_closed_file(file) || (!xm_io_file_is_file(file) && file->std_ref == tb_null)) + xm_io_file* file = xm_io_getfile(lua); + if (xm_io_file_is_closed(file)) { lua_pushboolean(lua, tb_true); xm_io_file_success(); @@ -53,9 +53,12 @@ tb_int_t xm_io_file_close(lua_State* lua) { if (!tb_file_exit(file->file_ref)) xm_io_file_error(lua, "failed to close file"); file->file_ref = tb_null; - tb_free(file->path); - file->path = tb_null; - tb_strcpy(file->name, "file: (closed file)"); + 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(); } diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c index 3e40f527f..a750c1b1f 100644 --- a/core/src/xmake/io/file_flush.c +++ b/core/src/xmake/io/file_flush.c @@ -45,10 +45,10 @@ tb_int_t xm_io_file_flush(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* file = xm_io_getfile(lua); - if (xm_io_file_is_closed_file(file)) xm_io_file_error_closed(lua); - tb_bool_t succeed = xm_io_file_is_file(file) ? tb_file_sync(file->file_ref) : std_flush(file); + if (xm_io_file_is_closed(file)) xm_io_file_error_closed(lua); + tb_bool_t succeed = xm_io_file_is_file(file) ? tb_file_sync(file->file_ref) : !fflush(file->std_ref); if (succeed) { lua_pushboolean(lua, tb_true); diff --git a/core/src/xmake/io/file_isatty.c b/core/src/xmake/io/file_isatty.c index 0f29f98bc..0d89078d6 100644 --- a/core/src/xmake/io/file_isatty.c +++ b/core/src/xmake/io/file_isatty.c @@ -43,7 +43,7 @@ tb_int_t xm_io_file_isatty(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // get file pointer - xm_io_file* fp = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* fp = xm_io_getfile(lua); if (xm_io_file_is_file(fp)) { diff --git a/core/src/xmake/io/file_path.c b/core/src/xmake/io/file_path.c index 37191ab93..bda236077 100644 --- a/core/src/xmake/io/file_path.c +++ b/core/src/xmake/io/file_path.c @@ -43,19 +43,11 @@ tb_int_t xm_io_file_path(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // get file pointer - xm_io_file* fp = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* fp = xm_io_getfile(lua); - if (xm_io_file_is_file(fp)) - { - if (xm_io_file_is_closed_file(fp)) xm_io_file_error_closed(lua); - lua_pushstring(lua, fp->path); - // ok - return 1; - } - else - { - lua_pushstring(lua, fp->path); - // ok - return 1; - } + if (xm_io_file_is_closed(fp)) xm_io_file_error_closed(lua); + + lua_pushstring(lua, fp->path); + // ok + return 1; } diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index 56d8ba5cb..5727b2b16 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -589,10 +589,12 @@ tb_int_t xm_io_file_read(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* file = xm_io_getfile(lua); 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(file && mode && continuation, 0); + tb_long_t count = -1; if (lua_isnumber(lua, 2)) count = (tb_long_t)lua_tointeger(lua, 2); diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c index cc0e65594..5e75012a5 100644 --- a/core/src/xmake/io/file_seek.c +++ b/core/src/xmake/io/file_seek.c @@ -43,9 +43,12 @@ tb_int_t xm_io_file_seek(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* file = xm_io_getfile(lua); 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(file && whence, 0); + if (xm_io_file_is_file(file)) { if (xm_io_file_is_closed_file(file)) xm_io_file_error_closed(lua); diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c index 81b6fe74e..6fe4620bb 100644 --- a/core/src/xmake/io/file_write.c +++ b/core/src/xmake/io/file_write.c @@ -115,25 +115,29 @@ tb_int_t xm_io_file_write(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file* file = xm_io_getfile(lua); tb_int_t narg = lua_gettop(lua); - // set to utf-8 if not specified - if (file->encoding == XM_IO_FILE_ENCODING_UNKNOWN) file->encoding = TB_CHARSET_TYPE_UTF8; - tb_bool_t direct = file->encoding == TB_CHARSET_TYPE_UTF8 || file->encoding == XM_IO_FILE_ENCODING_BINARY; - if (xm_io_file_is_closed_file(file)) xm_io_file_error_closed(lua); + if (xm_io_file_is_closed(file)) xm_io_file_error_closed(lua); - for (tb_int_t i = 2; i <= narg; i++) + if (narg > 1) { - size_t datasize; - tb_char_t const* data = luaL_checklstring(lua, i, &datasize); + // set to utf-8 if not specified + if (file->encoding == XM_IO_FILE_ENCODING_UNKNOWN) file->encoding = TB_CHARSET_TYPE_UTF8; + tb_bool_t direct = file->encoding == TB_CHARSET_TYPE_UTF8 || file->encoding == XM_IO_FILE_ENCODING_BINARY; - if (!xm_io_file_is_file(file)) - std_write(file, data, (tb_size_t)datasize); - else if (direct) - direct_write(file, data, (tb_size_t)datasize); - else - transcode_write(file, data, (tb_size_t)datasize); + for (tb_int_t i = 2; i <= narg; i++) + { + size_t datasize; + tb_char_t const* data = luaL_checklstring(lua, i, &datasize); + + if (xm_io_file_is_std(file)) + std_write(file, data, (tb_size_t)datasize); + else if (direct) + direct_write(file, data, (tb_size_t)datasize); + else + transcode_write(file, data, (tb_size_t)datasize); + } } lua_settop(lua, 1); diff --git a/core/src/xmake/io/open.c b/core/src/xmake/io/open.c index fae03781f..7cc506b69 100644 --- a/core/src/xmake/io/open.c +++ b/core/src/xmake/io/open.c @@ -42,6 +42,9 @@ tb_int_t xm_io_open(lua_State* lua) { tb_char_t const* path = luaL_checkstring(lua, 1); tb_char_t const* mode = luaL_optstring(lua, 2, "r"); + + tb_check_return_val(path && mode, 0); + tb_size_t tb_mode = TB_FILE_MODE_RW; switch (mode[0]) { diff --git a/core/src/xmake/io/std.c b/core/src/xmake/io/std.c index 4a79f5397..583a8f1f9 100644 --- a/core/src/xmake/io/std.c +++ b/core/src/xmake/io/std.c @@ -69,6 +69,8 @@ static tb_size_t xm_isatty(tb_size_t type) static void xm_init(lua_State* lua, tb_size_t type) { + tb_check_return(lua); + tb_char_t const *name, *path; FILE* fp; switch (type) |
