'========================================================================|
' Fileops is a set functions to manipulate file names and paths.         |
' QUOTE - Returns a quoted string                                        |
' STRIPPATH - Returns file path (without file name)                      |
' STIPFILENAME - Returns file name (without path)                        |
' STRIPFILEEXT - Returns file extension (like ".exe", ".html" etc.)      |
' FILENAMENOEXT - Returns file name without extension                    |
' FULLPATHNOEXT - Returns full path without file extension               |
'========================================================================|

$include "fileops.inc"

declare sub file_operations

create form as qform
       center
       width = 500

       create fl as qfilelistbox
       align = 3
       ondblclick = file_operations
       end create

       create re as qrichedit
       align = 5
       font.name = "Courier new"
       wordwrap = 0
       end create
showmodal
end create

sub file_operations
re.clear
dim sta as string
sta = fl.filename
re.addstrings "ORIGINAL FILE NAME" + chr$(9) + sta, _
              "QUOTED FILE NAME" + chr$(9) + quote(sta), _
              "FILE PATH" + chr$(9) + chr$(9) + StripPath(sta), _
              "FILE NAME" + chr$(9) + chr$(9) + stripfilename(sta), _
              "FILE EXTENSION" + chr$(9) + stripfileext(sta), _
              "WITHOUT EXTENSION" + chr$(9) + filenamenoext(sta), _
              "FULL NAME (NO EXT)" + chr$(9) + fullpathnoext(sta)
end sub


' QUOTE : Returns a quoted string 
function Quote (StringToQuote as string) as string
StringToQuote =  chr$(34) + StringToQuote + chr$(34)
result = replacesubstr$(stringtoquote, chr$(34) + chr$(34), chr$(34))
end function

'=============================================================================== 
' STRIPPATH : Returns file path (without file name) 
Function StripPath (fullname as string) as string
result = left$(fullname, rinstr(fullname, "\"))
end function

'=============================================================================== 
' STRIPFILENAME : Returns file name (without path) 
Function StripFileName (fullname as string) as string
result = right$(fullname, len(fullname) - rinstr(fullname, "\"))
end function

'=============================================================================== 
' STRIPFILEEXT : Returns file extension (like ".exe", ".html" etc.) 
Function StripFileExt (fullname as string) as string
result = right$(fullname, len(fullname) - rinstr(fullname, ".") + 1)
end function

'=============================================================================== 
' FILENAMENOEXT : Returns file name without extension 
function FileNameNoExt(fullname as string) as string
fullname = right$(fullname, len(fullname) - rinstr(fullname, "\"))
result = left$(fullname, rinstr(fullname, ".") - 1)
end function

'=============================================================================== 
' FULLPATHNOEXT : Returns full path without file extension 
function FullPathNoExt(fullname as string) as string
result = left$(fullname, rinstr(fullname, ".") - 1)
end function

'=============================================================================== 
' C_Style : Returns "slashed" path from a "backslashed" one 
function C_Style (fullname as string) as string
fullname = replacesubstr$(fullname, "\\", "\")
result = replacesubstr$(fullname, "\", "/")
end function

'=============================================================================== 
' SYSDIR : Retrieves windows shell directories 

'-------------------------| 
' Allowed values for dir  | 
'-----------------------------------------------------------------------------| 
' Desktop                 | Templates               | AppData                 | 
' Start Menu              | Programs                | Startup                 | 
' Fonts                   | SendTo                  | Recent                  | 
' Favorites               | Cache                   | Cookies                 | 
' History                 | NetHood                 | Personal                | 
' PrintHood               | Local AppData           | My Pictures             | 
' Administrative Tools    |                         |                         | 
'-----------------------------------------------------------------------------| 

function SysDir (dir as string) as string
DIM fo_reg AS QRegistry
fo_reg.RootKey = &H80000001
fo_reg.openkey ("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", 0)
result = fo_reg.readstring(dir) + "\"
end function

'=============================================================================== 
' HOMEDIR : The folder where the application is 

function homedir() as string
result = left$(command$(0), rinstr(command$(0), "\"))
end function

'=============================================================================== 
' BROWSEFORFOLDERS : Returns the selected folder 

function BrowseForFolders (initialdir as string, wincapt as string) as string
dim bff_form as qform
with bff_form
.height = 400
.center
.caption = wincapt
.delbordericons 2
end with
dim bff_tree as qdirtree
with bff_tree
.parent = bff_form
.align = 5
end with
if bff_form.caption = "" then bff_form.caption = "Select folder"
if direxists(initialdir) then
bff_tree.directory = initialdir
else
bff_tree.directory = curdir$
end if
bff_form.showmodal
result = bff_tree.directory
end function

'=============================================================================== 
' BROWSEFORFILE : Returns the selected folder 

Function BrowseForFile (caption as string, filter as string, _
initialdir as string) as string

dim bff_od as qopendialog
with bff_od
.caption = caption
.filter = filter
.initialdir = initialdir

if .execute then
    result = .filename
end if
end with

end function