123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- module System.IO.Uniform.File (
- FileIO,
- openFile
- ) where
- import System.IO.Uniform
- import System.IO.Uniform.External
- import Foreign
- import Foreign.C.String
- import Foreign.C.Error
- import qualified Data.ByteString as BS
- import System.Posix.Types (Fd(..))
- -- | UniformIO type for file IO.
- instance UniformIO FileIO where
- uRead s n = do
- allocaArray n (
- \b -> do
- count <- c_recv (fd s) b $ fromIntegral n
- if count < 0
- then throwErrno "could not read"
- else BS.packCStringLen (b, fromIntegral count)
- )
- uPut s t = do
- BS.useAsCStringLen t (
- \(str, n) -> do
- count <- c_send (fd s) str $ fromIntegral n
- if count < 0
- then throwErrno "could not write"
- else return ()
- )
- uClose s = do
- f <- Fd <$> c_prepareToClose (fd s)
- closeFd f
- startTls _ f = return f
- isSecure _ = True
-
-
- -- | Open a file for bidirectional IO.
- openFile :: String -> IO FileIO
- openFile fileName = do
- r <- withCString fileName (
- \f -> fmap FileIO $ c_createFile f
- )
- if fd r == nullPtr
- then throwErrno "could not open file"
- else return r
|