0

Is there a way to load a dynamic INI file like the one below.

[basic]
number_of_servers=3

[server1]
ip=10.20.30.40
password=sdfslkhf    

[server2]
ip=10.20.30.41
password=sdfslkhf

[server3]
ip=10.20.30.42
password=sdfslkhf

Here the idea is that the servers that are defined here is very specific to the software's deployment; so the admin decides how many servers participate in the configuration.

Is there a way to handle this in boost program_options?

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54

4 Answers4

3

Another, potentially more standard way, would be like this:

[basic]
number_of_servers=3

[server]
name=server1
ip=10.20.30.40
password=sdfslkhf    

[server]
name=server2
ip=10.20.30.41
password=sdfslkhf

[server]
name=server3
ip=10.20.30.42
password=sdfslkhf

This way you don't need to worry about undefined section names, and I think this style is more widely used as well (definitely it's how QuickFIX does it, in a way very similar to what I outlined).

And you can probably remove the number_of_servers entry, and just use the count() function to find how many server sections there are.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Thank John; I tried to do it that way, but I can't seem to get it to work. BTW, I looked for QuickFIX, I couldn't locate it anywhere either. – ϹοδεMεδιϲ Dec 14 '10 at 23:18
  • What do you mean you couldn't seem to get it to work? Have you given up or come up with another solution? And what do you mean you couldn't locate QuickFIX? Google finds it no problem. – John Zwinck Dec 18 '10 at 17:45
  • May be I have to spend more time trying to crack this. I had my priorities redone recently; this is still on my list. Sorry I couldn't get back. On QuickFIX, I did see the source tree, but I couldnt see the usage of program_options in there. – ϹοδεMεδιϲ Dec 21 '10 at 11:11
  • I am accepting this not because it answered my question, but it helped me to take the right approach. – ϹοδεMεδιϲ Dec 22 '10 at 11:55
  • QuickFIX doesn't use Boost program_options. My point was that it uses a config format like I wrote. I was intending to suggest that you could accomplish the same parsing using Boost as they do with hand-written code. – John Zwinck Dec 24 '10 at 00:45
2

There is an optional bool parameter to allow for unregistered entries in the parse_config_file function. It's set to false by default. See the documentation here:

http://www.boost.org/doc/libs/1_45_0/doc/html/boost/program_options/parse_config_file_id991860.html

If you call this function with true then it will add any unregistered entries into the variables_map as strings. You can check whether they exist with the variables_map::count function.

I hope that helps.

dvide
  • 437
  • 4
  • 8
0

Sure you can. The server sections have a pattern: just load all those matching the pattern into a list of servers.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • thanks. not sure how to do what you suggested. I am pretty new to program_options. As far as I know, I need to define the options in options_description before I can parse the config file. Is that not right? – ϹοδεMεδιϲ Dec 12 '10 at 23:35
0

The challenges I faced while resolving this was to make sure the sections are kept together and are no way mixed up.

In the end I relied on a options_description with the known/finite options and then using the parsed_options that come out of parse_config_file, I had to collect all unrecognized options ( collect_unrecognized ). Then I had to iterate it to pick the options in order.

Thanks every one for their contribution.

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54