Difference between revisions of "Reference:File I/O Directives"
Jholsenback (talk | contribs) m (1 revision: Reference Migration Initial Load) |
(indexentry fix) |
||
(9 intermediate revisions by one other user not shown) | |||
Line 10: | Line 10: | ||
this feature.</p> | this feature.</p> | ||
<p class="Note"><strong>Note:</strong> Some platform versions of POV-Ray (e.g. Windows) | <p class="Note"><strong>Note:</strong> Some platform versions of POV-Ray (e.g. Windows) | ||
− | provide means to restrict the ability of scene files to read | + | provide means to restrict the ability of scene files to read and write files.</p> |
− | {{#indexentry:#fopen | + | ==The fopen Directive== |
+ | {{#indexentry:#fopen}} | ||
+ | {{#indexentry:fopen}} | ||
+ | {{#indexentry:append}} | ||
{{#indexentry:Directives, language, #fopen}} | {{#indexentry:Directives, language, #fopen}} | ||
− | |||
<p>Users may open a text file using the <code>#fopen</code> directive. The syntax is as follows:</p> | <p>Users may open a text file using the <code>#fopen</code> directive. The syntax is as follows:</p> | ||
<pre> | <pre> | ||
FOPEN_DIRECTIVE: | FOPEN_DIRECTIVE: | ||
− | #fopen | + | #fopen FILE_HANDLE_IDENTIFIER "filename" OPEN_TYPE |
OPEN_TYPE: | OPEN_TYPE: | ||
read | write | append | read | write | append | ||
</pre> | </pre> | ||
− | <p>Where <em> | + | <p>Where <em>FILE_HANDLE_IDENTIFIER</em> is an undefined identifier used to reference this file as a file handle, <em>"filename"</em> is any string literal or string expression which specifies the file name. Files opened with the <code>read</code> are open for read only. Those opened with <code>write</code> create a new file with the specified name and it overwrites any existing file with that name. Those opened with <code>append</code> opens a file for writing but appends the text to the end of any existing file.</p> |
<p>The file handle identifier created by <code>#fopen</code> is always global and remains in effect (and the file remains open) until the scene parsing is complete or until you <code>#fclose</code> the file. You may use <code>#ifdef</code> <em>FILE_HANDLE_IDENTIFIER</em> to see if a file is open.</p> | <p>The file handle identifier created by <code>#fopen</code> is always global and remains in effect (and the file remains open) until the scene parsing is complete or until you <code>#fclose</code> the file. You may use <code>#ifdef</code> <em>FILE_HANDLE_IDENTIFIER</em> to see if a file is open.</p> | ||
− | {{#indexentry:#fclose | + | ==The fclose Directive== |
+ | {{#indexentry:#fclose}} | ||
+ | {{#indexentry:fclose}} | ||
{{#indexentry:Directives, language, #fclose}} | {{#indexentry:Directives, language, #fclose}} | ||
− | |||
<p>Files opened with the <code>#fopen</code> directive are automatically | <p>Files opened with the <code>#fopen</code> directive are automatically | ||
closed when scene parsing completes however you may close a file using the | closed when scene parsing completes however you may close a file using the | ||
Line 37: | Line 40: | ||
<p>Where <em>FILE_HANDLE_IDENTIFIER</em> is previously opened file opened | <p>Where <em>FILE_HANDLE_IDENTIFIER</em> is previously opened file opened | ||
− | with the <code>#fopen</code> directive | + | with the <code>#fopen</code> directive.</p> |
==The read Directive== | ==The read Directive== | ||
− | {{#indexentry:#read | + | {{#indexentry:#read}} |
+ | {{#indexentry:read}} | ||
{{#indexentry:Directives, language, #read}} | {{#indexentry:Directives, language, #read}} | ||
<p>You may read string, float or vector values from a plain ASCII text file directly into POV-Ray variables using the <code>#read</code> directive. The file must first be opened in <em>read</em> mode using the <code>#fopen</code> directive. The syntax for <code>#read</code> is as follows:</p> | <p>You may read string, float or vector values from a plain ASCII text file directly into POV-Ray variables using the <code>#read</code> directive. The file must first be opened in <em>read</em> mode using the <code>#fopen</code> directive. The syntax for <code>#read</code> is as follows:</p> | ||
Line 51: | Line 55: | ||
</pre> | </pre> | ||
− | <p>Where <em>FILE_HANDLE_IDENTIFIER</em> is the previously opened file. It is followed by one or more <em>DATA_IDENTIFIER</em>s separated by commas. The parentheses around the identifier list are required. A <em>DATA_IDENTIFIER</em> is any undeclared identifier or any previously declared string identifier, float identifier, or vector identifier. Undefined identifiers will be turned into global identifiers of the type determined by the data which is read. Previously defined identifiers remain at whatever global/local status they had when originally created. Type checking is performed to insure that the proper type data is read into these identifiers.</p> | + | <p>Where <em>FILE_HANDLE_IDENTIFIER</em> is the previously opened file. It is followed by one or more <em>DATA_IDENTIFIER</em>(s) separated by commas. The parentheses around the identifier list are required. A <em>DATA_IDENTIFIER</em> is any undeclared identifier or any previously declared string identifier, float identifier, or vector identifier. Undefined identifiers will be turned into global identifiers of the type determined by the data which is read. Previously defined identifiers remain at whatever global/local status they had when originally created. Type checking is performed to insure that the proper type data is read into these identifiers.</p> |
<p>The format of the data to be read must be a series of valid string literals, float literals, or vector literals separated by commas. Expressions or identifiers are not permitted in the data file however unary minus signs and exponential notation are permitted on float values.</p> | <p>The format of the data to be read must be a series of valid string literals, float literals, or vector literals separated by commas. Expressions or identifiers are not permitted in the data file however unary minus signs and exponential notation are permitted on float values.</p> | ||
− | <p>If you attempt to read past end-of-file, the file is automatically closed and the <em>FILE_HANDLE_IDENTIFIER</em> is deleted from the symbol table. This means that the boolean function <code>defined(</code><em> | + | <p>If you attempt to read past end-of-file, the file is automatically closed and the <em>FILE_HANDLE_IDENTIFIER</em> is deleted from the symbol table. This means that the boolean function <code>defined(</code><em>FILE_HANDLE_IDENTIFIER</em><code>)</code> can be used to detect end-of-file.</p> |
<p>For example:</p> | <p>For example:</p> | ||
<pre> | <pre> | ||
− | #fopen | + | #fopen FILE_HANDLE_IDENTIFIER "mydata.txt" read |
− | #while (defined( | + | #while (defined(FILE_HANDLE_IDENTIFIER)) |
− | #read ( | + | #read (FILE_HANDLE_IDENTIFIER,Var1,Var2,Var3) |
... | ... | ||
#end | #end | ||
</pre> | </pre> | ||
− | {{#indexentry:#write | + | ==The write Directive== |
+ | {{#indexentry:#write}} | ||
+ | {{#indexentry:write}} | ||
{{#indexentry:Directives, language, #write}} | {{#indexentry:Directives, language, #write}} | ||
− | {{#indexentry:uint8 | + | {{#indexentry:uint8}} |
− | + | {{#indexentry:sint8}} | |
+ | {{#indexentry:uint16be}} | ||
+ | {{#indexentry:uint16le}} | ||
+ | {{#indexentry:sint16be}} | ||
+ | {{#indexentry:sint16le}} | ||
+ | {{#indexentry:sint32be}} | ||
+ | {{#indexentry:sint32le}} | ||
<p>You may write string, float or vector values to a plain ASCII text file from POV-Ray variables using the <code>#write</code> directive. Additionally it is now possible to write 8, 16, and 32-bit words to an external file.</p> | <p>You may write string, float or vector values to a plain ASCII text file from POV-Ray variables using the <code>#write</code> directive. Additionally it is now possible to write 8, 16, and 32-bit words to an external file.</p> | ||
<p>These words may be arranged in either little or big-endian fashion. Placing one of the following keywords in the argument list of a <code>#write</code> statement causes the values up to the next comma to be written in binary format, using 2's complement integer representation, rounded to the nearest integer in the representable range:</p> | <p>These words may be arranged in either little or big-endian fashion. Placing one of the following keywords in the argument list of a <code>#write</code> statement causes the values up to the next comma to be written in binary format, using 2's complement integer representation, rounded to the nearest integer in the representable range:</p> | ||
Line 82: | Line 94: | ||
<pre> | <pre> | ||
WRITE_DIRECTIVE: | WRITE_DIRECTIVE: | ||
− | #write( FILE_HANDLE_IDENTIFIER, [BINARY_WORD_TYPE,] DATA_ITEM [,DATA_ITEM]...) | + | #write (FILE_HANDLE_IDENTIFIER, [BINARY_WORD_TYPE,] DATA_ITEM [,DATA_ITEM]...) |
BINARY_WORD_TYPE: | BINARY_WORD_TYPE: | ||
uint8 | sint8 | uint16be | uint16le | sint16be | sint16le | sint32be | sint32le | uint8 | sint8 | uint16be | uint16le | sint16be | sint16le | sint32be | sint32le | ||
Line 88: | Line 100: | ||
FLOAT | VECTOR | STRING | FLOAT | VECTOR | STRING | ||
</pre> | </pre> | ||
− | <p>Where <em>FILE_HANDLE_IDENTIFIER</em> is the previously opened file. It is followed by one or more <em>DATA_ITEM</em>s separated by commas. The parentheses around the identifier list are required. A <em>DATA_ITEM</em> is any valid string expression, float expression, or vector expression. Float expressions are evaluated and written as signed float literals. If you require format control, you should use the <code>str(VALUE,L,P)</code> function to convert it to a formatted string. See <!--<linkto "String Functions">String Functions</linkto>--->[[Reference:Strings# | + | <p>Where <em>FILE_HANDLE_IDENTIFIER</em> is the previously opened file. It is followed by one or more <em>DATA_ITEM</em>(s) separated by commas. The parentheses around the identifier list are required. A <em>DATA_ITEM</em> is any valid string expression, float expression, or vector expression. Float expressions are evaluated and written as signed float literals. If you require format control, you should use the <code>str(VALUE,L,P)</code> function to convert it to a formatted string. See <!--<linkto "String Functions">String Functions</linkto>--->[[Reference:Strings#String Functions|String Functions]] for details on the <code>str</code> function. Vector expressions are evaluated into three signed float constants and are written with angle brackets and commas in standard POV-Ray vector notation. String expressions are evaluated and written as specified.</p> |
<p class="Note"><strong>Note:</strong> Data read by the <code>#read</code> directive must have comma | <p class="Note"><strong>Note:</strong> Data read by the <code>#read</code> directive must have comma | ||
delimiters between values and quotes around string data but the <code>#write</code> directive does not automatically output commas or quotes.</p> | delimiters between values and quotes around string data but the <code>#write</code> directive does not automatically output commas or quotes.</p> | ||
<p>For example the following <code>#read</code> directive reads a string, float and vector.</p> | <p>For example the following <code>#read</code> directive reads a string, float and vector.</p> | ||
<pre> | <pre> | ||
− | #read ( | + | #read (FILE_HANDLE_IDENTIFIER,MyString,MyFloat,MyVect) |
</pre> | </pre> | ||
<p>It expects to read something like:</p> | <p>It expects to read something like:</p> | ||
Line 103: | Line 115: | ||
#declare Val1 = -123.45; | #declare Val1 = -123.45; | ||
#declare Vect1 = <1,2,-3>; | #declare Vect1 = <1,2,-3>; | ||
− | #write( | + | #write (FILE_HANDLE_IDENTIFIER,"\"A quote delimited string\",",Val1,",",Vect1,"\n") |
</pre> | </pre> | ||
<p>See <!--<linkto "String Literals">String Literals</linkto>--->[[Reference:Strings#String Literals|String Literals]] and <!--<linkto "String Literals">Text Formatting</linkto>--->[[Reference:User Message Directives#Text Formatting|Text Formatting]] for details on writing special characters such as quotes, newline, etc.</p> | <p>See <!--<linkto "String Literals">String Literals</linkto>--->[[Reference:Strings#String Literals|String Literals]] and <!--<linkto "String Literals">Text Formatting</linkto>--->[[Reference:User Message Directives#Text Formatting|Text Formatting]] for details on writing special characters such as quotes, newline, etc.</p> |
Latest revision as of 23:13, 3 May 2016
You may open, read, write, append, and close plain ASCII text files while parsing POV-Ray scenes. This feature is primarily intended to help pass information between frames of an animation. Values such as an object's position can be written while parsing the current frame and read back during the next frame. Clever use of this feature could allow a POV-Ray scene to generate its own include files or write self-modifying scripts. We trust that users will come up with other interesting uses for this feature.
Note: Some platform versions of POV-Ray (e.g. Windows) provide means to restrict the ability of scene files to read and write files.
The fopen Directive
Users may open a text file using the #fopen
directive. The syntax is as follows:
FOPEN_DIRECTIVE: #fopen FILE_HANDLE_IDENTIFIER "filename" OPEN_TYPE OPEN_TYPE: read | write | append
Where FILE_HANDLE_IDENTIFIER is an undefined identifier used to reference this file as a file handle, "filename" is any string literal or string expression which specifies the file name. Files opened with the read
are open for read only. Those opened with write
create a new file with the specified name and it overwrites any existing file with that name. Those opened with append
opens a file for writing but appends the text to the end of any existing file.
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. You may use #ifdef
FILE_HANDLE_IDENTIFIER to see if a file is open.
The fclose Directive
Files opened with the #fopen
directive are automatically
closed when scene parsing completes however you may close a file using the
#fclose
directive. The syntax is as follows:
FCLOSE_DIRECTIVE: #fclose FILE_HANDLE_IDENTIFIER
Where FILE_HANDLE_IDENTIFIER is previously opened file opened
with the #fopen
directive.
The read Directive
You may read string, float or vector values from a plain ASCII text file directly into POV-Ray variables using the #read
directive. The file must first be opened in read mode using the #fopen
directive. The syntax for #read
is as follows:
READ_DIRECTIVE: #read (FILE_HANDLE_IDENTIFIER, DATA_IDENTIFIER[,DATA_IDENTIFIER]..) DATA_IDENTIFIER: UNDECLARED_IDENTIFIER | FLOAT_IDENTIFIER | VECTOR_IDENTIFIER | STRING_IDENTIFIER
Where FILE_HANDLE_IDENTIFIER is the previously opened file. It is followed by one or more DATA_IDENTIFIER(s) separated by commas. The parentheses around the identifier list are required. A DATA_IDENTIFIER is any undeclared identifier or any previously declared string identifier, float identifier, or vector identifier. Undefined identifiers will be turned into global identifiers of the type determined by the data which is read. Previously defined identifiers remain at whatever global/local status they had when originally created. Type checking is performed to insure that the proper type data is read into these identifiers.
The format of the data to be read must be a series of valid string literals, float literals, or vector literals separated by commas. Expressions or identifiers are not permitted in the data file however unary minus signs and exponential notation are permitted on float values.
If you attempt to read past end-of-file, the file is automatically closed and the FILE_HANDLE_IDENTIFIER is deleted from the symbol table. This means that the boolean function defined(
FILE_HANDLE_IDENTIFIER)
can be used to detect end-of-file.
For example:
#fopen FILE_HANDLE_IDENTIFIER "mydata.txt" read #while (defined(FILE_HANDLE_IDENTIFIER)) #read (FILE_HANDLE_IDENTIFIER,Var1,Var2,Var3) ... #end
The write Directive
You may write string, float or vector values to a plain ASCII text file from POV-Ray variables using the #write
directive. Additionally it is now possible to write 8, 16, and 32-bit words to an external file.
These words may be arranged in either little or big-endian fashion. Placing one of the following keywords in the argument list of a #write
statement causes the values up to the next comma to be written in binary format, using 2's complement integer representation, rounded to the nearest integer in the representable range:
uint8 - unsigned byte (0..255) sint8 - signed byte (-128..127) uint16be, uint16le - unsigned 16-bit word (0..65535) sint16be, sint16le - signed 16-bit word (-32768..32767) sint32be, sint32le - signed 32-bit word (-2^31..2^31-1)
Note: Currently, unsigned 32-bit words are not supported.
Keywords ending in "be" will cause the values to be written most significant byte first (big endian, aka network byte order) while those ending in "le" will instead write the least significant byte first (little endian, Intel format).
Note: See the sample macro ARRAYS_WriteDF3
in arrays.inc to see how this feature may be used.
As always, the file must first be opened in either write
or append
mode using the #fopen
directive. The syntax for the #write
directive is as follows:
WRITE_DIRECTIVE: #write (FILE_HANDLE_IDENTIFIER, [BINARY_WORD_TYPE,] DATA_ITEM [,DATA_ITEM]...) BINARY_WORD_TYPE: uint8 | sint8 | uint16be | uint16le | sint16be | sint16le | sint32be | sint32le DATA_ITEM: FLOAT | VECTOR | STRING
Where FILE_HANDLE_IDENTIFIER is the previously opened file. It is followed by one or more DATA_ITEM(s) separated by commas. The parentheses around the identifier list are required. A DATA_ITEM is any valid string expression, float expression, or vector expression. Float expressions are evaluated and written as signed float literals. If you require format control, you should use the str(VALUE,L,P)
function to convert it to a formatted string. See String Functions for details on the str
function. Vector expressions are evaluated into three signed float constants and are written with angle brackets and commas in standard POV-Ray vector notation. String expressions are evaluated and written as specified.
Note: Data read by the #read
directive must have comma
delimiters between values and quotes around string data but the #write
directive does not automatically output commas or quotes.
For example the following #read
directive reads a string, float and vector.
#read (FILE_HANDLE_IDENTIFIER,MyString,MyFloat,MyVect)
It expects to read something like:
"A quote delimited string", -123.45, <1,2,-3>
The POV-Ray code to write this might be:
#declare Val1 = -123.45; #declare Vect1 = <1,2,-3>; #write (FILE_HANDLE_IDENTIFIER,"\"A quote delimited string\",",Val1,",",Vect1,"\n")
See String Literals and Text Formatting for details on writing special characters such as quotes, newline, etc.