API way to add backslash to a path

Share your advanced PureBasic knowledge/code with the community.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: API way to add backslash to a path

Post by Psychophanta »

Simpler:

Code: Select all

Procedure PathAddBackslash(*Path$)
  If Right(*Path$,1)<>"\"
    *Path$+"\"
  EndIf
EndProcedure
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
pcfreak
User
User
Posts: 75
Joined: Sat May 22, 2004 1:38 am

Re: API way to add backslash to a path

Post 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
Post Reply