]> gitweb.factorcode.org Git - factor.git/blob - core/io/pathnames/pathnames.factor
core: removing unnecessary method stack effects.
[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 assocs combinators io.backend kernel math
4 math.order 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?
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 -- ? )
65     {
66         { [ dup "\\\\?\\" head? ] [ t ] }
67         { [ dup length 2 < ] [ f ] }
68         { [ dup second CHAR: : = ] [ t ] }
69         [ f ]
70     } cond nip ;
71
72 : special-path? ( path -- rest ? )
73     {
74         { [ "resource:" ?head ] [ t ] }
75         { [ "vocab:" ?head ] [ t ] }
76         [ f ]
77     } cond ;
78
79 PRIVATE>
80
81 : absolute-path? ( path -- ? )
82     {
83         { [ dup empty? ] [ drop f ] }
84         { [ dup special-path? nip ] [ drop t ] }
85         { [ os windows? ] [ windows-absolute-path? ] }
86         { [ dup first path-separator? ] [ drop t ] }
87         [ drop f ]
88     } cond ;
89
90 : append-relative-path ( path1 path2 -- path )
91     [ trim-tail-separators ]
92     [ trim-head-separators ] bi* "/" glue ;
93
94 : append-path ( path1 path2 -- path )
95     {
96         { [ over empty? ] [ append-path-empty ] }
97         { [ dup empty? ] [ drop ] }
98         { [ over trim-tail-separators "." = ] [ nip ] }
99         { [ dup absolute-path? ] [ nip ] }
100         { [ dup head.? ] [ rest trim-head-separators append-path ] }
101         { [ dup head..? ] [
102             2 tail trim-head-separators
103             [ parent-directory ] dip append-path
104         ] }
105         { [ over absolute-path? over first path-separator? and ] [
106             [ 2 head ] dip append
107         ] }
108         [ append-relative-path ]
109     } cond ;
110
111 : prepend-path ( path1 path2 -- path )
112     swap append-path ; inline
113
114 : 3append-path ( path chunk1 chunk2 -- path' )
115     [ append-path ] dip append-path ; inline
116
117 : file-name ( path -- string )
118     dup root-directory? [
119         trim-tail-separators
120         dup last-path-separator [ 1 + tail ] [
121             drop special-path? [ file-name ] when
122         ] if
123     ] unless ;
124
125 : file-stem ( path -- stem )
126     file-name "." split1-last drop ;
127
128 : file-extension ( path -- extension )
129     file-name "." split1-last nip ;
130
131 : path-components ( path -- seq )
132     normalize-path path-separator split harvest ;
133
134 HOOK: resolve-symlinks os ( path -- path' )
135
136 M: object resolve-symlinks normalize-path ;
137
138 : resource-path ( path -- newpath )
139     "resource-path" get prepend-path ;
140
141 HOOK: home io-backend ( -- dir )
142
143 M: object home "" resource-path ;
144
145 GENERIC: vocab-path ( path -- newpath )
146
147 GENERIC: absolute-path ( path -- path' )
148
149 M: string absolute-path
150     "resource:" ?head [
151         trim-head-separators resource-path
152         absolute-path
153     ] [
154         "vocab:" ?head [
155             trim-head-separators vocab-path
156             absolute-path
157         ] [
158             "~" ?head [
159                 trim-head-separators home prepend-path
160                 absolute-path
161         ] [
162             current-directory get prepend-path
163         ] if ] if
164     ] if ;
165
166 M: object normalize-path
167     absolute-path ;
168
169 : root-path* ( path -- path' )
170     dup absolute-path? [
171         dup [ path-separator? ] find
172         drop 1 + head
173     ] when ;
174
175 HOOK: root-path os ( path -- path' )
176
177 M: object root-path root-path* ;
178
179 : relative-path* ( path -- relative-path )
180     dup absolute-path? [
181         dup [ path-separator? ] find
182         drop 1 + tail
183     ] when ;
184
185 HOOK: relative-path os ( path -- path' )
186
187 M: object relative-path relative-path* ;
188
189 : canonicalize-path* ( path -- path' )
190     [
191         relative-path
192         [ path-separator? ] split-when
193         [ { "." "" } member? ] reject
194         V{ } clone [
195             dup ".." = [
196                 over empty?
197                 [ over push ]
198                 [ over ?last ".." = [ over push ] [ drop dup pop* ] if ] if
199             ] [
200                 over push
201             ] if
202         ] reduce
203     ] keep dup absolute-path? [
204         [
205             [ ".." = ] trim-head
206             path-separator join
207         ] dip root-path prepend-path
208     ] [
209         drop path-separator join [ "." ] when-empty
210     ] if ;
211
212 HOOK: canonicalize-path io-backend ( path -- path' )
213
214 M: object canonicalize-path canonicalize-path* ;
215
216 HOOK: canonicalize-drive io-backend ( path -- path' )
217
218 M: object canonicalize-drive ;
219
220 HOOK: canonicalize-path-full io-backend ( path -- path' )
221
222 M: object canonicalize-path-full canonicalize-path canonicalize-drive ;
223
224 : >windows-path ( path -- path' ) H{ { CHAR: / CHAR: \\ } } substitute ;
225
226 TUPLE: pathname string ;
227
228 C: <pathname> pathname
229
230 M: pathname absolute-path string>> absolute-path ;
231
232 M: pathname <=> [ string>> ] compare ;