views:

210

answers:

2

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!

A: 

You could create a formal description of the format using DataScript/antlr and have it create parse and i/o code for you (Java or C++).

karx11erx
Thanks, I'll have a look into that but I'm not sure if it's quite what I'm after.
adaephon
+3  A: 

These kind of rules are easily coded using a finite-state machine.

Each chunk should change the state you are in. Each state waiting for specific chunks afterwards. If you encounter a chunk that you should not encounter in the current, that's an error.

Vincent Robert
Thanks, that probably is the solution I'm looking for, now to implement :-) thanks for pointing me in the right direction.
adaephon