detect unix or dos line endings



On May 24, Raymond Toy wrote:
---------------------------------------
.... the maxima language allows you to
escape to Lisp using ?.  Thus you can use ?read\-char to read a
character from a stream and then use other maxima functions to
determine if you've encountered LF or CR/LF or plain CR (Mac)
end-of-line sequences.
---------------------------------------
Hi Ray,

I am able to use ?read\-char(stream) ok to be able
to detect the first (and perhaps only) end of line
character, but in looking for a possible second
character (as with CR LF dos file end), I run the
risk of reaching the end of the file (If the file
has only one line), and I don't know how to
avoid the kind of lisp error shown below.

The common lisp cookbook talks about
using a second argument NIL in the
lisp function read-char to get a return
value of NIL is end-of-line is found, but
I am probably not adapting this idea
correctly below??

I have a one line file with dos (CR LF) endings here
for the experiment.

I first determine the length of the first line (excluding
end of line chars) using readline.

...........................................................
Maxima 5.24.0 http://maxima.sourceforge.net
using Lisp GNU Common Lisp (GCL) GCL 2.6.8 (a.k.a. GCL)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
                                  2011-05-25


(%i1) display2d:false$

(%i2) ss : openr("ndata1.dat");
(%o2) ?\#\<input\ stream\ ndata1\.dat\>

(%i3) l1 : readline(ss);
(%o3) "2.3e9 \"Abc\""

(%i4) slength(l1);
(%o4) 11

(%i5) close(ss);
(%o5) true

(%i6) ss : openr("ndata1.dat");
(%o6) ?\#\<input\ stream\ ndata1\.dat\>

(%i7) fposition(ss,12);
(%o7) true

(%i8) cs : string(?read\-char(ss,nil));
(%o8) "?\\
"
(%i9) slength(cs);
(%o9) 3
(%i10) cint (charat (cs,3));
(%o10) 13

/* so there is the CR end of line char */

(%i11) cs : string(?read\-char(ss,nil));
(%o11) "?\\
"
(%i12) slength(cs);
(%o12) 3
(%i13) cint (charat (cs,3));
(%o13) 10

/* so there is the second expected end of line char LF */

/* but, what if there had only been one end of line char,
as in unix or mac file?  As an experiment, we go
for another char, not knowing if it is the end of file
*/
(%i14) cs : string(?read\-char(ss,nil));

Maxima encountered a Lisp error:

 Error in PROGN [or a callee]: Unexpected end of #<input stream 
"ndata1.dat">.

Automatically continuing.
To enable the Lisp debugger set *debugger-hook* to nil.

(%i15) close(ss);
(%o15) true

(%i16) printfile("ndata1.dat")$
2.3e9 "Abc"

................................................................

How can I avoid this read error?

Ted