-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathfile.lisp
725 lines (608 loc) · 25.3 KB
/
file.lisp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
(coalton-library/utils:defstdlib-package #:coalton-library/file
(:documentation "This is Coalton's library for directory utilities and file IO.
Most functions return outputs of type `(Result FileError :a)`, ensuring that errors can be assessed and handled.
File IO is handled using stream options, for instance:
```
(with-open-file (Bidirectional file EError)
(fn (stream)
(write-string stream \"Hello World!\")
(read-file-to-vector stream)
```
Common Lisp makes a distinction between file and directory paths. Directory paths are always terminated with a trailing slash, file paths must never have a trailing slash.")
(:use
#:coalton
#:coalton-library/classes
#:coalton-library/builtin
#:coalton-library/functions
#:coalton-library/system)
(:local-nicknames
(#:str #:coalton-library/string)
(#:iter #:coalton-library/iterator)
(#:cell #:coalton-library/cell)
(#:list #:coalton-library/list)
(#:vec #:coalton-library/vector)
(#:res #:coalton-library/result)
(#:types #:coalton-library/types)
(#:char #:coalton-library/char))
(:export
#:Pathname
#:FileError
#:directory-pathname?
#:file-pathname?
#:exists?
#:directory-exists?
#:file-exists?
#:merge
#:create-directory!
#:directory-files
#:subdirectories
#:empty?
#:remove-directory!
#:remove-directory-recursive!
#:system-relative-pathname
#:copy!
#:delete-file!
#:FileStream
#:IfExists
#:EError
#:Overwrite
#:Append
#:Supersede
#:StreamOptions
#:Input
#:Output
#:Bidirectional
#:close
#:abort
#:read-char
#:write-char
#:flush
#:file-position
#:set-file-position
#:File
#:open
#:read
#:write
#:with-open-file
#:read-vector
#:read-file-to-vector
#:write-vector
#:write-string
#:write-line
#:create-temp-directory!
#:create-temp-file!
#:with-temp-file
#:with-temp-directory
#:append-to-file!
#:write-to-file!
#:read-file-to-string
#:read-file-lines))
(in-package #:coalton-library/file)
(named-readtables:in-readtable coalton:coalton)
#+coalton-release
(cl:declaim #.coalton-impl/settings:*coalton-optimize-library*)
;;;
;;; Pathnames, lisp conditions, condition handling
;;;
(coalton-toplevel
(repr :native cl:pathname)
(define-type Pathname
"Pathname object. Equivalent to `cl:pathname`")
(define-instance (Into String Pathname)
(define (into s)
(lisp Pathname (s)
(cl:pathname s))))
(define-instance (Into Pathname String)
(define (into p)
(lisp String (p)
(cl:namestring p))))
(define-instance (Eq Pathname)
(define (== a b)
(lisp Boolean (a b)
(cl:equalp a b))))
(define-instance (Ord Pathname)
(define (<=> p q)
(<=> (the String (into p))
(the String (into q))))))
;;;
;;; Handling Errors
;;;
(coalton-toplevel
(define-type FileError
"Errors for file functions."
(FileError String)
(PathError String Pathname)
(LispError LispCondition)
EOF)
(define-instance (Signalable FileError)
(define (error ferr)
(match ferr
((FileError str1)
(error (lisp String (str1)
(cl:format cl:nil "File Error:~%~%~a" str1))))
((PathError str path)
(error (lisp String (str path)
(cl:format cl:nil "Path Error:~%~%~a ~a" str path))))
((LispError c)
(error c))
((EOF)
(error #.(cl:format cl:nil "Error~%~%End of File")))))))
;;;
;;; Macro for handling lisp errors in file functions
;;;
(cl:defmacro %handle-file-function ((func cl:&rest args))
"A macro for handling potentially erroring lisp file operations.
Automatically returns the lisp condition if one is thrown."
(cl:let ((c (cl:gensym "C")))
`(cl:handler-case (Ok (,func ,@args))
(cl:error (,c) (Err (LispError ,c))))))
;;;
;;; Handling existential path queries
;;;
(coalton-toplevel
(declare directory-pathname? ((Into :a Pathname) => :a -> Boolean))
(define (directory-pathname? path)
"Returns True if a pathname has no file component."
(let p = (the Pathname (into path)))
(lisp Boolean (p)
(to-boolean (uiop:directory-pathname-p p))))
(declare file-pathname? ((Into :a Pathname) => :a -> Boolean))
(define (file-pathname? path)
"Returns True if a pathname has a file component."
(let p = (the Pathname (into path)))
(lisp Boolean (p)
(to-boolean (uiop:file-pathname-p p))))
(declare exists? ((Into :a Pathname) => :a -> (Result FileError Boolean)))
(define (exists? path)
"Returns whether a file or directory exists."
(let p = (the Pathname (into path)))
(lisp (Result FileError Boolean) (p)
(%handle-file-function (to-boolean (cl:probe-file p)))))
(declare %if-directory-path ((Into :a Pathname)
=> (Pathname -> (Result FileError :b))
-> :a
-> (Result FileError :b)))
(define (%if-directory-path action path)
"Performs an operation only if the path is a valid directory pathname."
(let p = (the Pathname (Into path)))
(let dir = (directory-pathname? p))
(if dir
(action p)
(Err (PathError "Invalid directory path." p))))
(declare directory-exists? ((Into :a Pathname) => :a -> (Result FileError Boolean)))
(define (directory-exists? path)
"Returns True if a pathname names a directory that exists."
(%if-directory-path
(fn (p)
(lisp (Result FileError Boolean) (p)
(%handle-file-function (to-boolean (uiop:directory-exists-p p)))))
path))
(declare file-exists? ((Into :a Pathname) => :a -> (Result FileError Boolean)))
(define (file-exists? path)
"Returns True if a pathname names a file that exists."
(do
(let p = (the Pathname (Into path)))
(let file = (file-pathname? p))
(if file
(lisp (Result FileError Boolean) (p)
(%handle-file-function (to-boolean (uiop:file-exists-p p))))
(Err (PathError "Invalid file path." p))))))
;;;
;;; Merging, semigroup and monoid
;;;
(coalton-toplevel
(declare merge ((Into :a Pathname) (Into :b Pathname) => :a -> :b -> Pathname))
(define (merge path1 path2)
"Merges two pathnames together. The directory pathname should be the first argument."
(let p1 = (the Pathname (into path1)))
(let p2 = (the Pathname (into path2)))
(if (not (directory-pathname? p1))
(error (PathError "Merge: first argument must be a directory path." p1))
(lisp Pathname (p1 p2)
(cl:merge-pathnames p2 p1))))
(define-instance (Semigroup Pathname)
(define <> merge))
(define-instance (Monoid Pathname)
(define mempty (the Pathname (into "")))))
;;;
;;; Working with directories
;;;
(coalton-toplevel
(declare create-directory! ((Into :a Pathname) => :a -> (Result FileError Pathname)))
(define (create-directory! path)
"This is equivalent to `mkdir -p`. Creates a directory and its parents. The pathname must be a valid directory pathname."
(%if-directory-path (fn (p)
(lisp (Result FileError Pathname) (p)
(%handle-file-function (cl:ensure-directories-exist p))))
path))
(declare directory-files ((Into :a Pathname) => :a -> (Result FileError (List Pathname))))
(define (directory-files path)
"Returns all files within the directory. Returns an error if the pathname is not a directory pathname."
(%if-directory-path (fn (p)
(lisp (Result FileError (List Pathname)) (p)
(%handle-file-function (uiop:directory-files p))))
path))
(declare subdirectories ((Into :a Pathname) => :a -> (Result FileError (List Pathname))))
(define (subdirectories path)
"Returns all subdirectories within the directory. Returns an error if the pathname is not a directory pathname."
(%if-directory-path (fn (p)
(lisp (Result FileError (List Pathname)) (p)
(%handle-file-function (uiop:subdirectories p))))
path))
;;
;; Handling directory behavior that depends on emptiness
;;
(declare empty? ((Into :a Pathname) => :a -> (Result FileError Boolean)))
(define (empty? path)
"Checks whether a directory is empty."
(%if-directory-path (fn (p)
(pure (lisp Boolean (p)
(cl:null (cl:directory (cl:merge-pathnames uiop:*wild-directory* p))))))
path))
(declare remove-directory! ((Into :a Pathname) => :a -> (Result FileError :a)))
(define (remove-directory! path)
"Deletes an empty directory."
(let p = (the Pathname (into path)))
(lisp (Result FileError :a) (p)
(%handle-file-function (uiop:delete-empty-directory p))))
(declare remove-directory-recursive! ((Into :a Pathname) => :a -> (Result FileError Unit)))
(define (remove-directory-recursive! path)
"Deletes a target directory recursively. Equivalent to `rm -r`. Errors if the path is not a directory."
(%if-directory-path (fn (p)
(lisp (Result FileError Unit) (p)
(%handle-file-function (uiop:delete-directory-tree p :validate cl:t))))
path))
(declare system-relative-pathname ((Into :a String) => :a -> String -> (Result FileError Pathname)))
(define (system-relative-pathname system-name name)
"Generates a system-relative-pathname for a given filename or path. This is a wrapper for `asdf:system-relative-pathname`. `Name` will likely be an empty string unless a subdirectory or filename is specified."
(lisp (Result FileError Pathname) (system-name name)
(cl:handler-case (Ok (asdf:system-relative-pathname system-name name))
(cl:error (c) (Err (LispError c)))))))
;;;
;;; Basic File Operations
;;;
(coalton-toplevel
(declare copy! ((Into :a Pathname) (Into :b Pathname) => :a -> :b -> (Result FileError Unit)))
(define (copy! input output)
"Copies a file to a new location."
(do
(let in = (the Pathname (into input)))
(let out = (the Pathname (into output)))
(if (not (file-pathname? in))
(Err (PathError "Invalid input for copying, path is not a file:" in))
(lisp (Result FileError :c) (in out)
(%handle-file-function (uiop:copy-file in out))))))
(declare delete-file! ((Into :a Pathname) => :a -> (Result FileError Unit)))
(define (delete-file! path)
"Deletes a given file if the file exists."
(do
(let p = (the Pathname (into path)))
(lisp (Result FileError Unit) (p)
(%handle-file-function (uiop:delete-file-if-exists p))))))
;;;
;;; FStreams, FileStreams, and options
;;;
(coalton-toplevel
(repr :native cl:file-stream)
(define-type (FileStream :a)
"Represents a file stream, using `cl:file-stream`.")
(repr :enum)
(define-type IfExists
"Possible options for opening a stream when the file exists."
EError
Overwrite
Append
Supersede)
(define-type StreamOptions
"A type for providing parameters for opening streams. StreamOptions take strings for pathnames, but they will error if they are not proper and appropriate pathnames."
(Input Pathname) "Constructor for opening an input stream"
(Output Pathname IfExists) "Constructor for opening an output stream."
(Bidirectional Pathname IfExists) "Constructor for opening a bidirectional stream.")
;;
;; Opening Streams
;;
(declare %open-input (Pathname -> types:lisptype -> (Result FileError (FileStream :a))))
(define (%open-input path etype)
"Opens an input stream for the given filepath, and for a given type."
(lisp (Result FileError (FileStream :a)) (path etype)
(%handle-file-function (cl:open path
:direction ':input
:element-type etype
:if-does-not-exist ':error))))
(declare %open-output (Pathname -> IfExists -> types:lisptype -> (Result FileError (FileStream :a))))
(define (%open-output path if-exists etype)
"Opens an output stream for the given filepath, and for a given type."
(lisp (Result FileError (FileStream :a)) (path if-exists etype)
(%handle-file-function (cl:open path
:direction ':output
:element-type etype
:if-exists (cl:case if-exists
(IfExists/EError ':error)
(IfExists/Overwrite ':overwrite)
(IfExists/Append ':append)
(IfExists/Supersede ':supersede))
:if-does-not-exist ':create))))
(declare %open-bidirectional (Pathname -> IFExists -> types:lisptype -> (Result FileError (FileStream :a))))
(define (%open-bidirectional path if-exists etype)
"Opens a two way stream for the given filepath and for a given type."
(lisp (Result FileError (FileStream :a)) (path if-exists etype)
(%handle-file-function (cl:open path
:direction ':io
:element-type etype
:if-exists (cl:case if-exists
(IfExists/EError ':error)
(IfExists/Overwrite ':overwrite)
(IfExists/Append ':append)
(IfExists/Supersede ':supersede))
:if-does-not-exist ':create))))
(declare %open (StreamOptions -> types:lisptype -> (Result FileError (FileStream :a))))
(define (%open stream-options etype)
"Opens a FileStream for a given type and StreamOptions."
(match stream-options
((Input path)
(%open-input path etype))
((Output path exists)
(%open-output path exists etype))
((Bidirectional path exists)
(%open-bidirectional path exists etype))))
;;
;; Other basic stream operations
;;
(declare close ((FileStream :a) -> (Result FileError :b)))
(define (close stream)
"Closes a FileStream."
(lisp (Result FileError :a) (stream)
(%handle-file-function (cl:close stream))))
(declare abort ((FileStream :a) -> (Result FileError :b)))
(define (abort stream)
"Closes a FileStream and aborts all operations.."
(lisp (Result FileError :a) (stream)
(%handle-file-function (cl:close stream :abort cl:t))))
(declare read-char ((FileStream Char) -> (Result FileError Char)))
(define (read-char stream)
"Reads a character from an FileStream."
(lisp (Result FileError Char) (stream)
(cl:handler-case (Ok (cl:read-char stream))
(cl:end-of-file () (Err (EOF)))
(cl:error (c) (Err (LispError c))))))
(declare write-char ((FileStream Char) -> Char -> (Result FileError Unit)))
(define (write-char stream data)
"Writes a `Char` to the stream."
(lisp (Result FileError Unit) (stream data)
(%handle-file-function (cl:write-char data stream))))
(define-class (%FileByte :a)
"A class of `byte` types that can be written to and read from files.")
(declare %read-byte ((%FileByte :a) => (FileStream :a) -> (Result FileError :a)))
(define (%read-byte stream)
"Reads a byte from a FileStream"
(lisp (Result FileError :a) (stream)
(cl:handler-case (Ok (cl:read-byte stream))
(cl:end-of-file () (Err (EOF)))
(cl:error (c) (Err (LispError c))))))
(declare %write-byte ((%FileByte :a) => (FileStream :a) -> :a -> (Result FileError Unit)))
(define (%write-byte stream data)
"Writes a `Char` to the stream."
(lisp (Result FileError Unit) (stream data)
(%handle-file-function (cl:write-byte data stream))))
(declare flush ((FileStream :a) -> (Result FileError :b)))
(define (flush stream)
"Blocks until `stream` has been flushed. Calls `cl:finish-output`."
(lisp (Result FileError :b) (stream)
(%handle-file-function (cl:finish-output stream))))
(declare file-position ((FileStream :a) -> (Result FileError UFix)))
(define (file-position stream)
"Finds the file-position of a file stream."
(lisp (Result FileError UFix) (stream)
(%handle-file-function (cl:file-position stream))))
(declare set-file-position ((FileStream :a) -> UFix -> (Result FileError Unit)))
(define (set-file-position stream i)
"Sets the file position of a file stream."
(lisp (Result FileError Unit) (stream i)
(%handle-file-function (cl:file-position stream i)))))
;;;
;;; File Class
;;;
(coalton-toplevel
(define-class (File :a)
"A class of types which are able to be written to or read from a file."
(open (StreamOptions -> (Result FileError (Filestream :a))))
(read ((FileStream :a) -> (Result FileError :a)))
(write ((FileStream :a) -> :a -> (Result FileError Unit))))
(define-instance (File Char)
(define (open stream-options)
(let ((type (types:runtime-repr (the (types:Proxy Char) types:Proxy))))
(%open stream-options type)))
(define (read fs)
(read-char fs))
(define (write fs data)
(write-char fs data))))
;;;
;;; File instances for supported Integer/Byte types
;;;
(cl:eval-when (:compile-toplevel :load-toplevel)
(cl:defmacro define-file-type (type)
`(progn (define-instance (%FileByte ,type))
(define-instance (File ,type)
(define (open stream-options)
(let ((t (types:runtime-repr (the (types:Proxy ,type) types:Proxy))))
(%open stream-options t)))
(define (read fs)
(%read-byte fs))
(define (write fs data)
(%write-byte fs data))))))
(coalton-toplevel
(define-file-type IFix)
(define-file-type UFix)
(define-file-type I8)
(define-file-type U8)
(define-file-type I16)
(define-file-type U16)
(define-file-type I32)
(define-file-type U32)
(define-file-type I64)
(define-file-type U64))
;;;
;;;
;;;
(coalton-toplevel
(declare with-open-file ((File :a)
=> StreamOptions
-> ((FileStream :a) -> (Result FileError :b))
-> (Result FileError :b)))
(define (with-open-file stream-options thunk)
"Opens a file stream, performs `thunk` on it, then closes the stream."
(bracket (open stream-options)
close
thunk))
(declare read-vector ((File :a)
=> (FileStream :a)
-> UFix
-> (Result FileError (vec:Vector :a))))
(define (read-vector stream chunk-size)
"Reads a chunk of a file into a vector of type `:a`."
(let p = types:Proxy)
(let p_ = (types:proxy-inner p))
(let type = (types:runtime-repr p_))
(types:as-proxy-of
(lisp (Result FileError (vec:Vector :a)) (stream type chunk-size)
(%handle-file-function (cl:let ((v (cl:make-array chunk-size :adjustable cl:t :element-type type)))
(cl:read-sequence v stream)
v)))
p))
(declare read-file-to-vector ((types:RuntimeRepr :a) (File :a)
=> (FileStream :a)
-> (Result FileError (vec:Vector :a))))
(define (read-file-to-vector stream)
"Reads a file into a vector of type `:a`."
(let p = types:Proxy)
(let p_ = (types:proxy-inner p))
(let type = (types:runtime-repr p_))
(types:as-proxy-of
(lisp (Result FileError (vec:Vector :a)) (stream type)
(%handle-file-function (cl:let ((v (cl:make-array (cl:file-length stream) :adjustable cl:t :element-type type)))
(cl:read-sequence v stream)
v)))
p))
(declare write-vector ((types:RuntimeRepr :a) (File :a)
=> (FileStream :a)
-> (vec:Vector :a)
-> (Result FileError Unit)))
(define (write-vector stream v)
"Writes elements of an vector of type `:a` to a stream of type `:a`."
(lisp (Result FileError Unit) (stream v)
(%handle-file-function (cl:write-sequence v stream))))
(declare write-string ((FileStream Char) -> String -> (Result FileError Unit)))
(define (write-string fs s)
"Writes a `string` to a FileStream of type Char."
(write-vector fs (iter:collect! (str:chars s))))
(declare write-line ((FileStream Char) -> String -> (Result FileError Unit)))
(define (write-line stream s)
"Writes a string with an appended newline to a filestream of type Char."
(write-vector stream (iter:collect! (Str:chars s)))
(write stream #\NewLine)))
;;;
;;; Temporary files and directories
;;;
(coalton-toplevel
(declare %temp-directory (Unit -> Pathname))
(define (%temp-directory)
"Returns the current temporary directory, `uiop:*temporary-directory*`."
(lisp Pathname ()
(uiop:temporary-directory)))
(declare %set-temp-directory ((Into :a Pathname) => :a -> (Result FileError Unit)))
(define (%set-temp-directory path)
"Sets the temporary directory."
(%if-directory-path (fn (p)
(lisp (Result FileError Unit) (p)
(%handle-file-function (cl:setf uiop:*temporary-directory* p))))
path))
(declare %make-temp-dir-pathname (Unit -> Pathname))
(define (%make-temp-dir-pathname)
"Makes a temporary directory pathname."
(merge (%temp-directory)
(lisp Pathname ()
(cl:pathname
(cl:format cl:nil "~acoal~36R-tmp/"
(uiop:temporary-directory)
(cl:random (cl:expt 36 8)))))))
(declare %make-temp-file-pathname (String -> Pathname))
(define (%make-temp-file-pathname file-ext)
"Makes a pathname of a file in the temporary directory.
File extensions need to include `.`, like \".txt\"."
(merge (%temp-directory)
(lisp Pathname (file-ext)
(cl:pathname
(cl:format cl:nil "~36R-tmp~A"
(cl:random (cl:expt 36 8))
file-ext)))))
(declare create-temp-directory! (Unit -> (Result FileError Pathname)))
(define (create-temp-directory!)
"This configures a default temporary directory for use."
(create-directory! (%make-temp-dir-pathname)))
(declare create-temp-file! (String -> (Result FileError Pathname)))
(define (create-temp-file! file-ext)
"This configures a default temporary file for use."
(let filepath = (%make-temp-file-pathname file-ext))
(do
(lisp (Result FileError :a) (filepath)
(%handle-file-function (cl:open filepath :direction :output :if-does-not-exist :create)))
(pure filepath)))
(declare with-temp-file ((File :a)
=> String
-> ((FileStream :a) -> (Result FileError :b))
-> (Result FileError :b)))
(define (with-temp-file file-type thunk)
"Performs an operation `thunk` on a temporary file. File type extensions need to include `.`, like \".txt\"."
(let file = (%make-temp-file-pathname file-type))
(bracket
(open (Bidirectional file Overwrite))
(fn (_)
(delete-file! file))
thunk))
(declare with-temp-directory ((Pathname -> (Result FileError :a)) -> (Result FileError :a)))
(define (with-temp-directory thunk)
"Performs an operation `thunk` inside a temporary directory."
(bracket
(create-temp-directory!)
remove-directory-recursive!
thunk)))
;;;
;;; Top-level functions, i.e. those that don't require FileStream or File knowledge
;;;
(coalton-toplevel
(declare append-to-file! ((types:Runtimerepr :a)
(Into :p Pathname)
(File :a)
=> :p
-> (vec:Vector :a)
-> (Result FileError Unit)))
(define (append-to-file! path data)
"Opens and appends a file with data of type :a."
(with-open-file (Output (into path) Append)
(fn (stream)
(write-vector stream data))))
(declare write-to-file! ((types:Runtimerepr :a)
(Into :p Pathname)
(File :a)
=> :p
-> (vec:Vector :a)
-> (Result FileError Unit)))
(define (write-to-file! path data)
"Opens and writes to a file with data of type :a. Supersedes existing data on the file."
(with-open-file (Output (into path) Supersede)
(fn (stream)
(write-vector stream data))))
(declare read-file-to-string ((Into :a Pathname) => :a -> (Result FileError String)))
(define (read-file-to-string path)
"Reads a file into a string, given a pathname string."
(let p = (the Pathname (into path)))
(lisp (Result FileError String) (p)
(%handle-file-function (uiop:read-file-string p))))
(declare read-file-lines ((Into :a Pathname) => :a -> (Result FileError (List String))))
(define (read-file-lines path)
"Reads a file into lines, given a pathname or string."
(let p = (the Pathname (into path)))
(lisp (Result FileError (List String)) (p)
(%handle-file-function (uiop:read-file-lines p)))))
#+sb-package-locks
(sb-ext:lock-package "COALTON-LIBRARY/FILE")