forked from jhfrench/Workstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudf_relativefilepath.cfm
98 lines (93 loc) · 2.49 KB
/
udf_relativefilepath.cfm
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!-- sourcecode/udf_relativefilepath.cfm
Author: Jeromy French -->
<!---
<fusedoc language="ColdFusion MX" specification="2.0" template="udf_relativefilepath.cfm">
<responsibilities>
</responsibilities>
<properties>
<history email="jeromy_french@hotmail.com" author="Jeromy French" type="create" date="5/29/2007" role="FuseCoder" comments="Created File">
$Id:$
</history>
</properties>
<IO>
<in>
</in>
<passthrough>
</passthrough>
<out>
</out>
</IO>
</fusedoc>
--->
<!--- Fusebox.org thanks Massimo Foti for contributing this code --->
<cfscript>
/**
* @param startFile First file. (Required)
* @param endFile Second file. (Required)
* @return Returns a string.
*/
function fb_relativeFilePath(startFile,endFile){
//In case we have absolute local paths, turn backward to forward slashes
var startpath=replace(startFile,"\","/","ALL");
var endPath=replace(endFile,"\","/","ALL");
//Declare variables
var i=1;
var j=1;
var endStr="";
var commonStr="";
var retVal="";
var whatsLeft="";
var slashPos="";
var slashCount=0;
var dotDotSlash="";
//Be sure the paths aren't equal
if(startpath NEQ endPath){
//If the starting path is longer, the destination path is our starting point
if(len(startpath) GT len(endPath)){
endStr=len(endPath);
}
//Else the starting point is the start path
else{
endStr=len(startpath);
}
//Check if the two paths share a base path and store it into the commonStr variable
for(i;i LT endStr; i=i+1){
//Compare one character at time
if(mid(startpath,i,1) EQ mid(endPath,i,1)){
commonStr=commonStr & mid(startpath,i,1);
}
else{
break;
}
}
//We just need the base directory
commonStr=REReplaceNoCase(commonStr,"[^/]*$","");
//If there is a common base path, remove it
if(len(commonStr) GT 0){
whatsLeft=mid(startpath,len(commonStr)+1,len(startpath));
}
else{
whatsLeft=startpath;
}
slashPos=find("/",startpath);
//Count how many directories we have to climb
while(slashPos NEQ 0){
slashCount=slashCount+1;
slashPos=find("/",whatsLeft,slashPos+1);
}
//Append "../" for each directory we have to climb
for(j;j LT slashCount; j=j+1){
dotDotSlash=dotDotSlash & "../";
}
//Assemble the final path
retVal=dotDotSlash & mid(endPath,len(commonStr)+1,len(endPath));
if (find("/", retVal) EQ 0)
retVal="./" & retVal;
}
//Paths are the same
else{
retVal="";
}
return retVal;
}
</cfscript>