module LLVM.Internal.RawOStream where

import LLVM.Prelude

import Control.Monad.AnyCont
import Control.Monad.Catch
import Control.Monad.IO.Class

import Data.IORef
import Foreign.C
import Foreign.Ptr

import LLVM.Exception

import qualified LLVM.Internal.FFI.RawOStream as FFI
import qualified LLVM.Internal.FFI.PtrHierarchy as FFI

import LLVM.Internal.Coding
import LLVM.Internal.String ()

-- May throw 'FdStreamException'.
withFileRawOStream ::
  (MonadThrow m, MonadIO m, MonadAnyCont IO m)
  => String
  -> Bool
  -> Bool
  -> (Ptr FFI.RawOStream -> IO ())
  -> m ()
withFileRawOStream :: String -> Bool -> Bool -> (Ptr RawOStream -> IO ()) -> m ()
withFileRawOStream path :: String
path excl :: Bool
excl text :: Bool
text c :: Ptr RawOStream -> IO ()
c =
  String -> Bool -> Bool -> (Ptr RawPWriteStream -> IO ()) -> m ()
forall (m :: * -> *).
(MonadThrow m, MonadIO m, MonadAnyCont IO m) =>
String -> Bool -> Bool -> (Ptr RawPWriteStream -> IO ()) -> m ()
withFileRawPWriteStream String
path Bool
excl Bool
text (Ptr RawOStream -> IO ()
c (Ptr RawOStream -> IO ())
-> (Ptr RawPWriteStream -> Ptr RawOStream)
-> Ptr RawPWriteStream
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr RawPWriteStream -> Ptr RawOStream
forall a b. DescendentOf a b => Ptr b -> Ptr a
FFI.upCast)

-- May throw 'FdStreamException'.
withFileRawPWriteStream ::
  (MonadThrow m, MonadIO m, MonadAnyCont IO m)
  => String
  -> Bool
  -> Bool
  -> (Ptr FFI.RawPWriteStream -> IO ())
  -> m ()
withFileRawPWriteStream :: String -> Bool -> Bool -> (Ptr RawPWriteStream -> IO ()) -> m ()
withFileRawPWriteStream path :: String
path excl :: Bool
excl text :: Bool
text c :: Ptr RawPWriteStream -> IO ()
c = do
  CString
path <- String -> m CString
forall (e :: * -> *) h c. EncodeM e h c => h -> e c
encodeM String
path
  LLVMBool
excl <- Bool -> m LLVMBool
forall (e :: * -> *) h c. EncodeM e h c => h -> e c
encodeM Bool
excl
  LLVMBool
text <- Bool -> m LLVMBool
forall (e :: * -> *) h c. EncodeM e h c => h -> e c
encodeM Bool
text
  Ptr (OwnerTransfered CString)
msgPtr <- m (Ptr (OwnerTransfered CString))
forall a (m :: * -> *).
(Storable a, MonadAnyCont IO m) =>
m (Ptr a)
alloca
  Bool
succeeded <- LLVMBool -> m Bool
forall (d :: * -> *) h c. DecodeM d h c => c -> d h
decodeM (LLVMBool -> m Bool) -> m LLVMBool -> m Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (IO LLVMBool -> m LLVMBool
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO LLVMBool -> m LLVMBool) -> IO LLVMBool -> m LLVMBool
forall a b. (a -> b) -> a -> b
$ CString
-> LLVMBool
-> LLVMBool
-> Ptr (OwnerTransfered CString)
-> (Ptr RawPWriteStream -> IO ())
-> IO LLVMBool
FFI.withFileRawPWriteStream CString
path LLVMBool
excl LLVMBool
text Ptr (OwnerTransfered CString)
msgPtr Ptr RawPWriteStream -> IO ()
c)
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
succeeded (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    String
s <- Ptr (OwnerTransfered CString) -> m String
forall (d :: * -> *) h c. DecodeM d h c => c -> d h
decodeM Ptr (OwnerTransfered CString)
msgPtr
    FdStreamException -> m ()
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (FdStreamException -> m ()) -> FdStreamException -> m ()
forall a b. (a -> b) -> a -> b
$ String -> FdStreamException
FdStreamException String
s

withBufferRawOStream ::
  (MonadIO m, DecodeM IO a (Ptr CChar, CSize))
  => (Ptr FFI.RawOStream -> IO ())
  -> m a
withBufferRawOStream :: (Ptr RawOStream -> IO ()) -> m a
withBufferRawOStream c :: Ptr RawOStream -> IO ()
c = (Ptr RawPWriteStream -> IO ()) -> m a
forall (m :: * -> *) a.
(MonadIO m, DecodeM IO a (CString, CSize)) =>
(Ptr RawPWriteStream -> IO ()) -> m a
withBufferRawPWriteStream (Ptr RawOStream -> IO ()
c (Ptr RawOStream -> IO ())
-> (Ptr RawPWriteStream -> Ptr RawOStream)
-> Ptr RawPWriteStream
-> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr RawPWriteStream -> Ptr RawOStream
forall a b. DescendentOf a b => Ptr b -> Ptr a
FFI.upCast)

withBufferRawPWriteStream ::
  (MonadIO m, DecodeM IO a (Ptr CChar, CSize))
  => (Ptr FFI.RawPWriteStream -> IO ())
  -> m a
withBufferRawPWriteStream :: (Ptr RawPWriteStream -> IO ()) -> m a
withBufferRawPWriteStream c :: Ptr RawPWriteStream -> IO ()
c = IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ do
  IORef a
resultRef <- a -> IO (IORef a)
forall a. a -> IO (IORef a)
newIORef a
forall a. HasCallStack => a
undefined
  let saveBuffer :: Ptr CChar -> CSize -> IO ()
      saveBuffer :: CString -> CSize -> IO ()
saveBuffer start :: CString
start size :: CSize
size = do
        a
r <- (CString, CSize) -> IO a
forall (d :: * -> *) h c. DecodeM d h c => c -> d h
decodeM (CString
start, CSize
size)
        IORef a -> a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef a
resultRef a
r
  (CString -> CSize -> IO ())
-> (Ptr RawPWriteStream -> IO ()) -> IO ()
FFI.withBufferRawPWriteStream CString -> CSize -> IO ()
saveBuffer Ptr RawPWriteStream -> IO ()
c
  IORef a -> IO a
forall a. IORef a -> IO a
readIORef IORef a
resultRef