How to effectively parse the contents of an INI file?
-
I have an INI file that looks something like this:
[General] Version=160 Necessary=true
I do not have access to the INI file itself, but I do have access to the data inside. So I can easily get a string like:
[General]\nVersion=160\nNecessary=true
And I can easily manipulate that string to get the information I need.
However, does Qt offer a better way to handle this?
I thought about using QSettings, but I don't really have access to the file itself, as I said.
-
@JonB
I can't really pass the string as a parameter like this, though, right?// settings is just a member variable of type QSettings* settings = new QSettings("[General]\nVersion=160\nNecessary=true", QSettings::IniFormat);
I tried that, but it just returns an empty QSettings object.
I just want an easier way to handle a string that is formatted like an INI file. Preferably without writing the file out, as that INI file isn't really supposed to be available to everyone, but I don't mind a solution that writes it out for the moment.
@heftlas said in How to effectively parse the contents of an INI file?:
I can't really pass the string as a parameter like this, though, right?
No, it does not have a constructor which takes a string and parses it. But if you look at
beginGroup()
andsetValue()
you can construct an in-memoryQSettings
which matches your string.If you want something which parses an existing string, without having that in a file which can be read,
QSettings
won't do that. Unless you want to write that string to a file, getQSettings
to parse that, and then delete the file. -
@ollarch said in How to effectively parse the contents of an INI file?:
If you have the data as a string, write it to a file and read it using QSettings.
Do you know of any other way? Writing it to a file just to read it and probably then delete that file just seems wrong to me. If it comes to that, then probably creating my own INI parser is better…but I believe Qt would have something like this done for me already.
-
@ollarch said in How to effectively parse the contents of an INI file?:
If you have the data as a string, write it to a file and read it using QSettings.
Do you know of any other way? Writing it to a file just to read it and probably then delete that file just seems wrong to me. If it comes to that, then probably creating my own INI parser is better…but I believe Qt would have something like this done for me already.
@heftlas
It does.QSettings
will read the INI file you show. And also write it if you need that too.I thought about using QSettings, but I don't really have access to the file itself, as I said.
I don't know what this means, or what your question about this is.
-
-
I have an INI file that looks something like this:
[General] Version=160 Necessary=true
I do not have access to the INI file itself, but I do have access to the data inside. So I can easily get a string like:
[General]\nVersion=160\nNecessary=true
And I can easily manipulate that string to get the information I need.
However, does Qt offer a better way to handle this?
I thought about using QSettings, but I don't really have access to the file itself, as I said.
@heftlas said in How to effectively parse the contents of an INI file?:
However, does Qt offer a better way to handle this?
I thought about using QSettings, but I don't really have access to the file itself, as I said.Yes, Qt offers a better way to handle this. It is via
QSettings
.You do not need to have the existing file. You can create what you have in
QSettings
from code without reading a file. It will write the file out, but what do you care?So I can easily get a string like:
[General]\nVersion=160\nNecessary=trueAnd I can easily manipulate that string to get the information I need.
If that is what you want to do without going via
QSettings
then there is no point asking about usingQSettings
instead.If this does not answer your question I really do not understand exactly what you are looking for.
-
@heftlas said in How to effectively parse the contents of an INI file?:
However, does Qt offer a better way to handle this?
I thought about using QSettings, but I don't really have access to the file itself, as I said.Yes, Qt offers a better way to handle this. It is via
QSettings
.You do not need to have the existing file. You can create what you have in
QSettings
from code without reading a file. It will write the file out, but what do you care?So I can easily get a string like:
[General]\nVersion=160\nNecessary=trueAnd I can easily manipulate that string to get the information I need.
If that is what you want to do without going via
QSettings
then there is no point asking about usingQSettings
instead.If this does not answer your question I really do not understand exactly what you are looking for.
@JonB
I can't really pass the string as a parameter like this, though, right?// settings is just a member variable of type QSettings* settings = new QSettings("[General]\nVersion=160\nNecessary=true", QSettings::IniFormat);
I tried that, but it just returns an empty QSettings object.
I just want an easier way to handle a string that is formatted like an INI file. Preferably without writing the file out, as that INI file isn't really supposed to be available to everyone, but I don't mind a solution that writes it out for the moment.
-
@JonB
I can't really pass the string as a parameter like this, though, right?// settings is just a member variable of type QSettings* settings = new QSettings("[General]\nVersion=160\nNecessary=true", QSettings::IniFormat);
I tried that, but it just returns an empty QSettings object.
I just want an easier way to handle a string that is formatted like an INI file. Preferably without writing the file out, as that INI file isn't really supposed to be available to everyone, but I don't mind a solution that writes it out for the moment.
@heftlas said in How to effectively parse the contents of an INI file?:
I can't really pass the string as a parameter like this, though, right?
No, it does not have a constructor which takes a string and parses it. But if you look at
beginGroup()
andsetValue()
you can construct an in-memoryQSettings
which matches your string.If you want something which parses an existing string, without having that in a file which can be read,
QSettings
won't do that. Unless you want to write that string to a file, getQSettings
to parse that, and then delete the file. -
@heftlas said in How to effectively parse the contents of an INI file?:
I can't really pass the string as a parameter like this, though, right?
No, it does not have a constructor which takes a string and parses it. But if you look at
beginGroup()
andsetValue()
you can construct an in-memoryQSettings
which matches your string.If you want something which parses an existing string, without having that in a file which can be read,
QSettings
won't do that. Unless you want to write that string to a file, getQSettings
to parse that, and then delete the file. -
Hi,
For that use case, you might be interested by QTemporaryFile.