From: Kaz Kylheku on
On 2010-01-31, piscesboy <oraclmaster(a)gmail.com> wrote:
> I have a recursive function that writes out to a file using (with-open-
> file). I pass a pointer to the file to be written as a parameter to
> recursive calls to the function. But if the file is already open I
> don't recursive calls to (with-open-file) to open it again. I need a
> way to test if the file pointer is already open and if it is, to
> simply use it to write to the file.

Why don't you just assume that if there /is/ a file object, that it's
open? If there is no file object, that argument can be some
special value which indicates that you don't have a file
object. Any guess about what value we can use for that?

You can't use with-open-file for this, because you can't wrap code in
with-open-file just in case it /might/ need to open stream. If
execution steps into with-open-file, it will try to open a file,

You can make your own macro for this: with-open-or-existing-file.
From: Paul Donnelly on
Barry Margolin <barmar(a)alum.mit.edu> writes:

> In article <87aavux87t.fsf(a)plap.localdomain>,
> Paul Donnelly <paul-donnelly(a)sbcglobal.net> wrote:
>
>> piscesboy <oraclmaster(a)gmail.com> writes:
>>
>> > I have a recursive function that writes out to a file using (with-open-
>> > file). I pass a pointer to the file to be written as a parameter to
>> > recursive calls to the function. But if the file is already open I
>> > don't recursive calls to (with-open-file) to open it again. I need a
>> > way to test if the file pointer is already open and if it is, to
>> > simply use it to write to the file.
>> >
>> > Maybe there is a better way to do this altogether...?
>>
>> Yeah, either drop the recursion and do your writing in a loop:
>>
>> (with-open-file (foo "file")
>> (loop write to foo))
>>
>> Or have two functions, one for the external interface and one for
>> the loop:
>>
>> (defun write-out ()
>> (with-open-file (foo "file")
>> (write-out1 foo)))
>>
>> (defun write-out1 (stream)
>> write some stuff to stream
>> (write-out1 stream))
>>
>> If you've got initialization work to do, don't test whether you need to
>> redo it every time through the loop. It may not be a bottleneck,
>> especially since you're doing file IO, but come on.
>
> You could do it all in one function:
>
> (defun write-out (... &optional stream)
> (flet ((write-out-internal (s)
> write stuff to s
> ;; and recurse
> (write-out ... stream)))
> (if stream
> (write-out-internal s)
> (with-open-file (new-stream...)
> (write-out-internal ... new-stream)))))

Oh, duh. But did you mean to use labels rather than flet and call
write-out-internal from write-out-internal?
From: Barry Margolin on
In article <871vh4xkav.fsf(a)plap.localdomain>,
Paul Donnelly <paul-donnelly(a)sbcglobal.net> wrote:

> Barry Margolin <barmar(a)alum.mit.edu> writes:
>
> > In article <87aavux87t.fsf(a)plap.localdomain>,
> > Paul Donnelly <paul-donnelly(a)sbcglobal.net> wrote:
> >
> >> piscesboy <oraclmaster(a)gmail.com> writes:
> >>
> >> > I have a recursive function that writes out to a file using (with-open-
> >> > file). I pass a pointer to the file to be written as a parameter to
> >> > recursive calls to the function. But if the file is already open I
> >> > don't recursive calls to (with-open-file) to open it again. I need a
> >> > way to test if the file pointer is already open and if it is, to
> >> > simply use it to write to the file.
> >> >
> >> > Maybe there is a better way to do this altogether...?
> >>
> >> Yeah, either drop the recursion and do your writing in a loop:
> >>
> >> (with-open-file (foo "file")
> >> (loop write to foo))
> >>
> >> Or have two functions, one for the external interface and one for
> >> the loop:
> >>
> >> (defun write-out ()
> >> (with-open-file (foo "file")
> >> (write-out1 foo)))
> >>
> >> (defun write-out1 (stream)
> >> write some stuff to stream
> >> (write-out1 stream))
> >>
> >> If you've got initialization work to do, don't test whether you need to
> >> redo it every time through the loop. It may not be a bottleneck,
> >> especially since you're doing file IO, but come on.
> >
> > You could do it all in one function:
> >
> > (defun write-out (... &optional stream)
> > (flet ((write-out-internal (s)
> > write stuff to s
> > ;; and recurse
> > (write-out ... stream)))
> > (if stream
> > (write-out-internal s)
> > (with-open-file (new-stream...)
> > (write-out-internal ... new-stream)))))
>
> Oh, duh. But did you mean to use labels rather than flet and call
> write-out-internal from write-out-internal?

Yeah, probably. I initially thought there would be stuff outside the
internal function, so I did the recursion at the main function level.
Then I moved everything into the internal function, but didn't think to
redo the recursion. Either way should work.

--
Barry Margolin, barmar(a)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 ***