0

I'm trying to determine if one of several directories are present from within a lua script. It works on OSX, but not on Windows (linux is currently untested, but I expect that one to work). When the following code runs, I get an error:

failed with this C:\Program Files (x86)\VideoLAN\VLC\lua\playlist\: No such file or directory

I can confirm that that directory exists. I've escaped the slashes, I'm not sure what else could be the issue.

local oses = { "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/"; "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\"; "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\"; "/usr/lib/vlc/lua/playlist" }

-- Determine which OS this is (and where to find share/lua).
local f,err = io.open( oses[1], "r")
if not err then
    opsys = "OSX"
    scriptpath = oses[1] .. script
    f:close()
else
    f,err = io.open( oses[2], "r")
    if not err then
        opsys = "Win32"
        scriptpath = oses[2] .. script
        f:close()
    else
        f,err = io.open( oses[3], "r")
        vlc.msg.dbg( dhead .. 'failed with this ' .. err .. dtail ) 
        if not err then
            opsys = "Win64"
            scriptpath = oses[3] .. script
            f:close()
        else
            f,err = io.open( oses[4], "r")
            if not err then
                opsys = "Linux/Unix"
                scriptpath = oses[4] .. script
                f:close()
            else
                return false
            end
        end
    end 
end
4

1 に答える 1

3

ファイル「C:\Program Files\VideoLAN\VLC\lua\playlist\」が存在しません。末尾のスラッシュを削除すると、ディレクトリを開こうとして、おそらくアクセス許可エラーが発生します。どちらの方法でもうまくいきません。この方法で OS を判別する場合は、ファイルを開こうとする必要があります。

たとえば、スクリプト パスを作成し、そのファイルを開いてみて、それを使用て合格/不合格を判断します。

補足として、コードの構造は大幅に改善される可能性があります。インデックスが異なる重複コードが多数ある場合は常に、ループを使用する必要があります。たとえば、コードを次のように置き換えることができます。

local oses = {
    ["OSX"]        = "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/",
    ["Win32"]      = "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\",
    ["Win64"]      = "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\",
    ["Linux/Unix"] = "/usr/lib/vlc/lua/playlist",
}
for osname, directory in pairs(oses) do
    local scriptpath = directory..script
    local f,err = io.open( scriptpath, "r")
    if not err then
        f:close()
        return scriptpath, osname
    end
end
于 2012-08-27T16:52:22.813 に答える