Operations for input and output of characters and strings.
The TEXT_IO interface provides input/output of characters and
strings. Most of the operations themselves are defined in the
IMPERATIVE_IO signature.
The TEXT_IO interface is matched by two structures, the required
TextIO and the optional WideTextIO. The former implements strings
based on the extended ASCII 8-bit characters. The latter provides
strings of characters of some size greater than or equal to 8 bits.
The signature given below for TEXT_IO is not valid SML, in that the
substructure StreamIO is respecified. (It is initially specified as a
substructure having signature STREAM_IO in the included signature
IMPERATIVE_IO.) This abuse of notation seems acceptable in that the
intended meaning is clear (a structure matching TEXT_IO also matches
IMPERATIVE_IO and has a substructure StreamIO that matches
TEXT_STREAM_IO) while avoiding a textual inclusion of the whole
signature of IMPERATIVE_IO except its StreamIO substructure.
closes stream istr. Has no effect if istr is closed
already. Further operations on istr will behave as if istr is at
end of stream (that is, will return "" or NONE or true).
reads some elements from istr, returning a vector v of
those elements. The vector will be empty (size v = 0) if and only
if istr is at end of stream or is closed. May block (not return
until data are available in the external world).
reads and returns the string v of all characters
remaining in istr up to end of stream.
[inputNoBlock istr]
returns SOME(v) if some elements v can be read
without blocking; returns SOME("") if it can be determined without
blocking that istr is at end of stream; returns NONE otherwise. If
istr does not support non-blocking input, raises
Io.NonblockingNotSupported.
returns SOME(e) if at least one element e of istr is
available; returns NONE if istr is at end of stream or is closed;
blocks if necessary until one of these conditions holds.
returns the next n characters from istr as a string,
if that many are available; returns all remaining characters if end of
stream is reached before n characters are available; blocks if
necessary until one of these conditions holds. (This is the behaviour
of the `input' function prescribed in the 1990 Definition of Standard
ML).
returns one line of text, including the terminating
newline character. If end of stream is reached before a newline
character, then the remaining part of the stream is returned, with a
newline character added. If istr is at end of stream or is closed,
then the empty string "" is returned.
returns false if any elements are available in
istr; returns true if istr is at end of stream or closed; blocks if
necessary until one of these conditions holds.
returns SOME(e) where e is the next element in the
stream; returns NONE if istr is at end of stream or is closed; blocks
if necessary until one of these conditions holds. Does not advance
the stream.
turns the instream istr into a character source
and applies the scanner `scan' to that source. See StringCvt for more
on character sources and scanners. The Moscow ML implementation
currently can backtrack only 512 characters, and raises Fail if the
scanner backtracks further than that.
creates a new outstream associated with the file named s.
If file s does not exist, and the directory exists and is writable,
then a new file is created. If file s exists, it is truncated (any
existing contents are lost).
creates a new outstream associated with the file named
s. If file s does not exist, and the directory exists and is
writable, then a new file is created. If file s exists, any existing
contents are retained, and output goes at the end of the file.
prints the string s to stdOut and flushes the stdOut stream.
Discussion
This structure provides input/output functions on text
streams. The functions are state-based: reading from or writing to a
stream changes the state of the stream. The streams are buffered:
output to a stream may not immediately affect the underlying file or
device. Note that under DOS, Windows, OS/2, and MacOS, text streams
will be `translated' by converting (e.g.) the double newline CRLF to a
single newline character \n. Type instream is the type of state-based
characters input streams, and type outstream is the type of
state-based character output streams. Type elem is the type char of
characters, and type vector is the type of character vectors
(strings).