I am writing an application that has a very loosely defined config file that is read in during startup. The file is easy to parse and looks something like the following:
tag1 = value1
tag2 = value2
tag3 = value3
tag4 = value4
with tags and values being the information that a user may want to configure. As I said previously, the file is loosely defined – AKA, I don’t require all (or any) of the fields, and the values don’t have to be in any specific order. The only real rule is that it has to be in the format above, and any tags I don’t recognize get ignored.
Is there any good way to check to make sure a file is valid while parsing with the above constraints in mind? I’m developing in C#, version 2.0.x of .NET runtime. Thank you.
I suggest parsing the file using a regular expression into a dictionary. You can add default values for all supported keys to dictionary before reading the file. This way you get a dictionary with a consistent set of keys independent of the keys found in the file. In this case you might further consider adding a list of supported keys and checking every key against this list as you read the file reporting unsupported keys.