1 ! Copyright (C) 2004, 2009 Slava Pestov, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators io.backend kernel math math.order
4 namespaces sequences splitting strings system ;
7 SYMBOL: current-directory
9 : path-separator? ( ch -- ? ) os windows? "/\\" "/" ? member? ;
11 : path-separator ( -- string ) os windows? "\\" "/" ? ;
13 : trim-tail-separators ( string -- string' )
14 [ path-separator? ] trim-tail ;
16 : trim-head-separators ( string -- string' )
17 [ path-separator? ] trim-head ;
19 : last-path-separator ( path -- n ? )
20 [ length 1 - ] keep [ path-separator? ] find-last-from ;
22 HOOK: root-directory? io-backend ( path -- ? )
24 M: object root-directory? ( path -- ? )
25 [ f ] [ [ path-separator? ] all? ] if-empty ;
27 ERROR: no-parent-directory path ;
29 : parent-directory ( path -- parent )
32 dup last-path-separator [
37 { "" "." ".." } member? [
44 : head-path-separator? ( path1 ? -- ?' )
46 [ t ] [ first path-separator? ] if-empty
51 : head.? ( path -- ? ) "." ?head head-path-separator? ;
53 : head..? ( path -- ? ) ".." ?head head-path-separator? ;
55 : append-path-empty ( path1 path2 -- path' )
58 rest trim-head-separators append-path-empty
60 { [ dup head..? ] [ drop no-parent-directory ] }
64 : windows-absolute-path? ( path -- path ? )
66 { [ dup "\\\\?\\" head? ] [ t ] }
67 { [ dup length 2 < ] [ f ] }
68 { [ dup second CHAR: : = ] [ t ] }
72 : special-path? ( path -- rest ? )
74 { [ "resource:" ?head ] [ t ] }
75 { [ "vocab:" ?head ] [ t ] }
79 : absolute-path? ( path -- ? )
81 { [ dup empty? ] [ f ] }
82 { [ dup special-path? nip ] [ t ] }
83 { [ os windows? ] [ windows-absolute-path? ] }
84 { [ dup first path-separator? ] [ t ] }
90 : append-path ( path1 path2 -- path )
92 { [ over empty? ] [ append-path-empty ] }
93 { [ dup empty? ] [ drop ] }
94 { [ over trim-tail-separators "." = ] [ nip ] }
95 { [ dup absolute-path? ] [ nip ] }
96 { [ dup head.? ] [ rest trim-head-separators append-path ] }
98 2 tail trim-head-separators
99 [ parent-directory ] dip append-path
101 { [ over absolute-path? over first path-separator? and ] [
102 [ 2 head ] dip append
105 [ trim-tail-separators "/" ] dip
106 trim-head-separators 3append
110 : prepend-path ( path1 path2 -- path )
111 swap append-path ; inline
113 : file-name ( path -- string )
114 dup root-directory? [
116 dup last-path-separator [ 1 + tail ] [
117 drop special-path? [ file-name ] when
121 : file-stem ( path -- stem )
122 file-name "." split1-last drop ;
124 : file-extension ( path -- extension )
125 file-name "." split1-last nip ;
127 : path-components ( path -- seq )
128 normalize-path path-separator split harvest ;
130 HOOK: resolve-symlinks os ( path -- path' )
132 M: object resolve-symlinks normalize-path ;
134 : resource-path ( path -- newpath )
135 "resource-path" get prepend-path ;
137 GENERIC: vocab-path ( path -- newpath )
139 GENERIC: absolute-path ( path -- path' )
141 M: string absolute-path
143 trim-head-separators resource-path
147 trim-head-separators vocab-path
150 current-directory get prepend-path
154 M: object normalize-path ( path -- path' )
157 TUPLE: pathname string ;
159 C: <pathname> pathname
161 M: pathname absolute-path string>> absolute-path ;
163 M: pathname <=> [ string>> ] compare ;
165 HOOK: home io-backend ( -- dir )
167 M: object home "" resource-path ;