Node:Port Implementation, Previous:C Port Interface, Up:I/O Extensions
This section describes how to implement a new port type in C.
As described in the previous section, a port type object (ptob) is
a structure of type scm_ptob_descriptor
. A ptob is created by
calling scm_make_port_type
.
All of the elements of the ptob, apart from name
, are procedures
which collectively implement the port behaviour. Creating a new port
type mostly involves writing these procedures.
scm_make_port_type
initializes three elements of the structure
(name
, fill_input
and write
) from its arguments.
The remaining elements are initialized with default values and can be
set later if required.
name
scm_ptob_descriptor
which is not
a procedure. Set via the first argument to scm_make_port_type
.
mark
SCM
components. Set using scm_set_port_mark
.
free
scm_set_port_free
.
print
write
is called on the port object, to print a
port description. e.g., for an fport it may produce something like:
#<input: /etc/passwd 3>
. Set using scm_set_port_print
.
equalp
scm_set_port_equalp
.
close
scm_set_port_close
.
write
scm_make_port_type
.
flush
rw_active
to SCM_PORT_NEITHER
.
Set using scm_set_port_flush
.
end_input
rw_active
to SCM_PORT_NEITHER
.
Set using scm_set_port_end_input
.
fill_input
scm_make_port_type
.
input_waiting
rw_active
is SCM_PORT_NEITHER
.
Set using scm_set_port_input_waiting
.
seek
rw_active
when it's
called. It can reset the buffers first if desired by using something
like:
if (pt->rw_active == SCM_PORT_READ) scm_end_input (object); else if (pt->rw_active == SCM_PORT_WRITE) ptob->flush (object);
However note that this will have the side effect of discarding any data
in the unread-char buffer, in addition to any side effects from the
end_input
and flush
ptob procedures. This is undesirable
when seek is called to measure the current position of the port, i.e.,
(seek p 0 SEEK_CUR)
. The libguile fport and string port
implementations take care to avoid this problem.
The procedure is set using scm_set_port_seek
.
truncate
rw_active
is SCM_PORT_NEITHER
.
Set using scm_set_port_truncate
.