-- GENERATED by C->Haskell Compiler, version 0.17.2 Crystal Seed, 24 Jan 2009 (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           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 x) = SomeObject x
	fromForeignPtr = Bytes

instance Concrete Bytes where
	concreteType _ = bytesType

bytesType :: (Type)
bytesType =
  unsafePerformIO $
  let {res = bytesType'_} in
  peekStaticObject res >>= \res' ->
  return (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 a1 =
  withObject a1 $ \a1' -> 
  fromObject'_ a1' >>= \res ->
  stealObject res >>= \res' ->
  return (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 ->
	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'_ :: (Ptr ())

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

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

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

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

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