5

给 Lua 在 windows 下换上 utf-8 文件名支持

 2 years ago
source link: https://blog.codingnow.com/2017/10/lua_windows_utf8.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

给 Lua 在 windows 下换上 utf-8 文件名支持

最近在 windows 做开发比较多,lua 原生库使用的都是 C 标准库中的函数,比如文件操作就是用的 fopen 打开文件。这对 unicode 支持的很糟糕。我希望所有和文件名打交道的地方都使用 utf-8 编码,所以今天花了一点时间实现了这么一个库。

我把 lua 原生库中和文件名有关的 api 都重新实现了一遍,包括了:loadfile , dofile , os.rename , os.remove , os.execute, os.getenv , 以及 io.open 。除了 require 都可以在接口上使用 utf-8 字符串了。这里 require 是偷懒没支持 :)

用的比较多的 lfs 库也缺乏 utf-8 支持,我挑选了一部分 api 改写了 utf-8 版本。其中包括 lfs.dir , lfs.currentdir, lfs.chdir, lfs.touch, lfs.mkdir, lfs.rmdir, lfs.attributes 。

对于 windows 还有两个特别的新 api 很可能用得上:

winfile.shortname 可以用来获取短文件名,这样可以很好的去除掉长文件名中的空格。

winfile.personaldir 用来获取 My Document 目录。我可不想在 windows 下使用注册表来保存数据,还是去我的文档下建个目录来存放比较好。

最后: github 仓库传送门

云风 提交于 October 25, 2017 11:25 PM | 固定链接


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK