2007-05-05 18:32:56 -04:00
|
|
|
|
|
|
|
(library (ikarus io-primitives unsafe)
|
|
|
|
(export $write-char $read-char $unread-char $peek-char
|
|
|
|
$reset-input-port! $flush-output-port
|
|
|
|
$close-input-port $close-output-port)
|
|
|
|
(import
|
|
|
|
(ikarus)
|
2007-05-06 18:28:21 -04:00
|
|
|
(ikarus system $ports)
|
|
|
|
(ikarus system $strings)
|
2007-05-17 06:27:59 -04:00
|
|
|
(ikarus system $chars)
|
|
|
|
(ikarus system $bytevectors)
|
2007-05-06 18:28:21 -04:00
|
|
|
(ikarus system $fx))
|
2007-05-05 18:32:56 -04:00
|
|
|
|
|
|
|
(define $write-char
|
|
|
|
(lambda (c p)
|
|
|
|
(let ([idx (port-output-index p)])
|
|
|
|
(if ($fx< idx ($port-output-size p))
|
|
|
|
(begin
|
2007-05-17 05:41:00 -04:00
|
|
|
(string-set! ($port-output-buffer p) idx c)
|
2007-05-05 18:32:56 -04:00
|
|
|
($set-port-output-index! p ($fxadd1 idx)))
|
|
|
|
(($port-handler p) 'write-char c p)))))
|
|
|
|
|
|
|
|
(define $read-char
|
|
|
|
(lambda (p)
|
|
|
|
(let ([idx ($port-input-index p)])
|
|
|
|
(if ($fx< idx ($port-input-size p))
|
2007-05-17 06:27:59 -04:00
|
|
|
(let ([b ($bytevector-u8-ref ($port-input-buffer p) idx)])
|
|
|
|
(cond
|
|
|
|
[($fx<= b 127)
|
|
|
|
($set-port-input-index! p ($fxadd1 idx))
|
|
|
|
($fixnum->char b)]
|
|
|
|
[else (($port-handler p) 'read-char p)]))
|
|
|
|
(($port-handler p) 'read-char p)))))
|
2007-05-05 18:32:56 -04:00
|
|
|
|
|
|
|
(define $peek-char
|
|
|
|
(lambda (p)
|
|
|
|
(let ([idx ($port-input-index p)])
|
|
|
|
(if ($fx< idx ($port-input-size p))
|
2007-05-17 06:27:59 -04:00
|
|
|
(let ([b ($bytevector-u8-ref ($port-input-buffer p) idx)])
|
|
|
|
(cond
|
|
|
|
[($fx<= b 127)
|
|
|
|
($fixnum->char b)]
|
|
|
|
[else (($port-handler p) 'peek-char p)]))
|
2007-05-05 18:32:56 -04:00
|
|
|
(($port-handler p) 'peek-char p)))))
|
|
|
|
|
|
|
|
(define $unread-char
|
|
|
|
(lambda (c p)
|
2007-05-17 06:27:59 -04:00
|
|
|
(let ([idx ($fxsub1 ($port-input-index p))]
|
|
|
|
[b ($char->fixnum c)])
|
|
|
|
(if (and ($fx<= b 127)
|
|
|
|
($fx>= idx 0)
|
2007-05-05 18:32:56 -04:00
|
|
|
($fx< idx ($port-input-size p)))
|
|
|
|
(begin
|
|
|
|
($set-port-input-index! p idx)
|
2007-05-17 06:27:59 -04:00
|
|
|
($bytevector-set! ($port-input-buffer p) idx b))
|
2007-05-05 18:32:56 -04:00
|
|
|
(($port-handler p) 'unread-char c p)))))
|
|
|
|
|
|
|
|
(define $reset-input-port!
|
|
|
|
(lambda (p)
|
|
|
|
($set-port-input-size! p 0)))
|
|
|
|
|
|
|
|
(define $close-input-port
|
|
|
|
(lambda (p)
|
|
|
|
(($port-handler p) 'close-port p)))
|
|
|
|
|
|
|
|
(define $close-output-port
|
|
|
|
(lambda (p)
|
|
|
|
(($port-handler p) 'close-port p)))
|
|
|
|
|
|
|
|
(define $flush-output-port
|
|
|
|
(lambda (p)
|
|
|
|
(($port-handler p) 'flush-output-port p))))
|