why is the file: "/path3/itemC" two times in your virtual filesystem ?
Check out how short=elegant SortFS () is :-)
Joshy
Code: Select all
#include "crt.bi"
function getDepth(path as string) as integer
var c=0,n = instr(path,"/")
while n : c+=1 : n = instr(n+1,path,"/") : wend
return c
end function
function SplitPath(path as string, splitted() as string) as integer
var nSize = ubound(splitted)+1
var s = path & "/"
var c=0,p = strtok(strptr(s),strptr("/"))
while (p<>NULL) andalso c<nSize
splitted(c) = *p : c+=1
p = strtok(NULL,strptr("/"))
wend
return c
end function
' return "true" if strings (l) and (r) must be swapped otherwise "false"
function SortString(l as string, r as string) as boolean
var n1 = len(l), n2 = len(r), n = iif(n1<n2, n1, n2)
if n<1 then return false
for i as integer = 0 to n-1
if l[i]=asc("/") then continue for
if r[i]=asc("/") then continue for
if l[i]>r[i] then return true
next
return false
end function
sub SortFS(FS() as string)
var last = ubound(FS)-1
var flag = true
while flag=true : flag=false
for i as integer = 0 to last
var d1=len(FS(i )), d2=len(FS(i+1)),n = iif(d1<=d2,d1,d2)
var index=0, l="", r=""
while l=r andalso index<n
l &= chr(FS( i)[index])
r &= chr(FS(i+1)[index])
index+=1
wend
if SortString(l,r) then swap FS(i),FS(i+1) : flag=true : exit for
next
wend
end sub
dim as string VirtualFS(...) = { _
"/itemE",_
"/path1/path2/itemC", _
"/path1/path2/itemB", _
"/path1/path2/itemA", _
"/path2/path2/itemX", _
"/path4/path5/path6/path7/itemQ", _
"/path4/path5/path8/path7/itemR", _
"/path4/path5/path6/path7/itemP", _
"/path3/itemC", _
"/path3/itemB", _
"/itemD"}
print "virtual file system"
for i as integer = 0 to ubound(VirtualFS)
print VirtualFS(i)
next
print
' sort the FS.
SortFS(VirtualFS())
print "sorted:"
for i as integer = 0 to ubound(VirtualFS)
print VirtualFS(i)
next
print
print "root"
for i as integer = 0 to ubound(VirtualFS)
var depth = getDepth(VirtualFS(i))
dim as string splitted(depth-1)
var path = VirtualFS(i) & "/"
var c = SplitPath(path,splitted())
for j as integer = 0 to c-1
print space(j*2) & "+ " & splitted(j)
next
next
sleep