Email or username:

Password:

Forgot your password?
Andrew Tropin

The best way to get all available stuff from textual port I found so far is:
(define (read-all-chars-as-string port)
(let loop ((chars '()))
(if (char-ready? port)
(loop (cons (read-char port) chars))
(reverse-list->string chars))))

#scheme #guile #lisp

5 comments
otterz

@abcdw neat! short and very readable. wouldn't "char-ready?" also return #f in cases other than EOF though?

Andrew Tropin

@otterz yes and it's exactly what I want. I want to read everything available at the moment to start processing what is already came to the pipe.

There is another function, get-string-all from (ice-9 textual-ports), which looks for EOF instead and thus blocks until everything will arrive to the pipe.

Ramin Honary

@abcdw @otterz creating a very large linked-list like this would be very slow though, and the reverse->list function is an O(n) operation.

It might be better to write to a string port instead, and then return the string when (char-ready? port) returns #f.

Andrew Tropin

@ramin_hal9001 @otterz Sounds very reasonable to me! Thank you for the idea, I guess I will use it in the implementation :)

Ramin Honary

@abcdw @otterz No problem!

(define (read-all-chars-as-string in-port)
  (let ((out-port (open-string-port)))
    (let loop ()
      (if (char-ready? in-port)
          (begin
            (write-char (read-char in-port) out-port)
            (loop))
          (get-output-string out-port)))))
Go Up