Time Waster wrote:
Quote:
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?
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
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]`.
Quote:
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