On this page:
Port.Input
Port.Input.String
Port.Input.Progress
Port.Input.current
stdin
Port.Input.using
Port.Input.open_  bytes
Port.Input.open_  file
Port.Input.open_  string
Port.Input.open_  nowhere
Port.Input.read_  byte
Port.Input.read_  bytes
Port.Input.read_  bytes_  to
Port.Input.read_  char
Port.Input.read_  string
Port.Input.read_  string_  to
Port.Input.read_  bytes_  line
Port.Input.read_  line
Port.Input.peek_  byte
Port.Input.peek_  bytes
Port.Input.peek_  bytes_  to
Port.Input.peek_  char
Port.Input.peek_  string
Port.Input.peek_  string_  to
Port.Input.copy_  to
Port.Input.Progress.evt
Port.Input.Progress.is_  evt
Port.Input.Progress.commit
Port.Input.Read  Line  Mode
Port.Input.Read  Line  Mode.linefeed
Port.Input.Read  Line  Mode.return
Port.Input.Read  Line  Mode.return_  linefeed
Port.Input.Read  Line  Mode.any
Port.Input.Read  Line  Mode.any_  one
8.16.0.1

13.2 Input Ports🔗ℹ

An input port is a port specifically for input. Moreover, an input string port reads from a byte string.

annotation

Port.Input

 

annotation

Port.Input.String

 

annotation

Port.Input.Progress

The Port.Input annotation recognizes input ports. The Port.Input.String annotation recognizes input string ports. The Port.Input.Progresse annotation recognizes input ports that can provide a progress event via Port.Input.Progress.evt.

context parameter

Parameter.def Port.Input.current :: Port.Input

 

expression

stdin

 

expression

Port.Input.using port_expr:

  body

  ...

 

expression

Port.Input.using ~file path_expr:

  body

  ...

The Port.Input.current context parameter determines a default port to use when reading. The stdin form is a shorthand for Port.Input.current() to get that default port.

The Port.Input.using form sets Port.Input.current while evaluating the body result. If ~file is not supplied, then port_expr must produce a Port.Input, otherwise path_expr must produce a PathString. Breaks are disabled during the evaluation of port_expr or opening the file indicated by path_expr, the same as for Closeable.def, and the port is similarly closed on return or escape from the body sequence.

function

fun Port.Input.open_bytes(bstr :: Bytes,

                          name :: Symbol = #'string)

  :: Port.Input.String

Creates an input string port that reads bytes from the byte string bstr. The optional name is used as the name for the returned port.

function

fun Port.Input.open_file(file :: PathString,

                         ~mode: mode :: Port.Mode = #'binary)

  :: Port.Input && Port.FileStream

Creates an input port that reads from the path file.

function

fun Port.Input.open_string(str :: ReadableString,

                           name :: Symbol = #'string)

  :: Port.Input.String

Creates an input string port that reads characters from the string str. The optional name is used as the name for the returned port.

function

fun Port.Input.open_nowhere(name :: Symbol = #'nowhere)

  :: Port.Input

Creates an input port that is empty. The optional name is used as the name for the returned port.

method

method (in :: Port.Input).read_byte(

  ~special_wrap: special_wrap :: maybe(Any -> Any) = #false,

  ~source_name: source_name :: Any = #false

) :: Byte || Port.EOF || Any

Normally reads a single byte from in. If no bytes are available before an end-of-file, then Port.eof is returned.

If special_wrap is not #false and if the port can supply “special” non-byte results, then the result can be special_wrap applied to the special result. Supply values as special_wrap to enable special results without extra treatment. The source_name argument can be anything, and it is delivered to the port’s implementation to potentially specialize its behavior.

method

method (in :: Port.Input).read_bytes(amount :: NonnegInt)

  :: Bytes || Port.EOF

Reads a byte string containing the next amount bytes from in. If amount is 0, then an empty byte string is returned. Otherwise if fewer than amount bytes are available before an end-of-file is encountered, then the returned byte string will contain only those bytes before the end-of-file; that is, the returned byte string’s length will be less than amount. If no bytes are available before an end-of-file, then Port.eof is returned.

method

method (in :: Port.Input).read_bytes_to(

  bytes :: MutableBytes,

  ~start: start :: NonnegInt = 0,

  ~end: end :: NonnegInt = bytes.length(),

  ~wait: wait :: Port.WaitMode = #'all

) :: NonnegInt || Port.EOF

Like Port.Input.read_bytes, but delivering the read bytes to a mutable byte string, bytes. The read bytes are written to the start through end substring of bytes. The result is the number of bytes written to bytes; the result is 0 only when start == end, otherwise Port.eof is returned if no bytes are read due to an end-of-file.

If wait is #'all, then end-start bytes are written to bytes unless an end-of-file is found in the input. If wait is #'some, then the number of read bytes may be fewer, but reading will wait until at least one byte is read or an end-of-file is found. If wait is #'none, then reading will always return without waiting, even if no bytes are immediately available. If wait is #'enable_break, then waiting is like #'some, but asynchronous break exceptions are enabled during the wait; in that case, if breaks are disabled before the call to Port.Input.read_bytes_to either some bytes will be read or a break exception will be thrown, but not both.

method

method (in :: Port.Input).read_char(

  ~special_wrap: special_wrap :: maybe(Any -> Any) = #false,

  ~source_name: source_name :: Any = #false

) :: Char || Port.EOF || Any

Reads a single character from in which may involve reading several bytes to UTF-8-decode them into a character; a minimal number of bytes are read/peeked to perform the decoding. If no bytes are available before an end-of-file, then Port.eof is returned.

The result can be a non-Char, non-Port.eof value if special_wrap is not #false. The special_wrap and source_name argument as used as by Port.peek_byte.

Returns a string containing the next amount characters from in.

If amount is 0, then the empty string is returned. Otherwise, if fewer than amount characters are available before an end-of-file is encountered, then the returned string will contain only those characters before the end-of-file; that is, the returned string’s length will be less than amount. (A temporary string of size amount is allocated while reading the input, even if the size of the result is less than amount characters.) If no characters are available before an end-of-file, then Port.eof is returned.

method

method (in :: Port.Input).read_string_to(

  str :: String,

  ~start: start :: NonnegInt = 0,

  ~end: end :: NonnegInt = str.length()

) :: NonnegInt || Port.EOF

Like Port.Input.read_bytes_to, but delivering decoded characters to a mutable string, str in its start through end substring.

method

method (in :: Port.Input).read_bytes_line(

  ~mode: mode :: Port.Input.ReadLineMode = #'any

) :: Bytes || Port.EOF

 

method

method (in :: Port.Input).read_line(

  ~mode: mode :: Port.Input.ReadLineMode = #'any

) :: String || Port.EOF

Returns a byte string or string containing the next line of bytes or characters from in.

Bytes are read from in until a line separator or an end-of-file is read. The line separator is not included in the result string (but it is removed from the port’s stream). If no characters are read before an end-of-file is encountered, Port.eof is returned.

The mode argument determines the line separator(s). It must be one of the following symbols:

method

method (in :: Port.Input).peek_byte(

  ~skip_bytes: skip :: NonnegInt = 0,

  ~special_wrap: special_wrap :: maybe(Any -> Any) = #false,

  ~source_name: source_name :: Any = #false

):: Byte || Port.EOF

Like Port.Input.read_byte, but peeks instead of reading, and skips skip bytes at the start of the port.

method

method (in :: Port.Input).peek_bytes(

  amount :: NonnegInt,

  ~skip_bytes: skip :: NonnegInt = 0

) :: Bytes || Port.EOF

Like Port.Input.read_bytes, but peeks instead of reading, and skips skip bytes at the start of the port.

method

method (in :: Port.Input).peek_bytes_to(

  bytes :: MutableBytes,

  ~start: start :: NonnegInt = 0,

  ~end: end :: NonnegInt = bytes.length(),

  ~skip_bytes: skip :: NonnegInt = 0,

  ~wait: wait :: Port.WaitMode = #'all,

  ~progress: progress :: maybe(Evt) = #false

) :: NonnegInt || Port.EOF

Like Port.Input.peek_bytes, but delivering the read bytes to a mutable byte string, bytes. The read bytes are written to the start through end substring of bytes. The result is the number of bytes written to bytes.

If wait is not #'all, then progress can be an event produced by Port.Input.Progress.evt(port). Bytes are peeked only when progress is not ready for synchronization, otherwise the result is 0.

method

method (in :: Port.Input).peek_char(

  ~skip_bytes: skip :: NonnegInt = 0,

  ~special_wrap: special_wrap :: maybe(Any -> Any) = #false,

  ~source_name: source_name :: Any = #false

) :: Char || Port.EOF

Like Port.Input.read_char, but peeks instead of reading, and skips skip bytes (not characters) at the start of the port.

method

method (in :: Port.Input).peek_string(

  amount :: NonnegInt,

  ~skip_bytes: skip :: NonnegInt = 0

) :: String || Port.EOF

Like Port.Input.read_string, but peeks instead of reading, and skips skip bytes at the start of the port.

method

method (in :: Port.Input).peek_string_to(

  str :: MutableString,

  ~start: start :: NonnegInt = 0,

  ~end: end :: NonnegInt = str.length(),

  ~skip_bytes: skip :: NonnegInt = 0

) :: NonnegInt || Port.EOF

Like Port.Input.peek_string, but delivering decoded characters to a mutable string, str in its start through end substring.

method

method (in :: Port.Input).copy_to(out :: Port.Output, ...) :: Void

Read from in until an end-of-file and writes all read content to out. Bytes are read from in as available and written to each out before continuing (as opposed to waiting until all data can be read from in).

method

method (port :: Port.Input.Progress).evt() :: Evt

 

method

method (port :: Port.Input.Progress).is_evt(e :: Evt) :: Boolean

The Port.Input.Progress.evt method returns a a synchronizable evt that becomes ready for synchronization when data is read from port. The Port.Input.Progress.evt method check whether e belongs to port.

method

method (port :: Port.Input.Progress).commit(

  amt :: NonnegInt,

  progress :: Evt,

  evt :: Evt

) :: Boolean

Attempts to commit as read the first amt previously peeked bytes, non-byte specials, and Port.eofs from port, or the first Port.eof or special value peeked from post. Mid-stream Port.eofs can be committed, but a Port.eof when the port is exhausted does not necessarily commit, since it does not correspond to data in the stream.

The read commits only if progress does not become ready first (i.e., if no other process reads from port first), and only if evt is chosen by a sync within port-commit-peeked (in which case the event result is ignored); the evt must be either a channel-put event, channel, semaphore, semaphore-peek event, “always” event, or “never” event. Suspending the thread that calls Port.Input.Progress.commit may or may not prevent the commit from proceeding.

The result is #true if data has been committed, and #false otherwise.

If no data has been peeked from port and progress is not ready, then an Exn.Fail.Annot exception is thrown. If fewer than amt items have been peeked at the current start of port’s stream, then only the peeked items are committed as read. If port’s stream currently starts at a Port.eof or a non-byte special value, then only the Port.eof or special value is committed as read.

If progress is not a result of Port.Input.Progress.evt(port), then an Exn.Fail.Annot exception is thrown.

Line reading modes for Port.Input.read_line.