Page 2 of 2

Re: API way to add backslash to a path

Posted: Fri Jan 20, 2012 2:41 pm
by Psychophanta
Simpler:

Code: Select all

Procedure PathAddBackslash(*Path$)
  If Right(*Path$,1)<>"\"
    *Path$+"\"
  EndIf
EndProcedure

Re: API way to add backslash to a path

Posted: Fri Jan 20, 2012 8:09 pm
by pcfreak
But using PathAddBackslash_() can create a buffer overflow if the string memory is not large enough.

http://msdn.microsoft.com/en-us/library ... s.85).aspx
Parameters
lpszPath [in, out]
Type: LPTSTR
A pointer to a buffer with a string that represents a path. The size of this buffer must be set to MAX_PATH to ensure that it is large enough to hold the returned string.
Even having said that it surely is faster...

Code: Select all

OpenConsole()

#count = 5000000

time.q = ElapsedMilliseconds()
For i = 0 To #count
 p1$="c:"
 If Right(p1$,1)<>"\" : p1$+"\" : EndIf
Next
t1.q = ElapsedMilliseconds() - time

time.q = ElapsedMilliseconds()
For i = 0 To #count
 p2$="c:"
 PathAddBackslash_(p2$)
Next
t2.q = ElapsedMilliseconds() - time

PrintN("t1 = " + Str(t1))
PrintN("t2 = " + Str(t2))

PrintN("")
Print("- ENTER -")
Input()
My result:
t1 = 2418
t2 = 874