reading from stdin on mac via buffer

Machine-specific discussion
Unix, Linux, OS X, OS/2, Windows, ..?
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

reading from stdin on mac via buffer

Post by cormullion »

I'm using this code to read from stdin on MacOS:

Code: Select all

(set 'counter 0)
(while (read-buffer 0 'buff 256 "\r") 
  (print (inc 'counter) { } buff))
It works OK, but doesn't get the final line (which probably doesn't have a "\r" to read). What's the best way to get the last bit?

(this is part of my ongoing battle getting Services to work with ThisService and those Mac applications that use "\r" as line endings... :-))

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

Post by Lutz »

Yoy shouldn't use '\r' (carriage return) to look for an end-of-line but "\n". (line-feed). The following code does it all and will not require a line-feed after the last line:

Code: Select all

(while (read-line)
        (println (current-line)))
or like this:

Code: Select all

(while (set 'line (read-line))
        (println line))
This code will work not only on Mac OS X but on any UNIX and Win32.

See also here: http://newlisp.org/CodePatterns.html#scripts in the sub chapter: 'Scripts as pipes'

Lutz

ps: in your example it did get the second line. Although 'read-buffer' returned 'nil' the last characters are in 'buff'. See also http://newlisp.org/downloads/newlisp_ma ... ead-buffer for this.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Unfortunately "\n" doesn't work, since the lines are separated by "\r"...

(see http://www.alh.net/newlisp/phpbb/viewtopic.php?t=891)

But I'll see if buff stores the remaining characters...

thanks lutz

Locked