GetPathPart() GetFilePart()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

GetPathPart() GetFilePart()

Post by dmoc »

Would be nice to see these extended to handle URL's as well :D And to complete the set... GetDrive(). Initially I thought of a seperate set of commands but essentially it's the same "type" of operation. With the new file systems on the horizon I think it *will* be the same operation 'coz everything in the universe will have a url!

Which reminds me, didn't I see something recently about internet "name spaces"? Basic IIRC all URL's will eventually start with "<NAMESPACE>:...".

@Fred: Adopt my suggestions and get waaaay ahead of the curve :P
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

I'd also like to see some commands for adding paths and filenames together, that take care of the correct directory separator and look after not adding duplicate directory separators, and things like that.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Until then, v0.01, only tested quickly, ...

Code: Select all

Procedure.s URLHostPart(u.s)
  If UCase(Left(u,7))="HTTP://"
    ProcedureReturn StringField(u,3,"/")
  Else
    ProcedureReturn StringField(u,1,"/")
  EndIf
EndProcedure

Procedure.s URLResourcePart(u.s)
  Protected p.l
  
  If FindString(u,".",1)=0: ProcedureReturn "": EndIf
    
  For p=Len(u) To 1 Step -1
    If Mid(u, p, 1)="/"
      Break
    EndIf
  Next
  ProcedureReturn Right(u, Len(u)-p)
EndProcedure

Procedure.s URLPathPart(u.s)
  Protected r.s, tl.l, tr.l
  tl=0: tr=0
  If UCase(Left(u,7))="HTTP://": tl=8: EndIf
  tl+Len(URLHostPart(u))
  tr=Len(URLResourcePart(u))
  ProcedureReturn Mid(u, tl, Len(u)-tl-tr)
EndProcedure
Edit: just a small correction in URLHostPart()
Last edited by dmoc on Sat Nov 08, 2003 8:52 pm, edited 1 time in total.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

dmoc wrote:"HTTP://"
URL's don't have to be HTTP AFAIK, that just designates the protocol used to get the resource. Also, you'd maybe want something to extract the username, password and port (or skip them).

Looking forward to v0.02 ;)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post by dmoc »

Correct, but only http interests me at the mo :wink:

Ach, here yer go then, v0.02

PS: These are nothing special, just part of a web-whacker I'm writing, but may save someone from re-creating them.

Code: Select all

Procedure.s URLProtoPart(u.s)
  Protected p.l
  p = FindString(u,"://",1)
  If p
    ProcedureReturn Left(u,p-1)
  Else
    ProcedureReturn ""
  EndIf
EndProcedure

Procedure.s URLHostPart(u.s)
  If FindString(u,"://",1)
    ProcedureReturn StringField(u,3,"/")
  Else
    ProcedureReturn StringField(u,1,"/")
  EndIf
EndProcedure

Procedure.s URLResourcePart(u.s)
  Protected r.s, p.l
  
  If FindString(u,".",1)=0: ProcedureReturn "": EndIf
  
  For p=Len(u) To 1 Step -1
    If Mid(u, p, 1)="/"
      Break
    EndIf
  Next
  ProcedureReturn Right(u, Len(u)-p)
EndProcedure

Procedure.s URLPathPart(u.s)
  Protected r.s, tl.l, tr.l
  tl=0: tr=0
  If UCase(Left(u,7))="HTTP://": tl=8: EndIf
  tl+Len(URLHostPart(u))
  tr=Len(URLResourcePart(u))
  ProcedureReturn Mid(u, tl, Len(u)-tl-tr)
EndProcedure
Post Reply