diff --git a/scsh/rw.scm b/scsh/rw.scm index c0ff5b3..28b4ade 100644 --- a/scsh/rw.scm +++ b/scsh/rw.scm @@ -21,28 +21,50 @@ (cond ((integer? fd/port) (let ((port (fdes->inport fd/port))) - (set-port-buffering port bufpol/block (- end start)) - (read-string!/partial port start end))) + (set-port-buffering port bufpol/none) + (read-string!/partial s port start end))) - (else ; no differnce between fd/ports and s48 ports - (let* ((buffer (make-string (- end start))) - (needed (if (> (byte-vector-length (port-buffer fd/port)) 1) - 'any 'immediate)) ;bufpol/none may return with 0 - (len (read-block buffer 0 needed fd/port))) - (if (eof-object? len) - #f - (begin - (copy-bytes! buffer 0 s start len) - len))))))) + ((open-input-port? fd/port) + (if (= start end) + 0 + (let* ((buffer (if (= start 0) + s + (make-string (- end start)))) + (needed (if (> (byte-vector-length (port-buffer fd/port)) 1) + 'any + 'immediate)) ;bufpol/none may return with 0 + (nread (read-block buffer 0 needed fd/port))) + (if (eof-object? nread) + #f + (begin + (if (not (eq? s buffer)) + (copy-bytes! buffer 0 s start nread)) + nread))))) + + (else + (apply error "Not a fd/port in read-string!/partial" s args))))) (define (read-string/partial len . maybe-fd/port) - (let* ((s (make-string len)) - (fd/port (:optional maybe-fd/port (current-input-port))) - (nread (read-string!/partial s fd/port 0 len))) - (cond ((not nread) #f) ; EOF - ((= nread len) s) - (else (substring s 0 nread))))) + (let* ((fd/port (:optional maybe-fd/port (current-input-port)))) + (cond ((integer? fd/port) + (let ((port (fdes->inport fd/port))) + (set-port-buffering port bufpol/none) + (read-string/partial len port))) + + ((open-input-port? fd/port) + (if (= len 0) + 0 + (let* ((buffer (make-string len)) + (needed (if (> (byte-vector-length (port-buffer fd/port)) 1) + 'any + 'immediate));; bufpol/none may return with 0 + (nread (read-block buffer 0 needed fd/port))) + (cond ((eof-object? nread) #f) + ((= nread len) buffer) + (else (substring buffer 0 nread)))))) + (else + (error "Not a fd/port in read-string/partial" len fd/port))))) ;;; Persistent reading