Add dirwalk generator

This commit is contained in:
Folkert Kevelam 2024-10-12 16:16:38 +02:00
parent 902001697b
commit 24af2b56e0

View File

@ -91,8 +91,50 @@ function path.Get_Ext( P, Extension_List )
end
end
function path.Walk_Up( P )
if P == "/" then
return nil
end
local rev = string.reverse(P)
local start = 1
if P:sub(-1) == "/" then
start = start + 1
end
local slash_first_idx, _ = string.find(rev, "/", start, true)
return string.sub(P, 1, #P - slash_first_idx + 1)
end
function path.Generate_Dir_Walk( Base, Descend_List )
local Descend_Length = #Descend_List
local i = -1
local Base_Dir = Base
function Inner()
if i == Descend_Length then
local Temp_Dir = path.Walk_Up(Base_Dir)
if Temp_Dir ~= nil then
Base_Dir = Temp_Dir
i = -1
end
end
i = i + 1
if i == 0 then
return Base_Dir
elseif i <= Descend_Length then
return Base_Dir .. Descend_List[i]
end
end
return Inner
end
print(path.Common_Root("/home/folkert/Documents/Projects","/home/folkert/Downloads/sub/sub2/sub3/sub4/hello.txt"))
print(path.Get_Ext("/home/folkert/Documents/projects.wo.cp", {"o.cp"}))
for x in path.Generate_Dir_Walk("/home/folkert/", {"include", "src"}) do
print(x)
end
return path