rawRead using a struct with variable leght

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 7 12:22:47 PDT 2017


On Wednesday, 7 June 2017 at 18:31:41 UTC, H. S. Teoh wrote:
> "Structs" with variable size fields have no direct equivalent 
> in D's type system, so you'll probably have a hard time mapping 
> this directly.
>
> What you *could* do, though, is to load the data into a ubyte[] 
> buffer, then create a proxy struct containing arrays where you 
> have variable-sized fields, with the arrays slicing the ubyte[] 
> buffer appropriately.  Unfortunately, yes, this means you have 
> to parse the fields individually in order to construct these 
> slices.

  I'm reminded a little bit of how I ended up handling the records 
and subrecords for Morrowind files; I ended up creating a range 
type which recognized the different types and returned the 
records, then a second one that cycled through the sub records 
and generated the structs as it went.

  Although those were incredibly simple, it was 2 fields, the name 
of the field and then the length of the whole thing together for 
the record (char, int). For subrecords it was the same, except 
additional int and other string fields, all fixed length, no 
weird dynamic allocation required.

  Unless the arrays are stored/saved after the rest of the data, I 
don't see how you could bulk load the other fields so easily.


More information about the Digitalmars-d-learn mailing list