java-like properties files (key/value)?

Discussion in 'C++' started by Markus Dehmann, Oct 6, 2005.

  1. 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
     
    Markus Dehmann, Oct 6, 2005
    #1
    1. Advertising

  2. Markus Dehmann

    Kai-Uwe Bux Guest

    Markus Dehmann wrote:

    > 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
     
    Kai-Uwe Bux, Oct 6, 2005
    #2
    1. Advertising

  3. Markus Dehmann wrote:
    > Is there a library that supports Java-like .properties files?

    Yes, try boost::program_options (www.boost.org). It's quite powerful.

    --
    Matthias Kaeppler | Tel: +49 631 3405805
    Gerhart-Hauptmann-Str. 16a | Mob: +49 176 20108693
    D-67663 Kaiserslautern | E-Mail: matthias at finitestate dot org
     
    Matthias Kaeppler, Oct 6, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
  2. Karen Grube

    Urgent: How can I get the enter key to behave like a tab key?

    Karen Grube, Nov 28, 2004, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    300
    Karen Grube
    Nov 29, 2004
  3. M P
    Replies:
    1
    Views:
    483
  4. Une bévue
    Replies:
    5
    Views:
    153
    Une bévue
    Aug 10, 2006
  5. Antonio Quinonez
    Replies:
    2
    Views:
    174
    Antonio Quinonez
    Aug 14, 2003
Loading...

Share This Page