0 | module System.File.ReadWrite
2 | import public Data.Fuel
6 | import System.File.Handle
7 | import public System.File.Error
8 | import System.File.Support
9 | import public System.File.Types
14 | %foreign supportC "idris2_seekLine"
15 | supportNode "seekLine"
16 | prim__seekLine : FilePtr -> PrimIO Int
18 | %foreign supportC "idris2_readLine"
19 | supportNode "readLine"
20 | prim__readLine : FilePtr -> PrimIO (Ptr String)
22 | %foreign supportC "idris2_readChars"
23 | prim__readChars : Int -> FilePtr -> PrimIO (Ptr String)
24 | %foreign "C:fgetc,libc 6"
25 | prim__readChar : FilePtr -> PrimIO Int
27 | %foreign supportC "idris2_writeLine"
28 | "node:lambda:(filePtr, line) => require('fs').writeSync(filePtr.fd, line, undefined, 'utf-8')"
29 | prim__writeLine : FilePtr -> String -> PrimIO Int
31 | %foreign supportC "idris2_eof"
32 | "node:lambda:f=>(f.eof?1:0)"
33 | prim__eof : FilePtr -> PrimIO Int
35 | %foreign supportC "idris2_removeFile"
36 | supportNode "removeFile"
37 | prim__removeFile : String -> PrimIO Int
46 | fSeekLine : HasIO io => (h : File) -> io (Either FileError ())
47 | fSeekLine (FHandle f)
48 | = do res <- primIO (prim__seekLine f)
56 | getStringAndFree : HasIO io => File -> Ptr String -> io (Either FileError String)
57 | getStringAndFree f res
58 | = if prim__nullPtr res /= 0
59 | then if !(fileError f) then returnError else pure @{Compose} ""
60 | else do let s = prim__getString res
61 | free $
prim__forgetPtr res
70 | fGetLine : HasIO io => (h : File) -> io (Either FileError String)
71 | fGetLine ff@(FHandle f)
72 | = do res <- primIO (prim__readLine f)
73 | getStringAndFree ff res
80 | fGetChars : HasIO io => (h : File) -> (max : Int) -> io (Either FileError String)
81 | fGetChars ff@(FHandle f) max
82 | = do res <- primIO (prim__readChars max f)
83 | getStringAndFree ff res
89 | fGetChar : HasIO io => (h : File) -> io (Either FileError Char)
90 | fGetChar h@(FHandle f)
91 | = do c <- primIO (prim__readChar f)
102 | fPutStr : HasIO io => (h : File) -> (str : String) -> io (Either FileError ())
103 | fPutStr (FHandle f) str
104 | = do res <- primIO (prim__writeLine f str)
114 | fPutStrLn : HasIO io => (fh : File) -> (str : String) -> io (Either FileError ())
115 | fPutStrLn fh str = fPutStr fh (str ++ "\n")
121 | fEOF : HasIO io => (h : File) -> io Bool
123 | = do res <- primIO (prim__eof f)
129 | fRead : HasIO io => (h : File) -> io (Either FileError String)
130 | fRead h = fRead' h [<]
132 | fRead' : HasIO io' => (h : File) -> (acc : SnocList String) -> io' (Either FileError String)
135 | then pure $
Right $
concat acc
137 | Right line <- fGetLine h
138 | | Left err => pure $
Left err
139 | fRead' h $
acc :< line
145 | removeFile : HasIO io => (fname : String) -> io (Either FileError ())
147 | = do res <- primIO (prim__removeFile fname)
166 | readLinesOnto : HasIO io => (acc : List String) ->
170 | io (Either FileError (Bool, List String))
171 | readLinesOnto acc _ Dry h = pure (Right (False, reverse acc))
172 | readLinesOnto acc offset (More fuel) h
173 | = do False <- fEOF h
174 | | True => pure $
Right (True, reverse acc)
176 | (S offset') => (fSeekLine h *> readLinesOnto acc offset' (More fuel) h) @{Applicative.Compose}
177 | 0 => (fGetLine h >>= \str => readLinesOnto (str :: acc) 0 fuel h) @{Monad.Compose}
203 | readFilePage : HasIO io => (offset : Nat) -> (until : Fuel) -> (fname : String) ->
204 | io (Either FileError (Bool, List String))
205 | readFilePage offset fuel fname
206 | = withFile fname Read pure $
207 | readLinesOnto [] offset fuel
215 | readFile : HasIO io => (fname : String) -> io (Either FileError String)
216 | readFile = (map $
map (fastConcat . snd)) . readFilePage 0 forever
225 | writeFile : HasIO io =>
226 | (filePath : String) -> (contents : String) ->
227 | io (Either FileError ())
228 | writeFile file contents
229 | = withFile file WriteTruncate pure $
230 | flip fPutStr contents
238 | appendFile : HasIO io =>
239 | (filePath : String) -> (contents : String) ->
240 | io (Either FileError ())
241 | appendFile file contents
242 | = withFile file Append pure $
243 | flip fPutStr contents