Arrays and Duplicates
When writing a template, regular arrays can be declaring using the same syntax as scripts (see Arrays and Strings). However, 010 Editor has a special syntax that allows arrays to be built in a special way. When declaring template variables, multiple copies of the same variable can be declared. For example:
int x;
int y;
int x;
010 Editor allows you to treat the multiple declarations of the variable as an array (this is called a Duplicate Array). In this example, x[0] could be used to reference the first occurrence of x and x[1] could be used to reference the second occurrence of x. Duplicate arrays can even be defined with for or while loops. For example:
local int i;
for( i = 0; i < 5; i++ )
int x;
For another example, see the 'ZIPTemplate.bt' file that uses duplicate arrays to parse ZIP files. Duplicate arrays are also useful because of how 010 Editor deals with arrays of structs.
Optimizing Structs
010 Editor contains an optimization that allows it to generate arrays of structures with millions and millions of elements. The optimization calculates the size of the first element of the array and assumes all elements of the array are the same size. This optimization will cause incorrect results if the elements can be variable size (010 Editor will display a warning in the Output text area if it detects this may be happening). To turn the optimization off, use the syntax '<optimize=false>' after the array declaration. For example:
typedef struct {
int id;
int length;
uchar data[ length ];
} RECORD;
RECORD record[5] <optimize=false>;
If 010 Editor displays a warning in the Output text area, but you would still like to use the optimization, use the syntax '<optimize=true>' to turn off display of the warning. The '<optimize=false>' syntax can also be used with struct typedefs to indicate that optimization should not be used on any arrays using that structure. Unoptimized arrays can also be rewritten as duplicate arrays (see above) since the elements of duplicate arrays can have different sizes.
|