1

I'm currently using SimpleINI and I'm not sure if I can do it with this but my configuration file is going to look like this

name = someone 
service = something
match = blahblahblah
match = something
match = some more junk

I know in advance which of the keys support multiple values and I want those values to be stored in an array or something so I can loop through them later (order doesn't matter).

If not SimpleIni then which other library will support this? I'm a beginner to C++ so I'm looking for something easy to use. I have boost libraries but not sure if I should use it (seems complicated).

My application is windows specific so I don't need a cross platform solution in this case.

I've already seen this question - What is the easiest way to parse an INI File in C++? but not sure which of them I can use to accomplish this.

Any suggestions?

Community
  • 1
  • 1
codefrog
  • 611
  • 3
  • 10
  • 13

4 Answers4

0

Do you not have an option to change the names to something like match1, match2, match3, etc? That would seem to be the most straight forward way.

Beyond that, I've done things like this all the time. I simply wrote a few lines of code to parse the text file myself. It's not a complex task. But if you'd prefer to work with regular INI files, you need to look at changing the value names in the INI file.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • I've set the cli option to use --match and want the same command to be used as keys in the conf file. I think it's better this way. The format doesn't have to be ini. It can be anything. – codefrog Dec 03 '10 at 05:59
  • Repeated keys in an INI file are very common. Think of them like an array – John Dibling Dec 03 '10 at 06:12
0

Given you're on windows, you may not need a library at all.

You would never know it by just browsing the documentation, but GetPrivateProfileString() in the WINAPI may do exactly what you want.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • But then won't I need to write to the windows registry? I don't want to do that. I'm leaving the configuration file in the same folder where the executable is and the user is meant to edit it manually without involving the registry. – codefrog Dec 03 '10 at 06:31
  • @codefrog: No, it doesnt use the registry. What made you think that? – John Dibling Dec 03 '10 at 15:16
  • Oh. It says "Applications should store initialization information in the registry." so I thought I wouldn't be able to use it without writing to the registry. How do I use this function to retrieve multiple values where the keys are the same (same section)? – codefrog Dec 04 '10 at 16:06
0

My Qt solution on the other SO thread applies. It is better because

  • Cross platform
  • Easy conversion to values other than strings
  • Simple

If you have an ini file like this (can be auto-generated from your list of objects using Qt API)

[Matches]
1\match=1
2\match=2
3\match=3
size=3

Here is the code that read them back

QSettings settings("test.ini", QSettings::IniFormat);
int size = settings.beginReadArray("Matches");
for (int i = 0; i < size; ++i) {
    settings.setArrayIndex(i);
    std::cout << settings.value("match").toInt() << std::endl;
}
settings.endArray();

Of course, another obvious option will be to use comma separated string as your value and use QString::split()

Dat Chu
  • 10,822
  • 13
  • 58
  • 82
0

SimpleINI accepts multiKey.

/** Are multiple values permitted for the same key? */
bool m_bAllowMultiKey;


[section]
name = someone
service = something
match = value1
match = othervalue
match = anotherValue 
match = value4

Just create the CSimpleIniA with the second parameter as true.

// CSimpleIniA(bool a_bIsUtf8, bool a_bAllowMultiKey, bool a_bAllowMultiLine)
CSimpleIniA myINI{ false,true,false };  

Use GetAllValues to get a list with all the values.

// from SimpleIni.h => typedef std::list<Entry> TNamesDepend;
CSimpleIniA::TNamesDepend values;
myINI.GetAllValues("section", "match", values);

Header file: SimpleIni.h

gotanod
  • 299
  • 1
  • 3
  • 7