【问题标题】:Handle special characters in lua file path (umlauts)处理 lua 文件路径中的特殊字符(变音符号)
【发布时间】:2016-08-11 13:18:26
【问题描述】:

我有一个小 lua 函数来检查文件是否存在

function file_exists( filePath )
    local handler = io.open( filePath )
    if handler then
        io.close( handler )
        return true
    end
    return false
end

但是,当文件路径包含特殊字符(例如德语变音符号 (äöü))时,这将始终返回 false。有没有办法解决?

非常感谢!

【问题讨论】:

  • 您需要在 Windows 默认代码页(通常是 win12xx)而不是 UTF-8 中对文件名进行编码。
  • 对不起,你的意思是什么?我从最初存储它们的不同来源获取文件路径
  • 我的意思是你的路径(从不同的来源接收)可能在错误的代码页中。用yourpath:byte(1,-1)检查它以猜测编码。
  • 是的,它们以 UTF-8 存储。但是我想lua中没有办法转换成不同的代码页,是吗?
  • 当然有办法。您的 Windows 代码页是什么?您可以从HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Nls\CodePage\ACP 或通过在 LuaJIT 中运行此程序来确定您的代码页:f=require"ffi" f.cdef"int GetACP();" print(f.C.GetACP())

标签: lua windows-server-2008-r2 lua-5.1


【解决方案1】:
utf8_to_cp1252 = (
   function(cp1252_description)
      local unicode_to_1252 = {}
      for code, unicode in cp1252_description:gmatch'\n0x(%x%x)%s+0x(%x+)' do
         unicode_to_1252[tonumber(unicode, 16)] = tonumber(code, 16)
      end
      local undefined = ('?'):byte()
      return
         function (utf8str)
            local pos, result = 1, {}
            while pos <= #utf8str do
               local code, size = utf8str:byte(pos, pos), 1
               if code >= 0xC0 and code < 0xFE then
                  local mask = 64
                  code = code - 128
                  repeat
                     local next_byte = utf8str:byte(pos+size, pos+size) or 0
                     if next_byte >= 0x80 and next_byte < 0xC0 then
                        code, size = (code - mask - 2) * 64 + next_byte, size+1
                     else
                        code, size = utf8str:byte(pos, pos), 1
                     end
                     mask = mask * 32
                  until code < mask
               end
               pos = pos + size
               table.insert(result, 
                  string.char(unicode_to_1252[code] or undefined))
            end
            return table.concat(result)
         end
   end
)[[
download 
http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT
and insert the whole text here:

#
#    Name:     cp1252 to Unicode table
#    Unicode version: 2.0
#    Table version: 2.01
..................................
0xFD    0x00FD  #LATIN SMALL LETTER Y WITH ACUTE
0xFE    0x00FE  #LATIN SMALL LETTER THORN
0xFF    0x00FF  #LATIN SMALL LETTER Y WITH DIAERESIS
]]

用法:

cp1252_filename = utf8_to_cp1252(your_utf8_filename)

现在您可以使用cp1252_filename 调用标准Lua 库中的io.open()os.rename()os.execute() 和其他函数。

【讨论】:

  • 非常感谢!我会试一试,让你知道这是否适合我
  • 虽然我真的不明白这里的全部魔法这实际上对我有用:) 谢谢!
  • 这不适用于日文、俄文和其他一些语言字符。
  • @mrg95 - 见this project - 它确实支持日语、俄语等。
【解决方案2】:

Lua 及其微小的标准库是平台中立的,并且不知道正确的 Windows 函数来读取完整的 unicode 名称。您可以使用winapi 模块来获取此任务的一些特定于 Windows 的功能。请注意,它需要在目标磁盘上启用短名称生成。

local handler = io.open( winapi.short_path(filePath) )
if handler then
    -- etc
end

也可以通过LuaRocks轻松安装:luarocks install winapi

【讨论】:

  • 我应该补充一点,我们正在使用 lua 作为另一个应用程序的一部分——它提供了 lua 组件。用 luarocks 安装 lua 扩展的方法可以吗?我们还没有安装 luarocks。
  • 您可以简单地将所有文件放在require 可以手动读取的任何路径中。
猜你喜欢
  • 1970-01-01
  • 2016-10-10
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-31
  • 2021-09-07
相关资源
最近更新 更多