module Sound.MED.Basic.Amiga (
  Peek, Reader(..), peekPTR,
  StorableReader, runStorable,
  ByteStringReader, runByteString,
  PTR, LONG, ULONG, WORD, UWORD, BYTE, UBYTE,
  loadMEM, freeMEM,
  ) where

import Sound.MED.Basic.AmigaPrivate

import Sound.MED.Basic.Storable (MEM)
import Sound.MED.Basic.Utility (PTR, LONG, ULONG, WORD, UWORD, BYTE, UBYTE)

import qualified System.IO as IO

import qualified Foreign.Marshal.Alloc as Alloc

import Control.Monad (when)
import Control.Applicative ((<$>))


loadMEM :: String -> IO MEM
loadMEM :: String -> IO MEM
loadMEM String
s =
  forall r. String -> IOMode -> (Handle -> IO r) -> IO r
IO.withBinaryFile String
s IOMode
IO.ReadMode forall a b. (a -> b) -> a -> b
$ \Handle
h -> do
    Int
size <- forall a. Num a => Integer -> a
fromInteger forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Handle -> IO Integer
IO.hFileSize Handle
h
    MEM
ptr <- forall a. Int -> IO (Ptr a)
Alloc.mallocBytes Int
size
    Int
readSize <- forall a. Handle -> Ptr a -> Int -> IO Int
IO.hGetBuf Handle
h MEM
ptr Int
size
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
readSizeforall a. Ord a => a -> a -> Bool
<Int
size) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"loadMEM: incomplete load of " forall a. [a] -> [a] -> [a]
++ String
s
    forall (m :: * -> *) a. Monad m => a -> m a
return MEM
ptr

freeMEM :: MEM -> IO ()
freeMEM :: MEM -> IO ()
freeMEM = forall a. Ptr a -> IO ()
Alloc.free