-- GENERATED by C->Haskell Compiler, version 0.28.7 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "lib/CPython/Types/ByteArray.chs" #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-- Copyright (C) 2009 John Millikin <jmillikin@gmail.com>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

module CPython.Types.ByteArray
  ( ByteArray
  , byteArrayType
  , toByteArray
  , fromByteArray
  , fromObject
  , append
  , length
  , resize
  ) where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified System.IO.Unsafe as C2HSImp





import           Prelude hiding (length)
import qualified Data.ByteString as B
import qualified Data.ByteString.Unsafe as B

import           CPython.Internal

newtype ByteArray = ByteArray (ForeignPtr ByteArray)

instance Object ByteArray where
  toObject :: ByteArray -> SomeObject
toObject (ByteArray x :: ForeignPtr ByteArray
x) = ForeignPtr ByteArray -> SomeObject
forall a. Object a => ForeignPtr a -> SomeObject
SomeObject ForeignPtr ByteArray
x
  fromForeignPtr :: ForeignPtr ByteArray -> ByteArray
fromForeignPtr = ForeignPtr ByteArray -> ByteArray
ByteArray

instance Concrete ByteArray where
  concreteType :: ByteArray -> Type
concreteType _ = Type
byteArrayType

byteArrayType :: (Type)
byteArrayType =
  C2HSImp.unsafePerformIO $
  byteArrayType'_ IO (Ptr ()) -> (Ptr () -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO Type
forall obj a. Object obj => Ptr a -> IO obj
peekStaticObject Ptr ()
res IO Type -> (Type -> IO Type) -> IO Type
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: Type
res' ->
  Type -> IO Type
forall (m :: * -> *) a. Monad m => a -> m a
return (Type
res')

{-# LINE 47 "lib/CPython/Types/ByteArray.chs" #-}


toByteArray :: B.ByteString -> IO ByteArray
toByteArray bytes = let
  mkByteArray = pyByteArrayFromStringAndSize
{-# LINE 51 "lib/CPython/Types/ByteArray.chs" #-}

  in B.unsafeUseAsCStringLen bytes $ \(cstr, len) ->
     stealObject =<< mkByteArray cstr (fromIntegral len)

fromByteArray :: ByteArray -> IO B.ByteString
fromByteArray py =
  withObject py $ \pyPtr -> do
  size' <- pyByteArraySize pyPtr
  bytes <- pyByteArrayAsString pyPtr
  B.packCStringLen (bytes, fromIntegral size')

-- | Create a new byte array from any object which implements the buffer
-- protocol.
fromObject :: Object self  => (self) -> IO ((ByteArray))
fromObject :: self -> IO ByteArray
fromObject a1 :: self
a1 =
  self -> (Ptr () -> IO ByteArray) -> IO ByteArray
forall obj a b. Object obj => obj -> (Ptr a -> IO b) -> IO b
withObject self
a1 ((Ptr () -> IO ByteArray) -> IO ByteArray)
-> (Ptr () -> IO ByteArray) -> IO ByteArray
forall a b. (a -> b) -> a -> b
$ \a1' :: Ptr ()
a1' -> 
  Ptr () -> IO (Ptr ())
fromObject'_ Ptr ()
a1' IO (Ptr ()) -> (Ptr () -> IO ByteArray) -> IO ByteArray
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res :: Ptr ()
res ->
  Ptr () -> IO ByteArray
forall obj a. Object obj => Ptr a -> IO obj
stealObject Ptr ()
res IO ByteArray -> (ByteArray -> IO ByteArray) -> IO ByteArray
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \res' :: ByteArray
res' ->
  ByteArray -> IO ByteArray
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteArray
res')

{-# LINE 67 "lib/CPython/Types/ByteArray.chs" #-}


append :: (ByteArray) -> (ByteArray) -> IO ((ByteArray))
append a1 a2 =
  withObject a1 $ \a1' -> 
  withObject a2 $ \a2' -> 
  append'_ a1' a2' >>= \res ->
  stealObject res >>= \res' ->
  return (res')

{-# LINE 72 "lib/CPython/Types/ByteArray.chs" #-}


length :: (ByteArray) -> IO ((Integer))
length a1 =
  withObject a1 $ \a1' -> 
  length'_ a1' >>= \res ->
  checkIntReturn res >>= \res' ->
  return (res')

{-# LINE 76 "lib/CPython/Types/ByteArray.chs" #-}


resize :: (ByteArray) -> (Integer) -> IO ((()))
resize a1 a2 =
  withObject a1 $ \a1' -> 
  let {a2' = fromIntegral a2} in 
  resize'_ a1' a2' >>= \res ->
  checkStatusCode res >>= \res' ->
  return (res')

{-# LINE 81 "lib/CPython/Types/ByteArray.chs" #-}


foreign import ccall unsafe "CPython/Types/ByteArray.chs.h hscpython_PyByteArray_Type"
  byteArrayType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_FromStringAndSize"
  pyByteArrayFromStringAndSize :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CLong -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
  pyByteArraySize :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CLong))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_AsString"
  pyByteArrayAsString :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr C2HSImp.CChar)))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_FromObject"
  fromObject'_ :: ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ())))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Concat"
  append'_ :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr ()) -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Size"
  length'_ :: ((C2HSImp.Ptr ()) -> (IO C2HSImp.CLong))

foreign import ccall safe "CPython/Types/ByteArray.chs.h PyByteArray_Resize"
  resize'_ :: ((C2HSImp.Ptr ()) -> (C2HSImp.CLong -> (IO C2HSImp.CInt)))