1 -- !!! file positions (hGetPosn and hSetPosn)
2 module Main(main) where
5 import Monad ( sequence )
6 #if defined(__MINGW32__)
7 import PrelHandle(hSetBinaryMode)
10 testPosns :: Handle -> BufferMode -> IO ()
11 testPosns hdl bmo = do
13 putStrLn ("Testing positioning with buffer mode set to: " ++ show bmo)
16 bmo_ls = [NoBuffering, LineBuffering, BlockBuffering Nothing,
17 BlockBuffering (Just 511),BlockBuffering (Just 3), BlockBuffering (Just 11)]
20 hdl <- openFile "hSeek003.hs" ReadMode
21 # if defined(__MINGW32__)
22 hSetBinaryMode hdl True
24 sequence (zipWith testPosns (repeat hdl) bmo_ls)
27 testPositioning hdl = do
28 hSeek hdl AbsoluteSeek 0 -- go to the beginning of the file again.
29 ps <- getFilePosns 10 hdl
30 hSeek hdl AbsoluteSeek 0
31 putStr "First ten chars: "
32 ls <- hGetChars 10 hdl
35 hSeek hdl SeekFromEnd 0
36 ls <- sequence (map (\ p -> hSetPosn p >> hGetChar hdl) ps)
37 putStr "First ten chars: "
40 -- position ourselves in the middle.
42 hSeek hdl AbsoluteSeek (sz `div` 2)
43 ls <- sequence (map (\ p -> hSetPosn p >> hGetChar hdl) ps)
44 putStr "First ten chars: "
47 hGetChars :: Int -> Handle -> IO String
48 hGetChars n h = sequence (replicate n (hGetChar h))
50 getFilePosns :: Int -> Handle -> IO [HandlePosn]
51 getFilePosns 0 h = return []
55 ps <- getFilePosns (x-1) h