@@ -10,7 +10,8 @@ import Prelude.Cast
10import Prelude.Bool
10import Prelude.Bool
11import Prelude.Basics
11import Prelude.Basics
12import Prelude.Classes
12import Prelude.Classes
13−import Prelude.Monad
13+import Prelude.Either
16%access public
17%access public
@@ -19,6 +20,31 @@ import IO
20data File = FHandle Ptr
21data File = FHandle Ptr
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
30+ | GenericFileError Int -- errno
33+strError : Int -> String
34+strError err = unsafePerformIO -- yeah, yeah...
35+ (foreign FFI_C "idris_showerror" (Int -> IO String) err)
37+getFileError : IO FileError
38+getFileError = do MkRaw err <- foreign FFI_C "idris_mkFileError"
39+ (Ptr -> IO (Raw FileError)) prim__vm
42+instance Show FileError where
43+ show Success = "Success"
44+ show FileNotFound = "File Not Found"
45+ show PermissionDenied = "Permission Denied"
46+ show (GenericFileError errno) = strError errno
22||| Standard input
48||| Standard input
23stdin : File
49stdin : File
24stdin = FHandle prim__stdin
50stdin = FHandle prim__stdin
@@ -31,6 +57,13 @@ stdout = FHandle prim__stdout
31stderr : File
57stderr : File
32stderr = FHandle prim__stderr
58stderr = FHandle prim__stderr
60+do_ferror : Ptr -> IO Int
61+do_ferror h = foreign FFI_C "fileError" (Ptr -> IO Int) h
63+ferror : File -> IO Bool
64+ferror (FHandle h) = do err <- do_ferror h
65+ return (not (err == 0))
34||| Call the RTS's file opening function
67||| Call the RTS's file opening function
35do_fopen : String -> String -> IO Ptr
68do_fopen : String -> String -> IO Ptr
36do_fopen f m
69do_fopen f m
@@ -39,12 +72,14 @@ do_fopen f m
39||| Open a file
72||| Open a file
40||| @ f the filename
73||| @ f the filename
41||| @ m the mode as a String (`"r"`, `"w"`, or `"r+"`)
74||| @ m the mode as a String (`"r"`, `"w"`, or `"r+"`)
42−fopen : (f : String) -> (m : String) -> IO File
75+fopen : (f : String) -> (m : String) -> IO (Either FileError File)
43fopen f m = do h <- do_fopen f m
76fopen f m = do h <- do_fopen f m
78+ then do err <- getFileError
44− return (FHandle h)
79+ return (Left err)
80+ else return (Right (FHandle h))
46||| Check whether a file handle is actually a null pointer
82||| Check whether a file handle is actually a null pointer
48validFile : File -> IO Bool
83validFile : File -> IO Bool
49validFile (FHandle h) = do x <- nullPtr h
84validFile (FHandle h) = do x <- nullPtr h
50 return (not x)
85 return (not x)
@@ -55,33 +90,26 @@ data Mode = Read | Write | ReadWrite
55||| Open a file
90||| Open a file
56||| @ f the filename
91||| @ f the filename
57||| @ m the mode
92||| @ m the mode
59−openFile : (f : String) -> (m : Mode) -> IO File
93+openFile : (f : String) -> (m : Mode) -> IO (Either FileError File)
60openFile f m = fopen f (modeStr m) where
94openFile f m = fopen f (modeStr m) where
61 modeStr Read = "r"
95 modeStr Read = "r"
62 modeStr Write = "w"
96 modeStr Write = "w"
63 modeStr ReadWrite = "r+"
97 modeStr ReadWrite = "r+"
66do_fclose : Ptr -> IO ()
99do_fclose : Ptr -> IO ()
67do_fclose h = foreign FFI_C "fileClose" (Ptr -> IO ()) h
100do_fclose h = foreign FFI_C "fileClose" (Ptr -> IO ()) h
70closeFile : File -> IO ()
102closeFile : File -> IO ()
71closeFile (FHandle h) = do_fclose h
103closeFile (FHandle h) = do_fclose h
74do_fread : Ptr -> IO' l String
105do_fread : Ptr -> IO' l String
75do_fread h = prim_fread h
106do_fread h = prim_fread h
77−fgetc : File -> IO Char
108+fgetc : File -> IO (Either FileError Char)
78−fgetc (FHandle h) = return (cast !(foreign FFI_C "fgetc" (Ptr -> IO Int) h))
80−fgetc' : File -> IO (Maybe Char)
82− = do x <- foreign FFI_C "fgetc" (Ptr -> IO Int) h
109+fgetc (FHandle h) = do let c = cast !(foreign FFI_C "fgetc" (Ptr -> IO Int) h)
110+ if !(ferror (FHandle h))
83− if (x < 0) then return Nothing
111+ then return (Left FileReadError)
84− else return (Just (cast x))
112+ else return (Right c)
86fflush : File -> IO ()
114fflush : File -> IO ()
87fflush (FHandle h) = foreign FFI_C "fflush" (Ptr -> IO ()) h
115fflush (FHandle h) = foreign FFI_C "fflush" (Ptr -> IO ()) h
@@ -89,9 +117,12 @@ fflush (FHandle h) = foreign FFI_C "fflush" (Ptr -> IO ()) h
89do_popen : String -> String -> IO Ptr
117do_popen : String -> String -> IO Ptr
90do_popen f m = foreign FFI_C "do_popen" (String -> String -> IO Ptr) f m
118do_popen f m = foreign FFI_C "do_popen" (String -> String -> IO Ptr) f m
92−popen : String -> Mode -> IO File
120+popen : String -> Mode -> IO (Either FileError File)
93popen f m = do ptr <- do_popen f (modeStr m)
121popen f m = do ptr <- do_popen f (modeStr m)
123+ then do err <- getFileError
94− return (FHandle ptr)
125+ else return (Right (FHandle ptr))
96 modeStr Read = "r"
127 modeStr Read = "r"
97 modeStr Write = "w"
128 modeStr Write = "w"
@@ -103,20 +134,25 @@ pclose (FHandle h) = foreign FFI_C "pclose" (Ptr -> IO ()) h
103-- mkForeign (FFun "idris_readStr" [FPtr, FPtr] (FAny String))
134-- mkForeign (FFun "idris_readStr" [FPtr, FPtr] (FAny String))
104-- prim__vm h
135-- prim__vm h
107−fread : File -> IO' l String
137+fread : File -> IO (Either FileError String)
108−fread (FHandle h) = do_fread h
138+fread (FHandle h) = do str <- do_fread h
109−
139+ if !(ferror (FHandle h))
110−partial
140+ then return (Left FileReadError)
141+ else return (Right str)
111−do_fwrite : Ptr -> String -> IO ()
143+do_fwrite : Ptr -> String -> IO (Either FileError ())
112−do_fwrite h s = do prim_fwrite h s
144+do_fwrite h s = do res <- prim_fwrite h s
113− return ()
145+ if (res /= 0)
114−
146+ then do errno <- getErrno
115−partial
147+ if errno == 0
148+ then return (Left FileWriteError)
149+ else do err <- getFileError
151+ else return (Right ())
116−fwrite : File -> String -> IO ()
153+fwrite : File -> String -> IO (Either FileError ())
117fwrite (FHandle h) s = do_fwrite h s
154fwrite (FHandle h) s = do_fwrite h s
120do_feof : Ptr -> IO Int
156do_feof : Ptr -> IO Int
121do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
157do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
@@ -125,32 +161,27 @@ feof : File -> IO Bool
125feof (FHandle h) = do eof <- do_feof h
161feof (FHandle h) = do eof <- do_feof h
126 return (not (eof == 0))
162 return (not (eof == 0))
129−do_ferror : Ptr -> IO Int
130−do_ferror h = foreign FFI_C "fileError" (Ptr -> IO Int) h
132−ferror : File -> IO Bool
133−ferror (FHandle h) = do err <- do_ferror h
134− return (not (err == 0))
136fpoll : File -> IO Bool
164fpoll : File -> IO Bool
137fpoll (FHandle h) = do p <- foreign FFI_C "fpoll" (Ptr -> IO Int) h
165fpoll (FHandle h) = do p <- foreign FFI_C "fpoll" (Ptr -> IO Int) h
138 return (p > 0)
166 return (p > 0)
140||| Read the contents of a file into a string
168||| Read the contents of a file into a string
141−partial -- no error checking!
169+partial -- might be reading something infinitely long like /dev/null ...
142−readFile : String -> IO String
171+readFile : String -> IO (Either FileError String)
143−readFile fn = do h <- openFile fn Read
172+readFile fn = do Right h <- openFile fn Read
173+ | Left err => return (Left err)
144 c <- readFile' h ""
174 c <- readFile' h ""
145 closeFile h
175 closeFile h
149− readFile' : File -> String -> IO String
179+ readFile' : File -> String -> IO (Either FileError String)
150 readFile' h contents =
180 readFile' h contents =
151 do x <- feof h
181 do x <- feof h
152− if not x then do l <- fread h
182+ if not x then do Right l <- fread h
183+ | Left err => return (Left err)
153 readFile' h (contents ++ l)
184 readFile' h (contents ++ l)
154− else return contents
185+ else return (Right contents)