forked from paulscherrerinstitute/PsiIpPackage
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPsiUtilPath.tcl
More file actions
42 lines (40 loc) · 1.57 KB
/
PsiUtilPath.tcl
File metadata and controls
42 lines (40 loc) · 1.57 KB
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
##############################################################################
# Copyright (c) 2019 by Paul Scherrer Institute, Switzerland
# All rights reserved.
# Authors: Oliver Bruendler
##############################################################################
namespace eval psi::util::path {
#Get Relative path from a given directory to a given file
#
# @param fromDir Directory the path should be relative to
# @param toFile File the path points to
# @param currentFolderDot true: use ./anyFile.bla false: use anyFile.bla
proc relTo {fromDir toFile {currentFolderDot true}} {
set fromDirParts [file split [file normalize $fromDir]]
set toFileParts [file split [file normalize $toFile]]
if {![string equal [lindex $fromDirParts 0] [lindex $toFileParts 0]]} {
# not on *n*x then
return -code error "$targetfile not on same volume as $currentpath"
}
while {[string equal [lindex $fromDirParts 0] [lindex $toFileParts 0]] && [llength $fromDirParts] > 0} {
# discard matching components from the front
set fromDirParts [lreplace $fromDirParts 0 0]
set toFileParts [lreplace $toFileParts 0 0]
}
set prefix ""
if {[llength $fromDirParts] == 0} {
# just the file name, so targetfile is lower down (or in same place)
if {$currentFolderDot} {
set prefix "."
} else {
set prefix ""
}
}
# step up the tree
for {set i 0} {$i < [llength $fromDirParts]} {incr i} {
append prefix " .."
}
# stick it all together (the eval is to flatten the targetfile list)
return [eval file join $prefix $toFileParts]
}
}