Jookia
Jookia

Reputation: 6880

Parsing INI-like configuration files

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.

Upvotes: 2

Views: 3275

Answers (6)

sid_cypher
sid_cypher

Reputation: 111

If Boost is an overkill for you, try this 6 Kb BSD-licensed code.

"inih (INI Not Invented Here) is a simple .INI file parser written in C. It's only a couple of pages of code, and it was designed to be small and simple, so it's good for embedded systems."

http://code.google.com/p/inih/

There's also a C++ class, if you prefer objects, and it supports both ";" and "#" comments.

Upvotes: 0

Denis Shevchenko
Denis Shevchenko

Reputation: 1448

Try Configurator. It's easy-to-use and flexible C++ library for configuration file parsing (from simplest INI to complex files with arbitrary nesting and semantic checking). Header-only and cross-platform. Uses Boost C++ libraries.

See: http://opensource.dshevchenko.biz/configurator

Upvotes: 1

Jeremy Friesner
Jeremy Friesner

Reputation: 73071

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.

Upvotes: 0

Klaim
Klaim

Reputation: 69682

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 :

Upvotes: 1

Paul R
Paul R

Reputation: 212969

There are plenty of open source libraries out there already which you can probably use with little or no modification, e.g. libini.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490108

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.

Upvotes: 3

Related Questions