I need to implement a function to read from a port until a certain char is reached, I want it to stop if there is no such char is retrieved from the string. The original intent is to retrieve data from a network connection, but I am testing it with a string for now, why does it fail with a: "*** ERROR IN ##thread-deadlock-action! -- Deadlock detected"? Please find the code below, Regards, Francisco
;; Reads from connection until stop-char appears (define (read-string-until-char connection stop-char) (letrec ((str "") (reader (lambda () (let ((c (read-char connection))) (cond ((or (char=? c stop-char) (eof-object? c)) str) (else (set! str (string-append-char str c)) (reader))))))) (reader) str))
;; Reads from connection until stop-char appears (define (read-string-until-char connection stop-char) (letrec ((str "") (reader (lambda () (let ((c (read-char connection))) (cond ((or (char=? c stop-char) ;it never stops here!! (eof-object? c)) str) (else (set! str (string-append-char str c)) (reader))))))) (reader) str))
;; Appends a char to a string (define (string-append-char str c) (let* ((expanded-str (string-append str " ")) (position (string-length str))) (string-set! expanded-str position c) expanded-str))
(define (with-string-stop str c) (let ((port (open-string str))) (read-string-until-char port c)))
(display (with-string-stop "123456" #\5)) (display (with-string-stop "123456" #\7))