diff options
| author | ruki <[email protected]> | 2026-01-15 00:57:06 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2026-01-15 00:57:06 +0800 |
| commit | dd18f22140b3d8a0a8491105f052df99273dab99 (patch) | |
| tree | f296f2d23ca08531267eaf041b12a23522dff78d /xmake/core/base/io.lua | |
| parent | 4ecd3eff0b966f7d16cf5d3dc413add12bb19486 (diff) | |
add io.convert
Diffstat (limited to 'xmake/core/base/io.lua')
| -rw-r--r-- | xmake/core/base/io.lua | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua index f35c01ab3..b6bb79ecc 100644 --- a/xmake/core/base/io.lua +++ b/xmake/core/base/io.lua @@ -598,6 +598,27 @@ function io.close(file) return (file or io.stdout):close() end +-- convert file encoding +function io.convert(inputfile, outputfile, opt) + opt = opt or {} + inputfile = tostring(inputfile) + outputfile = tostring(outputfile) + local from = opt.from or "utf8" + local to = opt.to or "utf8" + + -- try using c implementation first + if io.file_convert and io.file_convert(inputfile, outputfile, from, to) then + return true + end + + -- fallback to lua implementation (slow but works) + local content, errors = io.readfile(inputfile, {encoding = from}) + if not content then + return false, errors + end + return io.writefile(outputfile, content, {encoding = to}) +end + -- save object the the given filepath function io.save(filepath, object, opt) opt = opt or {} |
