Page 1 of 1

Add Constant for OS default path separator

Posted: Wed May 24, 2017 12:57 pm
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.

Re: Add Constant for OS default path separator

Posted: Wed May 24, 2017 1:37 pm
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.

Re: Add Constant for OS default path separator

Posted: Wed May 24, 2017 2:09 pm
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.

Re: Add Constant for OS default path separator

Posted: Wed May 24, 2017 3:50 pm
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)

Re: Add Constant for OS default path separator

Posted: Thu May 25, 2017 5:47 pm
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

Re: Add Constant for OS default path separator

Posted: Thu May 25, 2017 7:06 pm
by ts-soft
kenmo wrote:"/" is valid in Windows,
Not always, some API required "\" (backslash), most Requester for example.

+1

Re: Add Constant for OS default path separator

Posted: Thu May 25, 2017 9:03 pm
by davido
+1

Re: Add Constant for OS default path separator

Posted: Fri May 26, 2017 11:47 am
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.

Re: Add Constant for OS default path separator

Posted: Fri May 26, 2017 10:37 pm
by RSBasic
+1

Re: Add Constant for OS default path separator

Posted: Fri Jul 21, 2017 10:28 pm
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.

Re: Add Constant for OS default path separator

Posted: Sat Jul 22, 2017 8:10 pm
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!

Re: Add Constant for OS default path separator

Posted: Mon Aug 21, 2017 6:57 pm
by Justin
+1 for the constant. i can't beleive this is not included yet.

Re: Add Constant for OS default path separator

Posted: Mon Aug 21, 2017 7:45 pm
by Justin
Or better, includefile could resolve the paths automatically without us having to substitute the constant