Repository navigation
Expand file tree
/
Copy pathfile.red
More file actions
195 lines (181 loc) · 5.37 KB
/
Copy pathfile.red
File metadata and controls
195 lines (181 loc) · 5.37 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Red [
Title: "Red file functions"
Author: "Gregg Irwin"
File: %file.red
Tabs: 4
; Rights: "Copyright (C) 2013 All Mankind. All rights reserved."
; License: {
; Distributed under the Boost Software License, Version 1.0.
; See https://github.com/dockimbel/Red/blob/master/BSL-License.txt
; }
]
at-suffix: function [
"Returns a filespec at its suffix or tail."
path [any-string!]
][
any [find/last path suffix? path tail path]
]
change-suffix: func [
"Changes the suffix of a filespec and returns the new filespec."
path [any-string!] "(modified)"
suffix [any-string!] "The new suffix"
][
append remove-suffix path suffix
]
;clean-path
dir?: func [
"Returns true if a filespec ends with a path marker."
path [file! url!]
][
true? find "/\" last path
]
dirize: func [
"Returns a copy of a filespec with a trailing path marker."
path [file! string! url!]
][
path: copy path
either slash = last path [path] [append path slash]
]
file-of: func [
"Returns the filename portion of a filespec."
path [any-string!]
][
second split-path path
]
path-of: func [
"Returns the path portion of a filespec."
path [any-string!]
][
first split-path path
]
remove-suffix: func [
"Removes the suffix from a filespec."
path [any-string!] "(modified)"
][
head clear at-suffix path
]
; Modeled on Ladislav's implementation where it holds that
; file = rejoin split-path file
; This does not match current REBOL behavior. It arguably
; makes more sense, but will break code in cases like this:
; %/c/test/test2/
; REBOL == [%/c/test/ %test2/]
; Ladislav's == [%/c/test/test2/ %""]
; Ladislav's func only seems to go really wrong in the case
; of ending with a slash an that's the only slash in the
; value which return an empty path and entire filespec as
; the target.
; Schemes (http://) don't work well either.
;split-path: func [
; "Returns a block containing a path and target, by splitting a filespec."
; filespec [any-string!]
; /local target
;][
; target: tail filespec
; if slash = last target [decr target]
; target: any [find/reverse/tail target slash filespec]
; ;!! TBD: Do we want to use TO FILE! on target, once TO is in Red?
; reduce [copy/part filespec target copy target]
;]
;split-path: func [
; "Returns a block containing a path and target, by splitting a filespec."
; filespec [any-string!]
; /local target
;][
; either any [
; ; It's a url ending with a slash. This doesn't account for
; ; formed URLs. To do that, we would have to search for "://"
; all [slash = last filespec]
; ;!! TBD: Need url support
; ;all [url? filespec slash = last filespec]
; ; Only one slash, and it's at the tail.
; all [target: find/tail filespec slash tail? target]
; ][
; reduce [copy filespec clear copy %.] ; copy %"" seems to make Red unhappy when compiling
; ][
; target: tail filespec
; if slash = last target [decr target]
; target: any [find/reverse/tail target slash filespec]
; ;!! TBD: Use TO FILE! on target, once TO is in Red.
; reduce [copy/part filespec target copy target]
; ]
;]
split-path: func [
"Returns a block containing a path and target."
path [any-string!]
/local target
][
target: any [find/last/tail path slash path]
reduce [copy/part path target to file! target]
]
;; These tests match current REBOL behavior.
;split-path-tests: compose/deep [
; %foo [%./ %foo]
; %"" [%./ %""]
; %/c/rebol/tools/test/test.r [%/c/rebol/tools/test/ %test.r]
; %/c/rebol/tools/test/ [%/c/rebol/tools/ %test/]
; %/c/rebol/tools/test [%/c/rebol/tools/ %test]
; %/c/test/test2/file.x [%/c/test/test2/ %file.x]
; %/c/test/test2/ [%/c/test/ %test2/]
; %/c/test/test2 [%/c/test/ %test2]
; %/c/test [%/c/ %test]
; %//test [%// %test]
; %/test [%/ %test]
; %/c/ [%/ %c/]
; %/ [%/ (none)]
; %// [%/ %/]
; %. [%./ (none)]
; %./ [%./ (none)]
; %./. [%./ %./]
; %.. [%../ (none)]
; %../ [%../ (none)]
; %../.. [%../ %../]
; %../../test [%../../ %test]
; %foo/.. [%foo/ %../]
; %foo/. [%foo/ %./]
; %foo/../. [%foo/../ %./]
; %foo/../bar [%foo/../ %bar]
; %foo/./bar [%foo/./ %bar]
; %/c/foo/../bar [%/c/foo/../ %bar]
; %/c/foo/./bar [%/c/foo/./ %bar]
; http://www.rebol.com/index.html [http://www.rebol.com/ %index.html]
; http://www.rebol.com/ [http:// %www.rebol.com/]
; http://www.rebol.com [http:// %www.rebol.com]
; http:// [http:/ %/] ; What should we do in this case?
; http://.. [http:// %../]
; http://. [http:// %./]
; http://../. [http://../ %./]
; http://../bar [http://../ %bar]
; http://./bar [http://./ %bar]
; (at %/vol/dir/file.r 6) [%dir/ %file.r]
;]
;foreach [test result] split-path-tests [
; if result <> actual: split-path test [
; print [mold test 'expected mold result "but got" mold actual]
; ]
;]
;foreach [test result] split-path-tests [
; if test <> rejoin res: split-path test [
; print ["REJOIN quality failed:" mold test mold rejoin res]
; ]
;]
suffix?: func [
"Returns the file extension or none."
path [any-string!]
][
all [
path: find/last path #"."
not find path slash ; can't have a slash after the suffix
copy path
]
]
;to-local-file
;to-rebol-file :to-red-file
;to-red-file
undirize: func [
"Returns a copy of a filespec with no trailing path marker."
path [file! string! url!]
][
path: copy path
either slash = last path [chop path] [path]
]