[Lazarus] Directory separator character / or \ for file path differences between Linux, Mac and Windows, etc

Michael Van Canneyt michael at freepascal.org
Tue Dec 28 21:53:40 CET 2010



On Wed, 29 Dec 2010, Peter E Williams wrote:

> Hi
> 
> This is really 4 questions:
> 
> One: Since Windows users the '\' (backslash) character and Linux uses the '/' (foreslash) character in file paths, have I got those
> the right way around?

You have.

> 
> Two: What do Macs use for directory path separator character?

Never hardcode the path separator.

The PathDelim constant in the system unit always contains the correct character.

Always use the IncludeTrailingPathDelimiter or ExcludeTrailingPathDelimiter functions to correct paths.
in the best case, you can do something like

Apath := IncludeTrailingPathDelimiter(Apath)+'asubdir'+PathDelim;

I suggest you check out the documentation of the system unit.

> 
> Three: Is the a cross-platform function (or procedure) for correcting the file path separator character? I believe that there is
> one.

There is. SetDirSeparators, in sysutils:

http://www.freepascal.org/docs-html/rtl/sysutils/setdirseparators.html

> 
> e.g. c:\Program Files\Fred's software\data (folder) >>> c:/Program Files/Fred's software/data (folder)
> 
> Yes, I am well aware the c:\Program Files\ does usually exist on Linux systems. Actually, if it does exist it will be:
> 
> ~/Home/.wine/drive_c/ (folder) with Wine installed.
> 
> So, it would become ~/Home/.wine/drive_c/Program Files/Fred's software/data/  (folder)

No, it will not become that. It will become c:/program files/.

We do not support a wine emulation under linux.

Never, ever, hardcode paths like C:\program files in your programs. 
It is not cross-platform.

(and furthermore, it may not always exist on windows, either)

> 
> Four: Is the a compiler directive (cross-platform) or other way of detecting the OS of the user's system?

Yes. See:

http://www.freepascal.org/docs-html/prog/progsu38.html#x44-430001.1.38

Basically,

Const
   CurrentOS = {$I %FPCTARGETOS };

Will create a string constant that contains the name of the target you compiled for.
(which under normal circumstances equals the system the binary can run on)

Michael.


More information about the Lazarus mailing list