Advertisement

Tiny .ini parser

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


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

I was saying that because Acharis asked for a cross-platform solution in this post... these things can be easy to miss, and it seems gratuitous to lock yourself to the Windows API when (a) there are plenty of drop-in libraries that are already cross-platform and (b) you can write your own basic parser anyway in under ten minutes if you really wanted to. Also, ini files aren't Windows specific, they just happen to be found more on Windows, you can certainly store your settings in any format you like on any system you like (let's face it, key-value stores aren't exactly an uncommon format).

Having a library that abstracts the actual configuration data from its storage sounds like a useful thing to have though.

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

Whoops - yes: the OP asked for a cross platform library; I missed that.

Luckily, both the toml and the Codeproject ini parser class are cross platform.

I also agree with the OP that SimpleIni is less desirable ;)

Too many projects; too much time

Advertisement
Yeah, I missed the cross-platform bit, sorry. (In my defense, I've never seen ini files used outside of Windows. OSX stores preferences differently, and even MS recommends people use the registry instead)

Admittedly, you could use those functions to make your own cross platform library that just happened to be a thin shell on Windows tongue.png

Tried 3 implementeations, none is good...

* CIniFile - adds space at the beginning of a line; weird

* CDataFile - can't compile #include <fstream.h> not found (googled it: "You're dealing with a pre-standard C++ library, and you've seen it won't compile with a standard compiler."

* IniParser - "can not parse file" error (LOL, can't handle a simple ini file!?)

Also:

* cpptoml - not standard .ini but some similar language???

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.

Exactly biggrin.png

Althrought, I start to think it might be faster to write it myself... Can't find a single one that works...

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

In CIniFile, 'trim' is only used when loading.

Should be easy enough to add it when saving. ? :)

Too many projects; too much time

Since I haven't used it in over 9 years, I couldn't find my actual source code on my current computer ... but when I wrote my own "subset of ini" file format classes - the first version was basically the following:

typedef list<string> ValueList;

struct SettingLine { string name; ValueList values; };

struct SettingGroup { string groupName; list<SettingLine> lines; }

struct SettingFile { string filePath; list<SettingGroup> groups; }

and then a parse methods that took a file path and returned a setting file, and a save method that took a setting file.

Of course once I had this up and running I made it more complex by adding support for having "comments" attached (preceeding) the group header, and sets of lines (a blank line started a new "line set" and could have a comment too ...

but none of this was necessary to get up and working with an interface that was simple and useful and bug free.

This topic is closed to new replies.

Advertisement