0 | module Control.App.FileIO
8 | toFileEx : FileError -> FileEx
9 | toFileEx (GenericFileError i) = GenericFileEx i
10 | toFileEx FileReadError = FileReadError
11 | toFileEx FileWriteError = FileWriteError
12 | toFileEx FileNotFound = FileNotFound
13 | toFileEx PermissionDenied = PermissionDenied
14 | toFileEx FileExists = FileExists
17 | interface Has [Exception IOError] e => FileIO e where
18 | withFile : String -> Mode ->
19 | (onError : IOError -> App e a) ->
20 | (onOpen : File -> App e a) ->
22 | fGetStr : File -> App e String
23 | fGetChars : File -> Int -> App e String
24 | fGetChar : File -> App e Char
25 | fPutStr : File -> String -> App e ()
26 | fPutStrLn : File -> String -> App e ()
27 | fflush : File -> App e ()
28 | fEOF : File -> App e Bool
33 | readFile : FileIO e => String -> App e String
35 | = withFile f Read throw $
\h =>
36 | do content <- read [] h
37 | pure (fastConcat content)
39 | read : List String -> File -> App e (List String)
41 | = do eof <- FileIO.fEOF h
43 | then pure (reverse acc)
44 | else do str <- fGetStr h
47 | fileOp : IO (Either FileError a) -> Has [PrimIO, Exception IOError] e => App e a
49 | = do Right res <- primIO $
fileRes
50 | | Left err => throw (FileErr (toFileEx err))
54 | Has [PrimIO, Exception IOError] e => FileIO e where
55 | withFile fname m onError proc
56 | = do Right h <- primIO $
openFile fname m
57 | | Left err => onError (FileErr (toFileEx err))
58 | res <- catch (proc h) onError
59 | primIO $
closeFile h
62 | fGetStr f = fileOp (fGetLine f)
64 | fGetChars f n = fileOp (fGetChars f n)
66 | fGetChar f = fileOp (fGetChar f)
68 | fPutStr f str = fileOp (fPutStr f str)
70 | fPutStrLn f str = fileOp (File.ReadWrite.fPutStrLn f str)
72 | fflush f = primIO $
fflush f
74 | fEOF f = primIO $
fEOF f
77 | withFileIO : Has [PrimIO] e =>
78 | App (IOError :: e) a ->
79 | (ok : a -> App e b) ->
80 | (err : IOError -> App e b) -> App e b
81 | withFileIO prog ok err = handle prog ok err