[Lazarus] CreateRelativePath document error.

Mattias Gärtner nc-gaertnma at netcologne.de
Tue Feb 9 15:20:19 CET 2010


Zitat von Michael Van Canneyt <michael at freepascal.org>:

>
>
> On Tue, 9 Feb 2010, Paul van Helden wrote:
>
>>
>> On 2010/02/09 02:48 PM, Mattias Gärtner wrote:
>>> Zitat von Paul van Helden <paul at planetgis.co.za>:
>>>
>>>>
>>>> Good. What is the recommended function for the inverse process?
>>>
>>> I don't know for the ExtractRelativePath, but for  
>>> CreateRelativePath it is simple:
>>>
>>> AbsolutePath:=AppendPathDelim(ABasePath)+RelPath
>>>
>>>
>> I'm talking specifically about /relative/ paths, ie. ones with  
>> ../../../ in them.
>>
> [...]
>>>  - ExpandFileName()
> [...]
>>>  - CleanAndExpandFilename()
>>>  - CleanAndExpandDirectory()
>>>
>>>
>> Graeme: you also missed this. None of those functions take a base  
>> path and applies a relative path to produce a new absolute path.  
>> The function must be able to go up some directories and then down  
>> some others.
>
> ExpandFileName does exactly that.

No. ExpandFilename does much more and less. It corrects \ and / to  
path delim, expands a relative filename with the *current working  
directory* (Get/SetCurrentDir) and it expands tilde '~' to $HOME under  
Linux.

If you want only create an absolute path from a directory and a  
relative path use:
AbsPath:=AppendPathDelim(BaseDir)+RelPath;

This can create a file like '/usr/../lib' or '/usr/./lib' or  
'/usr//lib', which are all valid file names, just somewhat ugly.
To resolve '..', '.' and double path delims use TrimFilename:

AbsPath:=TrimFilename(BaseDir+PathDelim+RelPath);

The above three would result in '/usr/lib'.

To even resolve system specials like '~' and cut trailing path delim  
use CleanAndExpandFilename:

AbsPath:=CleanAndExpandFilename(BaseDir+PathDelim+RelPath);

CleanAndExpandDirectory is like CleanAndExpandFilename but appends a  
path delim if not already there.

If you even want to resolve symlinks or get the real file name (e.g.  
OS X UTF8 normalization, case insensitive file systems) there are  
functions for that too.


Mattias






More information about the Lazarus mailing list