@@ -3,6 +3,7 @@ module Prelude.File
3import Builtins
3import Builtins
4import Prelude.List
4import Prelude.List
5import Prelude.Maybe
5import Prelude.Maybe
6import Prelude.Monad
7import Prelude.Monad
7import Prelude.Chars
8import Prelude.Chars
8import Prelude.Strings
9import Prelude.Strings
@@ -80,7 +81,7 @@ do_ferror h = foreign FFI_C "fileError" (Ptr -> IO Int) h
81ferror : File -> IO Bool
82ferror : File -> IO Bool
82ferror (FHandle h) = do err <- do_ferror h
83ferror (FHandle h) = do err <- do_ferror h
83− pure (not (err == 0))
84+ pure (err /= 0)
85||| Call the RTS's file opening function
86||| Call the RTS's file opening function
@@ -95,15 +96,13 @@ export
95fopen : (f : String) -> (m : String) -> IO (Either FileError File)
96fopen : (f : String) -> (m : String) -> IO (Either FileError File)
96fopen f m = do h <- do_fopen f m
97fopen f m = do h <- do_fopen f m
97 if !(nullPtr h)
98 if !(nullPtr h)
98− then do err <- getFileError
99+ then Left <$> getFileError
100 else pure (Right (FHandle h))
100 else pure (Right (FHandle h))
102||| Check whether a file handle is actually a null pointer
102||| Check whether a file handle is actually a null pointer
104validFile : File -> IO Bool
104validFile : File -> IO Bool
105−validFile (FHandle h) = do x <- nullPtr h
105+validFile (FHandle h) = not <$> nullPtr h
108||| Modes for opening files
107||| Modes for opening files
109data Mode = Read | WriteTruncate | Append | ReadWrite | ReadWriteTruncate | ReadAppend
108data Mode = Read | WriteTruncate | Append | ReadWrite | ReadWriteTruncate | ReadAppend
@@ -169,8 +168,7 @@ fileSize : File -> IO (Either FileError Int)
169fileSize (FHandle h)
168fileSize (FHandle h)
170 = do s <- do_getFileSize h
169 = do s <- do_getFileSize h
171 if (s < 0)
170 if (s < 0)
172− then do err <- getFileError
171+ then Left <$> getFileError
174 else pure (Right s)
172 else pure (Right s)
@@ -178,8 +176,7 @@ fileModifiedTime : File -> IO (Either FileError Integer)
178fileModifiedTime (FHandle h)
176fileModifiedTime (FHandle h)
179 = do s <- do_getFileModifiedTime h
177 = do s <- do_getFileModifiedTime h
180 if (s < 0)
178 if (s < 0)
181− then do err <- getFileError
179+ then Left <$> getFileError
183 else pure (Right s)
180 else pure (Right s)
@@ -187,8 +184,7 @@ fileAccessTime : File -> IO (Either FileError Integer)
187fileAccessTime (FHandle h)
184fileAccessTime (FHandle h)
188 = do s <- do_getFileAccessTime h
185 = do s <- do_getFileAccessTime h
189 if (s < 0)
186 if (s < 0)
190− then do err <- getFileError
187+ then Left <$> getFileError
192 else pure (Right s)
188 else pure (Right s)
@@ -196,11 +192,9 @@ fileStatusTime : File -> IO (Either FileError Integer)
196fileStatusTime (FHandle h)
192fileStatusTime (FHandle h)
197 = do s <- do_getFileStatusTime h
193 = do s <- do_getFileStatusTime h
198 if (s < 0)
194 if (s < 0)
199− then do err <- getFileError
195+ then Left <$> getFileError
201 else pure (Right s)
196 else pure (Right s)
205do_fread : Ptr -> IO' l String
199do_fread : Ptr -> IO' l String
206do_fread h = prim_fread h
200do_fread h = prim_fread h
@@ -264,8 +258,7 @@ do_fwrite h s = do res <- prim_fwrite h s
264 then do errno <- getErrno
258 then do errno <- getErrno
265 if errno == 0
259 if errno == 0
266 then pure (Left FileWriteError)
260 then pure (Left FileWriteError)
267− else do err <- getFileError
261+ else Left <$> getFileError
269 else pure (Right ())
262 else pure (Right ())
271||| Write a line to a file
264||| Write a line to a file
@@ -287,7 +280,7 @@ do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
288fEOF : File -> IO Bool
281fEOF : File -> IO Bool
289fEOF (FHandle h) = do eof <- do_feof h
282fEOF (FHandle h) = do eof <- do_feof h
290− pure (not (eof == 0))
283+ pure (eof /= 0)
293do_fremove : String -> IO Int
286do_fremove : String -> IO Int
@@ -317,7 +310,8 @@ readFile : (filepath : String) -> IO (Either FileError String)
317readFile fn = do Right h <- openFile fn Read
310readFile fn = do Right h <- openFile fn Read
318 | Left err => pure (Left err)
311 | Left err => pure (Left err)
319 Right max <- fileSize h
312 Right max <- fileSize h
313+ | Left err => do closeFile h
320− | Left err => pure (Left err)
314+ pure (Left err)
321 sb <- newStringBuffer (max + 1)
315 sb <- newStringBuffer (max + 1)
322 c <- readFile' h max sb
316 c <- readFile' h max sb
323 closeFile h
317 closeFile h
@@ -340,8 +334,11 @@ export
340writeFile : (filepath : String) -> (contents : String) ->
334writeFile : (filepath : String) -> (contents : String) ->
341 IO (Either FileError ())
335 IO (Either FileError ())
342writeFile fn contents = do
336writeFile fn contents = do
343− Right h <- openFile fn WriteTruncate | Left err => pure (Left err)
337+ Right h <- openFile fn WriteTruncate
338+ | Left err => pure (Left err)
344− Right () <- fPutStr h contents | Left err => pure (Left err)
339+ Right () <- fPutStr h contents
340+ | Left err => do closeFile h
345 closeFile h
342 closeFile h
346 pure (Right ())
343 pure (Right ())
@@ -351,8 +348,7 @@ dirOpen : (d : String) -> IO (Either FileError Directory)
352 = do dptr <- foreign FFI_C "idris_dirOpen" (String -> IO Ptr) d
349 = do dptr <- foreign FFI_C "idris_dirOpen" (String -> IO Ptr) d
353 if !(nullPtr dptr)
350 if !(nullPtr dptr)
354− then do err <- getFileError
351+ then Left <$> getFileError
356 else pure (Right (DHandle dptr))
352 else pure (Right (DHandle dptr))
@@ -363,7 +359,7 @@ export
363dirError : Directory -> IO Bool
359dirError : Directory -> IO Bool
364dirError (DHandle d)
360dirError (DHandle d)
365 = do err <- foreign FFI_C "idris_dirError" (Ptr -> IO Int) d
361 = do err <- foreign FFI_C "idris_dirError" (Ptr -> IO Int) d
366− pure (not (err == 0))
362+ pure (err /= 0)
369dirEntry : Directory -> IO (Either FileError String)
365dirEntry : Directory -> IO (Either FileError String)
@@ -379,8 +375,7 @@ createDir d
379 = do ok <- foreign FFI_C "idris_mkdir" (String -> IO Int) d
375 = do ok <- foreign FFI_C "idris_mkdir" (String -> IO Int) d
380 if (ok == 0)
376 if (ok == 0)
381 then pure (Right ())
377 then pure (Right ())
382− else do err <- getFileError
378+ else Left <$> getFileError
386changeDir : String -> IO Bool
381changeDir : String -> IO Bool