2007-05-05 18:32:56 -04:00
|
|
|
|
|
|
|
(library (ikarus io-primitives unsafe)
|
2007-05-18 08:15:51 -04:00
|
|
|
(export $write-char $write-byte $read-char $unread-char $peek-char
|
2007-05-05 18:32:56 -04:00
|
|
|
$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)
|
2007-05-19 22:09:30 -04:00
|
|
|
(let ([b ($char->fixnum c)])
|
|
|
|
(cond
|
|
|
|
[($fx<= b #x7F)
|
|
|
|
($write-byte b p)]
|
|
|
|
[($fx<= b #x7FF)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b11000000 ($fxsra b 6)) p)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b10000000 ($fxlogand b #b111111)) p)]
|
|
|
|
[($fx<= b #xFFFF)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b11100000 ($fxsra b 12)) p)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b10000000 ($fxlogand ($fxsra b 6) #b111111)) p)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b10000000 ($fxlogand b #b111111)) p)]
|
|
|
|
[else
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b11110000 ($fxsra b 18)) p)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b10000000 ($fxlogand ($fxsra b 12) #b111111)) p)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b10000000 ($fxlogand ($fxsra b 6) #b111111)) p)
|
|
|
|
($write-byte
|
|
|
|
($fxlogor #b10000000 ($fxlogand b #b111111)) p)]))))
|
2007-05-05 18:32:56 -04:00
|
|
|
|
2007-05-18 08:15:51 -04:00
|
|
|
(define $write-byte
|
|
|
|
(lambda (b p)
|
|
|
|
(let ([idx (port-output-index p)])
|
2007-08-25 11:24:05 -04:00
|
|
|
(if ($fx< idx ($port-size p))
|
2007-05-18 16:07:58 -04:00
|
|
|
(begin
|
2007-08-25 11:24:05 -04:00
|
|
|
($bytevector-set! ($port-buffer p) idx b)
|
|
|
|
($set-port-index! p ($fxadd1 idx)))
|
2007-05-18 08:15:51 -04:00
|
|
|
(($port-handler p) 'write-byte b p)))))
|
|
|
|
|
2007-05-05 18:32:56 -04:00
|
|
|
(define $read-char
|
|
|
|
(lambda (p)
|
2007-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
|
|
|
(let ([b ($bytevector-u8-ref ($port-buffer p) idx)])
|
2007-05-17 06:27:59 -04:00
|
|
|
(cond
|
|
|
|
[($fx<= b 127)
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-index! p ($fxadd1 idx))
|
2007-05-17 06:27:59 -04:00
|
|
|
($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)
|
2007-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
|
|
|
(let ([b ($bytevector-u8-ref ($port-buffer p) idx)])
|
2007-05-17 06:27:59 -04:00
|
|
|
(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-08-25 11:06:30 -04:00
|
|
|
(let ([idx ($fxsub1 ($port-index p))]
|
2007-05-17 06:27:59 -04:00
|
|
|
[b ($char->fixnum c)])
|
|
|
|
(if (and ($fx<= b 127)
|
|
|
|
($fx>= idx 0)
|
2007-08-25 11:06:30 -04:00
|
|
|
($fx< idx ($port-size p)))
|
2007-05-05 18:32:56 -04:00
|
|
|
(begin
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-index! p idx)
|
|
|
|
($bytevector-set! ($port-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)
|
2007-08-25 11:06:30 -04:00
|
|
|
($set-port-size! p 0)))
|
2007-05-05 18:32:56 -04:00
|
|
|
|
|
|
|
(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))))
|