@@ -152,11 +152,21 @@ fopen : String -> String -> IO File;
152152fopen f m = do { h <- do_fopen f m;
153153 return (FHandle h); };
154154
155+data Mode = Read | Write | ReadWrite;
156+
157+openFile : String -> Mode -> IO File;
158+openFile f m = fopen f (modeStr m) where {
159+ modeStr : Mode -> String;
160+ modeStr Read = "r";
161+ modeStr Write = "w";
162+ modeStr ReadWrite = "r+";
163+}
164+
155165do_fclose : Ptr -> IO ();
156166do_fclose h = mkForeign (FFun "fileClose" [FPtr] FUnit) h;
157167
158−fclose : File -> IO ();
159−fclose (FHandle h) = do_fclose h;
168+closeFile : File -> IO ();
169+closeFile (FHandle h) = do_fclose h;
160170
161171do_fread : Ptr -> IO String;
162172do_fread h = mkForeign (FFun "freadStr" [FPtr] FString) h;
@@ -177,6 +187,23 @@ feof : File -> IO Bool;
177187feof (FHandle h) = do { eof <- do_feof h;
178188 return (not (eof == 0)); };
179189
180−
181−
190+while : |(test : IO Bool) -> |(body : IO ()) -> IO ();
191+while t b = do { v <- t;
192+ if v then (do { b; while t b; }) else return (); };
193+
194+readFile : String -> IO String;
195+readFile fn = do { h <- openFile fn Read;
196+ c <- readFile' h "";
197+ closeFile h;
198+ return c;
199+ }
200+ where {
201+ readFile' : File -> String -> IO String;
202+ readFile' h contents =
203+ do { x <- feof h;
204+ if (not x) then (do { l <- fread h;
205+ readFile' h (contents ++ l);
206+ })
207+ else (return contents); };
208+ }
182209