I'm currently working on a PGN parser in C, a language I don't have much experience in. The reason I'm writing it in C is because I might want to open source it in the future if I can get proud enough of my code and it will be more useful to others if it can be integrated in C programs than if it's written in C++.
I'm at the step where I need to find out how I will store a parsed game in memory. The PGN specifications allows for lot of complicated cases. For example, a move can be followed by one of more of the following : A comment, a Numeric Annotation Glyph, a variation (this one can be nested indefinitely), the end of the game. To makes maters worse some of these elements can happens before any move (for example a comment before the first move of a variation.
Last year in this thread. H.G.Muller suggested that each types of elements that can be attached to a move (variations, comment, etc.) be stored in a separate linked list on each move. I like this idea, even if it has some shortcomings. For example, elements that happens before the first move need to be linked to the variation instead of a move. We also need a way to determine the order in which theses elements originaly appeared in the PGN file (Imagine somethings like : move - comment - variation - NAG - comment).
Considering all this I came up with the following structures. Since I'd like this parser to be useful to others, I though I would ask your opinion about this data structure.
Code: Select all
typedef struct PgnTag
{
char* name;
char* value;
struct PgnTag* next;
} PgnTag;
typedef struct PgnMove
{
char* move;
PgnComments* comments; /* linked list */
PgnNag* nags; /* linked list */
PgnVariation* variations; /* linked list */
PgnMove* next;
} PgnMove;
typedef struct PgnVariation
{
PgnMove* moves; /* linked list. Should I use an array instead? */
int order; /* orders within the list of elements (comments, variations, NAG) */
/* The next lines are for comments/nags/variations that would
* appears before the first move of the variation */
PgnComments* comments; /* linked list */
PgnNag* nags; /* linked list */
PgnVariation* variations; /* linked list */
} PgnVariation;
typedef struct PgnGame
{
char* Event;
char* Site;
char* Date;
char* Round;
char* White;
char* Black;
char* Result;
PgnTag* otherTags;
PgnVariation* mainVariation;
} PgnGame;