2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
|
|
|
;;; Copyright (C) 2006,2007 Abdulaziz Ghuloum
|
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-05 18:04:38 -04:00
|
|
|
|
|
|
|
(library (ikarus io-ports)
|
2007-08-26 20:04:00 -04:00
|
|
|
(export make-input-port make-output-port
|
2007-05-05 18:04:38 -04:00
|
|
|
port-handler
|
|
|
|
port-input-buffer port-output-buffer
|
|
|
|
port-input-index set-port-input-index!
|
|
|
|
port-input-size set-port-input-size!
|
|
|
|
port-output-index set-port-output-index!
|
|
|
|
port-output-size set-port-output-size!)
|
|
|
|
(import
|
2007-05-06 18:25:53 -04:00
|
|
|
(ikarus system $ports)
|
|
|
|
(ikarus system $strings)
|
2007-05-17 06:27:59 -04:00
|
|
|
(ikarus system $bytevectors)
|
2007-05-06 18:25:53 -04:00
|
|
|
(ikarus system $fx)
|
2007-08-25 10:49:39 -04:00
|
|
|
(except (ikarus)
|
|
|
|
make-input-port make-output-port
|
|
|
|
port-handler
|
2007-05-05 18:04:38 -04:00
|
|
|
port-input-buffer port-output-buffer
|
|
|
|
port-input-index set-port-input-index!
|
|
|
|
port-input-size set-port-input-size!
|
|
|
|
port-output-index set-port-output-index!
|
|
|
|
port-output-size set-port-output-size!))
|
|
|
|
;;; GENERIC PORTS: BASIC PRIMITIVES
|
|
|
|
;;;
|
|
|
|
;;; Exports:
|
|
|
|
;;; * Constructors:
|
|
|
|
;;; (make-input-port handler input-buffer)
|
|
|
|
;;; (make-output-port handler output-buffer)
|
|
|
|
;;;
|
|
|
|
;;; * Predicates:
|
|
|
|
;;; (port? x)
|
|
|
|
;;; (input-port? x)
|
|
|
|
;;; (output-port? x)
|
|
|
|
;;;
|
|
|
|
;;; * Accessors:
|
|
|
|
;;; (port-handler port)
|
2007-08-26 20:04:00 -04:00
|
|
|
;;; (port-buffer port)
|
|
|
|
;;; (port-index port)
|
|
|
|
;;; (port-size port)
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
;;; * Mutators:
|
2007-08-26 20:04:00 -04:00
|
|
|
;;; (set-port-index! port fixnum)
|
|
|
|
;;; (set-port-size! port fixnum)
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define $make-input-port
|
|
|
|
(lambda (handler buffer)
|
2007-08-25 11:24:05 -04:00
|
|
|
($make-port/input handler buffer 0 ($bytevector-length buffer))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define make-input-port
|
|
|
|
(lambda (handler buffer)
|
|
|
|
(if (procedure? handler)
|
2007-05-17 06:27:59 -04:00
|
|
|
(if (bytevector? buffer)
|
2007-05-05 18:04:38 -04:00
|
|
|
($make-input-port handler buffer)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'make-input-port "not a bytevector" buffer))
|
|
|
|
(error 'make-input-port "not a procedure" handler))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define $make-output-port
|
|
|
|
(lambda (handler buffer)
|
2007-08-25 11:24:05 -04:00
|
|
|
($make-port/output handler buffer 0 ($bytevector-length buffer))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define make-output-port
|
|
|
|
(lambda (handler buffer)
|
|
|
|
(if (procedure? handler)
|
2007-05-18 16:07:58 -04:00
|
|
|
(if (bytevector? buffer)
|
2007-05-05 18:04:38 -04:00
|
|
|
($make-output-port handler buffer)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'make-output-port "not a bytevector" buffer))
|
|
|
|
(error 'make-output-port "not a procedure" handler))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-handler
|
|
|
|
(lambda (x)
|
|
|
|
(if (port? x)
|
|
|
|
($port-handler x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-handler "not a port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-input-buffer
|
|
|
|
(lambda (x)
|
|
|
|
(if (input-port? x)
|
2007-08-25 11:06:30 -04:00
|
|
|
($port-buffer x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-input-buffer "not an input-port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-input-index
|
|
|
|
(lambda (x)
|
|
|
|
(if (input-port? x)
|
2007-08-25 11:06:30 -04:00
|
|
|
($port-index x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-input-index "not an input-port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-input-size
|
|
|
|
(lambda (x)
|
|
|
|
(if (input-port? x)
|
2007-08-25 11:06:30 -04:00
|
|
|
($port-size x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-input-size "not an input-port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-output-buffer
|
|
|
|
(lambda (x)
|
|
|
|
(if (output-port? x)
|
2007-08-25 11:24:05 -04:00
|
|
|
($port-buffer x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-output-buffer "not an output-port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-output-index
|
|
|
|
(lambda (x)
|
|
|
|
(if (output-port? x)
|
2007-08-25 11:24:05 -04:00
|
|
|
($port-index x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-output-index "not an output-port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define port-output-size
|
|
|
|
(lambda (x)
|
|
|
|
(if (output-port? x)
|
2007-08-25 11:24:05 -04:00
|
|
|
($port-size x)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'port-output-size "not an output-port" x))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define set-port-input-index!
|
|
|
|
(lambda (p i)
|
|
|
|
(if (input-port? p)
|
|
|
|
(if (fixnum? i)
|
|
|
|
(if ($fx>= i 0)
|
2007-08-25 11:06:30 -04:00
|
|
|
(if ($fx<= i ($port-size p))
|
|
|
|
($set-port-index! p i)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'set-port-input-index! "index is too big" i))
|
|
|
|
(error 'set-port-input-index! "index is negative" i))
|
|
|
|
(error 'set-port-input-index! "not a valid index" i))
|
|
|
|
(error 'set-port-input-index! "not an input-port" p))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define set-port-input-size!
|
|
|
|
(lambda (p i)
|
|
|
|
(if (input-port? p)
|
|
|
|
(if (fixnum? i)
|
|
|
|
(if ($fx>= i 0)
|
2007-08-25 11:06:30 -04:00
|
|
|
(if ($fx<= i ($bytevector-length ($port-buffer p)))
|
2007-05-05 18:04:38 -04:00
|
|
|
(begin
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-index! p 0)
|
|
|
|
($set-port-size! p i))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'set-port-input-size! "size is too big" i))
|
|
|
|
(error 'set-port-input-size! "size is negative" i))
|
|
|
|
(error 'set-port-input-size! "not a valid size" i))
|
|
|
|
(error 'set-port-input-size! "not an input-port" p))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define set-port-output-index!
|
|
|
|
(lambda (p i)
|
|
|
|
(if (output-port? p)
|
|
|
|
(if (fixnum? i)
|
|
|
|
(if ($fx>= i 0)
|
2007-08-25 11:24:05 -04:00
|
|
|
(if ($fx<= i ($port-size p))
|
|
|
|
($set-port-index! p i)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'set-port-output-index! "index is too big" i))
|
|
|
|
(error 'set-port-output-index! "index is negative" i))
|
|
|
|
(error 'set-port-output-index! "not a valid index" i))
|
|
|
|
(error 'set-port-output-index! "not an output-port" p))))
|
2007-05-05 18:04:38 -04:00
|
|
|
;;;
|
|
|
|
(define set-port-output-size!
|
|
|
|
(lambda (p i)
|
|
|
|
(if (output-port? p)
|
|
|
|
(if (fixnum? i)
|
|
|
|
(if ($fx>= i 0)
|
2007-08-25 11:24:05 -04:00
|
|
|
(if ($fx<= i ($bytevector-length ($port-buffer p)))
|
2007-05-05 18:04:38 -04:00
|
|
|
(begin
|
2007-08-25 11:24:05 -04:00
|
|
|
($set-port-index! p 0)
|
|
|
|
($set-port-size! p i))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'set-port-output-size! "size is too big" i))
|
|
|
|
(error 'set-port-output-size! "size is negative" i))
|
|
|
|
(error 'set-port-output-size! "not a valid size" i))
|
|
|
|
(error 'set-port-output-size! "not an output-port" p)))))
|
2007-08-26 20:04:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
|