Advertisement

Tiny .ini parser

Started by March 21, 2015 06:05 PM
14 comments, last by Xai 9 years, 9 months ago

I need a very small ini "library" (it does not need to be small in size, but just like 1-2 cpp files). I don't need fancy stuff, no need for UTF-16, I just want to load small dev config files. Must be cross platform and no boost, also fast.

So far I have been using SimpleIni and hated it. First it was buggy (till I got a newer version), now it don't compile under C++11 and the output .ini files (writing) is "untidy". Ugh...

I have found this list: http://blog.brush.co.nz/2009/02/inih/

Please, recommed me something (I don't want to try them one by one)...

Stellar Monarch (4X, turn based, released): GDN forum topic - Twitter - Facebook - YouTube

INI parsers are usually very minimal effort to write from scratch, especially if you're only going to read your own personal INI files with it.

- Read one line at a time.
- If line is completely whitespace, ignore.
- If line begins with the comment character (';' or '#' depending on what you want to support), ignore.
- If line starts with '[', set a 'currentSectionName' variable.
- Find first '=' character in the line, handle the part before the '=' as the key and everything after as the value. Send the current 'currentSection', 'key', and 'value' variables to your handler or store them in a data structure.

- Writing them is trivial.

If the only INI files you read are the ones you write yourself, you don't need any more than that. You can add any additional features (such as escape sequences for \n, \t, etc) when you need them.
Advertisement

I've used this several times in the past: http://www.codeproject.com/KB/files/CIniFile.aspx

I remember only good things. :)

Too many projects; too much time

https://github.com/skystrife/cpptoml

TOML is INI with extensions to handle more complex data type and hierarchical maps when you need them, so you don't have to switch to human-hostile formats like JSON or XML or oddities like YAML.

Sean Middleditch – Game Systems Engineer – Join my team!

cpptoml looks neat. However, it is c++11/C++14, right? Also, looks like it would add a bit of compile-time hit, but maybe not too much.

Too many projects; too much time

If it's not to your taste, it links three other TOML implementations right on its page; two C++11 parsers and one C parser. I'm sure you could throw together some tests to compare compile speed vs runtime speed and see which meets your needs better.

I think the compile-time complaint is kinda pointless since you would only be including this header in a tiny handful of source files. Possibly even only one source file. My personal reason to avoid all of these but the C one is that they rely on exceptions and cannot even be compiled with exceptions disabled; that'd be easy to fix and make a pull-request for to push back into the upstream project, of course. Oh, they also seem to pay no attention to custom allocators.

Sean Middleditch – Game Systems Engineer – Join my team!

Advertisement
Assuming you're on Windows - why not use the built-in commands?

For example:
GetPrivateProfileString
WritePrivateProfileString

Why use Windows, when you can write your own OS in assembler? smile.png

Let's assume that the OP, like most programmers, is after something he can just drop into his project and use. And perhaps hack to fit the needs better, but preferably something that doesn't need to be modified at all.

Too many projects; too much time

Assuming you're on Windows - why not use the built-in commands?

For example:
GetPrivateProfileString
WritePrivateProfileString

These aren't cross-platform to my knowledge.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

"Assuming you're on Windows.." does not imply that it is cross platform.laugh.png

You'd have to use fscanf or something.

Or simply use an existing ini parser, like the one I linked to. Or one of the toml alternatives.

<edit>

Ini files are Windows specific, so the whole topic implies that it is not going to be cross platform.

However, you could use code that automatically chooses the right format for the right platform, like ini on Windows and cfg on *nix.

When I need something like this, I usually use a library. Like Qt or wxWidgets or POCO, because it makes it easy to handle different backends (registry, etc.) and it figures out where to store the configuration.

<edit_again>

'People' usually use YAML, XML, JSON, Lua for 'this kind of thing', unless you're developing a desktop application.

When doing a quick test, however, I think it's dead simple to simply drop in a general purpose ini parser into your project and that's it.

Too many projects; too much time

This topic is closed to new replies.

Advertisement