POV-Ray : Newsgroups : povray.newusers : File handle and macros Server Time
1 May 2025 15:51:48 EDT (-0400)
  File handle and macros (Message 1 to 7 of 7)  
From: kurtz le pirate
Subject: File handle and macros
Date: 30 Apr 2025 05:28:18
Message: <6811ed32$1@news.povray.org>
Hello,


I have a problem with the file handle parameters in macros.

The following SDL shows the problem :
--------------------------------------------------------------------------
  1 : #macro fwrite (FILE, texte)
  2 : 	#write (FILE, concat(texte,"\n") )
  3 : #end
  4 :
  5 :
  6 : #macro OpenFile (FileName)
  7 : 	#fopen FILE_HANDLE_IDENTIFIER FileName write
  8 : 	fwrite (FILE_HANDLE_IDENTIFIER, "bla bla bla")
  9 : 	FILE_HANDLE_IDENTIFIER
10 : #end
11 :
12 :
13 : #declare MyFile1 = OpenFile ("testfile.txt");
14 :
--------------------------------------------------------------------------


Runnins this simple test raise this error :

line 8
Parse Error: Expected 'macro parameter', file identifier found instead


"fwrite" macro take two parameters : the file handle and a string.
On line 8, I call this macro with the two "correct" parameters.

I thought it was "correct" but POV no!

My idea for the fwrite() macro (simplified here) is that it allows you 
to write to different files open at the same time in the same way.


So we can't pass a file handle as a parameter ?
Thank you for your feedback.





-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: jr
Subject: Re: File handle and macros
Date: 30 Apr 2025 05:50:00
Message: <web.6811f1db7252bfd7721a48e56cde94f1@news.povray.org>
hi,

> I have a problem with the file handle parameters in macros.
> ...
> So we can't pass a file handle as a parameter ?

I think that is correct.  in my 'Filed()' macro[*] I open & close a file within
the same macro, the only way I could make it work.


regards, jr.

[*]
<https://drive.google.com/file/d/10pGH0yi_-8aBTQvTwQPB4AdfRl9JsTGg/view?usp=sharing>


Post a reply to this message

From: kurtz le pirate
Subject: Re: File handle and macros
Date: 30 Apr 2025 11:59:17
Message: <681248d5$1@news.povray.org>
On 30/04/2025 11:48, jr wrote:
> hi,
> 
>> I have a problem with the file handle parameters in macros.
>> ...
>> So we can't pass a file handle as a parameter ?
> 
> I think that is correct.  in my 'Filed()' macro[*] I open & close a file within
> the same macro, the only way I could make it work.
> 


Thanks for this bad news (but I suspected it) :( :(

And yes, I already know filed.inc. Good job.




-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: Bald Eagle
Subject: Re: File handle and macros
Date: 30 Apr 2025 13:25:00
Message: <web.68125bf97252bfd75e45bf3825979125@news.povray.org>
kurtz le pirate <kur### [at] freefr> wrote:

> So we can't pass a file handle as a parameter ?

https://wiki.povray.org/content/Reference:File_I/O_Directives



You can use ifdef and defined, so I'm wondering if it's just a special kind of
identifier that gets handled a little differently - just like things in the
scene parser get handled differently by the function VM.

Try assigning another identifier the value of your file handle.

#declare Temp = FILE_HANDLE_IDENTIFIER;

try sending its value to the debug stream

basically try operating on it in any way that you can think of, and then maybe
there's some sort of workaround that might be possible.

The other option is to use parse_string to use the literal name of the file
handle as a string argument to your macro, and then parse it _inside_ your macro
as an identifier.  Because the string and the identifier are two different
things.

- BW


Post a reply to this message

From: kurtz le pirate
Subject: Re: File handle and macros
Date: 1 May 2025 04:44:55
Message: <68133487$1@news.povray.org>
On 30/04/2025 19:20, Bald Eagle wrote:
> kurtz le pirate <kur### [at] freefr> wrote:
> 
>> So we can't pass a file handle as a parameter ?
> 
> https://wiki.povray.org/content/Reference:File_I/O_Directives
> 
> 
> 
> You can use ifdef and defined, so I'm wondering if it's just a special kind of
> identifier that gets handled a little differently - just like things in the
> scene parser get handled differently by the function VM.
> 
> Try assigning another identifier the value of your file handle.
> 
> #declare Temp = FILE_HANDLE_IDENTIFIER;
> 
> try sending its value to the debug stream
> 
> basically try operating on it in any way that you can think of, and then maybe
> there's some sort of workaround that might be possible.
> 
> The other option is to use parse_string to use the literal name of the file
> handle as a string argument to your macro, and then parse it _inside_ your macro
> as an identifier.  Because the string and the identifier are two different
> things.


Thank you for your advice but in what I was hoping to do "parse_string" 
is not suitable.



An other test.
--------------------------------------------------------------------------
#macro fwrite (FileHandle, texte)
	#write (FileHandle, concat(texte,"\n") )
#end

#fopen FileHandle1 "test1.txt" write
#fopen FileHandle2 "test2.txt" write
#fopen FileHandle3 "test3.txt" write

fwrite (FileHandle1, "writing to test1")
fwrite (FileHandle2, "writing to test2")
fwrite (FileHandle3, "writing to test3")

#fclose FileHandle3
#fclose FileHandle2
#fclose FileHandle1
--------------------------------------------------------------------------

Give the same error : Parse Error: Expected 'macro parameter', file 
identifier found instead.



For #fopen, the doc say : "The file handle identifier created by #fopen 
is always global and remains in effect (and the file remains open) until 
the scene parsing is complete or until you #fclose the file."



Conclusion : The file handle is an IDENTIFIER, that we cannot store in 
variable.





Of course this code work :
--------------------------------------------------------------------------
#fopen FileHandle1 "test1.txt" write
#fopen FileHandle2 "test2.txt" write
#fopen FileHandle3 "test3.txt" write

#write (FileHandle1, "writing to test1\n")
#write (FileHandle2, "writing to test2\n")
#write (FileHandle3, "writing to test3\n")

#fclose FileHandle3
#fclose FileHandle2
#fclose FileHandle1
--------------------------------------------------------------------------

...but the advantages of having one and only one macro that writes to 
files are lost :(






-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: Leroy
Subject: Re: File handle and macros
Date: 1 May 2025 13:20:00
Message: <web.6813ac317252bfd738fc462df712fc00@news.povray.org>
kurtz le pirate <kur### [at] freefr> wrote:
>
>
> Of course this code work :
> --------------------------------------------------------------------------
> #fopen FileHandle1 "test1.txt" write
> #fopen FileHandle2 "test2.txt" write
> #fopen FileHandle3 "test3.txt" write
>
> #write (FileHandle1, "writing to test1\n")
> #write (FileHandle2, "writing to test2\n")
> #write (FileHandle3, "writing to test3\n")
>
> #fclose FileHandle3
> #fclose FileHandle2
> #fclose FileHandle1
> --------------------------------------------------------------------------
>
> ...but the advantages of having one and only one macro that writes to
> files are lost :(
>
>
You could put each of those sections in their own macro.
like:
macro OpenW(F,FileName)
 #switch(F)
  #case(1) #fopen Afile FileName write  #break
  #case(2) #fopen Bfile FileName write  #break
  #case(3) #fopen Cfile FileName write  #break
 #end
#macro Write(F,String)
 #switch(F)
  #case(1) #write(Afile,String,"\n") #break
  #case(2) #write(Bfile,String,"\n") #break
  #case(3) #write(Cfile,String,"\n") #break
 #end
#end
#macro Close(F)
 #switch(F)
  #case(1) #fclose Afile #break
  #case(2) #fclose Bfile #break
  #case(3) #fclose Cfile #break
 #end
#end
You'll notice different names here that because I tested the Idea and just
copied the code.
Have Fun


Post a reply to this message

From: Bald Eagle
Subject: Re: File handle and macros
Date: 1 May 2025 14:00:00
Message: <web.6813b6687252bfd76563700825979125@news.povray.org>
kurtz le pirate <kur### [at] freefr> wrote:

> Thank you for your advice but in what I was hoping to do "parse_string"
> is not suitable.

Why not?

> #macro fwrite (FileHandleString, texte)
>  #write (Parse_String (FileHandleString), concat(texte,"\n") )
> #end

fwrite ("Filename", "Try this.")

Too much reading and writing to disk?

- BW


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.