mirror of
https://github.com/edwinb/Idris2-boot.git
synced 2024-11-28 05:32:03 +03:00
Reading/writing buffers can fail
So, make them return and Either and wrap the scheme definitions in an exception handler that returns an error code on failure
This commit is contained in:
parent
bf69b89b0d
commit
78e44a4353
@ -75,15 +75,21 @@ bufferData buf
|
||||
unpackTo (val :: acc) (loc - 1)
|
||||
|
||||
export
|
||||
readBufferFromFile : BinaryFile -> Buffer -> (maxbytes : Int) -> IO Buffer
|
||||
readBufferFromFile : BinaryFile -> Buffer -> (maxbytes : Int) ->
|
||||
IO (Either FileError Buffer)
|
||||
readBufferFromFile (FHandle h) (MkBuffer buf size loc) max
|
||||
= do read <- schemeCall Int "blodwen-readbuffer" [h, buf, loc, max]
|
||||
pure (MkBuffer buf size (loc + read))
|
||||
if read >= 0
|
||||
then pure (Right (MkBuffer buf size (loc + read)))
|
||||
else pure (Left FileReadError)
|
||||
|
||||
export
|
||||
writeBufferToFile : BinaryFile -> Buffer -> (maxbytes : Int) -> IO Buffer
|
||||
writeBufferToFile : BinaryFile -> Buffer -> (maxbytes : Int) ->
|
||||
IO (Either FileError Buffer)
|
||||
writeBufferToFile (FHandle h) (MkBuffer buf size loc) max
|
||||
= do let maxwrite = size - loc
|
||||
let max' = if maxwrite < max then maxwrite else max
|
||||
schemeCall () "blodwen-writebuffer" [h, buf, loc, max']
|
||||
pure (MkBuffer buf size (loc + max'))
|
||||
written <- schemeCall Int "blodwen-writebuffer" [h, buf, loc, max']
|
||||
if written == max'
|
||||
then pure (Right (MkBuffer buf size (loc + max')))
|
||||
else pure (Left FileWriteError)
|
||||
|
@ -85,10 +85,13 @@
|
||||
(utf8->string newvec)))
|
||||
|
||||
(define (blodwen-readbuffer h buf loc max)
|
||||
(get-bytevector-n! h buf loc max))
|
||||
(guard (x (#t -1))
|
||||
(get-bytevector-n! h buf loc max)))
|
||||
|
||||
(define (blodwen-writebuffer h buf loc max)
|
||||
(put-bytevector h buf loc max))
|
||||
(guard (x (#t -1))
|
||||
(put-bytevector h buf loc max)
|
||||
max))
|
||||
|
||||
;; Files: Much of the following adapted from idris-chez, thanks to Niklas
|
||||
;; Larsson
|
||||
|
@ -85,10 +85,14 @@
|
||||
(utf8->string newvec)))
|
||||
|
||||
(define (blodwen-readbuffer h buf loc max)
|
||||
(get-bytevector-n! h buf loc max))
|
||||
(with-handlers
|
||||
([(lambda (x) #t) (lambda (exn) -1)])
|
||||
(get-bytevector-n! h buf loc max)))
|
||||
|
||||
(define (blodwen-writebuffer h buf loc max)
|
||||
(put-bytevector h buf loc max))
|
||||
(with-handlers
|
||||
([(lambda (x) #t) (lambda (exn) -1)])
|
||||
(put-bytevector h buf loc max)))
|
||||
|
||||
;; Files : Much of the following adapted from idris-chez, thanks to Niklas
|
||||
;; Larsson
|
||||
|
@ -27,13 +27,27 @@ main
|
||||
|
||||
Right f <- openBinaryFile "test.buf" WriteTruncate
|
||||
| Left err => putStrLn "File error on write"
|
||||
writeBufferToFile f buf 100
|
||||
Right _ <- writeBufferToFile f buf 100
|
||||
| Left err => do putStrLn "Buffer write fail"
|
||||
closeFile f
|
||||
closeFile f
|
||||
|
||||
Right f <- openBinaryFile "test.buf" Read
|
||||
| Left err => putStrLn "File error on read"
|
||||
buf2 <- newBuffer 100
|
||||
readBufferFromFile f buf2 100
|
||||
Right _ <- readBufferFromFile f buf2 100
|
||||
| Left err => do putStrLn "Buffer read fail"
|
||||
closeFile f
|
||||
closeFile f
|
||||
|
||||
ds <- bufferData buf2
|
||||
printLn ds
|
||||
|
||||
Right f <- openBinaryFile "test.buf" Read
|
||||
| Left err => putStrLn "File error on read"
|
||||
buf3 <- newBuffer 99
|
||||
Right _ <- readBufferFromFile f buf3 100
|
||||
| Left err => do putStrLn "Buffer read fail"
|
||||
closeFile f
|
||||
closeFile f
|
||||
|
||||
|
@ -5,5 +5,6 @@
|
||||
"there!"
|
||||
[0, 94, 0, 0, 0, 0, 0, 0, 0, 0, 123, 20, 174, 71, 225, 154, 87, 64, 0, 0, 72, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
[0, 94, 0, 0, 0, 0, 0, 0, 0, 0, 123, 20, 174, 71, 225, 154, 87, 64, 0, 0, 72, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
||||
Buffer read fail
|
||||
1/1: Building Buffer (Buffer.idr)
|
||||
Main> Main> Bye for now!
|
||||
|
Loading…
Reference in New Issue
Block a user