Sicro wrote:Good trick, davido! If this is safe to use, it would save a lot of code.
But it's undocumented behavior, so currently it's not safe to use, in my opinion.
What exactly is undocumented in this context?
I'm copying the procedure here, so that it's easier to discuss about it:
Code: Select all
Procedure$ GetPreviousDirectory(Path$)
Protected Slash$
Protected StringLength
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
Slash$ = "\"
CompilerElse ; Linux, Mac
Slash$ = "/"
CompilerEndIf
Path$ = RTrim(Path$, Slash$)
StringLength = Len(Path$)
While Path$ <> "" And Right(Path$, 1) <> Slash$
StringLength - 1
Path$ = Left(Path$, StringLength)
Wend
ProcedureReturn Path$
EndProcedure
IMHO it never makes sense to do unnecessary work, neither for humans nor for computers.
Here, it's not necessary to re-build
Path$ in each iteration of the loop. The purpose of the loop "only" is, to find the
position of the regarding slash. Then the wanted path can be built afterwards
outside of the loop.
As I wrote previously, personally I am fine with the method that davido posted in his original message -- and I can't see why this should not be safe.
However, if I wanted to write some code for this myself, then I'd do it like this:
//edit:
Code changed according to helpy's hint (see below)
Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#DirSep$ = "/\"
CompilerElse
#DirSep$ = "/"
CompilerEndIf
Procedure.s ParentDirectory (path$)
Protected posn.i
posn = Len(path$) - 1
While posn > 0 And FindString(#DirSep$, Mid(path$, posn, 1)) = 0
posn - 1
Wend
ProcedureReturn Left(path$, posn)
EndProcedure
Using pointers will probably yield even faster code.