X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FIO%2FHandle.hs;h=ddf17e7540782bc91f93b4377b90b32af87e12c2;hb=b99920eab7fba4e027fd39985840d4e854b8f923;hp=1c61191d05cc5c1c827ce9978dbc752fc71763e0;hpb=c0918e14195921653a6293d8cb949d34ce9958f0;p=ghc-base.git diff --git a/GHC/IO/Handle.hs b/GHC/IO/Handle.hs index 1c61191..ddf17e7 100644 --- a/GHC/IO/Handle.hs +++ b/GHC/IO/Handle.hs @@ -1,5 +1,6 @@ -{-# OPTIONS_GHC -XNoImplicitPrelude -XRecordWildCards #-} {-# OPTIONS_GHC -fno-warn-unused-matches #-} +{-# LANGUAGE NoImplicitPrelude, RecordWildCards #-} + ----------------------------------------------------------------------------- -- | -- Module : GHC.IO.Handle @@ -205,32 +206,12 @@ hSetBuffering handle mode = _ -> do if mode == haBufferMode then return handle_ else do - {- Note: - - we flush the old buffer regardless of whether - the new buffer could fit the contents of the old buffer - or not. - - allow a handle's buffering to change even if IO has - occurred (ANSI C spec. does not allow this, nor did - the previous implementation of IO.hSetBuffering). - - a non-standard extension is to allow the buffering - of semi-closed handles to change [sof 6/98] - -} - flushCharBuffer handle_ - - let state = initBufferState haType - reading = not (isWritableHandleType haType) - - new_buf <- - case mode of - -- See [note Buffer Sizing], GHC.IO.Handle.Types - NoBuffering | reading -> newCharBuffer dEFAULT_CHAR_BUFFER_SIZE state - | otherwise -> newCharBuffer 1 state - LineBuffering -> newCharBuffer dEFAULT_CHAR_BUFFER_SIZE state - BlockBuffering Nothing -> newCharBuffer dEFAULT_CHAR_BUFFER_SIZE state - BlockBuffering (Just n) | n <= 0 -> ioe_bufsiz n - | otherwise -> newCharBuffer n state + -- See [note Buffer Sizing] in GHC.IO.Handle.Types - writeIORef haCharBuffer new_buf + -- check for errors: + case mode of + BlockBuffering (Just n) | n <= 0 -> ioe_bufsiz n + _ -> return () -- for input terminals we need to put the terminal into -- cooked or raw mode depending on the type of buffering. @@ -268,16 +249,16 @@ hSetBuffering handle mode = -- hSetEncoding :: Handle -> TextEncoding -> IO () hSetEncoding hdl encoding = do - withHandle "hSetEncoding" hdl $ \h_@Handle__{..} -> do + withAllHandles__ "hSetEncoding" hdl $ \h_@Handle__{..} -> do flushCharBuffer h_ + closeTextCodecs h_ openTextEncoding (Just encoding) haType $ \ mb_encoder mb_decoder -> do bbuf <- readIORef haByteBuffer ref <- newIORef (error "last_decode") return (Handle__{ haLastDecode = ref, haDecoder = mb_decoder, haEncoder = mb_encoder, - haCodec = Just encoding, .. }, - ()) + haCodec = Just encoding, .. }) -- | Return the current 'TextEncoding' for the specified 'Handle', or -- 'Nothing' if the 'Handle' is in binary mode. @@ -394,6 +375,9 @@ hSetPosn (HandlePosn h i) = hSeek h AbsoluteSeek i -- -- This operation may fail with: -- +-- * 'isIllegalOperationError' if the Handle is not seekable, or does +-- not support the requested seek mode. +-- -- * 'isPermissionError' if a system resource limit would be exceeded. hSeek :: Handle -> SeekMode -> Integer -> IO () @@ -418,20 +402,32 @@ hSeek handle mode offset = IODevice.seek haDevice mode offset +-- | Computation 'hTell' @hdl@ returns the current position of the +-- handle @hdl@, as the number of bytes from the beginning of +-- the file. The value returned may be subsequently passed to +-- 'hSeek' to reposition the handle to the current position. +-- +-- This operation may fail with: +-- +-- * 'isIllegalOperationError' if the Handle is not seekable. +-- hTell :: Handle -> IO Integer hTell handle = wantSeekableHandle "hGetPosn" handle $ \ handle_@Handle__{..} -> do posn <- IODevice.tell haDevice - cbuf <- readIORef haCharBuffer + -- we can't tell the real byte offset if there are buffered + -- Chars, so must flush first: + flushCharBuffer handle_ + bbuf <- readIORef haByteBuffer - let real_posn - | isWriteBuffer cbuf = posn + fromIntegral (bufR cbuf) - | otherwise = posn - fromIntegral (bufR cbuf - bufL cbuf) - - fromIntegral (bufR bbuf - bufL bbuf) + let real_posn + | isWriteBuffer bbuf = posn + fromIntegral (bufferElems bbuf) + | otherwise = posn - fromIntegral (bufferElems bbuf) + cbuf <- readIORef haCharBuffer debugIO ("\nhGetPosn: (posn, real_posn) = " ++ show (posn, real_posn)) debugIO (" cbuf: " ++ summaryBuffer cbuf ++ " bbuf: " ++ summaryBuffer bbuf) @@ -562,6 +558,7 @@ hSetBinaryMode handle bin = withAllHandles__ "hSetBinaryMode" handle $ \ h_@Handle__{..} -> do flushCharBuffer h_ + closeTextCodecs h_ let mb_te | bin = Nothing | otherwise = Just localeEncoding