1. Module (chicken port)
    1. Port attributes
      1. port-encoding
      2. port-name
      3. port-position
    2. Setting the file buffering mode
      1. set-buffering-mode!
    3. Terminal ports
      1. terminal-name
      2. terminal-port?
      3. terminal-size
    4. Input/output port extensions
      1. with-output-to-port
      2. make-input-port
      3. make-output-port
      4. with-error-output-to-port
      5. with-input-from-port
    5. String-port extensions
      1. call-with-input-string
      2. call-with-output-string
      3. with-input-from-string
      4. with-output-to-string
      5. with-error-output-to-string
    6. Port iterators
      1. port-for-each
      2. port-map
      3. port-fold
      4. copy-port
    7. Funky ports
      1. make-bidirectional-port
      2. make-broadcast-port
      3. make-concatenated-port

Module (chicken port)

This module contains various extended port definitions.

All errors related to failing port operations will signal a condition of kind exn.

Port attributes

port-encoding

[procedure] (port-encoding PORT)

Returns the encoding used for reading and writing data from and to the given port. Encoding is currently one of the symbols utf-8, latin-1 or binary. Note that the encoding can be changed by using the (SRFI-17) setter procedure for this operation:

(set! (port-encoding PORT) ENCODING)

port-name

[procedure] (port-name [PORT])

Fetch filename from PORT. This returns the filename that was used to open this file. Returns a special tag string, enclosed into parentheses for non-file ports. PORT defaults to the value of (current-input-port).

Note that the encoding can be changed by using the (SRFI-17) setter procedure for this operation:

(set! (port-name PORT) STRING)

port-position

[procedure] (port-position [PORT])

Returns the current position of PORT as two values: row and column number. If the port does not support such an operation an error is signaled. This procedure is currently only available for input ports. PORT defaults to the value of (current-input-port).

Setting the file buffering mode

set-buffering-mode!

[procedure] (set-buffering-mode! PORT MODE [BUFSIZE])

Sets the buffering-mode for the file associated with PORT to MODE, which should be one of the keywords #:full, #:line or #:none. If BUFSIZE is specified it determines the size of the buffer to be used (if any).

Terminal ports

terminal-name

[procedure] (terminal-name PORT)

Returns the name of the terminal that is connected to PORT.

On Windows, this procedure always raises an exception.

terminal-port?

[procedure] (terminal-port? PORT)

Returns #t if PORT is connected to a terminal and #f otherwise.

terminal-size

[procedure] (terminal-size PORT)

Returns two values, the number of rows and columns of the terminal that is connected to PORT or 0, 0 if the terminal size can not be obtained.

On Windows, this procedure always raises an exception.

Input/output port extensions

with-output-to-port

[procedure] (with-output-to-port PORT THUNK)

Call procedure THUNK with the current output-port temporarily bound to PORT.

make-input-port

[procedure] (make-input-port READ-CHAR CHAR-READY? CLOSE #!key peek-char read-bytevector read-line)

Returns a custom input port. Common operations on this port are handled by the given parameters, which should be procedures of no arguments. The following arguments are all different kinds of reader procedures:

All the optional procedures except for PEEK-CHAR are responsible for updating the port's position, which currently can only be done via low-level slot accessors like ##sys#setslot; slot 4 is the row number (ie, the line) and slot 5 is the column number (ie, the character on the line). If the port's positions are not updated, port-position won't work.

Note that reading binary input from a custom input is only possible when the read-bytevector operation is given, as byte-input can currently not ben synthesized from character-input operations.

make-output-port

[procedure] (make-output-port WRITE CLOSE #!key force-output)

Returns a custom output port. Common operations on this port are handled by the given parameters, which should be procedures. WRITE is called when output is sent to the port and receives a single argument, a string. CLOSE is called when the port is closed and should be a procedure of no arguments. force-output (if provided) is called for flushing the output port.

with-error-output-to-port

[procedure] (with-error-output-to-port PORT THUNK)

Call procedure THUNK with the current error output-port temporarily bound to PORT.

with-input-from-port

[procedure] (with-input-from-port PORT THUNK)

Call procedure THUNK with the current input-port temporarily bound to PORT.

String-port extensions

call-with-input-string

[procedure] (call-with-input-string STRING PROC)

Calls the procedure PROC with a single argument that is a string-input-port with the contents of STRING.

call-with-output-string

[procedure] (call-with-output-string PROC)

Calls the procedure PROC with a single argument that is a string-output-port. Returns the accumulated output-string.

with-input-from-string

[procedure] (with-input-from-string STRING THUNK)

Call procedure THUNK with the current input-port temporarily bound to an input-string-port with the contents of STRING.

with-output-to-string

[procedure] (with-output-to-string THUNK)

Call procedure THUNK with the current output-port temporarily bound to a string-output-port and return the accumulated output string.

with-error-output-to-string

[procedure] (with-error-output-to-string THUNK)

Call procedure THUNK with the current error output-port temporarily bound to a string-output-port and return the accumulated output string.

Port iterators

port-for-each

[procedure] (port-for-each FN THUNK)

Apply FN to successive results of calling the zero argument procedure THUNK (typically read) until it returns #!eof, discarding the results.

port-map

[procedure] (port-map FN THUNK)

Apply FN to successive results of calling the zero argument procedure THUNK (typically read) until it returns #!eof, returning a list of the collected results.

port-fold

[procedure] (port-fold FN ACC THUNK)

Apply FN to successive results of calling the zero argument procedure THUNK, (typically read) passing the ACC value as the second argument. The FN result becomes the new ACC value. When THUNK returns #!eof, the last FN result is returned.

copy-port

[procedure] (copy-port FROM TO [READ [WRITE]])

Reads all remaining data from port FROM using the reader procedure READ and writes it to port TO using the writer procedure WRITE. READ defaults to read-char and WRITE to write-char. Note that this procedure does not check FROM and TO for being ports, so the reader and writer procedures may perform arbitrary operations as long as they can be invoked as (READ FROM) and (WRITE X TO), respectively. copy-port returns an undefined value.

copy-port was introduced in CHICKEN 4.6.0.

Funky ports

make-bidirectional-port

[procedure] (make-bidirectional-port INPUT-PORT OUTPUT-PORT)

Returns a joint input/output port that proxies port operations to the given INPUT-PORT and OUTPUT-PORT, respectively. This port satisfies both input-port? and output-port?, and its two directions may be closed independently.

make-broadcast-port

[procedure] (make-broadcast-port PORT ...)

Returns a custom output port that emits everything written into it to the ports given as PORT .... Closing the broadcast port does not close any of the argument ports.

make-concatenated-port

[procedure] (make-concatenated-port PORT1 PORT2 ...)

Returns a custom input port that reads its input from PORT1, until it is empty, then from PORT2 and so on. Closing the concatenated port does not close any of the argument ports.


Previous: Module (chicken plist)

Next: Module (chicken pretty-print)