#undef DEBUG
-- -----------------------------------------------------------------------------
--- $Id: PrelHandle.hsc,v 1.4 2001/05/21 11:02:50 simonmar Exp $
+-- $Id: PrelHandle.hsc,v 1.5 2001/05/24 10:41:13 simonmar Exp $
--
-- (c) The University of Glasgow, 1994-2001
--
-- This module defines the basic operations on I/O "handles".
module PrelHandle (
- withHandle, withHandle_,
+ withHandle, withHandle', withHandle_,
wantWritableHandle, wantReadableHandle, wantSeekableHandle,
newEmptyBuffer, allocateBuffer, readCharFromBuffer, writeCharIntoBuffer,
flushWriteBufferOnly, flushWriteBuffer, flushReadBuffer, fillReadBuffer,
read_off,
- ioe_closedHandle, ioe_EOF,
+ ioe_closedHandle, ioe_EOF, ioe_notReadable, ioe_notWritable,
stdin, stdout, stderr,
IOMode(..), IOModeEx(..), openFile, openFileEx, openFd,
= case haType handle_ of
ClosedHandle -> ioe_closedHandle
SemiClosedHandle -> ioe_closedHandle
- ReadHandle -> ioException not_writeable_error
+ ReadHandle -> ioe_notWritable
ReadWriteHandle -> do
let ref = haBuffer handle_
buf <- readIORef ref
writeIORef ref new_buf
act handle_
_other -> act handle_
- where
- not_writeable_error =
- IOError Nothing IllegalOperation ""
- "handle is not open for writing" Nothing
-- ---------------------------------------------------------------------------
-- Wrapper for read operations.
case haType handle_ of
ClosedHandle -> ioe_closedHandle
SemiClosedHandle -> ioe_closedHandle
- AppendHandle -> ioException not_readable_error
- WriteHandle -> ioException not_readable_error
+ AppendHandle -> ioe_notReadable
+ WriteHandle -> ioe_notReadable
ReadWriteHandle -> do
let ref = haBuffer handle_
buf <- readIORef ref
writeIORef ref new_buf{ bufState=ReadBuffer }
act handle_
_other -> act handle_
- where
- not_readable_error =
- IOError Nothing IllegalOperation ""
- "handle is not open for reading" Nothing
-- ---------------------------------------------------------------------------
-- Wrapper for seek operations.
case haType handle_ of
ClosedHandle -> ioe_closedHandle
SemiClosedHandle -> ioe_closedHandle
- AppendHandle -> not_seekable_error
+ AppendHandle -> ioe_notSeekable
_ -> act handle_
-not_seekable_error
- = ioException (IOError Nothing IllegalOperation ""
- "handle is not seekable" Nothing)
-
-- -----------------------------------------------------------------------------
-- Handy IOErrors
-ioe_closedHandle :: IO a
-ioe_closedHandle = ioException (IOError Nothing IllegalOperation "" "" Nothing)
-
-ioe_EOF :: IO a
-ioe_EOF = ioException (IOError Nothing EOF "" "" Nothing)
+ioe_closedHandle, ioe_EOF,
+ ioe_notReadable, ioe_notWritable, ioe_notSeekable :: IO a
+
+ioe_closedHandle = ioException
+ (IOError Nothing IllegalOperation ""
+ "handle is closed" Nothing)
+ioe_EOF = ioException
+ (IOError Nothing EOF "" "" Nothing)
+ioe_notReadable = ioException
+ (IOError Nothing IllegalOperation ""
+ "handle is not open for reading" Nothing)
+ioe_notWritable = ioException
+ (IOError Nothing IllegalOperation ""
+ "handle is not open for writing" Nothing)
+ioe_notSeekable = ioException
+ (IOError Nothing IllegalOperation ""
+ "handle is not seekable" Nothing)
-- -----------------------------------------------------------------------------
-- Handle Finalizers
#undef DEBUG_DUMP
-- -----------------------------------------------------------------------------
--- $Id: PrelIO.hsc,v 1.3 2001/05/22 15:06:47 simonmar Exp $
+-- $Id: PrelIO.hsc,v 1.4 2001/05/24 10:41:13 simonmar Exp $
--
-- (c) The University of Glasgow, 1992-2001
--
-- unread portion of the channel or file managed by the handle, which
-- is made semi-closed.
+-- hGetContents on a DuplexHandle only affects the read side: you can
+-- carry on writing to it afterwards.
+
hGetContents :: Handle -> IO String
-hGetContents handle =
- -- can't use wantReadableHandle here, because we want to side effect
- -- the handle.
- withHandle "hGetContents" handle $ \ handle_ -> do
+hGetContents handle@(DuplexHandle r w)
+ = withHandle' "hGetContents" handle r (hGetContents' handle)
+hGetContents handle@(FileHandle m)
+ = withHandle' "hGetContents" handle m (hGetContents' handle)
+
+hGetContents' handle handle_ =
case haType handle_ of
ClosedHandle -> ioe_closedHandle
SemiClosedHandle -> ioe_closedHandle
- AppendHandle -> ioException not_readable_error
- WriteHandle -> ioException not_readable_error
+ AppendHandle -> ioe_notReadable
+ WriteHandle -> ioe_notReadable
_ -> do xs <- lazyRead handle
return (handle_{ haType=SemiClosedHandle}, xs )
- where
- not_readable_error =
- IOError (Just handle) IllegalOperation "hGetContents"
- "handle is not open for reading" Nothing
-- Note that someone may close the semi-closed handle (or change its
--- buffering), so each these lazy read functions are pulled on, they
--- have to check whether the handle has indeed been closed.
+-- buffering), so each time these lazy read functions are pulled on,
+-- they have to check whether the handle has indeed been closed.
lazyRead :: Handle -> IO String
lazyRead handle =