1 -- !!! file positions (hGetPosn and hSetPosn)
2 module Main(main) where
5 import Monad ( sequence )
7 testPosns :: Handle -> BufferMode -> IO ()
10 putStrLn ("Testing positioning with buffer mode set to: " ++ show bmo)
13 bmo_ls = [NoBuffering, LineBuffering, BlockBuffering Nothing,
14 BlockBuffering (Just 511),BlockBuffering (Just 3), BlockBuffering (Just 11)]
17 hdl <- openFile "hSeek003.hs" ReadMode
18 sequence (zipWith testPosns (repeat hdl) bmo_ls)
21 testPositioning hdl = do
22 hSeek hdl AbsoluteSeek 0 -- go to the beginning of the file again.
23 ps <- getFilePosns 10 hdl
24 hSeek hdl AbsoluteSeek 0
25 putStr "First ten chars: "
26 ls <- hGetChars 10 hdl
29 hSeek hdl SeekFromEnd 0
30 ls <- sequence (map (\ p -> hSetPosn p >> hGetChar hdl) ps)
31 putStr "First ten chars: "
34 -- position ourselves in the middle.
36 hSeek hdl AbsoluteSeek (sz `div` 2)
37 ls <- sequence (map (\ p -> hSetPosn p >> hGetChar hdl) ps)
38 putStr "First ten chars: "
41 hGetChars :: Int -> Handle -> IO String
42 hGetChars n h = sequence (replicate n (hGetChar h))
44 getFilePosns :: Int -> Handle -> IO [HandlePosn]
45 getFilePosns 0 h = return []
49 ps <- getFilePosns (x-1) h