Re: Converting a file to a bit-vector?
Tim Bradshaw <tfb@cley.com> writes:
> On 6 May 2013, at 22:09, Zach Beane wrote:
>>
>> A quick experiment suggests that LispWorks does not support
>> read-sequence for bit vectors.
>
> It looks to me as if it probably does but it doesn't support reading
> BITs from files, which makes the point moot really (ie when I tried
> this I get an error from READ-SEQUENCE when it tries to store
> something bigger than a BIT into the first element of the array).
>
> I am not sure what (open ... :element-type 'bit) is meant to do: for
> both the implementations I tried (LW and CCL) it seems to create a
> stream which reads 8-bit bytes, which strikes me as wrong (I'd want an
> error or a bit) but probably I am misreading the spec, since they both
> do this.
Well, the point is that it's implementation specific how binary elements
are stored into binary files (and therefore how they're read back).
What the standard says is that:
(let ((word (1+ (random 100))) ; no limit really
(bits #(0 1 1 1 0 1)))
(when (nth-value 1 (subtypep `(unsigned-byte ,word) `integer))
(with-open-file (out "TEST.BIN"
:element-type `(unsigned-byte ,word)
:direction :output
:if-does-not-exist :create
:if-exists :supersede)
(write-sequence bits out))
(with-open-file (inp "TEST.BIN"
:element-type `(unsigned-byte ,word)
:direction :input)
(let ((v (make-array (file-length inp) :element-type `(unsigned-byte ,word))))
(read-sequence v inp)
(assert (= (file-length inp) (length bits))
() "file length=~S expected=~S" (file-length inp) (length bits))
(assert (equalp bits v)
() "bits=~S read=~S" bits v)))))
For BITs, some implementation indeed write #(0 1 1 1 0 1) as (od -t x1):
0000000 00 01 01 01 00 01
0000006
while clisp packs bits (and bytes), but apart from when the byte size is
the same as the underlying file system byte size, this will require a
header specifying the file size (actually, just the number of bytes in
the last word):
0000000 06 00 00 00 2e
0000005
> Related to this is the question of bit-order: if you *could* read bits
> from a file what order do you want the bits in a byte in?
It's a concern for the implementation, if you use the BIT type.
Now, it is expected that (unsigned-byte 8) will map octet-by-octet the
contents of the file (on POSIX systems) to the octet vector. You can
then do whatever bit decoding you want, in whatever bit order or bytesex
you want. Of course, it wouldn't work to read 9-track magnetic tapes
on a 36-bit host.
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
You can take the lisper out of the lisp job, but you can't take the lisp out
of the lisper (; -- antifuchs
_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html