I basically want to manipulate byte arrays and byte streams in the style
of WITH-OUTPUT-TO-STRING, except that I'm using byte I/O operators (e.g.
WRITE-BYTE instead of WRITE-CHAR).
WITH-OUTPUT-TO-STRING and it's associated machinery (like
MAKE-STRING-OUTPUT-STREAM) accept an ELEMENT-TYPE keyword,
but the fundamental stream types still deal with elements of type CHARACTER
(ACL 5).
Is there any standards-supported way of doing the following without having to
write the whole implementation used for these semantics?
(with-output-to-byte-array (stream nil :element-type '(unsigned-byte 8))
(loop for x from 0 to 10 do (write-byte x stream)))
In ACL5, I can do this:
USER(18): (with-output-to-string (stream nil :element-type '(unsigned-byte 8))
(loop for x from 0 to 10 do (write-char (code-char x) stream)))
=> ... string of unprintable characters ...
But this is semantically distasteful, and the resulting string is still
(simple-array character (11)),
where I want an array of (simple-array '(unsigned-byte 8))
D. Tenny
············@mediaone.net - no spam please
From: Steve Gonedes
Subject: Re: WITH-OUTPUT-TO-STRING, equivalent capability for element-type '(UNSIGNED-BYTE 8)
Date:
Message-ID: <m24ss3fbmm.fsf@KludgeUnix.com>
············@mediaone.net writes:
< WITH-OUTPUT-TO-STRING and it's associated machinery (like
< MAKE-STRING-OUTPUT-STREAM) accept an ELEMENT-TYPE keyword,
< but the fundamental stream types still deal with elements of type CHARACTER
< (ACL 5).
<
< Is there any standards-supported way of doing the following without having to
< write the whole implementation used for these semantics?
<
< (with-output-to-byte-array (stream nil :element-type '(unsigned-byte 8))
< (loop for x from 0 to 10 do (write-byte x stream)))
<
< In ACL5, I can do this:
<
< USER(18): (with-output-to-string (stream nil :element-type '(unsigned-byte 8))
< (loop for x from 0 to 10 do (write-char (code-char x) stream)))
< => ... string of unprintable characters ...
You could use with-open-stream and define some stream-methods to work
with a fillable-extendible-vector of fixnums (in acl5). Be forewarned
- I know nothing of clos and methods so some of this might not be a
great idea.
(defmethod stream-write-byte ((byte fixnum) (output vector))
(vector-push-extend byte output))
(defmethod close ((stream vector) &key abort) t)
(let ((z (make-array 10
:element-type 'unsigned-byte
:adjustable t :fill-pointer 0)))
(with-open-stream (output z)
(loop for i below 10 do
(STREAM-write-byte i output)))
z)
Not sure if that's what you were after or not, and again; this is just
something I threw together real quick - not to sure about how
"correct" it is (especially that close method - eek).
I'm sure there is a better way to do this - just that I don't know
didly about clos and objects. I imagine that what you probably didn't
want to do was allocate a vector, maybe some methods could be defined
for with-output-to-string to do this for you...
You could write a macro to do almost the same thing, but you wouldn't
be able to use stream-x-y methods. Hope this helps some.