2023-05-12 12:51:12 +00:00
|
|
|
function getDirname(path, pathComponents, dirname) {
|
2023-05-19 11:47:59 +00:00
|
|
|
dirname = ""
|
2023-05-12 12:51:12 +00:00
|
|
|
pathComponents[1] = ""
|
2023-05-19 11:47:59 +00:00
|
|
|
|
|
|
|
sub("/$", "", path)
|
2023-05-12 12:51:12 +00:00
|
|
|
split(path, pathComponents, "/")
|
|
|
|
|
|
|
|
if (length(pathComponents) < 2) return "."
|
|
|
|
for (i = 1; i <= length(pathComponents) - 1; i++)
|
|
|
|
dirname = dirname pathComponents[i] "/"
|
|
|
|
|
|
|
|
return dirname
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolvePath(path, resolvedPath, pathComponents) {
|
|
|
|
pathComponents[1] = ""
|
|
|
|
resolvedPath = ""
|
|
|
|
|
2023-05-19 11:47:59 +00:00
|
|
|
sub("/$", "", path)
|
|
|
|
split(path, pathComponents, "/")
|
|
|
|
|
2023-05-12 12:51:12 +00:00
|
|
|
for (i = 1; i <= length(pathComponents); i++)
|
|
|
|
if (pathComponents[i] == "." || (i > 1 && pathComponents[i] == "")) continue
|
|
|
|
else if (i+1 <= length(pathComponents) && pathComponents[i+1] == "..") i++
|
|
|
|
else if (i == length(pathComponents)) resolvedPath = resolvedPath pathComponents[i]
|
|
|
|
else resolvedPath = resolvedPath pathComponents[i] "/"
|
|
|
|
|
|
|
|
return resolvedPath
|
|
|
|
}
|
|
|
|
|
|
|
|
# relative path
|
|
|
|
# assumes that base and path both start from the same root
|
|
|
|
# for example:
|
|
|
|
# when base = johncon/
|
|
|
|
# and path = lotus/garbage.txt
|
|
|
|
# the relative path is ../lotus/garbage.txt
|
2023-05-19 11:47:59 +00:00
|
|
|
# steps
|
|
|
|
# 1. normalize and clean the paths (perhaps using resolvePath)
|
|
|
|
# 2. remove the common path components from both the base and path
|
|
|
|
# 3. for the remaining amount of components in the base, add ../ to the resolvedPath
|
|
|
|
# 4. add the remaining components of path to resolvedPath
|
|
|
|
function relpath(base, path, resolvedPath, baseComponents, pathComponents, nbaseComponents, npathComponents) {
|
2023-05-12 12:51:12 +00:00
|
|
|
path = resolvePath(path)
|
2023-05-19 11:47:59 +00:00
|
|
|
base = resolvePath(base)
|
|
|
|
resolvedPath = ""
|
2023-05-12 12:51:12 +00:00
|
|
|
pathComponents[1] = ""
|
|
|
|
baseComponents[1] = ""
|
|
|
|
|
2023-05-19 11:47:59 +00:00
|
|
|
sub("/$", "", base)
|
|
|
|
sub("/$", "", path)
|
|
|
|
nbaseComponents = split(base, baseComponents, "/")
|
|
|
|
npathComponents = split(path, pathComponents, "/")
|
2023-05-12 12:51:12 +00:00
|
|
|
|
2023-05-19 11:47:59 +00:00
|
|
|
j = 1
|
|
|
|
while (baseComponents[j] == pathComponents[j] && j <= nbaseComponents && j <= npathComponents)
|
|
|
|
j++
|
2023-05-12 12:51:12 +00:00
|
|
|
|
2023-05-19 11:47:59 +00:00
|
|
|
for (i = j; i <= nbaseComponents; i++)
|
2023-05-12 12:51:12 +00:00
|
|
|
resolvedPath = resolvedPath "../"
|
|
|
|
|
2023-05-19 11:47:59 +00:00
|
|
|
for (i = j; i <= npathComponents; i++)
|
|
|
|
if (i < npathComponents)
|
|
|
|
resolvedPath = resolvedPath pathComponents[i] "/"
|
|
|
|
else
|
|
|
|
resolvedPath = resolvedPath pathComponents[i]
|
2023-05-12 12:51:12 +00:00
|
|
|
|
|
|
|
return resolvedPath
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFilename(string, result) {
|
|
|
|
result = tolower(string)
|
|
|
|
sub("^([ ]*)", "", result)
|
|
|
|
sub("([ ]*)$", "", result)
|
|
|
|
gsub(" ", "_", result)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
function hashbased_uuidgen(string, namespace, uuid) {
|
|
|
|
sprintf("uuidgen -s -N \"%s\" -n \"%s\"", string, namespace) | getline uuid
|
|
|
|
return uuid
|
|
|
|
}
|