| Copyright | (c) The University of Glasgow 1994-2009 |
|---|---|
| License | see libraries/base/LICENSE |
| Maintainer | libraries@haskell.org |
| Stability | provisional |
| Portability | non-portable |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
GHC.IO.Handle
Description
External API for GHC's Handle implementation
Haskell defines operations to read and write characters from and to files, represented by values of typeHandle. Each value of this type is ahandle: a record used by the Haskell run-time system tomanage I/O with file system objects. A handle has at least the following properties:
Most handles will also have a current I/O position indicating where the next input or output operation will occur. A handle isreadable if it manages only input or both input and output; likewise, it iswritable if it manages only output or both input and output. A handle isopen when first allocated. Once it is closed it can no longer be used for either input or output, though an implementation cannot re-use its storage while references remain to it. Handles are in theShow andEq classes. The string produced by showing a handle is system dependent; it should include enough information to identify the handle for debugging. A handle is equal according to== only to itself; no attempt is made to compare the internal state of different handles for equality.
Three kinds of buffering are supported: line-buffering, block-buffering or no-buffering. These modes have the following effects. For output, items are written out, orflushed, from the internal buffer according to the buffer mode:
hFlush is issued, or the handle is closed.hFlush is issued, or the handle is closed.An implementation is free to flush the buffer more frequently, but not less frequently, than specified above. The output buffer is emptied as soon as it has been written out.
Similarly, input occurs according to the buffer mode for the handle:
hLookAhead operation implies that even a no-buffered handle may require a one-character buffer.The default buffering mode when a handle is opened is implementation-dependent and may depend on the file system object which is attached to that handle. For most implementations, physical files will normally be block-buffered and terminals will normally be line-buffered.
Constructors
| NoBuffering | buffering is disabled if possible. |
| LineBuffering | line-buffering should be enabled if possible. |
| BlockBuffering (MaybeInt) | block-buffering should be enabled if possible. The size of the buffer is |
| EqBufferModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types | |
| OrdBufferModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types Methods compare ::BufferMode ->BufferMode ->Ordering# (<) ::BufferMode ->BufferMode ->Bool# (<=) ::BufferMode ->BufferMode ->Bool# (>) ::BufferMode ->BufferMode ->Bool# (>=) ::BufferMode ->BufferMode ->Bool# max ::BufferMode ->BufferMode ->BufferMode# min ::BufferMode ->BufferMode ->BufferMode# | |
| ReadBufferModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types | |
| ShowBufferModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types | |
Arguments
| :: (IODevice dev,BufferedIO dev,Typeable dev) | |
| => dev | the underlying IO device, which must support |
| ->FilePath | a string describing the |
| ->IOMode | |
| ->MaybeTextEncoding | |
| ->NewlineMode | |
| ->IOHandle |
makes a newHandle
mkDuplexHandle :: (IODevice dev,BufferedIO dev,Typeable dev) => dev ->FilePath ->MaybeTextEncoding ->NewlineMode ->IOHandleSource#
likemkFileHandle, except that aHandle is created with two independent buffers, one for reading and one for writing. Used for full-duplex streams, such as network sockets.
hFileSize ::Handle ->IOIntegerSource#
For a handlehdl which attached to a physical file,hFileSizehdl returns the size of that file in 8-bit bytes.
hSetFileSize ::Handle ->Integer ->IO ()Source#
hSetFileSizehdlsize truncates the physical file with handlehdl tosize bytes.
hIsEOF ::Handle ->IOBoolSource#
For a readable handlehdl,hIsEOFhdl returnsTrue if no further input can be taken fromhdl or for a physical file, if the current I/O position is equal to the length of the file. Otherwise, it returnsFalse.
NOTE:hIsEOF may block, because it has to attempt to read from the stream to determine whether there is any more data to be read.
hLookAhead ::Handle ->IOCharSource#
ComputationhLookAhead returns the next character from the handle without removing it from the input buffer, blocking until a character is available.
This operation may fail with:
isEOFError if the end of file has been reached.hSetBuffering ::Handle ->BufferMode ->IO ()Source#
ComputationhSetBufferinghdl mode sets the mode of buffering for handlehdl on subsequent reads and writes.
If the buffer mode is changed fromBlockBuffering orLineBuffering toNoBuffering, then
hdl is writable, the buffer is flushed as forhFlush;hdl is not writable, the contents of the buffer is discarded.This operation may fail with:
isPermissionError if the handle has already been used for reading or writing and the implementation does not allow the buffering mode to be changed.hSetBinaryMode ::Handle ->Bool ->IO ()Source#
Select binary mode (True) or text mode (False) on a open handle. (See alsoopenBinaryFile.)
This has the same effect as callinghSetEncoding withchar8, together withhSetNewlineMode withnoNewlineTranslation.
hSetEncoding ::Handle ->TextEncoding ->IO ()Source#
The actionhSetEncodinghdlencoding changes the text encoding for the handlehdl toencoding. The default encoding when aHandle is created islocaleEncoding, namely the default encoding for the current locale.
To create aHandle with no encoding at all, useopenBinaryFile. To stop further encoding or decoding on an existingHandle, usehSetBinaryMode.
hSetEncoding may need to flush buffered data in order to change the encoding.
hGetEncoding ::Handle ->IO (MaybeTextEncoding)Source#
Return the currentTextEncoding for the specifiedHandle, orNothing if theHandle is in binary mode.
Note that theTextEncoding remembers nothing about the state of the encoder/decoder in use on thisHandle. For example, if the encoding in use is UTF-16, then usinghGetEncoding andhSetEncoding to save and restore the encoding may result in an extra byte-order-mark being written to the file.
hFlush ::Handle ->IO ()Source#
The actionhFlushhdl causes any items buffered for output in handlehdl to be sent immediately to the operating system.
This operation may fail with:
isFullError if the device is full;isPermissionError if a system resource limit would be exceeded. It is unspecified whether the characters in the buffer are discarded or retained under these circumstances.hFlushAll ::Handle ->IO ()Source#
The actionhFlushAllhdl flushes all buffered data inhdl, including any buffered read data. Buffered read data is flushed by seeking the file position back to the point before the bufferred data was read, and hence only works ifhdl is seekable (seehIsSeekable).
This operation may fail with:
isFullError if the device is full;isPermissionError if a system resource limit would be exceeded. It is unspecified whether the characters in the buffer are discarded or retained under these circumstances;isIllegalOperation ifhdl has buffered read data, and is not seekable.hDuplicate ::Handle ->IOHandleSource#
Returns a duplicate of the original handle, with its own buffer. The two Handles will share a file pointer, however. The original handle's buffer is flushed, including discarding any input data, before the handle is duplicated.
hDuplicateTo ::Handle ->Handle ->IO ()Source#
Makes the second handle a duplicate of the first handle. The secondhandle will be closed first, if it is not already.
This can be used to retarget the standard Handles, for example:
do h <- openFile "mystdout" WriteMode hDuplicateTo h stdout
hClose ::Handle ->IO ()Source#
ComputationhClosehdl makes handlehdl closed. Before the computation finishes, ifhdl is writable its buffer is flushed as forhFlush. PerforminghClose on a handle that has already been closed has no effect; doing so is not an error. All other operations on a closed handle will fail. IfhClose fails for any reason, any further operations (apart fromhClose) on the handle will still fail as ifhdl had been successfully closed.
hClose_help :: Handle__ ->IO (Handle__,MaybeSomeException)Source#
Indicates a mode in which a file should be locked.
Constructors
| SharedLock | |
| ExclusiveLock |
hLock ::Handle ->LockMode ->IO ()Source#
If aHandle references a file descriptor, attempt to lock contents of the underlying file in appropriate mode. If the file is already locked in incompatible mode, this function blocks until the lock is established. The lock is automatically released upon closing aHandle.
Things to be aware of:
1) This function may block inside a C call. If it does, in order to be able to interrupt it with asynchronous exceptions and/or for other threads to continue working, you MUST use threaded version of the runtime system.
2) The implementation usesLockFileEx on Windows andflock otherwise, hence all of their caveats also apply here.
3) On non-Windows plaftorms that don't supportflock (e.g. Solaris) this function throwsFileLockingNotImplemented. We deliberately choose to not provide fcntl based locking instead because of its broken semantics.
Since: 4.10.0.0
Constructors
| HandlePosnHandleHandlePosition |
| EqHandlePosnSource# | Since: 4.1.0.0 |
Instance detailsDefined inGHC.IO.Handle | |
| ShowHandlePosnSource# | Since: 4.1.0.0 |
Instance detailsDefined inGHC.IO.Handle | |
hGetPosn ::Handle ->IOHandlePosnSource#
ComputationhGetPosnhdl returns the current I/O position ofhdl as a value of the abstract typeHandlePosn.
hSetPosn ::HandlePosn ->IO ()Source#
A mode that determines the effect ofhSeekhdl mode i.
Constructors
| AbsoluteSeek | the position of |
| RelativeSeek | the position of |
| SeekFromEnd | the position of |
| EnumSeekModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Device Methods succ ::SeekMode ->SeekModeSource# pred ::SeekMode ->SeekModeSource# toEnum ::Int ->SeekModeSource# fromEnum ::SeekMode ->IntSource# enumFrom ::SeekMode -> [SeekMode]Source# enumFromThen ::SeekMode ->SeekMode -> [SeekMode]Source# enumFromTo ::SeekMode ->SeekMode -> [SeekMode]Source# enumFromThenTo ::SeekMode ->SeekMode ->SeekMode -> [SeekMode]Source# | |
| EqSeekModeSource# | Since: 4.2.0.0 |
| OrdSeekModeSource# | Since: 4.2.0.0 |
| ReadSeekModeSource# | Since: 4.2.0.0 |
| ShowSeekModeSource# | Since: 4.2.0.0 |
| IxSeekModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Device Methods range :: (SeekMode,SeekMode) -> [SeekMode]Source# index :: (SeekMode,SeekMode) ->SeekMode ->IntSource# unsafeIndex :: (SeekMode,SeekMode) ->SeekMode ->Int inRange :: (SeekMode,SeekMode) ->SeekMode ->BoolSource# rangeSize :: (SeekMode,SeekMode) ->IntSource# unsafeRangeSize :: (SeekMode,SeekMode) ->Int | |
hSeek ::Handle ->SeekMode ->Integer ->IO ()Source#
ComputationhSeekhdl mode i sets the position of handlehdl depending onmode. The offseti is given in terms of 8-bit bytes.
Ifhdl is block- or line-buffered, then seeking to a position which is not in the current buffer will first cause any items in the output buffer to be written to the device, and then cause the input buffer to be discarded. Some handles may not be seekable (seehIsSeekable), or only support a subset of the possible positioning operations (for instance, it may only be possible to seek to the end of a tape, or to a positive offset from the beginning or current position). It is not possible to set a negative I/O position, or for a physical file, an I/O position beyond the current end-of-file.
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.hTell ::Handle ->IOIntegerSource#
ComputationhTellhdl returns the current position of the handlehdl, as the number of bytes from the beginning of the file. The value returned may be subsequently passed tohSeek to reposition the handle to the current position.
This operation may fail with:
isIllegalOperationError if the Handle is not seekable.hGetBuffering ::Handle ->IOBufferModeSource#
ComputationhGetBufferinghdl returns the current buffering mode forhdl.
hSetNewlineMode ::Handle ->NewlineMode ->IO ()Source#
Set theNewlineMode on the specifiedHandle. All buffered data is flushed first.
The representation of a newline in the external file or stream.
Specifies the translation, if any, of newline characters between internal Strings and the external file or stream. Haskell Strings are assumed to represent newlines with the '\n' character; the newline mode specifies how to translate '\n' on output, and what to translate into '\n' on input.
Constructors
| NewlineMode | |
| EqNewlineModeSource# | Since: 4.2.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types | |
| OrdNewlineModeSource# | Since: 4.3.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types Methods compare ::NewlineMode ->NewlineMode ->Ordering# (<) ::NewlineMode ->NewlineMode ->Bool# (<=) ::NewlineMode ->NewlineMode ->Bool# (>) ::NewlineMode ->NewlineMode ->Bool# (>=) ::NewlineMode ->NewlineMode ->Bool# | |
| ReadNewlineModeSource# | Since: 4.3.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types | |
| ShowNewlineModeSource# | Since: 4.3.0.0 |
Instance detailsDefined inGHC.IO.Handle.Types | |
noNewlineTranslation ::NewlineModeSource#
Do no newline translation at all.
noNewlineTranslation = NewlineMode { inputNL = LF, outputNL = LF }universalNewlineMode ::NewlineModeSource#
Map '\r\n' into '\n' on input, and '\n' to the native newline represetnation on output. This mode can be used on any platform, and works with text files using any newline convention. The downside is thatreadFile >>= writeFile might yield a different file.
universalNewlineMode = NewlineMode { inputNL = CRLF, outputNL = nativeNewline }nativeNewlineMode ::NewlineModeSource#
Use the native newline representation on both input and output
nativeNewlineMode = NewlineMode { inputNL = nativeNewline outputNL = nativeNewline }hWaitForInput ::Handle ->Int ->IOBoolSource#
ComputationhWaitForInputhdl t waits until input is available on handlehdl. It returnsTrue as soon as input is available onhdl, orFalse if no input is available withint milliseconds. Note thathWaitForInput waits until one or more fullcharacters are available, which means that it needs to do decoding, and hence may fail with a decoding error.
Ift is less than zero, thenhWaitForInput waits indefinitely.
This operation may fail with:
isEOFError if the end of file has been reached.NOTE for GHC users: unless you use the-threaded flag,hWaitForInput hdl t wheret >= 0 will block all other Haskell threads for the duration of the call. It behaves like asafe foreign call in this respect.
hGetChar ::Handle ->IOCharSource#
ComputationhGetCharhdl reads a character from the file or channel managed byhdl, blocking until a character is available.
This operation may fail with:
isEOFError if the end of file has been reached.hGetLine ::Handle ->IOStringSource#
ComputationhGetLinehdl reads a line from the file or channel managed byhdl.
This operation may fail with:
isEOFError if the end of file is encountered when reading thefirst character of the line.IfhGetLine encounters end-of-file at any other point while reading in a line, it is treated as a line terminator and the (partial) line is returned.
hGetContents ::Handle ->IOStringSource#
ComputationhGetContentshdl returns the list of characters corresponding to the unread portion of the channel or file managed byhdl, which is put into an intermediate state,semi-closed. In this state,hdl is effectively closed, but items are read fromhdl on demand and accumulated in a special list returned byhGetContentshdl.
Any operation that fails because a handle is closed, also fails if a handle is semi-closed. The only exception ishClose. A semi-closed handle becomes closed:
hClose is applied to it;Once a semi-closed handle becomes closed, the contents of the associated list becomes fixed. The contents of this final list is only partially specified: it will contain at least all the items of the stream that were evaluated prior to the handle becoming closed.
Any I/O errors encountered while a handle is semi-closed are simply discarded.
This operation may fail with:
isEOFError if the end of file has been reached.hPutChar ::Handle ->Char ->IO ()Source#
ComputationhPutCharhdl ch writes the characterch to the file or channel managed byhdl. Characters may be buffered if buffering is enabled forhdl.
This operation may fail with:
isFullError if the device is full; orisPermissionError if another system resource limit would be exceeded.hPutStr ::Handle ->String ->IO ()Source#
ComputationhPutStrhdl s writes the strings to the file or channel managed byhdl.
This operation may fail with:
isFullError if the device is full; orisPermissionError if another system resource limit would be exceeded.hGetBuf ::Handle ->Ptr a ->Int ->IOIntSource#
hGetBufhdl buf count reads data from the handlehdl into the bufferbuf until either EOF is reached orcount 8-bit bytes have been read. It returns the number of bytes actually read. This may be zero if EOF was reached before any data was read (or ifcount is zero).
hGetBuf never raises an EOF exception, instead it returns a value smaller thancount.
If the handle is a pipe or socket, and the writing end is closed,hGetBuf will behave as if EOF was reached.
hGetBuf ignores the prevailingTextEncoding andNewlineMode on theHandle, and reads bytes directly.
hGetBufNonBlocking ::Handle ->Ptr a ->Int ->IOIntSource#
hGetBufNonBlockinghdl buf count reads data from the handlehdl into the bufferbuf until either EOF is reached, orcount 8-bit bytes have been read, or there is no more data available to read immediately.
hGetBufNonBlocking is identical tohGetBuf, except that it will never block waiting for data to become available, instead it returns only whatever data is available. To wait for data to arrive before callinghGetBufNonBlocking, usehWaitForInput.
If the handle is a pipe or socket, and the writing end is closed,hGetBufNonBlocking will behave as if EOF was reached.
hGetBufNonBlocking ignores the prevailingTextEncoding andNewlineMode on theHandle, and reads bytes directly.
NOTE: on Windows, this function does not work correctly; it behaves identically tohGetBuf.
hPutBuf ::Handle ->Ptr a ->Int ->IO ()Source#
hPutBufhdl buf count writescount 8-bit bytes from the bufferbuf to the handlehdl. It returns ().
hPutBuf ignores any text encoding that applies to theHandle, writing the bytes directly to the underlying file or device.
hPutBuf ignores the prevailingTextEncoding andNewlineMode on theHandle, and writes bytes directly.
This operation may fail with:
ResourceVanished if the handle is a pipe or socket, and the reading end is closed. (If this is a POSIX system, and the program has not asked to ignore SIGPIPE, then a SIGPIPE may be delivered instead, whose default action is to terminate the program).Produced byHaddock version 2.20.0