File Parser Class Design:Need Suggestions
Hello all,
I've written a simple file parser that can handle two different, though fairly similar, types of files for a game. Everything works, but I wrote two classes for each type, and I think I could write it in a better way. Both types are read line by line in exactly the same way, as both have values separated by semi-colons.
My current idea is to write a template class. I have defined two structs, each defining the data that needs to be collected for each type of file. The readFile method will be the same for each type, as it simply reads everything one line at a time, skipping some garbage lines that are present. It then calls a parse method, which reads the data into a list of the struct type. The parse method will vary based on what sort of file it needs to read, with differing requirements. Finally, we make a call to a write function, which will again vary based on the type of file.
So, my idea is to pass along function pointers to the parse and write methods. Another possibility would be to create a class that has a definition for the read() method, but have methods for parsing and writing that have to be overridden in child classes.
What sort of approach would you guys have?