]> gitweb.factorcode.org Git - factor.git/blob - core/io/pathnames/pathnames.factor
(normalize-path) -> absolute-path, canonicalize-path -> resolve-symlinks
[factor.git] / core / io / pathnames / pathnames.factor
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 ;
5 IN: io.pathnames
6
7 SYMBOL: current-directory
8
9 : path-separator? ( ch -- ? ) os windows? "/\\" "/" ? member? ;
10
11 : path-separator ( -- string ) os windows? "\\" "/" ? ;
12
13 : trim-tail-separators ( string -- string' )
14     [ path-separator? ] trim-tail ;
15
16 : trim-head-separators ( string -- string' )
17     [ path-separator? ] trim-head ;
18
19 : last-path-separator ( path -- n ? )
20     [ length 1 - ] keep [ path-separator? ] find-last-from ;
21
22 HOOK: root-directory? io-backend ( path -- ? )
23
24 M: object root-directory? ( path -- ? )
25     [ f ] [ [ path-separator? ] all? ] if-empty ;
26
27 ERROR: no-parent-directory path ;
28
29 : parent-directory ( path -- parent )
30     dup root-directory? [
31         trim-tail-separators
32         dup last-path-separator [
33             1 + cut
34         ] [
35             drop "." swap
36         ] if
37         { "" "." ".." } member? [
38             no-parent-directory
39         ] when
40     ] unless ;
41
42 <PRIVATE
43
44 : head-path-separator? ( path1 ? -- ?' )
45     [
46         [ t ] [ first path-separator? ] if-empty
47     ] [
48         drop f
49     ] if ;
50
51 : head.? ( path -- ? ) "." ?head head-path-separator? ;
52
53 : head..? ( path -- ? ) ".." ?head head-path-separator? ;
54
55 : append-path-empty ( path1 path2 -- path' )
56     {
57         { [ dup head.? ] [
58             rest trim-head-separators append-path-empty
59         ] }
60         { [ dup head..? ] [ drop no-parent-directory ] }
61         [ nip ]
62     } cond ;
63
64 : windows-absolute-path? ( path -- path ? )
65     {
66         { [ dup "\\\\?\\" head? ] [ t ] }
67         { [ dup length 2 < ] [ f ] }
68         { [ dup second CHAR: : = ] [ t ] }
69         [ f ]
70     } cond ;
71
72 : special-path? ( path -- rest ? )
73     {
74         { [ "resource:" ?head ] [ t ] }
75         { [ "vocab:" ?head ] [ t ] }
76         [ f ]
77     } cond ;
78
79 : absolute-path? ( path -- ? )
80     {
81         { [ dup empty? ] [ f ] }
82         { [ dup special-path? nip ] [ t ] }
83         { [ os windows? ] [ windows-absolute-path? ] }
84         { [ dup first path-separator? ] [ t ] }
85         [ f ]
86     } cond nip ;
87
88 PRIVATE>
89
90 : append-path ( path1 path2 -- path )
91     {
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 ] }
97         { [ dup head..? ] [
98             2 tail trim-head-separators
99             [ parent-directory ] dip append-path
100         ] }
101         { [ over absolute-path? over first path-separator? and ] [
102             [ 2 head ] dip append
103         ] }
104         [
105             [ trim-tail-separators "/" ] dip
106             trim-head-separators 3append
107         ]
108     } cond ;
109
110 : prepend-path ( path1 path2 -- path )
111     swap append-path ; inline
112
113 : file-name ( path -- string )
114     dup root-directory? [
115         trim-tail-separators
116         dup last-path-separator [ 1 + tail ] [
117             drop special-path? [ file-name ] when
118         ] if
119     ] unless ;
120
121 : file-stem ( path -- stem )
122     file-name "." split1-last drop ;
123
124 : file-extension ( path -- extension )
125     file-name "." split1-last nip ;
126
127 : path-components ( path -- seq )
128     normalize-path path-separator split harvest ;
129
130 HOOK: resolve-symlinks os ( path -- path' )
131
132 M: object resolve-symlinks normalize-path ;
133
134 : resource-path ( path -- newpath )
135     "resource-path" get prepend-path ;
136
137 GENERIC: vocab-path ( path -- newpath )
138
139 GENERIC: absolute-path ( path -- path' )
140
141 M: string absolute-path
142     "resource:" ?head [
143         trim-head-separators resource-path
144         absolute-path
145     ] [
146         "vocab:" ?head [
147             trim-head-separators vocab-path
148             absolute-path
149         ] [
150             current-directory get prepend-path
151         ] if
152     ] if ;
153
154 M: object normalize-path ( path -- path' )
155     absolute-path ;
156
157 TUPLE: pathname string ;
158
159 C: <pathname> pathname
160
161 M: pathname absolute-path string>> absolute-path ;
162
163 M: pathname <=> [ string>> ] compare ;
164
165 HOOK: home io-backend ( -- dir )
166
167 M: object home "" resource-path ;