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 20:05:33 -04:00
|
|
|
|
|
|
|
(library (ikarus io output-strings)
|
2007-10-09 07:56:30 -04:00
|
|
|
(export open-output-string get-output-string with-output-to-string
|
|
|
|
open-string-output-port)
|
2007-05-05 20:05:33 -04:00
|
|
|
(import
|
2007-05-06 17:41:59 -04:00
|
|
|
(ikarus system $strings)
|
2007-05-18 15:47:06 -04:00
|
|
|
(ikarus system $bytevectors)
|
|
|
|
(ikarus system $chars)
|
2007-05-06 17:41:59 -04:00
|
|
|
(ikarus system $fx)
|
|
|
|
(ikarus system $pairs)
|
|
|
|
(ikarus system $ports)
|
|
|
|
(ikarus system $io)
|
2007-10-09 07:56:30 -04:00
|
|
|
(except (ikarus) open-output-string get-output-string with-output-to-string
|
|
|
|
open-string-output-port))
|
2007-05-05 20:05:33 -04:00
|
|
|
|
|
|
|
(define-syntax message-case
|
|
|
|
(syntax-rules (else)
|
|
|
|
[(_ msg args
|
|
|
|
[(msg-name msg-arg* ...) b b* ...] ...
|
|
|
|
[else else1 else2 ...])
|
|
|
|
(let ([tmsg msg] [targs args])
|
|
|
|
(define-syntax match-and-bind
|
|
|
|
(syntax-rules ()
|
|
|
|
[(__ y () body)
|
|
|
|
(if (null? y)
|
|
|
|
body
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'message-case "unmatched" (cons tmsg targs)))]
|
2007-05-05 20:05:33 -04:00
|
|
|
[(__ y (a a* (... ...)) body)
|
|
|
|
(if (pair? y)
|
|
|
|
(let ([a (car y)] [d (cdr y)])
|
|
|
|
(match-and-bind d (a* (... ...)) body))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'message-case "unmatched" (cons tmsg targs)))]))
|
2007-05-05 20:05:33 -04:00
|
|
|
(case tmsg
|
|
|
|
[(msg-name)
|
|
|
|
(match-and-bind targs (msg-arg* ...) (begin b b* ...))] ...
|
|
|
|
[else else1 else2 ...]))]))
|
|
|
|
|
2007-05-18 15:47:06 -04:00
|
|
|
(define concat-old
|
2007-05-05 20:05:33 -04:00
|
|
|
(lambda (str i ls)
|
|
|
|
(let ([n (sum i ls)])
|
2007-05-17 05:41:00 -04:00
|
|
|
(let ([outstr (make-string n)])
|
2007-05-05 20:05:33 -04:00
|
|
|
(let f ([n (copy outstr str i n)] [ls ls])
|
|
|
|
(if (null? ls)
|
|
|
|
outstr
|
|
|
|
(let ([a ($car ls)])
|
2007-05-17 05:41:00 -04:00
|
|
|
(f (copy outstr a (string-length a) n) ($cdr ls)))))))))
|
2007-05-05 20:05:33 -04:00
|
|
|
|
2007-05-18 15:47:06 -04:00
|
|
|
(define concat
|
|
|
|
(lambda (bv i ls)
|
|
|
|
(let ([n (sum i ls)])
|
2007-05-19 22:36:52 -04:00
|
|
|
(let ([outbv ($make-bytevector n)])
|
|
|
|
(let f ([n (copy outbv bv i n)] [ls ls])
|
2007-05-18 15:47:06 -04:00
|
|
|
(if (null? ls)
|
2007-05-20 00:57:28 -04:00
|
|
|
outbv
|
2007-05-18 15:47:06 -04:00
|
|
|
(let ([a ($car ls)])
|
2007-05-19 22:36:52 -04:00
|
|
|
(f (copy outbv a ($bytevector-length a) n) ($cdr ls)))))))))
|
2007-05-05 20:05:33 -04:00
|
|
|
(define sum
|
|
|
|
(lambda (ac ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) ac]
|
2007-05-18 15:47:06 -04:00
|
|
|
[else (sum ($fx+ ac ($bytevector-length ($car ls))) ($cdr ls))])))
|
2007-05-05 20:05:33 -04:00
|
|
|
|
2007-05-18 15:47:06 -04:00
|
|
|
(define sum-old
|
|
|
|
(lambda (ac ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) ac]
|
|
|
|
[else (sum ($fx+ ac (string-length ($car ls))) ($cdr ls))])))
|
|
|
|
|
|
|
|
(define copy-old
|
2007-05-05 20:05:33 -04:00
|
|
|
(lambda (dst src n end)
|
|
|
|
(let f ([di end]
|
|
|
|
[si n])
|
|
|
|
(cond
|
|
|
|
[($fx= si 0) di]
|
|
|
|
[else
|
|
|
|
(let ([di ($fxsub1 di)] [si ($fxsub1 si)])
|
2007-05-17 05:41:00 -04:00
|
|
|
(string-set! dst di (string-ref src si))
|
2007-05-05 20:05:33 -04:00
|
|
|
(f di si))]))))
|
2007-05-18 15:47:06 -04:00
|
|
|
|
|
|
|
(define copy
|
|
|
|
(lambda (dst src n end)
|
|
|
|
(let f ([di end]
|
|
|
|
[si n])
|
|
|
|
(cond
|
|
|
|
[($fx= si 0) di]
|
|
|
|
[else
|
|
|
|
(let ([di ($fxsub1 di)] [si ($fxsub1 si)])
|
2007-05-19 22:36:52 -04:00
|
|
|
($bytevector-set! dst di ($bytevector-u8-ref src si))
|
2007-05-18 15:47:06 -04:00
|
|
|
(f di si))]))))
|
|
|
|
|
2007-05-18 16:15:40 -04:00
|
|
|
(define bv-copy
|
|
|
|
(lambda (src)
|
|
|
|
(let ([n ($bytevector-length src)])
|
|
|
|
(let f ([src src] [dst ($make-bytevector n)] [i 0] [n n])
|
|
|
|
(cond
|
|
|
|
[($fx= i n) dst]
|
|
|
|
[else
|
|
|
|
($bytevector-set! dst i ($bytevector-u8-ref src i))
|
|
|
|
(f src dst ($fxadd1 i) n)])))))
|
|
|
|
|
2007-05-18 15:47:06 -04:00
|
|
|
|
2007-05-05 20:05:33 -04:00
|
|
|
|
2007-05-18 15:47:06 -04:00
|
|
|
(define make-output-string-handler
|
|
|
|
(lambda ()
|
|
|
|
(define buffer-list '())
|
|
|
|
(define open? #t)
|
|
|
|
(define output-handler
|
|
|
|
(lambda (msg . args)
|
|
|
|
(message-case msg args
|
|
|
|
[(write-byte b p)
|
|
|
|
(if (and (fixnum? b) ($fx<= 0 b) ($fx<= b 255))
|
|
|
|
(if (output-port? p)
|
2007-08-25 11:24:05 -04:00
|
|
|
(let ([idx ($port-index p)])
|
|
|
|
(if ($fx< idx ($port-size p))
|
2007-05-18 15:47:06 -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 16:15:40 -04:00
|
|
|
(if open?
|
2007-08-25 11:24:05 -04:00
|
|
|
(let ([buff ($port-buffer p)])
|
2007-05-18 16:15:40 -04:00
|
|
|
(set! buffer-list (cons (bv-copy buff) buffer-list))
|
|
|
|
($bytevector-set! buff 0 b)
|
2007-08-25 11:24:05 -04:00
|
|
|
($set-port-index! p 1))
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'write-byte "port is closed" p))))
|
|
|
|
(error 'write-byte "not an output-port" p))
|
|
|
|
(error 'write-byte "not a byte" b))]
|
2007-05-18 15:47:06 -04:00
|
|
|
[(write-char c p)
|
|
|
|
(if (char? c)
|
|
|
|
(if (output-port? p)
|
|
|
|
(let ([b ($char->fixnum c)])
|
2007-06-01 22:17:22 -04:00
|
|
|
(if ($fx<= b 127)
|
2007-05-18 15:47:06 -04:00
|
|
|
($write-byte b p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'write-char
|
|
|
|
"BUG: multibyte write of is not implemented" c)))
|
|
|
|
(error 'write-char "not an output-port" p))
|
|
|
|
(error 'write-char "not a character" c))]
|
2007-05-18 15:47:06 -04:00
|
|
|
[(flush-output-port p)
|
|
|
|
(void)]
|
|
|
|
[(close-port p)
|
|
|
|
(set! open? #f)]
|
|
|
|
[(port-name p) 'string-port]
|
|
|
|
[(get-output-string p)
|
2007-10-12 00:33:19 -04:00
|
|
|
(utf8->string
|
2007-05-20 00:57:28 -04:00
|
|
|
(concat
|
2007-08-25 11:24:05 -04:00
|
|
|
($port-buffer p)
|
|
|
|
($port-index p)
|
2007-05-20 00:57:28 -04:00
|
|
|
buffer-list))]
|
2007-10-25 14:32:26 -04:00
|
|
|
[else
|
|
|
|
(error 'output-handler "unhandled message" (cons msg args))])))
|
2007-05-18 15:47:06 -04:00
|
|
|
output-handler))
|
|
|
|
|
2007-05-05 20:05:33 -04:00
|
|
|
(define open-output-string
|
|
|
|
(lambda ()
|
|
|
|
(make-output-port
|
|
|
|
(make-output-string-handler)
|
2007-05-18 16:15:40 -04:00
|
|
|
($make-bytevector 59))))
|
2007-05-05 20:05:33 -04:00
|
|
|
|
|
|
|
(define get-output-string
|
|
|
|
(lambda (p)
|
|
|
|
(if (output-port? p)
|
|
|
|
(($port-handler p) 'get-output-string p)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'get-output-string "not an output port" p))))
|
2007-05-05 20:05:33 -04:00
|
|
|
|
|
|
|
(define with-output-to-string
|
|
|
|
(lambda (f)
|
|
|
|
(unless (procedure? f)
|
2007-10-25 14:32:26 -04:00
|
|
|
(error 'with-output-to-string "not a procedure" f))
|
2007-05-05 20:05:33 -04:00
|
|
|
(let ([p (open-output-string)])
|
|
|
|
(parameterize ([current-output-port p]) (f))
|
|
|
|
(get-output-string p))))
|
|
|
|
|
2007-10-09 07:56:30 -04:00
|
|
|
(define (open-string-output-port)
|
|
|
|
(let ([p (open-output-string)])
|
|
|
|
;;; FIXME: should empty string
|
|
|
|
(values p (lambda () (get-output-string p)))))
|
|
|
|
|
2007-05-05 20:05:33 -04:00
|
|
|
|
|
|
|
)
|