Parse Config File in C/C++

What c lib to use when I need to parse a simple config file under linux?

libconfig but it does quite more than what you're asking

Creating a simple configuration file and parser in C++

In general, it's easiest to parse such typical config files in two stages: first read the lines, and then parse those one by one.

In C++, lines can be read from a stream using std::getline(). While by default it will read up to the next '\n' (which it will consume, but not return), you can pass it some other delimiter, too, which makes it a good candidate for reading up-to-some-char, like = in your example.

For simplicity, the following presumes that the = are not surrounded by whitespace. If you want to allow whitespaces at these positions, you will have to strategically place is >> std::ws before reading the value and remove trailing whitespaces from the keys. However, IMO the little added flexibility in the syntax is not worth the hassle for a config file reader.

#include <sstream>
const char config[] = "url=http://example.com\n"
"file=main.exe\n"
"true=0";

std::istringstream is_file(config);

std::string line;
while( std::getline(is_file, line) )
{
std::istringstream is_line(line);
std::string key;
if( std::getline(is_line, key, '=') )
{
std::string value;
if( std::getline(is_line, value) )
store_line(key, value);
}
}

(Adding error handling is left as an exercise to the reader.)

Parsing sections in a configuration file in C++

Your configuration file just seems to be a ini?

There's a heap of ini parsers around that will save you the work of writing your own from scratch if that's a option:

  • inih
  • simpleini

File based configuration handling in C (Unix)

Okay, so let's hit the other part. You need to think about what you'd like to have as your "language". In the UNIX world, the sort of canonical version is probably whitespace-delimited text (think /etc/hosts) or ":" delimited text (like /etc/passwd).

You have a couple of options, the simplest in some sense being to use scanf(3). Again, read the man page for details, but if a line entry is something like

port    100

then you'll be looking for something like

char inbuf[MAXLINE];
int val;

scanf("%s %d\n", &inbuf[0], &val);

You can get a bit more flexibility if you write a simple FSA parse: read characters one at a time from the line, and use a finite automaton to define what to do.

Better way to parse a config File in C++

The rule of thumb with this stuff (IMHO) is to always use a good library to parse a standard data format, at least in any situation where your needs might continue to scale. For example, I tend to really be fond of JSON because it's a simple, easy to humanly read/write format, with quite a few good quality choices in C++.

This avoids having to deal with any bug prone parsing logic yourself. It also makes it very easy to e.g. write python scripts that generate or verify your configuration files (since python has very easy access to json as well, as about every language does).

In your code it may also be a good idea to cleanly separate the data format, and the collection into a map or whatever data structure you're using. That way, the portion of the code that will change if you decide to change which data format you use is contained.



Related Topics



Leave a reply



Submit