|
Declaring variables in templates is performed similar to ANSI C and Scripts, but with an important difference: every time a variable is declared in the Template, that variable is mapped to a set of bytes in a file. For example, running the template:
char header[4];
int numRecords;
Would create the character array header, which is mapped to the first 4 bytes of the current file, and the integer numRecords, which is mapped to the next 4 bytes of the file. Both variables will be displayed in the Template Results panel and can be used for editing the file. Variables can also be edited using Scripts (see Editing with Scripts).
The main way of grouping Template variables together is to declare structs or unions. See the Structs and Unions help topic for more information. Custom Variables can also be defined with special read and write functions (see Custom Variables).
Display Format
By default, all variables declared will be displayed in the Template Results panel in decimal format. To switch between hexadecimal, decimal, octal, or binary display formats, see the functions DisplayFormatDecimal, DisplayFormatHex, DisplayFormatBinary, and DisplayFormatOctal in Interface Functions.
An alternate way of specifying the format for a variable is to use the syntax '<format=hex|decimal|octal|binary>' after a variable declaration or a typedef. For example:
int id;
int crc <format=hex>;
int flags <format=binary>;
Colors
When parsing a file, different colors can be applied to variables by using a Template. For example, the header bytes of a file could be colored differently than the rest of the file. See the functions SetForeColor, SetBackColor, and SetColor in Interface Functions for more information.
Endian
Any data written or read from a file depends upon the endian of the file (see Introduction to Byte Ordering). By default, all variables declared will have the same endian as the file, but the endian can be modified by using the functions BigEndian or LittleEndian (see I/O Functions). Using this technique, the same file can contain both little and big endian data.
Order
After each Template variable is declared, the current file position is moved forward. The current file position can be examined using the function FTell. By using the functions FSeek or FSkip, the current position can be moved around the file. This technique allows a file to be parsed out of order. Note that to read from a file without defining a variable, the functions ReadByte, ReadShort, ReadInt, etc. can be used (see I/O Functions).
Local Variables
In some instances, a variable may be required which is not mapped to a file and not displayed in the Template results. In this case, the special keyword 'local' can be used before the declaration. For example:
local int i, total = 0;
int recordCounts[5];
for( i = 0; i < 5; i++ )
total += recordCounts[i];
double records[ total ];
In this example, i and total are not added to the Template Results panel.
Strings
Null-terminated strings are commonly defined in binary files. 010 Editor allows the special syntax in a template to read a null-terminated string:
char str[];
or
string str;
will both read a string until a 0 byte is encountered. See Arrays and Strings for more information on strings.
Enums
Enums are useful when writing Templates (see Data Types, Typedefs, and Enums for information on declaring enum types). When an enum variable is declared and the variable is selected in the Template Results, a down arrow will appear to the right of the text field. Clicking on the down arrow displays a drop-down list of all constants defined for the enum. Selecting an item from the drop-down list, or entering a constant in the edit field and pressing Enter will assign the variable to a new value.
|