Mixed type binary streams? 
Author Message
 Mixed type binary streams?

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



Sun, 01 May 2005 06:43:11 GMT  
 Mixed type binary streams?

Quote:

> 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



Sun, 01 May 2005 13:23:21 GMT  
 Mixed type binary streams?

Quote:

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

--

555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182  



Sun, 01 May 2005 17:00:01 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. ? Mix type BINARY data file

2. Distinguishing a binary Stream from a text Stream ?

3. Intermediate type of mixed-type expressions

4. Help with opening mixed stream file

5. Mixed Stream/"Formatted" I/O

6. Problem mixing print_exc and print in one file stream

7. Binary Stream

8. Binary Stream Design problem?

9. CORRECTION: binary stream address length?

10. How to split binary stream ?

11. Gwydion Dylan 2.0 binary dist for Win32 streams bug

12. Need some assistance in binary streaming and reading

 

 
Powered by phpBB® Forum Software