-- 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/Bytes.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.Bytes
  ( Bytes
  , bytesType
  , toBytes
  , fromBytes
  , fromObject
  , length
  , append
  ) 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 Bytes = Bytes (ForeignPtr Bytes)

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

instance Concrete Bytes where
  concreteType :: Bytes -> Type
concreteType _ = Type
bytesType

bytesType :: (Type)
bytesType =
  C2HSImp.unsafePerformIO $
  bytesType'_ >>= \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 46 "lib/CPython/Types/Bytes.chs" #-}


toBytes :: B.ByteString -> IO Bytes
toBytes bytes = let
  mkBytes = pyBytesFromStringAndSize
{-# LINE 50 "lib/CPython/Types/Bytes.chs" #-}

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

fromBytes :: Bytes -> IO B.ByteString
fromBytes py =
  alloca $ \bytesPtr ->
    alloca $ \lenPtr ->
      withObject py $ \pyPtr ->
        do
          pyBytesAsStringAndSize pyPtr bytesPtr lenPtr >>= checkStatusCode
          bytes <- peek bytesPtr
          len <- peek lenPtr
          B.packCStringLen (bytes, fromIntegral len)

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

{-# LINE 70 "lib/CPython/Types/Bytes.chs" #-}


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

{-# LINE 74 "lib/CPython/Types/Bytes.chs" #-}


append :: Bytes -> Bytes -> IO Bytes
append self next =
  alloca $ \tempPtr ->
    do
      withObject self $ \selfPtr -> 
        do incref selfPtr
           poke tempPtr selfPtr
      withObject next $ \nextPtr -> 
        do
          pyBytesConcat tempPtr nextPtr
          newSelf <- peek tempPtr
          stealObject newSelf

foreign import ccall unsafe "CPython/Types/Bytes.chs.h hscpython_PyBytes_Type"
  bytesType'_ :: (IO (C2HSImp.Ptr ()))

foreign import ccall safe "CPython/Types/Bytes.chs.h PyBytes_FromStringAndSize"
  pyBytesFromStringAndSize :: ((C2HSImp.Ptr C2HSImp.CChar) -> (C2HSImp.CLong -> (IO (C2HSImp.Ptr ()))))

foreign import ccall safe "CPython/Types/Bytes.chs.h PyBytes_AsStringAndSize"
  pyBytesAsStringAndSize :: ((C2HSImp.Ptr ()) -> ((C2HSImp.Ptr (C2HSImp.Ptr C2HSImp.CChar)) -> ((C2HSImp.Ptr C2HSImp.CLong) -> (IO C2HSImp.CInt))))

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

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

foreign import ccall safe "CPython/Types/Bytes.chs.h PyBytes_Concat"
  pyBytesConcat :: ((C2HSImp.Ptr (C2HSImp.Ptr ())) -> ((C2HSImp.Ptr ()) -> (IO ())))