diff options
| author | ruki <[email protected]> | 2021-09-22 22:52:53 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-09-22 11:26:36 +0800 |
| commit | 5e614774a6cc59832b66d007c59d6da3b77f124b (patch) | |
| tree | 87ff613be08437a46b2bd9e0b7e66b935762e2db /core/src | |
| parent | d62f35f77a30fe0280086241af2142b282fa1f17 (diff) | |
add bom for vs project generator
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/io/file_open.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/io/file_write.c | 38 | ||||
| -rw-r--r-- | core/src/xmake/io/prefix.h | 1 |
3 files changed, 45 insertions, 0 deletions
diff --git a/core/src/xmake/io/file_open.c b/core/src/xmake/io/file_open.c index c92380207..435e2aec0 100644 --- a/core/src/xmake/io/file_open.c +++ b/core/src/xmake/io/file_open.c @@ -231,6 +231,11 @@ tb_int_t xm_io_file_open(lua_State* lua) 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")) + utfbom = tb_true; + // open file tb_bool_t open_ok = tb_false; tb_stream_ref_t file_ref = tb_null; @@ -293,6 +298,7 @@ tb_int_t xm_io_file_open(lua_State* lua) file->mode = mode; file->type = XM_IO_FILE_TYPE_FILE; file->encoding = encoding; + file->utfbom = utfbom; // init the read/write line cache buffer tb_buffer_init(&file->rcache); diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c index 12ab829b3..c7db01937 100644 --- a/core/src/xmake/io/file_write.c +++ b/core/src/xmake/io/file_write.c @@ -33,6 +33,36 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ +static tb_void_t xm_io_file_write_file_utfbom(xm_io_file_t* file) +{ + // check + tb_assert(file && data && xm_io_file_is_file(file) && file->file_ref); + + // write bom + switch (file->encoding) + { + case TB_CHARSET_TYPE_UTF8: + { + static tb_byte_t bom[] = {0xef, 0xbb, 0xbf}; + tb_stream_bwrit(file->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->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->file_ref, bom, sizeof(bom)); + } + break; + default: + break; + } +} static tb_void_t xm_io_file_write_file_directly(xm_io_file_t* file, tb_char_t const* data, tb_size_t size) { // check @@ -141,7 +171,15 @@ tb_int_t xm_io_file_write(lua_State* lua) else if (is_binary) xm_io_file_write_file_directly(file, data, (tb_size_t)datasize); else + { + // write utf bom first? + if (file->utfbom) + { + xm_io_file_write_file_utfbom(file); + file->utfbom = tb_false; + } xm_io_file_write_file_transcrlf(file, data, (tb_size_t)datasize); + } } } lua_settop(lua, 1); diff --git a/core/src/xmake/io/prefix.h b/core/src/xmake/io/prefix.h index e11198da1..bec186a92 100644 --- a/core/src/xmake/io/prefix.h +++ b/core/src/xmake/io/prefix.h @@ -88,6 +88,7 @@ typedef struct __xm_io_file_t 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 |
