473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

handling Java properties

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
Nov 20 '08 #1
6 3100
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

Nov 20 '08 #2
Time Waster wrote:
Java property files are dead simple:
key1=val1
some.key2=val2
<off-topicSimple, but not quite *that* simple ... </off-topic>
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?
Much depends on what you mean by "handling." Do you need to read
properties? Write them? Both? With what kind of reliability and/or
promptness must updated properties make it into the file?
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"?
If performance is not a big issue (few keys, infrequent processing),
you could build a table that associates key strings with enum values
and just look 'em up:

static struct {
const char *keyString;
const int keyIndex;
} table[] = {
{ "key1", key1 },
{ "key2", key2 },
...
};

Or eliminate the middle-man by getting rid of the enum and just
associating key strings with value strings:

static struct {
const char *keyString;
/* const? */ char *keyValue;
} table[] = {
{ "key1", NULL },
{ "key2", NULL },
...
};

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.

--
Er*********@sun.com
Nov 20 '08 #3
In article <VB*****************@nwrddc02.gnilink.net>,
Time Waster <no***@nowhere.comwrote:
>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.

Nov 20 '08 #4
Eric Sosman <Er*********@sun.comwrote:
<off-topicSimple, 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!
Nov 21 '08 #5
Time Waster wrote:
Eric Sosman <Er*********@sun.comwrote:
> <off-topicSimple, 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
es*****@ieee-dot-org.invalid
Nov 21 '08 #6
Time Waster <bf*@fenway.our.netwrites:
Eric Sosman <Er*********@sun.comwrote:
> <off-topicSimple, 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) ks***@mib.org <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"
Nov 21 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
6
by: Josh Mcfarlane | last post by:
I keep trying to get myself out of the return-code mindset, but it doesn't seem to work. They are suppose to get rid of if-then statements of return codes, but you still have to do an if statement...
13
by: tolisss | last post by:
Hi i have setup a global exception handler b4 Application.Run like Application.ThreadException += new ThreadExceptionEventHandler(GlobalExceptionProcessing.AppThreadException ); then after...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
0
by: balaji krishna | last post by:
Hi, I need to handle the return set from COBOL stored procedure from my invoking Java program. I do not know, how many rows the stored proc SQL fetches.I have declared the cursor in that proc, but i...
1
by: skchonghk | last post by:
Dear all smart experts, I write a simple Java UDF, which should run on DB2 v8 on AIX. But it can't load the Java class. Help ugently needed!! Thanks! I develop and deploy the Java UDF with...
41
by: Zytan | last post by:
Ok something simple like int.Parse(string) can throw these exceptions: ArgumentNullException, FormatException, OverflowException I don't want my program to just crash on an exception, so I must...
15
by: Xah Lee | last post by:
On Java's Interface Xah Lee, 20050223 In Java the language, there's this a keyword “interface”. In a functional language, a function can be specified by its name and parameter specs....
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.