summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-22 22:52:53 +0800
committerruki <[email protected]>2021-09-22 11:26:36 +0800
commit5e614774a6cc59832b66d007c59d6da3b77f124b (patch)
tree87ff613be08437a46b2bd9e0b7e66b935762e2db
parentd62f35f77a30fe0280086241af2142b282fa1f17 (diff)
add bom for vs project generator
-rw-r--r--core/src/xmake/io/file_open.c6
-rw-r--r--core/src/xmake/io/file_write.c38
-rw-r--r--core/src/xmake/io/prefix.h1
-rw-r--r--xmake/plugins/project/vstudio/impl/vsfile.lua2
-rw-r--r--xmake/plugins/project/vsxmake/vsxmake.lua4
5 files changed, 49 insertions, 2 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
diff --git a/xmake/plugins/project/vstudio/impl/vsfile.lua b/xmake/plugins/project/vstudio/impl/vsfile.lua
index a17384716..040bbee2b 100644
--- a/xmake/plugins/project/vstudio/impl/vsfile.lua
+++ b/xmake/plugins/project/vstudio/impl/vsfile.lua
@@ -97,7 +97,7 @@ end
function open(filepath, mode)
-- open it
- local file = io.open(filepath, mode)
+ local file = io.open(filepath, mode, {encoding = "utf8bom"})
-- hook print, printf and write
file._print_impl = file.print
diff --git a/xmake/plugins/project/vsxmake/vsxmake.lua b/xmake/plugins/project/vsxmake/vsxmake.lua
index 0055aaff2..cc50cb2d3 100644
--- a/xmake/plugins/project/vsxmake/vsxmake.lua
+++ b/xmake/plugins/project/vsxmake/vsxmake.lua
@@ -167,7 +167,9 @@ function _writefileifneeded(file, content)
dprint("skipped file %s since the file has the same content", path.relative(file))
return
end
- io.writefile(file, content)
+ -- we need utf8 with bom encoding for unicode
+ -- @see https://github.com/xmake-io/xmake/issues/1689
+ io.writefile(file, content, {encoding = "utf8bom"})
end
function _clear_cacheconf()