Movatterモバイル変換


[0]ホーム

URL:


base-4.12.0.0: Basic libraries

Copyright(c) The University of Glasgow 1994-2009
Licensesee libraries/base/LICENSE
Maintainerlibraries@haskell.org
Stabilityprovisional
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell2010

GHC.IO.Handle

Description

External API for GHC's Handle implementation

Synopsis

Documentation

dataHandleSource#

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:

  • whether it manages input or output or both;
  • whether it isopen,closed orsemi-closed;
  • whether the object is seekable;
  • whether buffering is disabled, or enabled on a line or block basis;
  • a buffer (whose length may be zero).

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.

Instances
EqHandleSource#

Since: 4.1.0.0

Instance details

Defined inGHC.IO.Handle.Types

ShowHandleSource#

Since: 4.1.0.0

Instance details

Defined inGHC.IO.Handle.Types

dataBufferModeSource#

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:

  • line-buffering: the entire output buffer is flushed whenever a newline is output, the buffer overflows, ahFlush is issued, or the handle is closed.
  • block-buffering: the entire buffer is written out whenever it overflows, ahFlush is issued, or the handle is closed.
  • no-buffering: output is written immediately, and never stored in the buffer.

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:

  • line-buffering: when the buffer for the handle is not empty, the next item is obtained from the buffer; otherwise, when the buffer is empty, characters up to and including the next newline character are read into the buffer. No characters are available until the newline character is available or the buffer is full.
  • block-buffering: when the buffer for the handle becomes empty, the next block of data is read into the buffer.
  • no-buffering: the next input item is read and returned. ThehLookAhead 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 isn items if the argument isJustn and is otherwise implementation-dependent.

Instances
EqBufferModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Handle.Types

OrdBufferModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Handle.Types

ReadBufferModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Handle.Types

ShowBufferModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Handle.Types

mkFileHandleSource#

Arguments

:: (IODevice dev,BufferedIO dev,Typeable dev) 
=> dev

the underlying IO device, which must supportIODevice,BufferedIO andTypeable

->FilePath

a string describing theHandle, e.g. the file path for a file. Used in error messages.

->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.

isEOF ::IOBoolSource#

The computationisEOF is identical tohIsEOF, except that it works only onstdin.

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

  • ifhdl is writable, the buffer is flushed as forhFlush;
  • ifhdl 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#

dataLockModeSource#

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

hTryLock ::Handle ->LockMode ->IOBoolSource#

Non-blocking version ofhLock.

Since: 4.10.0.0

typeHandlePosition =IntegerSource#

dataHandlePosnSource#

Constructors

HandlePosnHandleHandlePosition 
Instances
EqHandlePosnSource#

Since: 4.1.0.0

Instance details

Defined inGHC.IO.Handle

ShowHandlePosnSource#

Since: 4.1.0.0

Instance details

Defined 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#

If a call tohGetPosnhdl returns a positionp, then computationhSetPosnp sets the position ofhdl to the position it held at the time of the call tohGetPosn.

This operation may fail with:

  • isPermissionError if a system resource limit would be exceeded.

dataSeekModeSource#

A mode that determines the effect ofhSeekhdl mode i.

Constructors

AbsoluteSeek

the position ofhdl is set toi.

RelativeSeek

the position ofhdl is set to offseti from the current position.

SeekFromEnd

the position ofhdl is set to offseti from the end of the file.

Instances
EnumSeekModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Device

EqSeekModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Device

OrdSeekModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Device

ReadSeekModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Device

ShowSeekModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Device

IxSeekModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Device

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.

hIsOpen ::Handle ->IOBoolSource#

hIsClosed ::Handle ->IOBoolSource#

hIsReadable ::Handle ->IOBoolSource#

hIsWritable ::Handle ->IOBoolSource#

hGetBuffering ::Handle ->IOBufferModeSource#

ComputationhGetBufferinghdl returns the current buffering mode forhdl.

hIsSeekable ::Handle ->IOBoolSource#

hSetEcho ::Handle ->Bool ->IO ()Source#

Set the echoing status of a handle connected to a terminal.

hGetEcho ::Handle ->IOBoolSource#

Get the echoing status of a handle connected to a terminal.

hIsTerminalDevice ::Handle ->IOBoolSource#

Is the handle connected to a terminal?

hSetNewlineMode ::Handle ->NewlineMode ->IO ()Source#

Set theNewlineMode on the specifiedHandle. All buffered data is flushed first.

dataNewlineSource#

The representation of a newline in the external file or stream.

Constructors

LF

'\n'

CRLF

'\r\n'

Instances
EqNewlineSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Handle.Types

OrdNewlineSource#

Since: 4.3.0.0

Instance details

Defined inGHC.IO.Handle.Types

ReadNewlineSource#

Since: 4.3.0.0

Instance details

Defined inGHC.IO.Handle.Types

ShowNewlineSource#

Since: 4.3.0.0

Instance details

Defined inGHC.IO.Handle.Types

dataNewlineModeSource#

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 

Fields

Instances
EqNewlineModeSource#

Since: 4.2.0.0

Instance details

Defined inGHC.IO.Handle.Types

OrdNewlineModeSource#

Since: 4.3.0.0

Instance details

Defined inGHC.IO.Handle.Types

ReadNewlineModeSource#

Since: 4.3.0.0

Instance details

Defined inGHC.IO.Handle.Types

ShowNewlineModeSource#

Since: 4.3.0.0

Instance details

Defined inGHC.IO.Handle.Types

nativeNewline ::NewlineSource#

The native newline representation for the current platform:LF on Unix systems,CRLF on Windows.

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 }

hShow ::Handle ->IOStringSource#

hShow is in theIO monad, and gives more comprehensive output than the (pure) instance ofShow forHandle.

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.
  • a decoding error, if the input begins with an invalid byte sequence in this Handle's encoding.

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:

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:

  • ifhClose is applied to it;
  • if an I/O error occurs when reading an item from the handle;
  • or once the entire contents of the handle has been read.

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:

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:

hPutStr ::Handle ->String ->IO ()Source#

ComputationhPutStrhdl s writes the strings to the file or channel managed byhdl.

This operation may fail with:

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).

hPutBufNonBlocking ::Handle ->Ptr a ->Int ->IOIntSource#

Produced byHaddock version 2.20.0


[8]ページ先頭

©2009-2025 Movatter.jp