ProgramFilename(), GetProtocolPart(), GetPathPart(), ...

Share your advanced knowledge/code with the community.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

ProgramFilename(), GetProtocolPart(), GetPathPart(), ...

Post by Peter »

Code: Select all

Procedure.s ProgramFilename()
  
  ; Returns the full path and filename of this program 
  
  ! return location.href;
  
EndProcedure

Procedure.s GetProtocolPart(FullPathName.s)
  
  ; Retrieves the protocol part of a full path.
  ; Returns the protocol part. For example, if the full path is "http://SpiderBasic/SB.html", the result will be "http".
  
  ProcedureReturn StringField(FullPathName, 1, ":")
  
EndProcedure

Procedure.s GetPathPart(FullPathName.s)
  
  ; Retrieves the path part of a full path.
  ; Returns the path part. For example, if the full path is "http://SpiderBasic/SB.html", the result will be "http://SpiderBasic/".
  
  ProcedureReturn Left(FullPathName, Len(FullPathName) - FindString(ReverseString(FullPathName), #PS$))
  
EndProcedure

Procedure.s GetFilePart(FullPathName.s, Mode = #PB_Ignore)
  
  ; Mode: #PB_FileSystem_NoExtension
  ; Retrieves the file part of a full path. 
  ; Returns the file name. For example, if the full path is "http://SpiderBasic/SB.html", the result will be "SB.html". 
  
  If Mode = #PB_FileSystem_NoExtension
    ProcedureReturn StringField(StringField(FullPathName.s, CountString(FullPathName, #PS$) + 1, #PS$), 1, ".")
  Else
    ProcedureReturn StringField(FullPathName.s, CountString(FullPathName, #PS$) + 1, #PS$)
  EndIf
  
EndProcedure

Debug "ProgramFilename: "           + ProgramFilename()
Debug "ProtocolPart: "              + GetProtocolPart(ProgramFilename())
Debug "PathPart: "                  + GetPathPart(ProgramFilename())
Debug "FilePart: "                  + GetFilePart(ProgramFilename())
Debug "FilePart (w/o extension)): " + GetFilePart(ProgramFilename(), #PB_FileSystem_NoExtension)
Debug "ExtensionPart: "             + GetExtensionPart(ProgramFilename())
ProgramFilename: http://127.0.0.1:9082/SpiderBasic_Compilation0.html
ProtocolPart: http
PathPart: http://127.0.0.1:9082
FilePart: SpiderBasic_Compilation0.html
FilePart (w/o extension)): SpiderBasic_Compilation0
ExtensionPart: html