[clean up file IO, fill out 'writeFile' and 'appendFile'
John Meacham <john@repetae.net>**20090822140908
 Ignore-this: e8fe38165e38a6c7eddf6f90ee0dc560
] hunk ./lib/base/System/IO.hs 48
+import System.C.Stdio
hunk ./lib/base/System/IO.hs 151
-    rc <- withHandle h $ fwrite p 1 count
+    rc <- withHandle h $ c_fwrite p 1 count
hunk ./lib/base/System/IO.hs 157
-    rc <- withHandle h $ fread p 1 count
+    rc <- withHandle h $ c_fread p 1 count
hunk ./lib/base/System/IO.hs 183
-foreign import ccall "stdio.h fwrite_unlocked" fwrite  :: Ptr a -> CSize -> CSize -> Ptr Handle -> IO CSize
-foreign import ccall "stdio.h fread_unlocked" fread :: Ptr a -> CSize -> CSize -> Ptr Handle -> IO CSize
hunk ./lib/base/System/IO.hs 185
+foreign import ccall "jhc_wait_for_input" c_wait_for_input :: FILE -> Int -> IO Bool
hunk ./lib/base/System/IO.hs 187
-foreign import ccall "stdio.h fflush" c_fflush :: Ptr Handle -> IO ()
-
-foreign import ccall "wchar.h jhc_utf8_getc" c_fgetwc :: Ptr Handle -> IO Int
-foreign import ccall "wchar.h jhc_utf8_putc" c_fputwc :: Int -> Ptr Handle -> IO Int
-
-foreign import ccall "jhc_wait_for_input" c_wait_for_input :: Ptr Handle -> Int -> IO Bool
-foreign import ccall "stdio.h feof" c_feof :: Ptr Handle -> IO CInt
-foreign import ccall "stdio.h ftell" c_ftell :: Ptr Handle -> IO IntMax                  -- XXX
-foreign import ccall "stdio.h fseek" c_fseek :: Ptr Handle -> IntMax -> CInt -> IO CInt  -- XXX
-
-foreign import primitive "const.SEEK_SET" c_SEEK_SET :: CInt
-foreign import primitive "const.SEEK_CUR" c_SEEK_CUR :: CInt
-foreign import primitive "const.SEEK_END" c_SEEK_END :: CInt
hunk ./lib/jhc/Jhc/Handle.hs 15
+import Foreign.C.Error
+import Foreign.C.String
+import Foreign.C.Types
+import Foreign.Marshal.Utils
hunk ./lib/jhc/Jhc/Handle.hs 21
-import Foreign.C.Types
-import Jhc.IO
hunk ./lib/jhc/Jhc/Handle.hs 22
-import Foreign.C.String
-import Foreign.Marshal.Utils
-import Foreign.C.Error
-import Jhc.Show
-import Jhc.Order
-import Jhc.Enum
hunk ./lib/jhc/Jhc/Handle.hs 23
-import Jhc.Monad
+import Jhc.Enum
+import Jhc.IO
hunk ./lib/jhc/Jhc/Handle.hs 26
+import Jhc.Monad
+import Jhc.Order
+import Jhc.Show
hunk ./lib/jhc/Jhc/Handle.hs 30
+import System.C.Stdio
+
hunk ./lib/jhc/Jhc/Handle.hs 37
+
hunk ./lib/jhc/Jhc/Handle.hs 40
-    handleFile :: !(Ptr (Ptr Handle)),
+    handleFile :: !(Ptr FILE),
hunk ./lib/jhc/Jhc/Handle.hs 50
-make_builtin mode name std = Handle { handleName = "(" ++ name ++ ")", handleFile = std, handleIOMode = mode }
+make_builtin mode name std = Handle { handleName = "(" ++ name ++ ")", handleFile = std, handleIOMode = mode, handleBinary = False }
hunk ./lib/jhc/Jhc/Handle.hs 62
-foreign import ccall "stdio.h &stdin" c_stdin :: Ptr (Ptr Handle)
-foreign import ccall "stdio.h &stdout" c_stdout :: Ptr (Ptr Handle)
-foreign import ccall "stdio.h &stderr" c_stderr :: Ptr (Ptr Handle)
+foreign import ccall "stdio.h &stdin" c_stdin :: Ptr FILE
+foreign import ccall "stdio.h &stdout" c_stdout :: Ptr FILE
+foreign import ccall "stdio.h &stderr" c_stderr :: Ptr FILE
hunk ./lib/jhc/Jhc/Handle.hs 106
-foreign import ccall "stdio.h fclose" c_fclose :: Ptr Handle -> IO CInt
-foreign import ccall "stdio.h fopen" c_fopen :: Ptr CChar -> Ptr CChar ->  IO (Ptr Handle)
-
hunk ./lib/jhc/Jhc/IO.hs 20
-    runNoWrapper,
hunk ./lib/jhc/Jhc/IO.hs 51
-fromUIO_ f = IO $ \w -> (# f w, () #) 
+fromUIO_ f = IO $ \w -> (# f w, () #)
hunk ./lib/jhc/Prelude/IO.hs 23
+import System.C.Stdio
hunk ./lib/jhc/Prelude/IO.hs 34
-
hunk ./lib/jhc/Prelude/IO.hs 36
-type  FilePath = String
-
+type FilePath = String
hunk ./lib/jhc/Prelude/IO.hs 70
-    if  (file == nullPtr) then (fail "Could not open file.") else do
+    if  (file == nullPtr) then (fail $ "Could not open file:" ++ fn) else do
hunk ./lib/jhc/Prelude/IO.hs 79
-foreign import ccall "stdio.h fopen" c_fopen :: CString -> CString -> IO (Ptr ())
-foreign import ccall "stdio.h fclose" c_fclose :: Ptr () -> IO CInt
-foreign import ccall "wchar.h jhc_utf8_getc" c_fgetwc :: Ptr () -> IO Int
-
hunk ./lib/jhc/Prelude/IO.hs 97
+writeFile'  :: FilePath -> String -> Addr__ -> IO ()
+writeFile' fn s mode = do
+    file <- withCString fn $ \fnc -> c_fopen fnc (Ptr mode)
+    if  (file == nullPtr) then (fail $ "Could not open file: " ++ fn) else do
+        mapM_ (flip c_fputwc file . ord) s
+        c_fclose file
+        return ()
hunk ./lib/jhc/Prelude/IO.hs 106
-writeFile  =  error "writeFile"
+writeFile fn s = writeFile' fn s "w"#
hunk ./lib/jhc/Prelude/IO.hs 108
-appendFile :: FilePath -> String -> IO ()
-appendFile =  error "appendFile"
+appendFile  :: FilePath -> String -> IO ()
+appendFile fn s = writeFile' fn s "a"#
hunk ./lib/jhc/Prelude/IO.hs 129
-foreign import ccall "stdio.h jhc_utf8_putchar" c_putwchar :: Int -> IO ()
-foreign import ccall "wchar.h jhc_utf8_getchar" c_getwchar :: IO Int
adddir ./lib/jhc/System/C
addfile ./lib/jhc/System/C/Stdio.hs
hunk ./lib/jhc/System/C/Stdio.hs 1
+{-# OPTIONS_JHC -N -fffi #-}
+module System.C.Stdio where
+
+import Foreign.C.Types
+import Foreign.Ptr
+import Jhc.Basics
+import Data.Int
+
+type FILE = Ptr CFile
+
+foreign import ccall "stdio.h fopen" c_fopen               :: Ptr CChar -> Ptr CChar -> IO FILE
+foreign import ccall "stdio.h fclose" c_fclose             :: FILE -> IO CInt
+foreign import ccall "stdio.h jhc_utf8_putchar" c_putwchar :: Int -> IO ()
+foreign import ccall "wchar.h jhc_utf8_getc" c_fgetwc      :: FILE -> IO Int
+foreign import ccall "wchar.h jhc_utf8_getchar" c_getwchar :: IO Int
+foreign import ccall "wchar.h jhc_utf8_putc" c_fputwc      :: Int -> FILE -> IO Int
+foreign import ccall "stdio.h fwrite_unlocked" c_fwrite    :: Ptr a -> CSize -> CSize -> FILE -> IO CSize
+foreign import ccall "stdio.h fread_unlocked" c_fread      :: Ptr a -> CSize -> CSize -> FILE -> IO CSize
+foreign import ccall "stdio.h fflush" c_fflush             :: FILE -> IO ()
+foreign import ccall "stdio.h feof" c_feof                 :: FILE -> IO Int
+foreign import ccall "stdio.h ftell" c_ftell               :: FILE -> IO IntMax
+foreign import ccall "stdio.h fseek" c_fseek               :: FILE -> IntMax -> CInt -> IO Int
+foreign import ccall "stdio.h fileno" c_fileno             :: FILE -> IO Int
+foreign import primitive "const.SEEK_SET" c_SEEK_SET :: CInt
+foreign import primitive "const.SEEK_CUR" c_SEEK_CUR :: CInt
+foreign import primitive "const.SEEK_END" c_SEEK_END :: CInt
+foreign import primitive "const._IOFBF" c__IOFBF :: CInt
+foreign import primitive "const._IOLBF" c__IOLBF :: CInt
+foreign import primitive "const._IONBF" c__IONBF :: CInt
hunk ./lib/jhc/jhc.cabal 52
+        System.C.Stdio,