diff options
| author | OpportunityLiu <[email protected]> | 2019-07-02 11:20:17 +0800 |
|---|---|---|
| committer | OpportunityLiu <[email protected]> | 2019-07-02 11:20:17 +0800 |
| commit | e2dc6e831a3c5114947040d11e5c0e6a9812e1ad (patch) | |
| tree | 621dbac0ce05a14896c4ae96975273c0bdf5cbec | |
| parent | d4ca003b4b507c61429d8c48075a547d8309166d (diff) | |
add name to error message
| -rw-r--r-- | core/src/xmake/io/file.h | 24 | ||||
| -rw-r--r-- | core/src/xmake/io/file___len.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_close___gc.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/io/file_flush.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_read.c | 25 | ||||
| -rw-r--r-- | core/src/xmake/io/file_seek.c | 2 |
6 files changed, 34 insertions, 25 deletions
diff --git a/core/src/xmake/io/file.h b/core/src/xmake/io/file.h index 8348eb136..27a39264a 100644 --- a/core/src/xmake/io/file.h +++ b/core/src/xmake/io/file.h @@ -40,6 +40,8 @@ typedef enum __xm_io_file_type_e 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_BINARY = -1, @@ -53,13 +55,13 @@ typedef struct __xm_io_file { union { - tb_file_ref_t file_ref; - FILE* std_ref; + tb_file_ref_t file_ref; // valid if type == XM_IO_FILE_TYPE_FILE + FILE* std_ref; // valid otherwise }; - tb_size_t mode; - tb_size_t type; - tb_size_t encoding; // value of xm_io_file_encoding_e or tb_charset_type_e + 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_char_t name[64]; tb_char_t const* path; } xm_io_file; @@ -84,15 +86,21 @@ typedef struct __xm_io_file return 1; \ } while (0) -#define xm_io_file_error(lua, reason) \ +#define xm_io_file_error(lua, file, reason) \ do \ { \ lua_pushnil(lua); \ - lua_pushliteral(lua, reason); \ + lua_pushfstring(lua, "error: %s (%s)", reason, file->name); \ return 2; \ } while (0) -#define xm_io_file_error_closed(lua) xm_io_file_error(lua, "file has been closed") +#define xm_io_file_error_closed(lua) \ + do \ + { \ + lua_pushnil(lua); \ + lua_pushliteral(lua, "error: file has been closed", reason); \ + return 2; \ + } while (0) /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces diff --git a/core/src/xmake/io/file___len.c b/core/src/xmake/io/file___len.c index fa454382e..0e81a94a0 100644 --- a/core/src/xmake/io/file___len.c +++ b/core/src/xmake/io/file___len.c @@ -52,5 +52,5 @@ tb_int_t xm_io_file___len(lua_State* lua) xm_io_file_success(); } else - xm_io_file_error(lua, "getting file size for this file is invalid"); + xm_io_file_error(lua, file, "getting file size for this file is invalid"); } diff --git a/core/src/xmake/io/file_close___gc.c b/core/src/xmake/io/file_close___gc.c index 26f584b9c..cb7e0bb79 100644 --- a/core/src/xmake/io/file_close___gc.c +++ b/core/src/xmake/io/file_close___gc.c @@ -52,7 +52,7 @@ static tb_int_t xm_io_file_close_impl(lua_State* lua, tb_bool_t allow_closed_fil } if (xm_io_file_is_file(file)) { - if (!tb_file_exit(file->file_ref)) xm_io_file_error(lua, "failed to close file"); + if (!tb_file_exit(file->file_ref)) xm_io_file_error(lua, file, "failed to close file"); file->file_ref = tb_null; if (file->path) { @@ -67,7 +67,7 @@ static tb_int_t xm_io_file_close_impl(lua_State* lua, tb_bool_t allow_closed_fil { // should we support close std files? - // if (fclose(file->std_ref)) xm_io_file_error(lua, "failed to close file"); + // if (fclose(file->std_ref)) xm_io_file_error(lua, file, "failed to close file"); // file->std_ref = tb_null; // file->path = tb_null; // tb_strcpy(file->name, "file: (closed file)"); diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c index 5c0027d80..dceb00e56 100644 --- a/core/src/xmake/io/file_flush.c +++ b/core/src/xmake/io/file_flush.c @@ -65,7 +65,7 @@ tb_int_t xm_io_file_flush(lua_State* lua) tb_bool_t succeed = xm_io_file_is_file(file) ? xm_io_file_flush(file) : xm_io_std_flush(file); - if (!succeed) xm_io_file_error(lua, "failed to flush file"); + if (!succeed) xm_io_file_error(lua, file, "failed to flush file"); lua_pushboolean(lua, tb_true); xm_io_file_success(); } diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index 4fb0ee597..533fe0758 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -371,7 +371,7 @@ static tb_int_t read_all(lua_State* lua, xm_io_file* file, tb_char_t const* cont case PL_FIN: case PL_CONL: has_content = tb_true; continue; case PL_FAIL: - default: luaL_pushresult(buf); xm_io_file_error(lua, "failed to readline"); + default: luaL_pushresult(buf); xm_io_file_error(lua, file, "failed to readline"); } } } @@ -394,7 +394,7 @@ static tb_int_t read_line(lua_State* lua, xm_io_file* file, tb_char_t const* con case PL_FIN: luaL_pushresult(buf); return 1; case PL_CONL: has_content = tb_true; continue; case PL_FAIL: - default: luaL_pushresult(buf); xm_io_file_error(lua, "failed to readline"); + default: luaL_pushresult(buf); xm_io_file_error(lua, file, "failed to readline"); } } } @@ -403,10 +403,11 @@ static tb_int_t read_n(lua_State* lua, xm_io_file* file, tb_char_t const* contin { tb_assert(lua && file && continuation && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); - if (*continuation != '\0') xm_io_file_error(lua, "continuation is not supported for read number of bytes"); + if (*continuation != '\0') xm_io_file_error(lua, file, "continuation is not supported for read number of bytes"); tb_size_t charset = file->encoding; tb_bool_t binary = charset == XM_IO_FILE_ENCODING_BINARY || charset == XM_IO_FILE_ENCODING_UNKNOWN; - if (!binary) xm_io_file_error(lua, "read number of bytes only allows binary file, reopen with 'rb' and try again"); + if (!binary) + xm_io_file_error(lua, file, "read number of bytes only allows binary file, reopen with 'rb' and try again"); if (n == 0) { tb_byte_t buf[1]; @@ -506,7 +507,7 @@ static tb_int_t std_read_line(lua_State* lua, xm_io_file* file, tb_char_t const* case PL_FIN: luaL_pushresult(buf); return 1; case PL_CONL: has_content = tb_true; continue; case PL_FAIL: - default: luaL_pushresult(buf); xm_io_file_error(lua, "failed to readline"); + default: luaL_pushresult(buf); xm_io_file_error(lua, file, "failed to readline"); } } } @@ -529,7 +530,7 @@ static tb_int_t std_read_all(lua_State* lua, xm_io_file* file, tb_char_t const* case PL_FIN: case PL_CONL: has_content = tb_true; continue; case PL_FAIL: - default: luaL_pushresult(buf); xm_io_file_error(lua, "failed to readline"); + default: luaL_pushresult(buf); xm_io_file_error(lua, file, "failed to readline"); } } } @@ -538,7 +539,7 @@ static tb_int_t std_read_n(lua_State* lua, xm_io_file* file, tb_char_t const* co { tb_assert(lua && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); - if (*continuation != '\0') xm_io_file_error(lua, "continuation is not supported for std streams"); + if (*continuation != '\0') xm_io_file_error(lua, file, "continuation is not supported for std streams"); if (n == 0) { #ifdef TB_CONFIG_OS_WINDOWS @@ -588,7 +589,7 @@ static tb_int_t std_read_num(lua_State* lua, xm_io_file* file, tb_char_t const* { tb_assert(lua && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); - if (*continuation != '\0') xm_io_file_error(lua, "continuation is not supported for std streams"); + if (*continuation != '\0') xm_io_file_error(lua, file, "continuation is not supported for std streams"); tb_double_t d; #ifdef TB_CONFIG_OS_WINDOWS if (fwscanf(file->std_ref, L"%lf", &d) == 1) @@ -631,7 +632,7 @@ tb_int_t xm_io_file_read(lua_State* lua) if (lua_isnumber(lua, 2)) { count = (tb_long_t)lua_tointeger(lua, 2); - if (count < 0) xm_io_file_error(lua, "invalid read size, must be positive nubmber or 0"); + if (count < 0) xm_io_file_error(lua, file, "invalid read size, must be positive nubmber or 0"); } else if (*mode == '*') mode++; @@ -663,9 +664,9 @@ tb_int_t xm_io_file_read(lua_State* lua) { case 'a': return read_all(lua, file, continuation); case 'L': return read_line(lua, file, continuation, tb_true); - case 'n': xm_io_file_error(lua, "read number is not implemented"); + case 'n': xm_io_file_error(lua, file, "read number is not implemented"); case 'l': return read_line(lua, file, continuation, tb_false); - default: xm_io_file_error(lua, "unknonwn read mode"); + default: xm_io_file_error(lua, file, "unknonwn read mode"); } } if (count >= 0) return std_read_n(lua, file, continuation, count); @@ -675,6 +676,6 @@ tb_int_t xm_io_file_read(lua_State* lua) case 'L': return std_read_line(lua, file, continuation, tb_true); case 'n': return std_read_num(lua, file, continuation); case 'l': return std_read_line(lua, file, continuation, tb_false); - default: xm_io_file_error(lua, "unknonwn read mode"); + default: xm_io_file_error(lua, file, "unknonwn read mode"); } } diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c index 5e75012a5..21d5b9c0f 100644 --- a/core/src/xmake/io/file_seek.c +++ b/core/src/xmake/io/file_seek.c @@ -68,6 +68,6 @@ tb_int_t xm_io_file_seek(lua_State* lua) } else { - xm_io_file_error(lua, "seek is not supported on this file"); + xm_io_file_error(lua, file, "seek is not supported on this file"); } } |
