Look-ahead |
Top Previous Next |
Examples > BinaryCheck > Look-ahead
The project consists of only two productions. One of them, IsBinary, is extremely simple:
SKIP? NULL
It can parse a string only successfully when it ends with a binary null.
This production is used for the look-ahead within the production BinaryCheck:
IF(!IsBinary()) SKIP? {{ out << xState.str(); }} END
The difference of the use as a look-ahead compared with a normal call of a production is indicated syntactically by the appended parenthesis "( )" within the IF brackets. The source file is processed from the current position on -- here the start of the file -- as long as either the production is finished successfully or till a fault appears. IsBinary is successful exactly in the case, that there is a null character in the file.
You can see this in the debugger. As the project was opened the file BinaryTest.pdf should have been loaded into the viewer too. In the hexadecimal mode of the viewer one can see the null characters. If you step into the look-ahead with
and press the button several times, finally the following picture arises, in which the found null character is highlighted:
The file is regarded here as a text file if there isn't any null character. (The file could actually nevertheless be designed for a binary use.) If it is a text file, SKIP? jumps to the end of the file and the complete text then is written into the output. So the project can be used for other projects as preprocessor which guarantees that the source file isn't binary.
In addition the size of the file is checked at the beginning of BinaryCheck. Binary files often are are very big, because they may contain graphical data or even voice recordings and films. As a limiting value 1000000 bytes are chosen here.
{{ int iMaxSize = 1000000; if( file_size(SourceName()) > iMaxSize ) throw CTT_Error("file size > " + itos(iMaxSize)); }}
Text files seldom achieve this size. Whether even bigger files shall be allowed depends on the kind of the files. You also should take into account that the source files are loaded completely into the working memory for parsing.
|
This page belongs to the TextTransformer Documentation |
Home Content German |