I'm writing an INI-like configuration file and I want to know the best way to parse it in C/C++. I'm writing it on POSIX, so I'll be using # instead of ;. Which doesn't matter. I just want to know, is there some kind of tutorial, article on parsing things or something.
There are lots of tutorials and articles on parsing, but what you're dealing with is so trivial that most of them will probably be overkill.
Given how often this has been done before, I'd start by looking at some of the existing code that already implements almost what you want.
There are plenty of open source libraries out there already which you can probably use with little or no modification, e.g. libini.
As often in C++ basic stuffs, boost have a library that can read ini files. In fact there is two libs, depending on what you want to achieve :
- boost program option allow you to manage program options from command line or from ini or mixed sources : http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html;
- boost property tree does the work for you : http://www.boost.org/doc/libs/1_44_0/doc/html/boost_propertytree/parsers.html
Depending on how much complexity you actually need, you might get away with just using fgets() in a loop, and parsing each line manually (e.g. with strstr(), strchr(), strcmp(), etc). There's little point in dragging in a parsing library if you're just going to need to grab a few values.