File: FileRead.hs

package info (click to toggle)
haskell-streaming-commons 0.2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 280 kB
  • sloc: haskell: 2,521; ansic: 297; makefile: 7
file content (37 lines) | stat: -rw-r--r-- 945 bytes parent folder | download | duplicates (13)
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
{-# LANGUAGE CPP #-}
-- | The standard @openFile@ call on Windows causing problematic file locking
-- in some cases. This module provides a cross-platform file reading API
-- without the file locking problems on Windows.
--
-- This module /always/ opens files in binary mode.
--
-- @readChunk@ will return an empty @ByteString@ on EOF.
module Data.Streaming.FileRead
    ( ReadHandle
    , openFile
    , closeFile
    , readChunk
    ) where

#if WINDOWS

import System.Win32File

#else

import qualified System.IO as IO
import qualified Data.ByteString as S
import Data.ByteString.Lazy.Internal (defaultChunkSize)

newtype ReadHandle = ReadHandle IO.Handle

openFile :: FilePath -> IO ReadHandle
openFile fp = ReadHandle `fmap` IO.openBinaryFile fp IO.ReadMode

closeFile :: ReadHandle -> IO ()
closeFile (ReadHandle h) = IO.hClose h

readChunk :: ReadHandle -> IO S.ByteString
readChunk (ReadHandle h) = S.hGetSome h defaultChunkSize

#endif