From: Scott Sheffield
Subject: Any way to do this in LISP?
Date: 
Message-ID: <384E5963.8407571@lmco.com>
I need a good example of LISP code  that will show me how you can
directly read and write structures directly to a binary file in LISP? If

it can be done, I would also like to know in how to define a four byte
structure in which the first two bytes are two characters, and the
second two bytes is one number. I would like to then read and write such

a structure to a file. I can do this in C; Can it be done in LISP? Is
there possibly a book out that would have some examples? I have checked
over half a dozen books and I can't seem to find out if this is even
possible.

Thanks,
Scott

From: Frode Vatvedt Fjeld
Subject: Re: Any way to do this in LISP?
Date: 
Message-ID: <2hvh69v3gg.fsf@dslab7.cs.uit.no>
Scott Sheffield <·················@lmco.com> writes:

> I need a good example of LISP code  that will show me how you can
> directly read and write structures directly to a binary file in
> LISP?

I've just written a package for reading binary files. You'll find it
at <URL:http://www.cs.uit.no/~frodef/sw/binary-types/>. Let me know if
you have problems with it.

> it can be done, I would also like to know in how to define a four
> byte structure in which the first two bytes are two characters, and
> the second two bytes is one number.

Using this package, you'd go:

(def-compound 'foo
  ((char-one 'char8)  ; (slot-name slot-type-id)
   (char-two 'char8)
   (number 'u16)      ; u16 == unsigned 16-bits
  ))

(with-open-file (stream path :direction :input
                             :element-type '(unsigned-byte 8))
  (let ((*endian* 'big-endian))
    (with-slots (char-one char-two number)
         (read-binary 'foo)
      (format t "I read chars ~S and ~S and number ~S~%"
              char-one char-two number)
      )))

It also supports enumerated types (automatically mapping integers to
symbols) bitfields (mapping bits to symbols), byte-padding, and nested
type declarations. But not writing yet, unfortunately.

> a structure to a file. I can do this in C; Can it be done in LISP?
> Is there possibly a book out that would have some examples? I have
> checked over half a dozen books and I can't seem to find out if this
> is even possible.

It is quite obviously possible, but you can't expect to find every
conceivable programming task described in books. If you understand how
to do it in C, it's not much different in lisp, although it's easier
in C to just skip all the (somewhat) difficult issues involved.

-- 
Frode Vatvedt Fjeld
From: Frode Vatvedt Fjeld
Subject: Re: Any way to do this in LISP?
Date: 
Message-ID: <2hemcxurjc.fsf@dslab7.cs.uit.no>
Frode Vatvedt Fjeld <······@acm.org> writes:

> I've just written a package for reading binary files. You'll find it
> at <URL:http://www.cs.uit.no/~frodef/sw/binary-types/>. Let me know
> if you have problems with it.

I've just updated it with some bug-fixes and support for writing
integers and compounds.

-- 
Frode Vatvedt Fjeld