[Gambas-user] Concatenate two path strings
BB
adamnt42 at gmail.com
Sun Oct 1 06:53:22 CEST 2023
On 1/10/23 3:01 pm, T Lee Davidson wrote:
> On 9/30/23 23:23, BB wrote:
>> I have two strings representing a "base" path and a "relative" path,
>> so that, for instance
>> basepath = "/home/me/ahab/bhab" ' or sometimes
>> "/home/me/ahab/bhab/"
>> relpath = "../../img/etc" ' or sometimes "img/etc" or "/img/etc"
>> or ./img/etc" or anything else that the user has constructed the
>> relative path as
>>
>> I know I can get a full path just by concatenting using &/ i.e.
>> fullpath = basepath &/ relpath
>>
>> which seems to work but results often in a crazy looking path. Short
>> of manually parsing the basepath and relpath variables into some sort
>> of standard using a lot of ifs, is there a simple Gambas way of
>> getting the proper path string? I can't find one.
>>
>> Or even a external call to something that could resolve this.
>> Strangely in all my years of using Gambas I have never had this need
>> before.
>>
>> tia
>>
>> bruce
>
> You simplified the problem description again, right? ;-)
Didn't think so? I have two strings that are supposed to represent
exactly a base and a relative path, but they are just strings at the
moment. I need to concatenate them to produce a string that represents
a real path
>
> Perhaps 'realpath' might help you normalize the relative path at the
> point of input:
This is the "good oil"! Thanks.
> [code]
> Public Sub main()
>
> Dim sRealPath As String
> Dim sUserInput As String = "../../img/etc"
>
> Exec ["realpath", sUserInput] To sRealPath With Error
> If InStr(sRealPath, "No such file or directory") Then
> Print "Bad user. Bad, bad user!"
> Quit
> Endif
> Print sRealPath
>
> End
> [/code]
>
> BTW, the two basepaths you mentioned appear to be the same.
No, in the second the sneaky bugger put a "/" on the end. They may be
equivalent paths but they are different strings.
> And, "/img/etc" is not a relative path. So, if a user might enter an
> absolute path for a relative path, you might want to check for that
> and strip it off first thing.
Good point! I haven't encountered this input before but it is a good
pre-emptive strike. Might be a bit tricky though, I'll have to be
careful. (No, on reflection, if they meant an absolute path they are
guilty of a transgression and I have no problem with the old 300kV to
the keyboard solution.)
ty
bruce
More information about the User
mailing list