reading lines from stream

Q&A's, tips, howto's
Locked
methodic
Posts: 58
Joined: Tue May 10, 2005 5:04 am

reading lines from stream

Post 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.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked