File: Util.hs

package info (click to toggle)
haskell-hdf5 1.8.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 876 kB
  • sloc: haskell: 2,515; ansic: 479; makefile: 6
file content (34 lines) | stat: -rw-r--r-- 1,140 bytes parent folder | download
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
module Spec.Util
  ( withTestFile
  , withTestFile'
  , withGroup
  ) where

import           Control.Exception     (bracket)
import           System.IO
import           System.IO.Temp

import qualified Bindings.HDF5.File    as F
import qualified Bindings.HDF5.Group   as G
import qualified Data.ByteString.Char8 as BS

-- TODO : Use H5FD_CORE driver to avoid writing files : will require high-level wrapper for H5Pset_fapl_core

-- | Call a handle with a temporary HDF5 file
withTestFile :: (FilePath -> F.File -> IO a) -> IO a
withTestFile callback = withSystemTempFile "hs-hdf5.hdf" callback2 where
  callback2 path handle = bracket aquire release run where
    aquire = do
      hClose handle
      F.createFile (BS.pack path) [F.Truncate] Nothing Nothing
    run file = do callback path file
    release file = do F.closeFile file

withTestFile' :: (F.File -> IO a) -> IO a
withTestFile' callback = withTestFile (\_path file -> callback file)

withGroup :: BS.ByteString -> (F.File -> IO a) -> IO a
withGroup name f = withTestFile' $ \file -> do
  group <- G.createGroup file name Nothing Nothing Nothing
  G.closeGroup group
  f file