Page 1 of 1

reading lines from stream

Posted: Tue Jun 07, 2005 10:10 pm
by methodic
just to clarify, a stream could be anything, all we can about is capturing on STDIN

Anyway, I have this code:

(do-until (= n "QUIT")
(set 'n (read-line))
(print n)
)

Basically everytime I read a line (termianted by CLRF), I want to be able to do something to what I just got (in this case, print it). I think this is a blocking issue, so is there a way to tell newlisp, globally, to do non-blocking on all IO operations?

Thanks.

Posted: Wed Jun 08, 2005 12:55 am
by Lutz
'read-line' by definition blocks until it receives a line termination character. I guess you are using your program in a UNIX pipe?

I rearranged your program so it can handle the end of input from STDIN when 'read-line' returns 'nil'. And it will work just fine printing out every line until a line with 'quit' is encountered.

Code: Select all

; pipedemo

(while (set 'n (read-line))
        (println n)
        (if (= n "QUIT") (exit)))
You could use this as a pipe:

Code: Select all

newlisp pipdemo < sometext.txt
and it will print the lines in a shell windows until STDIN is exhausted or a line containing QUIT comes by.

Your code will do fine if there is a 'QUIT' line, but else, when used in a pipe will spew out 'nil' after 'nil when STDIN is exausted.

Lutz