File: RWOps.hs

package info (click to toggle)
haskell-sdl 0.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 376 kB
  • ctags: 2
  • sloc: haskell: 200; ansic: 18; makefile: 12
file content (69 lines) | stat: -rw-r--r-- 1,967 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.UI.SDL.Video
-- Copyright   :  (c) David Himmelstrup 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  provisional
-- Portability :  portable
--
-----------------------------------------------------------------------------
module Graphics.UI.SDL.RWOps
    ( fromFile
    , tryFromFile
    , free
    , with
    , mkFinalizedRW
    ) where

import Foreign (Ptr, FunPtr,
#if defined(__GLASGOW_HASKELL__)
  finalizeForeignPtr,
#endif
  maybePeek, newForeignPtr)
import Foreign.C (withCString, CString)

import Control.Exception (bracket)

import Graphics.UI.SDL.Types (RWops, RWopsStruct)
import Graphics.UI.SDL.General (unwrapMaybe)


with :: FilePath -> String -> (RWops -> IO a) -> IO a
with path mode action
    = bracket (fromFile path mode)
              (free)
              action


-- extern DECLSPEC SDL_RWops * SDLCALL SDL_RWFromFile(const char *file, const char *mode);
foreign import ccall unsafe "SDL_RWFromFile" rwFromFile :: CString -> CString -> IO (Ptr RWopsStruct)
tryFromFile :: FilePath -> String -> IO (Maybe RWops)
tryFromFile filepath mode
    = withCString filepath $ \cPath ->
      withCString mode $ \cMode ->
      rwFromFile cPath cMode >>= maybePeek mkFinalizedRW

fromFile :: FilePath -> String -> IO RWops
fromFile filepath mode = unwrapMaybe "SDL_RWFromFile" (tryFromFile filepath mode)

-- extern DECLSPEC void SDLCALL SDL_FreeRW(SDL_RWops *area);
foreign import ccall unsafe "&SDL_FreeRW" rwFreeFinal :: FunPtr (Ptr RWopsStruct -> IO ())
mkFinalizedRW :: Ptr RWopsStruct -> IO RWops
mkFinalizedRW = newForeignPtr rwFreeFinal

free :: RWops -> IO ()
free =
#if defined(__GLASGOW_HASKELL__)
  finalizeForeignPtr
#else
  const (return ())
#endif

{-
foreign import ccall unsafe "SDL_FreeRW" rwFree :: Ptr RWopsStruct -> IO ()
free :: RWops -> IO ()
free rw = withForeignPtr rw rwFree 
-}