i dunno know

This commit is contained in:
Abdullah Islam 2023-05-19 17:47:59 +06:00
parent 3c8c51acc3
commit cb4a02a744
4 changed files with 62 additions and 26 deletions

5
.gawk_history Normal file
View File

@ -0,0 +1,5 @@
help
run
exit
run
exit

7
.gawkrc Normal file
View File

@ -0,0 +1,7 @@
option history_size = 100
option listsize = 15
option outfile = "/dev/stdout"
option prompt = "gawk> "
option save_history = 1
option save_options = 1
option trace = 0

View File

@ -0,0 +1,22 @@
function assertEq(realValue, expectedValue) {
if (realValue == expectedValue)
printf "-> %s is a good result\n", realValue
else
printf "-> mismatch: %s (realValue) != %s\n", realValue, expectedValue
}
BEGIN {
printf "wassup gamers"
printf "\n==> function: relpath(base, path)\n"
assertEq(relpath("amogus/fibrele", "amogus/tokus/henche.txt"), "../tokus/henche.txt")
assertEq(relpath("amogus/", "amogus/henche.txt"), "henche.txt")
assertEq(relpath("/home/amogus/", "/home/amogus/henche.txt"), "henche.txt")
# assertEq(relpath("/home/amogus/", "amogus/henche.txt"), "/home/amogus/amogus/henche.txt")
printf "\n==> function: resolvePath(path)\n"
assertEq(resolvePath("../amogus/../amogus/frux"), "../amogus/frux")
assertEq(resolvePath("../amogus/frux"), "../amogus/frux")
assertEq(resolvePath("amogus/frux"), "amogus/frux")
assertEq(resolvePath("~/amogus/./frux"), "~/amogus/frux")
}

54
lib.awk
View File

@ -1,9 +1,9 @@
function getDirname(path, pathComponents, dirname) {
sub("/$", "", path)
pathComponents[1] = ""
split(path, pathComponents, "/")
dirname = ""
pathComponents[1] = ""
sub("/$", "", path)
split(path, pathComponents, "/")
if (length(pathComponents) < 2) return "."
for (i = 1; i <= length(pathComponents) - 1; i++)
@ -13,12 +13,12 @@ function getDirname(path, pathComponents, dirname) {
}
function resolvePath(path, resolvedPath, pathComponents) {
sub("/$", "", path)
pathComponents[1] = ""
split(path, pathComponents, "/")
resolvedPath = ""
sub("/$", "", path)
split(path, pathComponents, "/")
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++
@ -34,33 +34,35 @@ function resolvePath(path, resolvedPath, pathComponents) {
# 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)
# 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) {
path = resolvePath(path)
base = resolvePath(base)
resolvedPath = ""
pathComponents[1] = ""
baseComponents[1] = ""
split(path, pathComponents, "/")
split(base, baseComponents, "/")
sub("/$", "", base)
sub("/$", "", path)
nbaseComponents = split(base, baseComponents, "/")
npathComponents = split(path, pathComponents, "/")
resolvedPath = ""
j = 1
while (baseComponents[j] == pathComponents[j] && j <= nbaseComponents && j <= npathComponents)
j++
i = 1
while (baseComponents[i] == pathComponents[i]) i++
for (j = i; j <= length(baseComponents); j++)
for (i = j; i <= nbaseComponents; i++)
resolvedPath = resolvedPath "../"
while (i <= length(pathComponents)) {
resolvedPath = resolvedPath baseComponents[i] "/"
i ++
}
for (i = j; i <= npathComponents; i++)
if (i < npathComponents)
resolvedPath = resolvedPath pathComponents[i] "/"
else
resolvedPath = resolvedPath pathComponents[i]
return resolvedPath
}