Add Constant for OS default path separator

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Add Constant for OS default path separator

Post by Tristano »

It would be useful to have a built-in PB Constant to represent the current OSs' default path separator as string.

Ie: "\" for Windows, and "/" for Linux and macOS.

In many string manipulation operations (eg: split and join path strings) this constant would be quite handy, especially for setting default values in procedures' parameters.

Not sure what a good name for this constant would be, but probably something like #DIR_SEP$ or #PATH_SEP$ would be fine.

Currently, I'm implementing this as:

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  ; ================================== Windows ===================================
  #DIR_SEP$ = "\"
CompilerElse
  ; ============================== Linux and macOS ===============================
  #DIR_SEP$ = "/"
CompilerEndIf
... but I think it's a feature common enough to be worth implementing into the lang.
The PureBASIC Archives: FOSS Resources:
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Add Constant for OS default path separator

Post by kenmo »

+1

I include something like this in almost every program...

Code: Select all

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)
  #PS  = '\'
  #NPS = '/'
CompilerElse
  #PS  = '/'
  #NPS = '\'
CompilerEndIf

#PS$  = Chr(#PS)
#NPS$ = Chr(#NPS)
I call it #PS for Path Separator.
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Re: Add Constant for OS default path separator

Post by Tristano »

I like your approach @kenmo: the fact that it also defines the non-valid separator can be quite handy.

Also, I didn't know about the syntax of assigning a char number via quotes.

Do you think it would be better to have the built-in separator constant as a string or as an Ascii code?

I suggested a string constant because I thought that it might be mostly used in string concatenation (on the lines of #CR$ and #CRLF$), but others might prefer an Ascii code instead. Surely, converting from one to another is fairly straight forward either way.

Maybe both? After all we have both #CR$ ("\r") and #CR (13) as built-in constants, so this one might follow the same path.
The PureBASIC Archives: FOSS Resources:
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Add Constant for OS default path separator

Post by kenmo »

Definitely both. String constants are useful for string variables and PB's functions, Integer constants are useful for parsing characters in-memory.

Yes, #CR, #LF, #TAB and the other ASCII control characters are provided as both.
I also use #SP and #SP$ for Space. :)


"/" is valid in Windows, but for cleaning up Windows filepaths it's nice to replace "/" (NPS) with "\" (PS)
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Add Constant for OS default path separator

Post by Sicro »

+1

It is very often needed if you work with paths and it should be crossplatform.

Other programming languages already have such constants:
https://secure.php.net/manual/en/dir.constants.php
https://msdn.microsoft.com/en-us/librar ... .110).aspx
https://www.freepascal.org/docs-html/rt ... rator.html
https://stackoverflow.com/questions/597 ... hseparator
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Add Constant for OS default path separator

Post by ts-soft »

kenmo wrote:"/" is valid in Windows,
Not always, some API required "\" (backslash), most Requester for example.

+1
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Add Constant for OS default path separator

Post by davido »

+1
DE AA EB
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Re: Add Constant for OS default path separator

Post by Tristano »

Thanks for the links @Sicro.

The MSDN link was broken, here is the fixed version:

https://msdn.microsoft.com/en-us/librar ... hseparator

I was recently looking at some Node.js libraries for handling path string operations, and I came across some really cool functions. For example, the path normalize function:

https://nodejs.org/api/path.html#path_p ... alize_path
The path.normalize() method normalizes the given path, resolving '..' and '.' segments.

When multiple, sequential path segment separation characters are found (e.g. / on POSIX and \ on Windows), they are replaced by a single instance of the platform specific path segment separator. Trailing separators are preserved.

If the path is a zero-length string, '.' is returned, representing the current working directory.
... and many other cross-platform functions dealing with path operations. It would be nice to see an implementation of similar functions in PB, and maybe even have them as a built-in library one day.

I think it would make sense, after all PB already offers a CGI library and other libs and commands which make it good at creating server/client applications, as well as general purpose console apps. Surely, some more functions for handling path (file) operations would be a good enhancement in that direction. But even a custom library. or bindings/wrappers to existing libs would be great in this respect.
The PureBASIC Archives: FOSS Resources:
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Add Constant for OS default path separator

Post by RSBasic »

+1
Image
Image
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Add Constant for OS default path separator

Post by Sicro »

Tristano wrote:I was recently looking at some Node.js libraries for handling path string operations, and I came across some really cool functions. For example, the path normalize function:

https://nodejs.org/api/path.html#path_p ... alize_path
The path.normalize() method normalizes the given path, resolving '..' and '.' segments.
[...]
Today I wrote an include "GetAbsolutePath" and added it to the CodeArchive that provides this functionality.
https://github.com/SicroAtGit/PureBasic ... tePath.pbi

Look at the other path functions as well:
https://github.com/SicroAtGit/PureBasic ... FileSystem

I will also look at the other path functions of nodejs and try to add them as a include to the CodeArchive.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
Tristano
Enthusiast
Enthusiast
Posts: 190
Joined: Thu Nov 26, 2015 6:52 pm
Location: Italy
Contact:

Re: Add Constant for OS default path separator

Post by Tristano »

Sicro wrote:Today I wrote an include "GetAbsolutePath" and added it to the CodeArchive that provides this functionality.
https://github.com/SicroAtGit/PureBasic ... tePath.pbi

Look at the other path functions as well:
https://github.com/SicroAtGit/PureBasic ... FileSystem

I will also look at the other path functions of nodejs and try to add them as a include to the CodeArchive.
Thanks Sicro! These are really useful indeed. Well done!
The PureBASIC Archives: FOSS Resources:
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Add Constant for OS default path separator

Post by Justin »

+1 for the constant. i can't beleive this is not included yet.
Justin
Addict
Addict
Posts: 829
Joined: Sat Apr 26, 2003 2:49 pm

Re: Add Constant for OS default path separator

Post by Justin »

Or better, includefile could resolve the paths automatically without us having to substitute the constant
Post Reply