141 lines
3.3 KiB
Lua
141 lines
3.3 KiB
Lua
local path = {}
|
|
|
|
function path.Common_Root( Left, Right, Separator )
|
|
local Sep = Separator or "/"
|
|
local smallest_string = nil
|
|
local largest_string = nil
|
|
if string.len(Left) > string.len(Right) then
|
|
smallest_string = Right
|
|
largest_string = Left
|
|
else
|
|
smallest_string = Left
|
|
largest_string = Right
|
|
end
|
|
|
|
local last_sep_idx = 1
|
|
local sep = string.byte(Sep)
|
|
for i = 1, #smallest_string do
|
|
if smallest_string:byte(i) == sep then
|
|
last_sep_idx = i
|
|
end
|
|
|
|
if smallest_string:byte(i) ~= largest_string:byte(i) then
|
|
break
|
|
end
|
|
end
|
|
|
|
local match = string.sub(smallest_string, 1, last_sep_idx - 1)
|
|
|
|
if match == '' then
|
|
return nil
|
|
else
|
|
return match
|
|
end
|
|
end
|
|
|
|
function path.Get_Extension( P )
|
|
if P == nil then
|
|
return nil
|
|
end
|
|
|
|
local rev_string = string.reverse(P)
|
|
local start_idx = 0
|
|
local end_idx = 0
|
|
start_idx, end_idx = string.find(rev_string, ".", 1, true)
|
|
|
|
if start_idx == nil then
|
|
return nil
|
|
else
|
|
return string.sub(P, #P - (start_idx -2))
|
|
end
|
|
end
|
|
|
|
function path.Get_Extension_From_List( P, Extension_List )
|
|
if Extension_List == nil or P == nil then
|
|
return nil
|
|
end
|
|
|
|
local ext_length = 0
|
|
for _,item in ipairs(Extension_List) do
|
|
local current_ext_length = #item
|
|
if current_ext_length > ext_length then
|
|
ext_length = current_ext_length
|
|
end
|
|
end
|
|
|
|
local rev_string = string.reverse(P)
|
|
local start_idx = 0
|
|
local end_idx = 0
|
|
local match = nil
|
|
while ( true ) do
|
|
start_idx, end_idx = string.find(rev_string, ".", start_idx+1, true)
|
|
if start_idx == nil or (start_idx-1) > ext_length then
|
|
break
|
|
end
|
|
|
|
for _,exts in ipairs(Extension_List) do
|
|
if exts == string.sub(P, #P - (start_idx -2)) then
|
|
match = exts
|
|
end
|
|
end
|
|
end
|
|
|
|
return match
|
|
end
|
|
|
|
function path.Get_Ext( P, Extension_List )
|
|
if Extension_List == nil then
|
|
return path.Get_Extension(P)
|
|
else
|
|
return path.Get_Extension_From_List(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
|