API way to add backslash to a path

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

API way to add backslash to a path

Post by PB »

For years I've been doing this to add a backslash to a path, because I didn't
know the Windows API could do it for me. The good thing is that the API won't
add a slash if it's already there, so you don't need to do an If/EndIf test. :)

Code: Select all

p1$="c:"
If Right(p1$,1)<>"\" : p1$+"\" : EndIf
Debug p1$

p2$="c:"
PathAddBackslash_(p2$)
Debug p2$
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

because the length of code doesn't always determine performance, i did a speed-test... ;)

Code: Select all

time1 = ElapsedMilliseconds()
For n=0 To 9999999
  p1$="c:" 
  If Right(p1$,1)<>"\" : p1$+"\" : EndIf 
Next
time1 = ElapsedMilliseconds() - time1

time2 = ElapsedMilliseconds()
For n=0 To 9999999
  p2$="c:" 
  PathAddBackslash_(p2$) 
Next
time2 = ElapsedMilliseconds() - time2

MessageRequester("speed test", "String-Operation: " + Str(time1) + #CRLF$ + "API-Call: " + Str(time2) )
surprisingly, the API is twice as fast, with Unicode activated even three times as fast.

...guess it's the Right()-operation, that needs that much time...
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Thanks Peebs, nice tip! I didn't know about this little gem.
BERESHEIT
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

Cool man, thanks :)
I love Purebasic.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You are making the string longer without reallocating it.
It works with such a short example, because windows allocates memory at a certain boundary (16 bytes iirc).
If you happen to have a string that exactly fills the allocated buffer, well... boom ;)

The chance for that is remote, but if it ever happens, good luck on tracing that bug...
quidquid Latine dictum sit altum videtur
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> If you happen to have a string that exactly fills the allocated buffer, well... boom ;)

Are you sure? The MSDN docs says the return value will be null if the backslash
could not be appended due to inadequate buffer size, which means no boom?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanks PB,
Seems to work fine (upto 256 file name characters), look:

Code: Select all

For t=0 To 270
  p2$=LSet("c:",t,"n")
  Debug Right(p2$,3)
  Debug Len(p2$)
  PathAddBackslash_(p2$)
  Debug Right(p2$,3)
  Debug Len(p2$)
Next
The way winapi manages strings is the same as C and also the same way as PureBasic do, or am i wrong? So I'd bet Timo is wrong for the first time
:)
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

PB wrote:> If you happen to have a string that exactly fills the allocated buffer, well... boom ;)

Are you sure? The MSDN docs says the return value will be null if the backslash
could not be appended due to inadequate buffer size, which means no boom?
It also says that the buffer should be MAX_PATH in size, which is not the case.
As the function takes no "size" parameter it cannot know how big the buffer really is,
so my guess is it simply assumes MAX_PATH as the size.

And no, PB's string management is very different ;)
quidquid Latine dictum sit altum videtur
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

OK, i'm confused here; The MSDN site says: "lpszPath
[in, out] Pointer to a buffer with a string that represents a path. The size of this buffer should be set to MAX_PATH to ensure that it is large enough to hold the returned string."

So this means if a buffer size was MAX_PATH it would always be big enough to hold the returned string? So if PathAddBackslash_(p2$) and
p2$ was a pointer to a buffer that was MAX_PATH in size then there wouldn't be any problem? Are are you saying that a pointer to the buffer that was MAX_PATH wouldn't matter that there would still be problems because the function doesn't take a size parameter?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

No if the buffer has length MAX_PATH then there wouldn't be any BOOM. But if the buffer is shorter there will be BOOM since there is no size parameter. Alternatively, turn down your loudspeakers. *Straight face*
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Trond wrote:No if the buffer has length MAX_PATH then there wouldn't be any BOOM. But if the buffer is shorter there will be BOOM since there is no size parameter. Alternatively, turn down your loudspeakers. *Straight face*
Right, but there is no garantee for that with a PB string.
quidquid Latine dictum sit altum videtur
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

So after all this, is it safer just to use If/EndIf to add a slash after all?
(And I love how BOOM is used now to indicate a crash; it's funny).
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

BOOM!
So after all this, is it safer just to use If/EndIf to add a slash after all?
Yeah, why not, if speed is not very important then go with it.
I like logic, hence I dislike humans but love computers.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

Why not simply use
path.s{#MAX_PATH}
for your path variables??
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: API way to add backslash to a path

Post by dobro »




Declare PathAddBackslash(Path)

a$ = "c:\Kcc"
PathAddBackslash(@a$) ; la soluce qui tue : o)
debug a$
calldebugger

a$ = "c:\toto\"
PathAddBackslash(@a$) ; la soluce qui tue : o)
debug a$
calldebugger

procedure PathAddBackslash(Path )
     ;Solution By Dobro patented copyrighté Supra Hadopi : o)
     ; ajoute un "\" si pas present a la fin
    mem=Path
    path$= reversestring ( peeks (mem))
     if mid (Path$,1,1) <> "\"
        Path$= "\" +Path$
        Path$= reversestring (Path$)
         pokes (mem,path$)
     endif
endprocedure

;
; EPb

Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
Post Reply