Relative Path Maker

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Relative Path Maker

Post by SirWobbyTheFirst » Tue Apr 23, 2013 1:54 pm

Hey guys, I consider myself to be pretty knowledgeable in LiveCode having worked with it since 2003 but there are times when I am left completely clueless, and this is one of those times, I need to generate a relative path. In my SDK project, I have to try and generate a relative path if it is possible to do so and the problem I have is that I don't know how to do that.

When a user picks a location to save a project, there is a possibility that they'll save it in a folder under the root solution folder, but then again there is a possibility that the user will choose a completely separate folder, hell even drive, and thus my question is how can I detect that a file is placed in a path that is relative to the solution folder.

For example if I have a solution folder of X:\Xenon Software Development Kit '13 and inside that I have a folder called Compiled Files and inside that I store my XenonSDK.rev file, how can I figure out the relative path whilst offering the choice of storing to a folder that is outside the scope of my solution folder and being as cross platform as possible.

I've tried making use of the Replace command in the form of the following:

Code: Select all

Put SdkGetSolutionProperty("cSolutionFolder") Into tSolutionFolder // This is the file path of the solution minus the filename. e.g. X:/Xenon Software Development Kit '13
Replace BackSlash With Slash In tSolutionFolder
Replace BackSlash With Slash In pPath // pPath contains the full file path that the OS gave us from the Ask dialog. e.g. X:/Xenon Software Development Kit '13/Project Files/LiveCode/XenonSDK.rev
Replace tSolutionFolder With Empty In pPath // Yields things like /Project Files/LiveCode Stacks/XenonSDK.rev which I think would probably break Unix based file paths and I cannot remove the forward slash because again it could break legit paths such as /root/Documents
If anyone has any idea I'd appreciate it if you could help, I am disappointed in myself for not knowing stuff like this already but I am completely stumped right now.

SirWobbyTheFirst
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 246
Joined: Tue Jun 30, 2009 11:15 pm

Re: Relative Path Maker

Post by SirWobbyTheFirst » Tue Apr 23, 2013 2:33 pm

And I think I just solved my problem. (Face Palm) I could append a forward slash to the end of Solution Folder path so that when a path is replaced, it removes the slash at the start of the path. I told you, 10 years experience, I'm brain farting left, right and center.

Code: Select all

Put SdkGetSolutionProperty("cSolutionFolder") Into tSolutionFolder
Replace BackSlash With Slash In tSolutionFolder
Replace BackSlash With Slash In pPath
If The Last Character Of tSolutionFolder <> Slash Then
   Put Slash After tSolutionFolder
End If
Replace tSolutionFolder With Empty In pPath
This example assumes that pPath is a parameter containing a full, no holds barred, absolute file path such as X:\Xenon Software Development Kit '13\Project Files\LiveCode Stacks\XenonSDK.rev and that the Solution Folder is X:/Xenon Software Development Kit '13 it first replaces any backslashes with forward slashes in both paths and then checks to see if the last character of the solution folder path is a forward slash and if necessary add one to the end.

Then deletes the solution folder value should it exist from pPath. Leaving paths outside of the scope of the solution folder, unharmed but converting folders inside the solution folder to relative paths whilst leaving paths such as ../Project Files unharmed as well. I've been literally bashing my head against the wall for about 2 hours trying to think of a way to make it work. Unbelievable.
</rant>

I hope someone else finds this useful, also bro tip, no matter how good we get, there are times when even the professionals are left scratching their heads.

Post Reply