From: rif
Subject: Mixed type binary streams?
Date: 
Message-ID: <wj0adkejtog.fsf@five-percent-nation.mit.edu>
Under cmucl, is there anyway to read integers of different widths from
the same file?  In particular, suppose the stream was generated by a C
program that alternately wrote 32-bit ints and 16-bit ints to the same
file.  Can I get these numbers back in Lisp?

I'm aware that the 32 bit numbers will become bignums if
they are bigger than fixnums.  That's OK.

rif

From: Peter Seibel
Subject: Re: Mixed type binary streams?
Date: 
Message-ID: <m3heemrqmc.fsf@localhost.localdomain>
rif <···@mit.edu> writes:

> Under cmucl, is there anyway to read integers of different widths from
> the same file?  In particular, suppose the stream was generated by a C
> program that alternately wrote 32-bit ints and 16-bit ints to the same
> file.  Can I get these numbers back in Lisp?


Well, I'm hardly a Lisp expert so there may be much better ways. But
here's the one I came up with when I was working on some code to read
Java .class files. Open your file like:

(with-open-file (in file) :element-type 'unsigned-byte) ...)

And then use these functions:

(defmacro read-n-bytes (in n)
  (labels ((exps (in n)
		 (if (= n 1)
		     `(read-byte ,in)
		   `(boole boole-ior
			   (ash (read-byte ,in) ,(* 8 (- n 1)))
			   ,(exps in (- n 1))))))
    (exps in n)))
  
(defun read-u4 (stream) (read-n-bytes stream 4))
(defun read-u2 (stream) (read-n-bytes stream 2))
(defun read-u1 (stream) (read-byte stream))

-Peter

-- 
Peter Seibel
·····@javamonkey.com
From: Duane Rettig
Subject: Re: Mixed type binary streams?
Date: 
Message-ID: <47kfhg8er.fsf@beta.franz.com>
rif <···@mit.edu> writes:

> Under cmucl, is there anyway to read integers of different widths from
> the same file?  In particular, suppose the stream was generated by a C
> program that alternately wrote 32-bit ints and 16-bit ints to the same
> file.  Can I get these numbers back in Lisp?

You can do this on bivalent streams, which are available in some Gray 
streams implementations, and also in simple-streams:

http://www.franz.com/support/documentation/6.2/doc/streams.htm

For simple-streams use on CMUCL, Paul Foley started an implementation,
and you can get it by going to his CL page:

http://users.actrix.gen.nz/mycroft/cl.html and downloading the
tar file.  I don't know what shape it's in, but it should give
you a good start.

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182