@@ -10,7 +10,8 @@ import Prelude.Cast
1010import Prelude.Bool
1111import Prelude.Basics
1212import Prelude.Classes
13−import Prelude.Monad
13+import Prelude.Either
14+import Prelude.Show
1415import IO
1516
1617%access public
@@ -19,6 +20,30 @@ import IO
1920abstract
2021data File = FHandle Ptr
2122
23+||| An error from a file operation
24+-- This is built in idris_mkFileError() in rts/idris_stdfgn.c. Make sure
25+-- the values correspond!
26+data FileError = FileReadError
27+ | FileWriteError
28+ | FileNotFound
29+ | PermissionDenied
30+ | GenericFileError Int -- errno
31+
32+private
33+strError : Int -> String
34+strError err = unsafePerformIO -- yeah, yeah...
35+ (foreign FFI_C "idris_showerror" (Int -> IO String) err)
36+
37+getFileError : IO FileError
38+getFileError = do MkRaw err <- foreign FFI_C "idris_mkFileError"
39+ (Ptr -> IO (Raw FileError)) prim__vm
40+ return err
41+
42+instance Show FileError where
43+ show FileNotFound = "File Not Found"
44+ show PermissionDenied = "Permission Denied"
45+ show (GenericFileError errno) = strError errno
46+
2247||| Standard input
2348stdin : File
2449stdin = FHandle prim__stdin
@@ -31,6 +56,13 @@ stdout = FHandle prim__stdout
3156stderr : File
3257stderr = FHandle prim__stderr
3358
59+do_ferror : Ptr -> IO Int
60+do_ferror h = foreign FFI_C "fileError" (Ptr -> IO Int) h
61+
62+ferror : File -> IO Bool
63+ferror (FHandle h) = do err <- do_ferror h
64+ return (not (err == 0))
65+
3466||| Call the RTS's file opening function
3567do_fopen : String -> String -> IO Ptr
3668do_fopen f m
@@ -39,12 +71,14 @@ do_fopen f m
3971||| Open a file
4072||| @ f the filename
4173||| @ m the mode as a String (`"r"`, `"w"`, or `"r+"`)
42−fopen : (f : String) -> (m : String) -> IO File
74+fopen : (f : String) -> (m : String) -> IO (Either FileError File)
4375fopen f m = do h <- do_fopen f m
44− return (FHandle h)
76+ if !(nullPtr h)
77+ then do err <- getFileError
78+ return (Left err)
79+ else return (Right (FHandle h))
4580
4681||| Check whether a file handle is actually a null pointer
47−partial
4882validFile : File -> IO Bool
4983validFile (FHandle h) = do x <- nullPtr h
5084 return (not x)
@@ -55,33 +89,26 @@ data Mode = Read | Write | ReadWrite
5589||| Open a file
5690||| @ f the filename
5791||| @ m the mode
58−partial
59−openFile : (f : String) -> (m : Mode) -> IO File
92+openFile : (f : String) -> (m : Mode) -> IO (Either FileError File)
6093openFile f m = fopen f (modeStr m) where
6194 modeStr Read = "r"
6295 modeStr Write = "w"
6396 modeStr ReadWrite = "r+"
6497
65−partial
6698do_fclose : Ptr -> IO ()
6799do_fclose h = foreign FFI_C "fileClose" (Ptr -> IO ()) h
68100
69−partial
70101closeFile : File -> IO ()
71102closeFile (FHandle h) = do_fclose h
72103
73−partial
74104do_fread : Ptr -> IO' l String
75105do_fread h = prim_fread h
76106
77−fgetc : File -> IO Char
78−fgetc (FHandle h) = return (cast !(foreign FFI_C "fgetc" (Ptr -> IO Int) h))
79−
80−fgetc' : File -> IO (Maybe Char)
81−fgetc' (FHandle h)
82− = do x <- foreign FFI_C "fgetc" (Ptr -> IO Int) h
83− if (x < 0) then return Nothing
84− else return (Just (cast x))
107+fgetc : File -> IO (Either FileError Char)
108+fgetc (FHandle h) = do let c = cast !(foreign FFI_C "fgetc" (Ptr -> IO Int) h)
109+ if !(ferror (FHandle h))
110+ then return (Left FileReadError)
111+ else return (Right c)
85112
86113fflush : File -> IO ()
87114fflush (FHandle h) = foreign FFI_C "fflush" (Ptr -> IO ()) h
@@ -89,9 +116,12 @@ fflush (FHandle h) = foreign FFI_C "fflush" (Ptr -> IO ()) h
89116do_popen : String -> String -> IO Ptr
90117do_popen f m = foreign FFI_C "do_popen" (String -> String -> IO Ptr) f m
91118
92−popen : String -> Mode -> IO File
119+popen : String -> Mode -> IO (Either FileError File)
93120popen f m = do ptr <- do_popen f (modeStr m)
94− return (FHandle ptr)
121+ if !(nullPtr ptr)
122+ then do err <- getFileError
123+ return (Left err)
124+ else return (Right (FHandle ptr))
95125 where
96126 modeStr Read = "r"
97127 modeStr Write = "w"
@@ -103,20 +133,25 @@ pclose (FHandle h) = foreign FFI_C "pclose" (Ptr -> IO ()) h
103133-- mkForeign (FFun "idris_readStr" [FPtr, FPtr] (FAny String))
104134-- prim__vm h
105135
106−partial
107−fread : File -> IO' l String
108−fread (FHandle h) = do_fread h
109−
110−partial
111−do_fwrite : Ptr -> String -> IO ()
112−do_fwrite h s = do prim_fwrite h s
113− return ()
114−
115−partial
116−fwrite : File -> String -> IO ()
136+fread : File -> IO (Either FileError String)
137+fread (FHandle h) = do str <- do_fread h
138+ if !(ferror (FHandle h))
139+ then return (Left FileReadError)
140+ else return (Right str)
141+
142+do_fwrite : Ptr -> String -> IO (Either FileError ())
143+do_fwrite h s = do res <- prim_fwrite h s
144+ if (res /= 0)
145+ then do errno <- getErrno
146+ if errno == 0
147+ then return (Left FileWriteError)
148+ else do err <- getFileError
149+ return (Left err)
150+ else return (Right ())
151+
152+fwrite : File -> String -> IO (Either FileError ())
117153fwrite (FHandle h) s = do_fwrite h s
118154
119−partial
120155do_feof : Ptr -> IO Int
121156do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
122157
@@ -125,32 +160,34 @@ feof : File -> IO Bool
125160feof (FHandle h) = do eof <- do_feof h
126161 return (not (eof == 0))
127162
128−partial
129−do_ferror : Ptr -> IO Int
130−do_ferror h = foreign FFI_C "fileError" (Ptr -> IO Int) h
131−
132−ferror : File -> IO Bool
133−ferror (FHandle h) = do err <- do_ferror h
134− return (not (err == 0))
135−
136163fpoll : File -> IO Bool
137164fpoll (FHandle h) = do p <- foreign FFI_C "fpoll" (Ptr -> IO Int) h
138165 return (p > 0)
139166
140167||| Read the contents of a file into a string
141−partial -- no error checking!
142−readFile : String -> IO String
143−readFile fn = do h <- openFile fn Read
168+partial -- might be reading something infinitely long like /dev/null ...
169+covering
170+readFile : String -> IO (Either FileError String)
171+readFile fn = do Right h <- openFile fn Read
172+ | Left err => return (Left err)
144173 c <- readFile' h ""
145174 closeFile h
146175 return c
147176 where
148177 partial
149− readFile' : File -> String -> IO String
178+ readFile' : File -> String -> IO (Either FileError String)
150179 readFile' h contents =
151180 do x <- feof h
152− if not x then do l <- fread h
181+ if not x then do Right l <- fread h
182+ | Left err => return (Left err)
153183 readFile' h (contents ++ l)
154− else return contents
155−
156−
184+ else return (Right contents)
185+
186+||| Write a string to a file
187+writeFile : (filepath : String) -> (contents : String) ->
188+ IO (Either FileError ())
189+writeFile fn contents = do
190+ Right h <- openFile fn Write | Left err => return (Left err)
191+ Right () <- fwrite h contents | Left err => return (Left err)
192+ closeFile h
193+ return (Right ())