Keys containing "/" in INI files
-
I'm looking for a way to work with a INI-like configuration file which should contain values like this:
@[Group]
Param1 = value1
Param2 = value2
Files/All/usr/bin/something = /opt/something/something@I mean, inside a Group there should be subgroup Files with subgroup All, and then there should be a key representing a filepath, e.g. /usr/bin/something must be parsed as a single key. If I would use JSON, this is what I would use:
@"Group": {
"Param1": "value1",
"Param2": "value2",
"Files": {
"All": {
"/usr/bin/something": "/opt/something/something"
}
}
}@But JSON looks not very human-friendly to me since it requires too many quoteation marks, so I prefer INI-style configuration, but I need to stop parsing subgroups after the 3rd level (Group -> Files -> All).
As far as I know, there is no way to do this with "QSettings":http://qt-project.org/doc/qt-5.0/qtcore/qsettings.html. Is there an external library that allows to do this? Or will it be easier to write my own INI-like parser?
-
Why "should" the file contain ambiguous entries like the example?
QSettings has a group mechanism that works in INI files but it uses / and \ as delimiters.
@
QSettings s("test.ini", QSettings::IniFormat);
s.beginGroup("Group");
s.beginGroup("Files");
s.beginGroup("All");
s.setValue("/usr/bin/something", "/opt/something/something");
s.endGroup();
s.endGroup();
s.endGroup();s.beginGroup("Group"); s.beginGroup("Files"); s.beginGroup("All"); qDebug() << s.value("/usr/bin/something").toString(); s.endGroup(); s.endGroup(); s.endGroup(); qDebug() << s.value("/Group/Files/All/usr/bin/something").toString();
@
The resulting file looks like:
@
[Group]
Files\All\usr\bin\something=/opt/something/something
@and the two qDebug() outputs are the same item. If all else fails you can encode the key that contains the '/' or use allKeys() and childKeys() to walk through the tree.
-
And what about iterating through keys() inside All? I'll see only usr, but I want to see /usr/bin/something (or usr/bin/something) there.
-
If you know the keys then you do not need to iterate over them. If you don't know the keys then you need to deal with the fact that your key is indistinguishable from another group. Encode the key any way that removes the / and the problem goes away.
@
QSettings s("test.ini", QSettings::IniFormat);
s.beginGroup("Group");
s.beginGroup("Files");
s.beginGroup("All");
s.setValue(QUrl::toPercentEncoding("/usr/bin/something"), "/opt/something/something");
s.setValue(QUrl::toPercentEncoding("/usr/bin/other"), "/opt/something/other");
s.endGroup();
s.endGroup();
s.endGroup();s.beginGroup("Group"); s.beginGroup("Files"); s.beginGroup("All"); foreach(QString key, s.childKeys()) qDebug() << QUrl::fromPercentEncoding(key.toUtf8());
// "/usr/bin/other"
// "/usr/bin/something"@
The resulting INI file looks like this:
@
[Group]
Files\All%252Fusr%252Fbin%252Fsomething=/opt/something/something
Files\All%252Fusr%252Fbin%252Fother=/opt/something/other
@You could even register your own QSettings format handler if you really need to read the exact format you started with. Writing would still be an issue though.