module Halfs.Buffer (Buffer
,strToBuff
,strToNewBuff
,buffToStrSize
,buffToStr
,withBuffSize
,buffToCBuff
,cBuffToBuff
,plusBuff
,buffSeek
,buffGetHandle
,newBuff
,buffSize
) where
import Foreign.C.Types (CSize)
import Foreign.Ptr(Ptr)
import Data.Word(Word8)
import Data.Array.SysArray
import Binary
newtype Buffer = Buffer (BinHandle)
newBuff :: Int -> IO Buffer
newBuff i = openBinMem i >>= return . Buffer
buffSize :: Buffer -> IO Int
buffSize (Buffer b) = sizeBinMem b
strToBuff :: String -> Buffer -> IO ()
strToBuff s (Buffer binHandle)
= resetBin binHandle >> mapM_ (put_ binHandle) s
strToNewBuff :: String -> IO Buffer
strToNewBuff [] = error "strToNewbuff empty list!"
strToNewBuff s = do
h <- openBinMem (length s)
let b = Buffer h
strToBuff s b
return b
buffToStrSize :: Int -> Buffer -> IO String
buffToStrSize sz (Buffer binHandle) = do
resetBin binHandle
s <- getMany binHandle sz
return s
buffToStr :: Buffer -> IO String
buffToStr buffer@(Buffer binHandle) = do
resetBin binHandle
len <- sizeBinMem binHandle
buffToStrSize len buffer
getMany :: BinHandle -> Int -> IO String
getMany _ 0 = return []
getMany bh n = do x <- Binary.get bh
xs <- getMany bh (n1)
return (x:xs)
withBuffSize :: Int -> (Buffer -> IO a) -> IO a
withBuffSize sz f
| sz <= 0 = error "withBuffSize 0!"
| otherwise = openBinMem sz >>= f . Buffer
buffToCBuff :: Buffer -> Ptr Word8 -> CSize -> IO ()
buffToCBuff = forwardTwixt loop
where loop _ _ 0 = return ()
loop ptr (Buffer binHandle) i = do
ptr_handle <- openFixedSysHandle
(mkSysArray (fromIntegral i)
ptr
False)
resetBin ptr_handle
copyBytes binHandle ptr_handle (fromIntegral i)
return ()
cBuffToBuff :: Buffer -> Ptr Word8 -> CSize -> IO ()
cBuffToBuff = forwardTwixt loop
where loop _ _ 0 = return ()
loop ptr (Buffer binHandle) i = do
ptr_handle <- openFixedSysHandle
(mkSysArray (fromIntegral i)
ptr
True)
resetBin ptr_handle
copyBytes ptr_handle binHandle (fromIntegral i)
return ()
forwardTwixt :: (Ptr Word8 -> Buffer -> CSize -> IO ()) -> Buffer -> Ptr Word8 -> CSize -> IO ()
forwardTwixt f b@(Buffer binHandle) ptr len
| len < 0 = error "FIX: negative number to cBuffToBuff"
| otherwise = do loc <- tellBin binHandle
f ptr b len
seekBin binHandle loc
plusBuff :: Buffer -> Int -> IO ()
plusBuff (Buffer binHandle) n = do
BinPtr curr <- tellBin binHandle
seekBin binHandle (BinPtr $ curr + n)
return ()
buffSeek :: Buffer -> Int -> IO ()
buffSeek (Buffer binHandle) n
= seekBin binHandle (BinPtr n)
buffGetHandle :: Buffer -> BinHandle
buffGetHandle (Buffer b) = b