title: Python construct - parsing a variable number of variable length records
body: <p>I'm using <a href="https://pypi.python.org/pypi/construct" rel="nofollow noreferrer">construct</a> 2.8 to reverse engineer the header of some files created by some long-lost Pascal program.</p>
  
  <p>The header is made of a number of different records, some of which are optional, and I'm not sure whether the order is fixed or not.</p>
  
  <p>For instance, two of the records look like this:</p>
  
  <pre class="lang-python prettyprint-override"><code>header_record_filetype = cs.Struct(
      'record_type' / cs.Int8ub,
      'file_type' / cs.PascalString(cs.Int16ub),
      'unknown' / cs.Int8ub
  )
  
  header_record_user = cs.Struct(
      'record_type' / cs.Int8ub,
      'user' / cs.PascalString(cs.Int16ub)
  )
  </code></pre>
  
  <p>And I've identified half a dozen more.</p>
  
  <p>How would I go about getting the parser to choose the correct record type based on the <code>record_type</code> member for an unknown number of records until it comes across a record with type 0 (or reaches the end of the file)?</p>