module NHC.Binary ( copyBits ) where import NHC.GreenCard import BinHandle ({-type-}BinHandle(..)) import BinPtr ({-type-}BinPtr(..)) import SeekBin (seekBin) import GetBits (getBits) import PutBits (putBits) %-#include "cLowBinary.h" -- %-#include copyBits :: BinHandle -> BinPtr -> BinHandle -> BinPtr -> Int -> IO () copyBits sbh (BP sptr) dbh (BP dptr) n = if sameBH sbh dbh then let dir = sptr < dptr in rehearse (n,32) (\(remain,s) -> let (step,termcond) = if remain> getBits sbh step >>= \v-> seekBin dbh (BP (pos dptr)) >> putBits dbh step v >> return termcond ) else seekBin sbh (BP sptr) >> seekBin dbh (BP dptr) >> rehearse (n,32) (\(remain,s) -> let (step,termcond) = if remain>= \v-> putBits dbh step v >> return termcond ) rehearse :: a -> (a -> IO (Maybe a)) -> IO () rehearse val func = func val >>= \maybe-> case maybe of Nothing -> return () (Just v) -> rehearse v func %fun sameBH :: BinHandle -> BinHandle -> Bool %call (binHandle sbh) (binHandle dbh) %code % b = (sbh==dbh) || % ((sbh->file == dbh->file) && (sbh->loc.fd==dbh->loc.fd)); %result (bool b)