On this page:
Port
Port.eof
Port.eof
Port.EOF
Port.close
Port.is_  closed
Port.buffer
Port.position
Port.locations_  enabled
Port.current_  enable_  locations
Port.next_  location
Port.Output
Port.name
Port.Mode
Port.Mode.binary
Port.Mode.text
Port.Buffer  Mode
Port.Buffer  Mode.none
Port.Buffer  Mode.line
Port.Buffer  Mode.block
Port.Wait  Mode
Port.Wait  Mode.all
Port.Wait  Mode.some
Port.Wait  Mode.none
Port.Wait  Mode.enable_  break
8.15.0.12

13.1 Ports🔗ℹ

A port is an input or output stream for a file, network connection, terminal, etc. An input port is specifically for input, while an output port is specifically for output; it is possible for an object to be both an input and output port.

annotation

Port

The Port annotation is satisfied by a port. See also Port.Input and Port.Output.

value

def Port.eof :: Port.EOF

 

binding operator

Port.eof

 

annotation

Port.EOF

The Port.eof value represents an end-of-file (distinct from all other values), and the Port.eof binding match matches that value.

The Port.EOF annotation is satisfied by the Port.eof value.

method

method (port :: Port).close() :: Void

 

method

method (port :: Port).is_closed() :: Boolean

Closes a port, equivalent to Port.Input.close or Port.Output.close, or checks whether a port has been closed. Closing an already-closed port has no effect.

method

method (port :: Port).buffer() :: maybe(Port.BufferMode)

 

method

method (port :: Port).buffer(mode :: Port.BufferMode) :: Void

Gets or sets the buffer mode for port as #'none, #'line (output only), or #'block. An exception is thrown if a port does not support a provided buffer mode. When no mode argument is provided, #false is returned if the buffer mode cannot be determined.

A port’s default buffer mode depends on the communication channel that it represents. A file port is nomrally #'block buffered. The initial stdin port is #'block buffered. The initial stdout port is #'line buffered if it writers to a terminal, #'block buffered otherwise. The initial stderr port’s buffer mode is #'none.

method

method (port :: Port).position() :: NonnegInt

 

method

method (port :: Port).position(pos :: NonnegInt || Port.EOF) :: Void

Gets or sets (if supported) a port’s position, which represents an offset in bytes.

Calling Port.position without pos on a port other than a Port.FileStream, Port.Input.String, or Port.Output.String port returns the number of bytes that have been read from that port if the position is known.

For Port.FileStream, Port.Input.String, or Port.Output.String ports, providing pos sets the read/write position relative to the beginning of the file or (byte) string if pos is a number, or to the current end of the file or (byte) string if pos is Port.eof. For other kinds of ports, an exception is thrown when pos is supplied. Furthermore, not all Port.FileStream ports support setting the position; if pos is supplied for such a port, the Exn.Fail.Filesystem exception is thrown.

When file-position sets the position beyond the current size of an output file or (byte) string, the file/string is enlarged to size pos, and the new region is filled with 0 bytes in the case of a file. In the case of a file output port, the file might not be enlarged until more data is written to the file; in that case, beware that writing to a file opened in #'append mode on Unix and Mac OS will reset the file pointer to the end of a file before each write, which defeats file enlargement via Port.position. If pos is beyond the end of an input file or (byte) string, then reading thereafter returns eof without changing the port’s position.

When changing the file position for an output port, the port is first flushed if its buffer is not empty. Similarly, setting the position for an input port clears the port’s buffer (even if the new position is the same as the old position). However, although input and output ports produced by open-input-output-file share the file position, setting the position via one port does not flush the other port’s buffer.

method

method (port :: Port).locations_enabled() :: Boolean

 

method

method (port :: Port).locations_enabled(on) :: Void

 

context parameter

Parameter.def Port.current_enable_locations

  :: Any.to_boolean

 

method

method (port :: Port).next_location()

  :: values(maybe(PostInt), maybe(NonnegInt), maybe(PosInt))

 

method

method (port :: Port).next_location(

  line :: maybe(PostInt),

  column :: maybe(NonnegInt),

  offset :: maybe(PosInt)

) :: Void

The Port.locations_enabled method checks or turns on whether line, column, and decoded-character offsets are tracked as bytes are read from a port or written to a port. Calling Port.locations_enabled with one argument attempts to enable or disable his location tracking, but the port’s state may not change, either because it does not support tracking or because it does not support disabling tracking after it’s enabled. The Port.current_enable_locations parameter determines whether tracking is enabled by default for a newly opened port, and its initial value is #false.

The Port.next_location method with zero arguments reports a line, column, and offset for the next character to be read from the port. If tracking is not enabled, then the first two results will be #false, but port.position()+1 may be returned as an approximation for the last result (i.e., a position measured in bytes used as an approximation of the number of characters read). Calling Port.next_location with arguments attempts to set the next location, but the attempt is ignored if location tracking has not been enabled or if the port does not support external adjustments.

function

fun Port.Output(

  path :: PathString,

  ~exists: exists_flag :: Port.Output.ExistsMode = #'error,

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

  ~permissions: permissions :: Int.in(0, 65535) = 0o666,

  ~replace_permissions: replace_permissions = #false

) :: values(Port.Input, Port.Output)

Like Port.Output.open_file, but returns both input and output ports.The two ports are connected in that they share the underlying file descriptor.

This procedure is intended for use with special devices that can be opened by only one process, such as "COM1" in Windows. For regular files, sharing the file descriptor can be confusing. For example, using one port does not automatically flush the other port’s buffer, and reading or writing in one port moves the file position (if any) for the other port. For regular files, use separate Port.Input.open_file and Port.Output.open_file calls to avoid confusion.

method

method (port :: Port).name() :: Any

Returns the port’s name, which is typically represented by a symbol or path, but can be any kind of value. A port’s name is used for printing and as a default for constructing source locations.

enumeration

enum Port.Mode:

  binary

  text

Modes for reading and writing files that determine how newlines are read and written.

enumeration

enum Port.BufferMode:

  none

  line

  block

Buffer modes for input and output ports; see Port.buffer. The #'line buffer mode is supported only for output ports.

enumeration

enum Port.WaitMode:

  all

  some

  none

  enable_break

Modes used for methods like Port.Input.read_bytes_to and Port.Output.write_bytes_to to determine how they block to wait for input or output.