@@ -92,7 +92,7 @@ data Mode = Read | Write | ReadWrite
93||| Open a file
93||| Open a file
94||| @ f the filename
94||| @ f the filename
95−||| @ m the mode
95+||| @ m the mode; either Read, Write, or ReadWrite
96openFile : (f : String) -> (m : Mode) -> IO (Either FileError File)
96openFile : (f : String) -> (m : Mode) -> IO (Either FileError File)
97openFile f m = fopen f (modeStr m) where
97openFile f m = fopen f (modeStr m) where
98 modeStr Read = "r"
98 modeStr Read = "r"
@@ -143,6 +143,13 @@ fread (FHandle h) = do str <- do_fread h
143 then return (Left FileReadError)
143 then return (Left FileReadError)
144 else return (Right str)
144 else return (Right str)
146+||| Read a line from a file
147+||| @h a file handle which must be open for reading
148+fGetLine : (h : File) -> IO (Either FileError String)
151+%deprecate fread "Use fGetLine instead"
146do_fwrite : Ptr -> String -> IO (Either FileError ())
153do_fwrite : Ptr -> String -> IO (Either FileError ())
147do_fwrite h s = do res <- prim_fwrite h s
154do_fwrite h s = do res <- prim_fwrite h s
148 if (res /= 0)
155 if (res /= 0)
@@ -156,14 +163,31 @@ do_fwrite h s = do res <- prim_fwrite h s
156fwrite : File -> String -> IO (Either FileError ())
163fwrite : File -> String -> IO (Either FileError ())
157fwrite (FHandle h) s = do_fwrite h s
164fwrite (FHandle h) s = do_fwrite h s
166+||| Write a line to a file
167+||| @h a file handle which must be open for writing
168+||| @str the line to write to the file
169+fPutStr : (h : File) -> (str : String) -> IO (Either FileError ())
170+fPutStr (FHandle h) s = do_fwrite h s
172+fPutStrLn : File -> String -> IO (Either FileError ())
173+fPutStrLn (FHandle h) s = do_fwrite h (s ++ "\n")
175+%deprecate fwrite "Use fPutStr instead"
159do_feof : Ptr -> IO Int
177do_feof : Ptr -> IO Int
160do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
178do_feof h = foreign FFI_C "fileEOF" (Ptr -> IO Int) h
162||| Check if a file handle has reached the end
180||| Check if a file handle has reached the end
163−feof : File -> IO Bool
181+fEOF : File -> IO Bool
164−feof (FHandle h) = do eof <- do_feof h
182+fEOF (FHandle h) = do eof <- do_feof h
165 return (not (eof == 0))
183 return (not (eof == 0))
185+||| Check if a file handle has reached the end
186+feof : File -> IO Bool
189+%deprecate feof "Use fEOF instead"
167fpoll : File -> IO Bool
191fpoll : File -> IO Bool
168fpoll (FHandle h) = do p <- foreign FFI_C "fpoll" (Ptr -> IO Int) h
192fpoll (FHandle h) = do p <- foreign FFI_C "fpoll" (Ptr -> IO Int) h
169 return (p > 0)
193 return (p > 0)
@@ -181,8 +205,8 @@ readFile fn = do Right h <- openFile fn Read
182 readFile' : File -> String -> IO (Either FileError String)
206 readFile' : File -> String -> IO (Either FileError String)
183 readFile' h contents =
207 readFile' h contents =
184− do x <- feof h
208+ do x <- fEOF h
185− if not x then do Right l <- fread h
209+ if not x then do Right l <- fGetLine h
186 | Left err => return (Left err)
210 | Left err => return (Left err)
187 readFile' h (contents ++ l)
211 readFile' h (contents ++ l)
188 else return (Right contents)
212 else return (Right contents)
@@ -192,6 +216,6 @@ writeFile : (filepath : String) -> (contents : String) ->
192 IO (Either FileError ())
216 IO (Either FileError ())
193writeFile fn contents = do
217writeFile fn contents = do
194 Right h <- openFile fn Write | Left err => return (Left err)
218 Right h <- openFile fn Write | Left err => return (Left err)
195− Right () <- fwrite h contents | Left err => return (Left err)
219+ Right () <- fPutStr h contents | Left err => return (Left err)
196 closeFile h
220 closeFile h
197 return (Right ())
221 return (Right ())