【发布时间】:2016-05-14 12:34:27
【问题描述】:
我将 xml 配置文件存储在动态切换的文件夹中。但行为是绝对路径,我需要一个相对路径。 lua 代码可用于 windows 路径(反斜杠)和 mac 路径(正斜杠)。
在我的 Mac 上,路径可能是 /folder/folder/profile1.xml。在正常应用程序中,程序将返回 profile1.xml 的文件/位置。它会在同一文件夹中找到下一个配置文件。 如果我使用 ../profile2.xml 等相对链接将应用程序定向到新文件夹,则程序将找到新配置文件并将文件/位置作为 ../profile2.xml 返回。然后它不会在同一个文件夹中找到下一个配置文件......它要么在一个步骤(../)中寻找下一个配置文件,要么在应用程序设置的原始文件夹中寻找下一个配置文件。我希望它在这个新文件夹位置中找到下一个请求的配置文件。
设置当前配置文件和配置文件路径的现有代码是这样的:
local loadedprofile = '' --set by application
local profilepath = '' --set by application and modified below
相关的切换函数好像是:
local function setDirectory(value)
profilepath = value
end
local function setFile(value)
if loadedprofile ~= value then
doprofilechange(value)
end
end
local function setFullPath(value)
local path, profile = value:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
profilepath = path
if profile ~= loadedprofile then
doprofilechange(profile)
end
我想我可能需要修改第三个函数的匹配条件以删除 ../.也许像这样删除可选的 .'s
local function setFullPath(value)
local path, profile = value:match("(.-)([^\\/]-([^%.\\/]*))$")
profilepath = path
if profile ~= loadedprofile then
doprofilechange(profile)
end
我真的不知道如何编写这段代码,我只是想调整这个开源代码 (MIDI2LR) 以满足我的需要。在我对代码的初步理解中,匹配标准似乎过于复杂。但我想知道我是否读对了。我将其解释为:
:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
(.-) --minimal return
( )$ --from the end of profile path
[^\\/]- --starts with \ or \\ or /, 0 or more occurrences first result
%.? --through, with dots optional
[^%.\\/]* --starts with . or \ or \\ or /, 0 or more occurrences all results
如果我没看错的话,第一个“开始于”似乎是完全多余的,或者“从头到尾”应该与第二个“开始于”相关联。
我已经注释掉了 setFullPath 函数,但没有得到想要的结果,这让我认为可能需要将匹配要求添加到 setDirectory 函数中。
任何帮助都非常感谢,因为我在我的头上。谢谢!
【问题讨论】: