I'm trying to read contents of a game's map/model files into a program for the purposes of writing a small model viewer and testing out some DirectX features. The model/map file formats are chunked in nature, and I know the format of these files. I can easily read the files by parsing through the individual chunks, using an approach like this:
class FileType1 {
private Chunk1 c1;
private Chunk2 c2; // etc
public void Read(BinaryReader reader) {
c1 = new Chunk1(reader);
c2 = new Chunk2(reader);
}
}
However, I am trying to think of some way to generically read these files, by specifying the format the file adheres to (i.e. Chunk1 is followed by Chunk2 etc etc) so that the reader can ensure the file is of an appropriate structure. I can use a Chunk super class and a Chunk factory to generically read all the chunks in any given file. Essentially, I would like to augment this with the additional functionality of a structure validator (or something similar) to result in a method similar to this:
public void Read(BinaryReader reader, ChunkFileFormat format) {
while (!EOF) {
char[] chunkID = reader.ReadChars(4);
Chunk c = chunkFactory.Create(chunkID);
if (c.GetType() != format.Next.GetType())
throw new Exception("File format is invalid");
format.SetCurrentRecord(c);
}
}
The idea here is that the ChunkFileFormat class specifies the structure of the file, indicating what chunk type is expected to be the next read in from the binary stream. This would allow subclasses of ChunkFileFormat to specify the layout of that particular format, and the single read method could be used for reading all different chunked file formats, rather than writing a long-winded and repetitive method for each.
My question is, is anyone aware of design patterns or approaches that could deal with this situation? The project I'm working on is currently in C# although I would be interested in solutions in C++ (or any language for that matter).
Thanks in advance!