How do I create a template for a recursive file format?

If I have a file format that has a self-describing, potentially recursive nature, how can I define that in an 010 template? For example: A file composed of a sequence of structures that each have a header that defined the type and one of those types was a list that could contain those same structure, like this pseudo-code:

struct foo {
string name;
string type;
int length;

switch (type) {
case “uint”:
DWORD value;
break;
case “list”:
foo value[length];
}
};

In this case, the use of “foo” in the case for “list” causes a syntax error, presumably because it’s not yet defined. I also don’t seem to be able to predefine a prototype for it.

Recursive structs are supported but you have include a forward declaration, which is the line ‘struct foo;’ at the beginning of the file. In your example you should also turn off optimizations for the ‘value’ array. This should work:

struct foo;

struct foo {
  string name;
  string type;
  int length;

  switch (type) {
     case “uint”:
       DWORD value;
       break;
     case “list”:
       foo value[length] <optimize=false>;
  }
};

Graeme Sweet
SweetScape Software

2 Likes