80 lines
2.0 KiB
Awk
80 lines
2.0 KiB
Awk
|
function getDirname(path, pathComponents, dirname) {
|
||
|
sub("/$", "", path)
|
||
|
|
||
|
pathComponents[1] = ""
|
||
|
split(path, pathComponents, "/")
|
||
|
dirname = ""
|
||
|
|
||
|
if (length(pathComponents) < 2) return "."
|
||
|
for (i = 1; i <= length(pathComponents) - 1; i++)
|
||
|
dirname = dirname pathComponents[i] "/"
|
||
|
|
||
|
return dirname
|
||
|
}
|
||
|
|
||
|
function resolvePath(path, resolvedPath, pathComponents) {
|
||
|
sub("/$", "", path)
|
||
|
|
||
|
pathComponents[1] = ""
|
||
|
split(path, pathComponents, "/")
|
||
|
resolvedPath = ""
|
||
|
|
||
|
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
|
||
|
function relpath(base, path, resolvedPath, baseComponents, pathComponents) {
|
||
|
# sprintf("realpath --relative-to=\"%s\" \"%s\"", base, path) | getline resolvedPath
|
||
|
sub("$/", "", path)
|
||
|
sub("$/", "", base)
|
||
|
|
||
|
base = resolvePath(base)
|
||
|
path = resolvePath(path)
|
||
|
|
||
|
pathComponents[1] = ""
|
||
|
baseComponents[1] = ""
|
||
|
|
||
|
split(path, pathComponents, "/")
|
||
|
split(base, baseComponents, "/")
|
||
|
|
||
|
resolvedPath = ""
|
||
|
|
||
|
i = 1
|
||
|
|
||
|
while (baseComponents[i] == pathComponents[i]) i++
|
||
|
|
||
|
for (j = i; j <= length(baseComponents); j++)
|
||
|
resolvedPath = resolvedPath "../"
|
||
|
|
||
|
while (i <= length(pathComponents)) {
|
||
|
resolvedPath = resolvedPath baseComponents[i] "/"
|
||
|
i ++
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|