1
votes

I have xml profiles stored in a folder that are switched dynamically. But the behavior is absolute path and I need a relative path. The lua code is written to work with both windows paths (back slashes) and with mac paths (forward slashes).

On my mac the path might be /folder/folder/profile1.xml. In normal application the program will return a file/location of profile1.xml. And it will find the next profile in the same folder. If I direct the application to a new folder using a relative link such as ../profile2.xml then the program will find the new profile and returns the file/location as ../profile2.xml. Then it will not find the next profile within the same folder... it's either looking for the next profile out a step (../) or within the original folder as set by the application. I want it to find the next requested profile within this new folder location.

The existing code that sets the current profile and profile path is this:

local loadedprofile = '' --set by application
local profilepath = '' --set by application and modified below

The relevant switching functions seem to be:

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 

I'm thinking I might need to modify the match criteria of the third function to remove the ../. Maybe something like this removing the optional .'s

local function setFullPath(value)
local path, profile = value:match("(.-)([^\\/]-([^%.\\/]*))$")
profilepath = path
if profile ~= loadedprofile then
doprofilechange(profile)
end 

I really have no clue as to how to write this code, I'm just trying to tweak this open source code (MIDI2LR) to suit my needs. In my rudimentary understanding of the code it seems the match criteria is overly convoluted. But I would like to know if I am reading it right. I interpret it as:

: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

If I am reading it right it would seem the first "starts with" is entirely redundant, or that the "from the end" should be associated with the second "starts with."

I have commented out the setFullPath function without the desired results which makes me think that a match requirement might be needed added to the setDirectory function.

Any help is greatly appreciated as I am in way over my head. Thanks!

1

1 Answers

0
votes

Your reading of the match is incorrect, here is a more accurate version:

:match("(.-)([^\\/]-%.?([^%.\\/]*))$")
(.-) -- Match and grab everything up until the first non slash character
(  )$ -- Grab everything up until the end
[^\\/]- -- Starts with any character OTHER THAN \ or /, 0 or more occurrences first result
%.? -- single dot optional in the middle of the name (for dot in something.ext)
[^%.\\/]* -- Any character OTHER THAN . or \ or /, 0 or more occurrences

A few notes - %. is a literal dot. [^xyz] is the inverse class, so every character other than x, y, or z. \\ is actually just one backslash, this is due to the escaping in a string.

This simpler version would break it in a similar way that is easier to work with: value:match("(.-)([^\\/]+)$")

You may want to provide more information on the profile loading behavior, its hard to tell what you need the code to do. What value would path and profile have in the example you give?