Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > handling Java properties

Reply
Thread Tools

handling Java properties

 
 
Time Waster
Guest
Posts: n/a
 
      11-20-2008
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


 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      11-20-2008
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

 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      11-20-2008
In article <VBeVk.2565$(E-Mail Removed)>,
Time Waster <(E-Mail Removed)> 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.

 
Reply With Quote
 
Time Waster
Guest
Posts: n/a
 
      11-21-2008
Eric Sosman <(E-Mail Removed)> 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!
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-21-2008
Time Waster wrote:
> Eric Sosman <(E-Mail Removed)> 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/ap...java.io.Reader)

</off-topic>

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-21-2008
Time Waster <(E-Mail Removed)> writes:
> Eric Sosman <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting properties (not necessarily "Properties") in Java 5.0 Wardie Java 4 11-08-2006 09:46 AM
CompositeControls: ViewState properties w/ Mapped properties probl =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?= ASP .Net 1 01-19-2006 09:19 AM
Making Custom Control Properties Visible in Visual Studio's Properties Palette Nathan Sokalski ASP .Net 0 10-17-2005 02:05 AM
Re: C++ properties Library Created (was Binding together Properties of Objects) Victor Porton C++ 1 08-29-2004 08:02 PM
Problems parsing when Properties.dtd.properties Kent Lichty Java 0 04-16-2004 03:08 PM



Advertisments