-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfile.lgt
279 lines (228 loc) · 6.62 KB
/
file.lgt
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
% A paraterized object representing a single file.
:- object(path(_Path)).
% TODO: See what can be moved here...
:- end_object.
:- object(file(Path), extends(path(Path))).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-06,
comment is 'File wrapper object. The parameter A is a filename atom.',
parnames is ['Path']
]).
:- uses(user, [
atomic_list_concat/2
]).
:- public(read/1).
read(Contents) :-
parameter(1,F),
os::absolute_file_name(F, P),
open(P,read,Stream),
catch(read_characters(Stream,Contents),_,true),
close(Stream).
:- public(write/1).
write(Contents) :-
parameter(1,F),
os::absolute_file_name(F, P),
open(P,write,Stream),
write_characters(Stream,Contents),
close(Stream).
:- public(append/1).
append(Contents) :-
parameter(1,F),
os::absolute_file_name(F, P),
open(P,append,Stream),
write_characters(Stream,Contents),
close(Stream).
:- public(copy_to/1).
copy_to(TargetFile) :-
parameter(1,File),
os::absolute_file_name(File, Path),
atomic_list_concat(['cp ',Path,' ',TargetFile],ShellCopyCommand),
os::shell(ShellCopyCommand).
:- public(exists/0).
:- info(exists/0, [
comment is 'True if the file (or directory) A exists.'
]).
exists :-
parameter(1,F),
os::file_exists(F).
:- public(touch/0).
touch :-
parameter(1,File),
os::absolute_file_name(File, Path),
open(Path,write,Stream),
close(Stream).
:- public(delete/0).
delete :-
parameter(1,F),
catch(os::delete_file(F), _, true).
:- public(dirname/1).
:- info(dirname/1, [
comment is 'Separate out the directory part (Directory) of a filename',
argnames is ['Directory']
]).
dirname(Directory) :-
::dir_file(Directory,_).
:- public(basename/1).
basename(Filename) :-
::dir_file(_,Filename).
:- private(dir_file/2).
:- info(dir_file/2,[
comment is 'Separates filename into a Directory part and a Filename part',
argnames is ['Directory','Filename']
]).
% FIXME: This is unix only and somewhat fragile. Write a better and portable version.
dir_file(Directory,Filepart) :-
parameter(1,Filename),
% everything before last '/'=47 is dirname:
atom_codes(Filename, CharCodes),
list::append(DirPartCodes, FilePartCodes, CharCodes),
list::append(_,[47],DirPartCodes), % DirPart should end with a '/'
\+ list::member(47,FilePartCodes),
atom_codes(Directory,DirPartCodes),
atom_codes(Filepart,FilePartCodes).
:- public(canonical/1).
:- info(canonical/1, [
comment is 'CanonicalFilename is Filename with backspaces->slashes and double slashes removed',
argnames is ['CanonicalFilename']
]).
canonical(CanonicalFilename) :-
parameter(1,Filename),
uri(Filename)::valid,
!,
uri(Filename)::elements(Protocol,Filepart),
atom_codes(Filepart,FilePartCodes),
% Replace backslash (code 92) with forward slash (code 47)
meta::map([X,Y]>>((X==92) -> Y=47 ; Y=X),FilePartCodes,UnixFilenameCodes),
remove_double_slashes(UnixFilenameCodes,Unslashed),
atom_codes(CanonicalFilepart,Unslashed),
atom_concat(Protocol,CanonicalFilepart,CanonicalFilename).
canonical(CanonicalFilename) :-
parameter(1,Filename),
atom_codes(Filename,FileCodes),
% Replace backslash (code 92) with forward slash (code 47)
meta::map([X,Y]>>((X==92) -> Y=47 ; Y=X),FileCodes,UnixFilenameCodes),
remove_double_slashes(UnixFilenameCodes,Unslashed),
atom_codes(CanonicalFilename,Unslashed).
:- private(remove_double_slashes/2).
:- info(remove_double_slashes/2,[
comment is 'replace adjacent slashes with one slash',
argnames is ['FilenameIn','FilenameOut']
]).
remove_double_slashes([],[]).
remove_double_slashes([47,47|FilenameCodesIn],[47|FilenameCodesOut]) :-
!,
remove_double_slashes(FilenameCodesIn,FilenameCodesOut).
remove_double_slashes([X|FilenameCodesIn],[X|FilenameCodesOut]) :-
remove_double_slashes(FilenameCodesIn,FilenameCodesOut).
:- private(read_characters/2).
read_characters(Stream,Contents) :-
get_code(Stream,Code),
( Code == -1 ->
Contents = []
; Contents = [Code|Rest],
read_characters(Stream,Rest)
).
:- private(write_characters/2).
write_characters(_,[]) :- !.
write_characters(Stream,[X|Xs]) :-
put_code(Stream,X),
write_characters(Stream,Xs).
:- end_object.
:- object(prolog_file(F), extends(file(F))).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-06,
comment is 'File wrapper object for Prolog files. The parameter A is a filename atom.',
parnames is ['File']
]).
:- public(read_terms/1).
:- info(read_terms/1, [
comment is 'reads Terms from file A', argnames is ['Terms']
]).
read_terms(Terms) :-
parameter(1,File),
os::absolute_file_name(File, Path),
open(Path,read,Stream),
stream_read_terms(Stream,Terms),
close(Stream).
:- public(write_terms/1).
:- info(write_terms/1, [
comment is 'writes Terms to file A', argnames is ['Terms']
]).
write_terms(Terms) :-
parameter(1,File),
os::absolute_file_name(File, Path),
open(Path,write,Stream),
stream_write_terms(Stream,Terms),
close(Stream).
:- public(member/1).
:- info(member/1, [
comment is 'Term is a member of file A', argnames is ['Term']
]).
member(Term) :-
read_terms(Terms),
!,
list::member(Term,Terms).
:- public(append/1).
:- info(append/1, [
comment is 'Appends Terms to file A',argnames is ['Terms']
]).
append(Terms) :-
parameter(1,File),
os::absolute_file_name(File, Path),
open(Path,append,Stream),
stream_write_terms(Stream,Terms),
close(Stream).
:- public(select/2).
:- info(select/2, [
comment is 'Term is a term from file A and Rest all other Terms in file A',argnames is ['Term','Rest']
]).
select(Term,Rest) :-
read_terms(FileTerms),
!,
list::select(Term,FileTerms,Rest).
:- private(stream_read_terms/2).
stream_read_terms(Stream,Terms) :-
read(Stream,Term),
( Term == end_of_file ->
Terms = []
; Terms = [Term|Rest],
stream_read_terms(Stream,Rest)
).
:- private(stream_write_terms/2).
stream_write_terms(_,[]).
stream_write_terms(Stream,[Term|Rest]) :-
writeq(Stream,Term),
write(Stream,'.\n'),
stream_write_terms(Stream,Rest).
:- end_object.
:- object(directory(Path), extends(path(Path))).
:- info([
version is 1:0:0,
author is 'Christian Theil Have',
date is 2012-11-06,
comment is '.',
parnames is ['Path']
]).
:- public(create/0).
:- info(create/0, [
comment is 'Creates the directory if it does not allready exist'
]).
create :-
parameter(1,Path),
((::exists) ->
true
;
os::make_directory(Path)).
:- public(exists/0).
:- info(exists/0, [
comment is 'True if the directory exists'
]).
% FIXME: This is not going to work for all Prologs
exists :-
parameter(1,Path),
file(Path)::exists.
:- end_object.