java-like properties files (key/value)?

M

Markus Dehmann

Is there a library that supports Java-like .properties files? I have
several settings for each of my classes. So, I want to have a file like
this:

MainClass.numOfLoops = 3232 # the number of loops
MainClass.version = 0.95
OtherClass.readDirectory = /etc/passwd
OtherClass.whatever = true

The library should read and parse this and provide the values in a
global/singleton hash or so. I tried boost::program_options, but there
you have to define exactly which options you are expecting. It's really
for command line options for executables. I want that the user can
define all kinds of keys/values in such a file. Each class should also
be allowed to read its own .properties file. In that case, the
keys/values are only visible for that class.

I should be able to say something like:

double version = properties.get("MainClass.version").toBool();
bool whatever = properties.get("OtherClass.whatever").toDouble();

Thanks!
Markus
 
K

Kai-Uwe Bux

Markus said:
Is there a library that supports Java-like .properties files? I have
several settings for each of my classes. So, I want to have a file like
this:

MainClass.numOfLoops = 3232 # the number of loops
MainClass.version = 0.95
OtherClass.readDirectory = /etc/passwd
OtherClass.whatever = true

The library should read and parse this and provide the values in a
global/singleton hash or so. I tried boost::program_options, but there
you have to define exactly which options you are expecting. It's really
for command line options for executables. I want that the user can
define all kinds of keys/values in such a file. Each class should also
be allowed to read its own .properties file. In that case, the
keys/values are only visible for that class.

I should be able to say something like:

double version = properties.get("MainClass.version").toBool();
bool whatever = properties.get("OtherClass.whatever").toDouble();

Thanks!
Markus

What about:

#include <boost/lexical_cast.hpp>
#include <map>
#include <string>
#include <iostream>

typedef std::map< std::string, std::string > OptionMap;

int main ( void ) {
OptionMap options;

options[ "opt_1" ] = "0.95";
options[ "opt_2" ] = "1";

double opt_1_val = boost::lexical_cast< double >( options[ "opt_1" ] );
bool opt_2_val = boost::lexical_cast< bool >( options[ "opt_2" ] );

std::cout << opt_1_val << " " << opt_2_val << '\n';
}


Now, with the boolean type, you will have to work a little bit to make
"true" and "false" work.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top