forked from jhfrench/Workstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudf_canonicalpath.cfm
56 lines (52 loc) · 1.48 KB
/
udf_canonicalpath.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
<!-- sourcecode/udf_canonicalpath.cfm
Author: Jeromy French -->
<!---
<fusedoc language="ColdFusion MX" specification="2.0" template="udf_canonicalpath.cfm">
<responsibilities>
</responsibilities>
<properties>
<history email="[email protected]" 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 Barney Boisvert for contributing this code --->
<cfscript>
/**
* @param path An absolute path to clean. (Required)
* @return Returns a string.
*
* I opted to not use the Java method for backwards compatibility,
* and because we might need to do relative paths at some point.
*/
function fb_canonicalPath(path, delim) {
var delimAtFront=left(path, 1) EQ delim;
var delimAtEnd=right(path, 1) EQ delim;
var dirArray=listToArray(path, "/\");
var i="";
for (i=1; i LTE arraylen(dirArray); i=i+1) {
// don't convert this loop to cfloop without thinking
if (dirArray[i] EQ ".") {
arrayDeleteAt(dirArray, i);
i=i-1;
} else if (dirArray[i] EQ "..") {
arrayDeleteAt(dirArray, i);
if (i GT 1 and right(dirArray[i-1], 1) NEQ ":") {
arrayDeleteAt(dirArray, i-1);
i=i-1;
}
i=i-1;
}
}
return iif(delimAtFront, "delim", de("")) & arrayToList(dirArray, delim) & iif(delimAtEnd, "delim", de(""));
}
</cfscript>