chickadee » ports » make-input-port

make-input-port READ-CHAR CHAR-READY? CLOSE #!optional PEEK-CHAR READ-STRING! READ-LINEprocedure

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:

  • READ-CHAR is the most fundamental reader, and must always be present. It is a thunk which is called when the next character is to be read and it should return a character or #!eof.
  • CHAR-READY? is a thunk which is called when char-ready? is called on this port and should return #t or #f.
  • CLOSE is a thunk which is called when the port is closed.
  • PEEK-CHAR is a thunk which is called when peek-char is called on this port and should return a character or #!eof. If it is not provided or #f, READ-CHAR will be used instead and the created port object handles peeking automatically (by calling READ and buffering the character).
  • READ-STRING! is called when read-string! is called (or the higher-level non-mutating read-string). It will be invoked with 4 arguments: the port created by make-input-port, the number of bytes to read, a string (or sometimes a blob) to read into (which may be assumed to be big enough to hold the data) and the offset into the buffer at which to put the data to read. It should return the number of bytes that have successfully been read, which should always be equal to the requested bytes unless EOF was hit, in which case it can be less. If this procedure is not provided or #f, the buffer will be filled by repeated reads to READ-CHAR.
  • READ-LINE is called when read-line is called. It will be invoked with two arguments: the port created by make-input-port and the maximum number of characters to read (or #f). If this procedure is not provided or #f, the buffer will be filled by repeated reads to READ-CHAR.

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.