In general, reading a well-formed binary

I am trying to read the contents of a map / game model file in a program to write a small model viewer and test some DirectX functions. The file formats of the models / maps are characteristic, and I know the format of these files. I can read files easily by analyzing individual chunks using this approach:

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 come up with some way to read these files in general by indicating the format the file belongs to (for example, Chunk1 follows Chunk2, etc. etc.) so that the reader can verify that the file has an appropriate structure, I can use the Chunk super class and the Chunk factory to read all the pieces in any file as a whole. Essentially, I would like to increase this with the added functionality of a structure validator (or something similar) to get a method like 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);
    }
}

, ChunkFileFormat , , . ChunkFileFormat , , .

: - , ? , , #, ++ ( ).

!

+3
2

.

, . . , , .

+2

DataScript/antlr / (Java ++).

0

Source: https://habr.com/ru/post/1717609/


All Articles