handling Java properties

Discussion in 'C Programming' started by Time Waster, Nov 20, 2008.

  1. Time Waster

    Time Waster Guest

    Java property files are dead simple:
    key1=val1
    some.key2=val2

    For simplicity on the Java side, I'd like to use these files from C
    as well (the C program and Java program must cooperate). Anyone have
    any bright ideas on handling this sort of thing from C? Is there
    any preexisting libraries or calls that would help?

    One idea I had was to have a big enumerated type of the key names (which are
    known ahead of time):
    enum { key1 = 0, key2, key3 , MAXKEY}
    char *keys[MAXKEY];

    Then as I read the keys from the file, I could somehow fill in
    the array:
    <read key1=val1>
    keys[key1] = val1;

    But that led me to the dead end because even if I have stringvar == "key1"
    that doesn't get me to the bareword needed to have an enumerated reference.
    Of course, I could have another stupid array of structs defined mapping the
    key1 enumeration thingy to "key1" as a string, but that seems kind of
    ugly. Is there a way to get the enumerated constant key1 from the
    string value "key1"?

    TIA
    Time Waster, Nov 20, 2008
    #1
    1. Advertising

  2. Time Waster

    Chris Dollin Guest

    Time Waster wrote:

    > Java property files are dead simple:
    > key1=val1
    > some.key2=val2
    >
    > For simplicity on the Java side, I'd like to use these files from C
    > as well (the C program and Java program must cooperate). Anyone have
    > any bright ideas on handling this sort of thing from C? Is there
    > any preexisting libraries or calls that would help?


    `fgets` to read lines, `strchr` to find characters, `malloc` to allocate
    space -- that sort of thing?

    > One idea I had was to have a big enumerated type of the key names (which are
    > known ahead of time):
    > enum { key1 = 0, key2, key3 , MAXKEY}
    > char *keys[MAXKEY];


    Mmmm.

    > Then as I read the keys from the file, I could somehow fill in
    > the array:
    > <read key1=val1>
    > keys[key1] = val1;


    All the values had better have the same type, then.

    > But that led me to the dead end because even if I have stringvar == "key1"
    > that doesn't get me to the bareword needed to have an enumerated reference.


    No. You'll have to map from the name to the value. Or you could just
    look the things up on demand.

    > Of course, I could have another stupid array of structs defined mapping the
    > key1 enumeration thingy to "key1" as a string, but that seems kind of
    > ugly.


    /Someone/ has to have that mapping. This is "Mr Bare Bones" C, so it's
    the programmers job to define the mapping.

    I'd just have a hash table mapping the keys (eg "some.key2") to their
    values ("val2") and load that from the property file. I'm likely to
    have hash-table code around anyway, so I'd use that. I wouldn't
    bother with the `keys` array: for rarely-accessed elements I don't
    think it's worth it, for frequently-accessed elements I'd probably
    end up stuffing them into some context object with a more useful
    access path than `keys[ENUM]`.

    > Is there a way to get the enumerated constant key1 from the
    > string value "key1"?


    Not built-in, no.

    --
    "It's just the beginning we've seen." - Colosseum, /Tomorrow's Blues/

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Nov 20, 2008
    #2
    1. Advertising

  3. In article <VBeVk.2565$>,
    Time Waster <> wrote:
    >Java property files are dead simple:
    >key1=val1
    >some.key2=val2
    >
    >For simplicity on the Java side, I'd like to use these files from C
    >as well (the C program and Java program must cooperate). Anyone have
    >any bright ideas on handling this sort of thing from C? Is there
    >any preexisting libraries or calls that would help?


    You're just going to get the usual fgets,index, etc re-invent the wheel
    stuff here. This being CLC and all. Anything else (external libraries)
    are:

    Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

    Unfortunately, I don't have a good suggestion for you, but I know that
    some years back, I was looking for something similar - that is,
    something in the Linux world that was similar to Windows INI files.
    Naturally, mentioning anything Windows-y in any of the Unix-y groups (a
    category into which, protestations to the contrary notwithstanding, CLC
    falls), raises flags in front of bulls...

    But a little newsgrouping and a little surfing did lead me to a
    "registry" package for Linux that, although I never ended up actually
    using it seriously, did look quite interesting. All the Unix-y types
    did, of course, crap all over it for being inspired by the Evil Empire.
    Kenny McCormack, Nov 20, 2008
    #3
  4. Time Waster

    Time Waster Guest

    Eric Sosman <> wrote:
    > <off-topic> Simple, but not quite *that* simple ... </off-topic>


    I don't see this as off-topic! The only wrinkle i noticed
    in property files was that you must escape equal signs in the
    data value (to be expected). Are there other gotchas?
    (I know about the comment character behavior as well.)

    > If there are a lot of keys or if you consult the table a lot,
    > consider using a hash table that maps key strings to enums or to
    > value strings.


    I think I like this thought the best. To fill out the answers a bit,
    I'll be writing the property files only from C. They are only occasionally
    used, mostly at startup time. Various C programs and one Java program
    will be the consumers, and just one C program, a configuration program,
    will be the only writer.

    It feels a bit wrong to cater to the 1 Java program, but I just wanted
    a simple unified configuration representation instead of the mish-mash
    of section headers and quasi-property-file stuff that is there currently.

    Thanks!
    Time Waster, Nov 21, 2008
    #4
  5. Time Waster

    Eric Sosman Guest

    [OT] Re: handling Java properties

    Time Waster wrote:
    > Eric Sosman <> wrote:
    >> <off-topic> Simple, but not quite *that* simple ... </off-topic>

    >
    > I don't see this as off-topic! The only wrinkle i noticed
    > in property files was that you must escape equal signs in the
    > data value (to be expected). Are there other gotchas?
    > (I know about the comment character behavior as well.)


    <off-topic>

    http://java.sun.com/javase/6/docs/api/java/util/Properties.html#load(java.io.Reader)

    </off-topic>

    --
    Eric Sosman
    lid
    Eric Sosman, Nov 21, 2008
    #5
  6. Time Waster <> writes:
    > Eric Sosman <> wrote:
    >> <off-topic> Simple, but not quite *that* simple ... </off-topic>

    >
    > I don't see this as off-topic! The only wrinkle i noticed
    > in property files was that you must escape equal signs in the
    > data value (to be expected). Are there other gotchas?
    > (I know about the comment character behavior as well.)

    [...]

    It's not obvious that an equals sign in the data value has to be
    escaped. For example, given:

    foo=bar=42

    the format could easily treat the first '=' on the line as the
    delimiter between the key and the value, and all other '=' characters
    as part of the value; so the key is "foo" and the value is "bar=42".

    Of course whatever standard defines the layout can require anything it
    likes.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 21, 2008
    #6
    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. Nathan Sokalski
    Replies:
    0
    Views:
    894
    Nathan Sokalski
    Oct 17, 2005
  2. =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=

    CompositeControls: ViewState properties w/ Mapped properties probl

    =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=, Jan 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,129
    Steven Cheng[MSFT]
    Jan 19, 2006
  3. Kent Lichty
    Replies:
    0
    Views:
    825
    Kent Lichty
    Apr 16, 2004
  4. Victor Porton
    Replies:
    1
    Views:
    688
    Steven T. Hatton
    Aug 29, 2004
  5. Wardie
    Replies:
    4
    Views:
    482
    Wardie
    Nov 8, 2006
Loading...

Share This Page