MicroHs-0.9.11.0: lib/System/IO/Extra.hs
module System.IO.Extra(openFileM, openTmpFile, splitTmp) where
import Foreign.C.String
splitTmp :: String -> (String, String)
splitTmp tmpl =
case span (/= '.') (reverse tmpl) of
(rsuf, "") -> (tmpl, "")
(rsuf, _:rpre) -> (reverse rpre, '.':reverse rsuf)
-- Create a temporary file, take a prefix and a suffix
-- and returns a malloc()ed string.
foreign import ccall "tmpname" c_tmpname :: CString -> CString -> IO CString
-- Create and open a temporary file.
openTmpFile :: String -> IO (String, Handle)
openTmpFile tmpl = do
let (pre, suf) = splitTmp tmpl
ctmp <- withCAString pre $ withCAString suf . c_tmpname
tmp <- peekCAString ctmp
free ctmp
h <- openFile tmp ReadWriteMode
return (tmp, h)