1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
simplifyPath =
# Could use strsplit, etc.
# simplifyPath2("XMLBasics/../WebTechData/RawData/KyphosisRpartExtract.xml")
# simplifyPath2("../WebTechData/RawData/KyphosisRpartExtract.xml")
# simplifyPath2("../../WebTechData/RawData/KyphosisRpartExtract.xml")
# simplifyPath2("a/b/../../WebTechData/RawData/KyphosisRpartExtract.xml")
# simplifyPath2("top/a/b/../../WebTechData/RawData/KyphosisRpartExtract.xml")
# simplifyPath2("abc/../../WebTechData/RawData/KyphosisRpartExtract.xml")
function(path)
{
els = strsplit(path, "/")[[1]]
GoOn = TRUE
els = els[ els != "."]
while(GoOn && length(i <- which(els == ".."))) {
i = min(i)
if(length(i) == 1 && i == 1)
break
if(all(els[ seq( 1, i) ] == ".."))
break
if(i == 2 && els[1] == "..")
break
els = els[ - c(i, i - 1L) ]
}
paste(els, collapse = "/")
}
if(FALSE) {
simplifyPath =
function(path)
{
path = gsub("/\\./", "/", path)
path = gsub("^\\./", "", path)
# Doesn't handle "../foo"
while(grepl("[^./]/\\.\\.", path)) {
path = gsub("/[^/.]+/\\.\\./?", "/", path)
}
path = gsub("^(\\./)+", "", path)
path
}
simplifyPath1 =
# Could use strsplit, etc.
function(path)
{
els = strsplit(path, "/")[[1]]
while(length(i <- which(els == ".."))) {
i = max(i)
if(length(i) == 1 && i == 1)
break
i = i[i != 1]
}
paste(els, sep = "/")
}
}
|