(sagittarius io) - Extra IO library

Library (sagittarius io)

This library provided extra IO related procedures.

Function with-input-from-port port thunk
Function with-output-to-port port thunk
Function with-error-to-port port thunk

Calls thunk. During evaluation of thunk, the current input port, current output port, current error port are set to port, respectively.

Function call-with-input-string str proc
Function call-with-output-string proc
Function with-input-from-string str thunk
Function with-output-to-string thunk

These utility functions are trivially defined as follows;

(define (call-with-input-string str proc)
  (proc (open-input-string str)))

(define (call-with-output-string proc)
  (let ((port (open-output-string)))
    (proc port)
    (get-output-string port)))

(define (with-input-from-string str thunk)
  (with-input-from-port (open-input-string str) thunk))

(define (with-output-to-string thunk)
  (let ((port (open-output-string)))
    (with-output-to-port port thunk)
    (get-output-string port)))
Function buffered-port port buffer-mode :key buffer
Function transcoded-port port transcoder

Re-export of buffered-port and transcoded-port.

Custom ports

Sagittarius provides means to create user defined ports. One of the ways is using R6RS custom port procedures. The other one is extending custom port class. The followings show how to extend it.

;; example for input port
(import (rnrs) (sagittarius io) (clos user))

;; make a custom binary input port with 'read slot
(get-u8 (make <custom-binary-input-port>
          :read (lambda (bv start count)
                  (bytevector-u8-set! bv start 1)
                1)))
;; example for output port
(import (rnrs) (sagittarius io) (clos user))

;; user defined custom binary output port
(define-class <my-port> (<custom-binary-output-port>) 
  ;; this port has own buffer 
  ((buffer :init-form (make-bytevector 5 0))))

;; create the port
(let ((out (make <my-port>)))
  ;; set 'write slot
  (slot-set! out 'write
    (lambda (bv start count)
       ;; just get the first element of given bytevector
       ;; and set it to own buffer
       (bytevector-copy! bv start (slot-ref out 'buffer) 0 count)
       count))
  ;; 
  (put-bytevector out #vu8(1 2 3 4 5))
  (slot-ref out 'buffer))
;; -> #vu8(1 0 0 0 0)

Custom port classes. All of these classes have the following slots:

Slot id

Identifier of the port. Must be string is specified.

Slot read
Slot write
Slot flush
Slot close

All of them must be either procedure or #f.

position procedure must accept 0 argument. The procedure should return the position of the port.

set-position procedure must accept 2 argument, position_and_whence. Whence shall be a symbol of begin, current or end. The procedure should set the position of the port according to the given whence and position.

read procedure must accept 3 argument. bv or string, start and count. The first argument is decided by the port type. If the port is binary port, then bytevector bv is passed. If the port is textual port, then string string is passed. The procedure should fill given bv or string in _count_data elements starting start. And return number of data filled.

write procedure must accept 3 argument. bv or string, start and count. The first argument is decided by the port type. If the port is binary port, then bytevector bv is passed. If the port is textual port, then string string is passed. The procedure should retrieve data from given bv or _string_upto count data elements starting start. And return number of data read.

ready procedure must accept 0 argument. The procedure should return true value if the port is ready to read. Otherwise #f.

flush procedure must accept 0 argument. The procedure should flush the port.

close procedure must accept 0 argument. The procedure should close the port.

If the creating port is input port, then read must be set before any port operation. If the creating port is output port, then writemust be set before any port operation. Other slots are optional.