From: Homayoon
Subject: Saving a squence of characters or octets
Date: 
Message-ID: <d57fe262-9673-4a76-8432-b4223146e86f@d1g2000hsg.googlegroups.com>
I'm trying to figure out a way to save the result of a function to a
file. I'm using a code similar to this:

(with-open-file (s "/home/hom/foo" :direction :output)
  (write-sequence (foo) s))

The problem is (foo) might return either a character sequence or an
octet sequence. The code gets an error if (foo) returns a sequence of
octets. If I add :element-type '(unsigned-byte 8) I get an error
message when (foo) returns a sequence of characters. Can anyone please
suggest a way to correct the code so that it works with any kind of
sequence? It seems to me that it should be trivial. Maybe I'm missing
something.

I'm using SBCL 1.0.16 if it matters to the problem.

From: Pascal J. Bourguignon
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <87bpyjj3vp.fsf@hubble.informatimago.com>
Homayoon <···········@gmail.com> writes:

> I'm trying to figure out a way to save the result of a function to a
> file. I'm using a code similar to this:
>
> (with-open-file (s "/home/hom/foo" :direction :output)
>   (write-sequence (foo) s))
>
> The problem is (foo) might return either a character sequence or an
> octet sequence. The code gets an error if (foo) returns a sequence of
> octets. If I add :element-type '(unsigned-byte 8) I get an error
> message when (foo) returns a sequence of characters. Can anyone please
> suggest a way to correct the code so that it works with any kind of
> sequence? It seems to me that it should be trivial. Maybe I'm missing
> something.
>
> I'm using SBCL 1.0.16 if it matters to the problem.

(let ((result (foo)))
  (with-open-file (s "/home/hom/foo" 
                    :direction :output
                    :element-type (typecase (elt result 0)
                                    (character 'character)
                                    (t '(unsigned-byte 8))))
    (write-sequence result s)))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Indentation! -- I will show you how to indent when I indent your skull!"
From: Homayoon
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <0bd6ac53-7495-4c43-9845-7d8771026731@2g2000hsn.googlegroups.com>
On Sep 20, 12:07 am, ····@informatimago.com (Pascal J. Bourguignon)
wrote:
> Homayoon <···········@gmail.com> writes:
> > I'm trying to figure out a way to save the result of a function to a
> > file. I'm using a code similar to this:
>
> > (with-open-file (s "/home/hom/foo" :direction :output)
> >   (write-sequence (foo) s))
>
> > The problem is (foo) might return either a character sequence or an
> > octet sequence. The code gets an error if (foo) returns a sequence of
> > octets. If I add :element-type '(unsigned-byte 8) I get an error
> > message when (foo) returns a sequence of characters. Can anyone please
> > suggest a way to correct the code so that it works with any kind of
> > sequence? It seems to me that it should be trivial. Maybe I'm missing
> > something.
>
> > I'm using SBCL 1.0.16 if it matters to the problem.
>
> (let ((result (foo)))
>   (with-open-file (s "/home/hom/foo"
>                     :direction :output
>                     :element-type (typecase (elt result 0)
>                                     (character 'character)
>                                     (t '(unsigned-byte 8))))
>     (write-sequence result s)))
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> "Indentation! -- I will show you how to indent when I indent your skull!"

Thanks. I should have thought of that myself, although admittedly I
expected there should be some way without enumerating all possible
result types.
From: alien_guy
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <pan.2008.09.20.00.33.49@l.org>
On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:

> I'm trying to figure out a way to save the result of a function to a
> file. I'm using a code similar to this:
> 
> (with-open-file (s "/home/hom/foo" :direction :output)
>   (write-sequence (foo) s))
> 
> The problem is (foo) might return either a character sequence or an
> octet sequence. The code gets an error if (foo) returns a sequence of
> octets. If I add :element-type '(unsigned-byte 8) I get an error message
> when (foo) returns a sequence of characters. Can anyone please suggest a
> way to correct the code so that it works with any kind of sequence? It
> seems to me that it should be trivial. Maybe I'm missing something.
> 
> I'm using SBCL 1.0.16 if it matters to the problem.

(with-open-file (s "/tmp/foo" :direction :output :element-type :default)
  (write-sequence "Sto'Vo'Kor" s))
From: Homayoon
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <f8d84c54-488f-48a5-8be6-5a47893bd1af@k13g2000hse.googlegroups.com>
On Sep 20, 4:33 am, alien_guy <····@l.org> wrote:
> On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> > I'm trying to figure out a way to save the result of a function to a
> > file. I'm using a code similar to this:
>
> > (with-open-file (s "/home/hom/foo" :direction :output)
> >   (write-sequence (foo) s))
>
> > The problem is (foo) might return either a character sequence or an
> > octet sequence. The code gets an error if (foo) returns a sequence of
> > octets. If I add :element-type '(unsigned-byte 8) I get an error message
> > when (foo) returns a sequence of characters. Can anyone please suggest a
> > way to correct the code so that it works with any kind of sequence? It
> > seems to me that it should be trivial. Maybe I'm missing something.
>
> > I'm using SBCL 1.0.16 if it matters to the problem.
>
> (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
>   (write-sequence "Sto'Vo'Kor" s))

This is exactly what I was looking for. Thanks a lot. Incidentally,
where can I find all possible values for element-type? I looked up the
CLHS but it wasn't there. Isn't element-type part of the ANSI standard?
From: Barry Margolin
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <barmar-A7E7EF.01095520092008@news.motzarella.org>
In article 
<····································@k13g2000hse.googlegroups.com>,
 Homayoon <···········@gmail.com> wrote:

> On Sep 20, 4:33�am, alien_guy <····@l.org> wrote:
> > On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> > > I'm trying to figure out a way to save the result of a function to a
> > > file. I'm using a code similar to this:
> >
> > > (with-open-file (s "/home/hom/foo" :direction :output)
> > > � (write-sequence (foo) s))
> >
> > > The problem is (foo) might return either a character sequence or an
> > > octet sequence. The code gets an error if (foo) returns a sequence of
> > > octets. If I add :element-type '(unsigned-byte 8) I get an error message
> > > when (foo) returns a sequence of characters. Can anyone please suggest a
> > > way to correct the code so that it works with any kind of sequence? It
> > > seems to me that it should be trivial. Maybe I'm missing something.
> >
> > > I'm using SBCL 1.0.16 if it matters to the problem.
> >
> > (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
> > � (write-sequence "Sto'Vo'Kor" s))
> 
> This is exactly what I was looking for. Thanks a lot. Incidentally,
> where can I find all possible values for element-type? I looked up the
> CLHS but it wasn't there. Isn't element-type part of the ANSI standard?

It's just a type specifier, and the allowable types depend on the 
implementation.

I think the above use of :DEFAULT is an implementation extension.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Pascal J. Bourguignon
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <87fxnvh04d.fsf@hubble.informatimago.com>
Homayoon <···········@gmail.com> writes:

> On Sep 20, 4:33�am, alien_guy <····@l.org> wrote:
>> On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
>> > I'm trying to figure out a way to save the result of a function to a
>> > file. I'm using a code similar to this:
>>
>> > (with-open-file (s "/home/hom/foo" :direction :output)
>> > � (write-sequence (foo) s))
>>
>> > The problem is (foo) might return either a character sequence or an
>> > octet sequence. The code gets an error if (foo) returns a sequence of
>> > octets. If I add :element-type '(unsigned-byte 8) I get an error message
>> > when (foo) returns a sequence of characters. Can anyone please suggest a
>> > way to correct the code so that it works with any kind of sequence? It
>> > seems to me that it should be trivial. Maybe I'm missing something.
>>
>> > I'm using SBCL 1.0.16 if it matters to the problem.
>>
>> (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
>> � (write-sequence "Sto'Vo'Kor" s))
>
> This is exactly what I was looking for. Thanks a lot. Incidentally,

I doubt it.
clhs open:
    element-type---a type specifier for recognizable subtype of
    character; or a type specifier for a finite recognizable subtype
    of integer; por one of the symbols signed-byte, unsigned-byte, or
    :default. The default is character.
              ^^^^^^^^^^^^^^^^^^^^^^^^^


C/USER[23]> (with-open-file (s "/tmp/foo" :direction :output
                                 :element-type :default)
              (write-sequence #(1 2 3) s))

*** - EXT:WRITE-CHAR-SEQUENCE: cannot output 1 into
 #<OUTPUT BUFFERED FILE-STREAM CHARACTER #P"/tmp/foo">, 
 not of type CHARACTER


> where can I find all possible values for element-type? I looked up the
> CLHS but it wasn't there. Isn't element-type part of the ANSI standard?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

"Debugging?  Klingons do not debug! Our software does not coddle the
weak."
From: Johan Ur Riise
Subject: Re: Saving a squence of characters or octets
Date: 
Message-ID: <87k5d4w2ny.fsf@morr.riise-data.net>
···@informatimago.com (Pascal J. Bourguignon) writes:

> Homayoon <···········@gmail.com> writes:
> 
> > On Sep 20, 4:33�am, alien_guy <····@l.org> wrote:
> >> On Fri, 19 Sep 2008 13:03:54 -0700, Homayoon wrote:
> >> > I'm trying to figure out a way to save the result of a function to a
> >> > file. I'm using a code similar to this:
> >>
> >> > (with-open-file (s "/home/hom/foo" :direction :output)
> >> > � (write-sequence (foo) s))
> >>
> >> > The problem is (foo) might return either a character sequence or an
> >> > octet sequence. The code gets an error if (foo) returns a sequence of
> >> > octets. If I add :element-type '(unsigned-byte 8) I get an error message
> >> > when (foo) returns a sequence of characters. Can anyone please suggest a
> >> > way to correct the code so that it works with any kind of sequence? It
> >> > seems to me that it should be trivial. Maybe I'm missing something.
> >>
> >> > I'm using SBCL 1.0.16 if it matters to the problem.
> >>
> >> (with-open-file (s "/tmp/foo" :direction :output :element-type :default)
> >> � (write-sequence "Sto'Vo'Kor" s))
> >
> > This is exactly what I was looking for. Thanks a lot. Incidentally,
> 
> I doubt it.
> clhs open:
>     element-type---a type specifier for recognizable subtype of
>     character; or a type specifier for a finite recognizable subtype
>     of integer; por one of the symbols signed-byte, unsigned-byte, or
>     :default. The default is character.

The default is normally the elected element-type when no type is 
specified. But SBCL has an extension here - bivalent streams, which
are requested in open using :element-type :default.

Use of keyword :default is quite confusing here.

From the SBCL manual:

9.1 Bivalent Streams

A bivalent stream can be used to read and write both character and
(unsigned-byte 8) values. A bivalent stream is created by calling open
with the argument :element-type :default. On such a stream, both
binary and character data can be read and written with the usual input
and output functions.

    Streams are not created bivalent by default for performance
    reasons. Bivalent streams are incompatible with fast-read-char, an
    internal optimization in sbcl's stream machinery that
    bulk-converts octets to characters and implements a fast path
    through read-char.