* named structure members
@ 2003-09-11 11:07 Matthew Harrison
2003-09-11 11:50 ` Jeff Woods
2003-09-11 14:03 ` Mariano Moreyra
0 siblings, 2 replies; 15+ messages in thread
From: Matthew Harrison @ 2003-09-11 11:07 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1865 bytes --]
hi,
another one for you gurus, i haven't found anything on this myself
as i don't really know what to call it where a search engine is
concerned.
i have the following code.
<snip>
for(i = 0; i < n_values; ++i)
{
strcpy(keyword, good_values[i]);
switch (read_config_var(values_file, keyword, value))
{
case 0:
strcpy(config.?, value);
break;
case -1:
printf("\nFile Error for [%s] \n", values_file);
break;
case -2:
printf("\nBad User Parm for [%s] \n", keyword);
break;
default:
printf("\nUnknown Error Occurred \n");
break;
}
}
</snip>
what i'm doing is stepping thru an array of different config options
calling the read_config_var on each one and checking the output.
if the output is good then i want to store the value in a member of
a structure. for example i want the config options to be stored like
this:
config.db_host = 'maiden.genestate.com'
config.db_user = 'root'
you get the idea. my question is how do i dynamically assign a name
to a structure member. I have good_values[i] which contains the
current config directive but if you look at line 8 of the example,
you can see the problem, how do i say config.good_values[i], when
good_values[i] is not itself a member.
sorry for not being able to explain this very well but you can see
what i mean.
thanks in advance
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 11:07 named structure members Matthew Harrison
@ 2003-09-11 11:50 ` Jeff Woods
2003-09-11 14:03 ` Mariano Moreyra
1 sibling, 0 replies; 15+ messages in thread
From: Jeff Woods @ 2003-09-11 11:50 UTC (permalink / raw)
To: Matthew Harrison; +Cc: linux-c-programming
At 9/11/2003 12:07 PM +0100, Matthew Harrison wrote:
><snip>
>for(i = 0; i < n_values; ++i)
> {
> strcpy(keyword, good_values[i]);
>
> switch (read_config_var(values_file, keyword, value))
> {
> case 0:
> strcpy(config.?, value);
> break;
> case -1:
> printf("\nFile Error for [%s] \n",
> values_file);
> break;
> case -2:
> printf("\nBad User Parm for [%s] \n",
> keyword);
> break;
> default:
> printf("\nUnknown Error Occurred \n");
> break;
> }
> }
></snip>
>
>what i'm doing is stepping thru an array of different config options
>calling the read_config_var on each one and checking the output. if the
>output is good then i want to store the value in a member of a structure.
>for example i want the config options to be stored like this:
>
>config.db_host = 'maiden.genestate.com'
>config.db_user = 'root'
>
>you get the idea. my question is how do i dynamically assign a name to a
>structure member. I have good_values[i] which contains the current config
>directive but if you look at line 8 of the example, you can see the
>problem, how do i say config.good_values[i], when good_values[i] is not
>itself a member.
It would help to understand better if you show us the declaration for the
"config" structure.
If I understand what you're asking (and I'm not at all sure that I am) I
think you want to use a C struct like an associative array from TCL or
(IIUC) Perl. C does not (directly) support associative arrays. If you
want that behavior, you have to code it explicitly. Structures in C must
have all the fields of the structure explicitly declared when the structure
is defined.
>sorry for not being able to explain this very well but you can see what i
>mean.
I'm not at all sure I understand what you're asking. If I've guessed
wrong, try rephrasing the question, or perhaps someone else will understand
what you mean.
P.S. Anyone trying to write or maintain C code should have a copy of K&R,
preferably 2nd Edition [
http://www.amazon.com/exec/obidos/tg/detail/-/0131103628 or
http://tinyurl.com/mziv ] these days. It's a good tutorial (at least for
someone experienced with other procedural languages) and a great reference
manual on C. Of course, it's a little terse so it takes multiple readings
(like a love letter) to get the full flavor.
--
Jeff Woods <kazrak+kernel@cesmail.net>
^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: named structure members
2003-09-11 11:07 named structure members Matthew Harrison
2003-09-11 11:50 ` Jeff Woods
@ 2003-09-11 14:03 ` Mariano Moreyra
2003-09-11 14:26 ` Matthew Harrison
1 sibling, 1 reply; 15+ messages in thread
From: Mariano Moreyra @ 2003-09-11 14:03 UTC (permalink / raw)
To: 'Matthew Harrison', linux-c-programming
> what i'm doing is stepping thru an array of different config options
> calling the read_config_var on each one and checking the output.
> if the output is good then i want to store the value in a member of
> a structure. for example i want the config options to be stored like
> this:
> config.db_host = 'maiden.genestate.com'
> config.db_user = 'root'
> you get the idea. my question is how do i dynamically assign a name
> to a structure member. I have good_values[i] which contains the
> current config directive but if you look at line 8 of the example,
> you can see the problem, how do i say config.good_values[i], when
> good_values[i] is not itself a member.
I think that I get the idea ...
But are you sure that is that what you want??
I mean, the idea is that in your config option you could have any config
directives (or keys) and values??
How you will handle that options later without knowing from the begining
what options you could have??
Anyway...I think that you could do is create your structure like this:
typedef struct {
char key[255];
cahr value[255];
} t_config;
t_config config[20]; // Change 20 and 255 to adapt to your needs
so your code would look like this:
for(i = 0; i < n_values; ++i)
{
strcpy(keyword, good_values[i]);
switch (read_config_var(values_file, keyword, value))
{
case 0:
strcpy(config[i].key, keyword);
strcpy(config[i].value, value);
break;
case -1:
printf("\nFile Error for [%s] \n",
values_file);
break;
case -2:
printf("\nBad User Parm for [%s] \n",
keyword);
break;
default:
printf("\nUnknown Error Occurred \n");
break;
}
}
I'm not sure if is that what you want...and if I really understood your
problem...
So, let me know if this mail sucks! :)
--
Mariano Moreyra <mariano_moreyra@aca.org.ar>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 14:03 ` Mariano Moreyra
@ 2003-09-11 14:26 ` Matthew Harrison
2003-09-11 14:41 ` Mariano Moreyra
2003-09-11 15:50 ` Glynn Clements
0 siblings, 2 replies; 15+ messages in thread
From: Matthew Harrison @ 2003-09-11 14:26 UTC (permalink / raw)
To: Mariano Moreyra; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 3259 bytes --]
this is getting very complex.
here is the struct that i want my config to go into:
<snip>
typedef struct _CFG_struct
{
char *db_host;
char *db_name;
char *db_pass;
char *db_user;
}CFG;
</snip>
and I am doing the following (pseudo code)
<snip>
for(i = 0; i <= array_of_config_items; i++)
{
keyword = array_of_config_items[i];
while (read_config_file != EOF)
{
check_return_value;
if keyword = 'item1'
{
item1 = value;
}
elsif keyword = 'item2'
{
item2 = value;
}
and_so_on();
}
}
</snip>
i can sort it now with the multiple if's but now it segfaults on
a strcpy for some reason and i'm still learning how to use gdb.
On Thu, Sep 11, 2003 at 11:03:59AM -0300, Mariano Moreyra wrote:
> > what i'm doing is stepping thru an array of different config options
> > calling the read_config_var on each one and checking the output.
> > if the output is good then i want to store the value in a member of
> > a structure. for example i want the config options to be stored like
> > this:
>
> > config.db_host = 'maiden.genestate.com'
> > config.db_user = 'root'
>
> > you get the idea. my question is how do i dynamically assign a name
> > to a structure member. I have good_values[i] which contains the
> > current config directive but if you look at line 8 of the example,
> > you can see the problem, how do i say config.good_values[i], when
> > good_values[i] is not itself a member.
>
> I think that I get the idea ...
> But are you sure that is that what you want??
> I mean, the idea is that in your config option you could have any config
> directives (or keys) and values??
> How you will handle that options later without knowing from the begining
> what options you could have??
> Anyway...I think that you could do is create your structure like this:
>
> typedef struct {
> char key[255];
> cahr value[255];
> } t_config;
>
> t_config config[20]; // Change 20 and 255 to adapt to your needs
>
>
> so your code would look like this:
>
> for(i = 0; i < n_values; ++i)
> {
> strcpy(keyword, good_values[i]);
>
> switch (read_config_var(values_file, keyword, value))
> {
> case 0:
> strcpy(config[i].key, keyword);
> strcpy(config[i].value, value);
> break;
> case -1:
> printf("\nFile Error for [%s] \n",
> values_file);
> break;
> case -2:
> printf("\nBad User Parm for [%s] \n",
> keyword);
> break;
> default:
> printf("\nUnknown Error Occurred \n");
> break;
> }
> }
>
>
> I'm not sure if is that what you want...and if I really understood your
> problem...
> So, let me know if this mail sucks! :)
>
> --
> Mariano Moreyra <mariano_moreyra@aca.org.ar>
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: named structure members
2003-09-11 14:26 ` Matthew Harrison
@ 2003-09-11 14:41 ` Mariano Moreyra
2003-09-11 15:28 ` Matthew Harrison
2003-09-11 15:50 ` Glynn Clements
1 sibling, 1 reply; 15+ messages in thread
From: Mariano Moreyra @ 2003-09-11 14:41 UTC (permalink / raw)
To: 'Matthew Harrison'; +Cc: linux-c-programming
Ok... I understood that what you wanted was to name your structure members
dynamically.
But if you will only have those for members, that's a good approach
Now, are you allocating memory for your strings before doing a strcpy??
Because you defined your members like char pointers, you have to dynamically
allocate some memory form them before use.
You could define your members like arrays instead of char pointers (i.e char
db_host[256])
------
this is getting very complex.
here is the struct that i want my config to go into:
<snip>
typedef struct _CFG_struct
{
char *db_host;
char *db_name;
char *db_pass;
char *db_user;
}CFG;
</snip>
and I am doing the following (pseudo code)
<snip>
for(i = 0; i <= array_of_config_items; i++)
{
keyword = array_of_config_items[i];
while (read_config_file != EOF)
{
check_return_value;
if keyword = 'item1'
{
item1 = value;
}
elsif keyword = 'item2'
{
item2 = value;
}
and_so_on();
}
}
</snip>
i can sort it now with the multiple if's but now it segfaults on
a strcpy for some reason and i'm still learning how to use gdb.
On Thu, Sep 11, 2003 at 11:03:59AM -0300, Mariano Moreyra wrote:
> > what i'm doing is stepping thru an array of different config options
> > calling the read_config_var on each one and checking the output.
> > if the output is good then i want to store the value in a member of
> > a structure. for example i want the config options to be stored like
> > this:
>
> > config.db_host = 'maiden.genestate.com'
> > config.db_user = 'root'
>
> > you get the idea. my question is how do i dynamically assign a name
> > to a structure member. I have good_values[i] which contains the
> > current config directive but if you look at line 8 of the example,
> > you can see the problem, how do i say config.good_values[i], when
> > good_values[i] is not itself a member.
>
> I think that I get the idea ...
> But are you sure that is that what you want??
> I mean, the idea is that in your config option you could have any config
> directives (or keys) and values??
> How you will handle that options later without knowing from the begining
> what options you could have??
> Anyway...I think that you could do is create your structure like this:
>
> typedef struct {
> char key[255];
> cahr value[255];
> } t_config;
>
> t_config config[20]; // Change 20 and 255 to adapt to your needs
>
>
> so your code would look like this:
>
> for(i = 0; i < n_values; ++i)
> {
> strcpy(keyword, good_values[i]);
>
> switch (read_config_var(values_file, keyword, value))
> {
> case 0:
> strcpy(config[i].key, keyword);
> strcpy(config[i].value, value);
> break;
> case -1:
> printf("\nFile Error for [%s] \n",
> values_file);
> break;
> case -2:
> printf("\nBad User Parm for [%s] \n",
> keyword);
> break;
> default:
> printf("\nUnknown Error Occurred \n");
> break;
> }
> }
>
>
> I'm not sure if is that what you want...and if I really understood your
> problem...
> So, let me know if this mail sucks! :)
>
> --
> Mariano Moreyra <mariano_moreyra@aca.org.ar>
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 14:41 ` Mariano Moreyra
@ 2003-09-11 15:28 ` Matthew Harrison
2003-09-11 15:35 ` Mariano Moreyra
0 siblings, 1 reply; 15+ messages in thread
From: Matthew Harrison @ 2003-09-11 15:28 UTC (permalink / raw)
To: Mariano Moreyra; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 4377 bytes --]
On Thu, Sep 11, 2003 at 11:41:34AM -0300, Mariano Moreyra wrote:
> Ok... I understood that what you wanted was to name your structure members
> dynamically.
> But if you will only have those for members, that's a good approach
> Now, are you allocating memory for your strings before doing a strcpy??
> Because you defined your members like char pointers, you have to dynamically
> allocate some memory form them before use.
> You could define your members like arrays instead of char pointers (i.e char
> db_host[256])
>
actually that was one of the things i tried to get rid of the segfault.
i used to have the struct below like:
typedef struct _CFG_struct
{
char db_host[32];
...
}
but then i changed it to the below option while trying to debug.
>
> ------
>
> this is getting very complex.
>
> here is the struct that i want my config to go into:
>
> <snip>
> typedef struct _CFG_struct
> {
> char *db_host;
> char *db_name;
> char *db_pass;
> char *db_user;
> }CFG;
> </snip>
>
> and I am doing the following (pseudo code)
>
> <snip>
> for(i = 0; i <= array_of_config_items; i++)
> {
> keyword = array_of_config_items[i];
>
> while (read_config_file != EOF)
> {
> check_return_value;
> if keyword = 'item1'
> {
> item1 = value;
> }
> elsif keyword = 'item2'
> {
> item2 = value;
> }
> and_so_on();
> }
> }
> </snip>
>
> i can sort it now with the multiple if's but now it segfaults on
> a strcpy for some reason and i'm still learning how to use gdb.
>
> On Thu, Sep 11, 2003 at 11:03:59AM -0300, Mariano Moreyra wrote:
> > > what i'm doing is stepping thru an array of different config options
> > > calling the read_config_var on each one and checking the output.
> > > if the output is good then i want to store the value in a member of
> > > a structure. for example i want the config options to be stored like
> > > this:
> >
> > > config.db_host = 'maiden.genestate.com'
> > > config.db_user = 'root'
> >
> > > you get the idea. my question is how do i dynamically assign a name
> > > to a structure member. I have good_values[i] which contains the
> > > current config directive but if you look at line 8 of the example,
> > > you can see the problem, how do i say config.good_values[i], when
> > > good_values[i] is not itself a member.
> >
> > I think that I get the idea ...
> > But are you sure that is that what you want??
> > I mean, the idea is that in your config option you could have any config
> > directives (or keys) and values??
> > How you will handle that options later without knowing from the begining
> > what options you could have??
> > Anyway...I think that you could do is create your structure like this:
> >
> > typedef struct {
> > char key[255];
> > cahr value[255];
> > } t_config;
> >
> > t_config config[20]; // Change 20 and 255 to adapt to your needs
> >
> >
> > so your code would look like this:
> >
> > for(i = 0; i < n_values; ++i)
> > {
> > strcpy(keyword, good_values[i]);
> >
> > switch (read_config_var(values_file, keyword, value))
> > {
> > case 0:
> > strcpy(config[i].key, keyword);
> > strcpy(config[i].value, value);
> > break;
> > case -1:
> > printf("\nFile Error for [%s] \n",
> > values_file);
> > break;
> > case -2:
> > printf("\nBad User Parm for [%s] \n",
> > keyword);
> > break;
> > default:
> > printf("\nUnknown Error Occurred \n");
> > break;
> > }
> > }
> >
> >
> > I'm not sure if is that what you want...and if I really understood your
> > problem...
> > So, let me know if this mail sucks! :)
> >
> > --
> > Mariano Moreyra <mariano_moreyra@aca.org.ar>
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: named structure members
2003-09-11 15:28 ` Matthew Harrison
@ 2003-09-11 15:35 ` Mariano Moreyra
2003-09-11 15:42 ` Matthew Harrison
0 siblings, 1 reply; 15+ messages in thread
From: Mariano Moreyra @ 2003-09-11 15:35 UTC (permalink / raw)
To: 'Matthew Harrison'; +Cc: linux-c-programming
Are you still getting that segfault??
Send me the section of the code where the strcpy is (if you want ofcourse
hehe)
-----Mensaje original-----
De: Matthew Harrison [mailto:matth@3d-computers.co.uk]
Enviado el: Jueves, 11 de Septiembre de 2003 12:28
Para: Mariano Moreyra
CC: linux-c-programming@vger.kernel.org
Asunto: Re: named structure members
On Thu, Sep 11, 2003 at 11:41:34AM -0300, Mariano Moreyra wrote:
> Ok... I understood that what you wanted was to name your structure members
> dynamically.
> But if you will only have those for members, that's a good approach
> Now, are you allocating memory for your strings before doing a strcpy??
> Because you defined your members like char pointers, you have to
dynamically
> allocate some memory form them before use.
> You could define your members like arrays instead of char pointers (i.e
char
> db_host[256])
>
actually that was one of the things i tried to get rid of the segfault.
i used to have the struct below like:
typedef struct _CFG_struct
{
char db_host[32];
...
}
but then i changed it to the below option while trying to debug.
>
> ------
>
> this is getting very complex.
>
> here is the struct that i want my config to go into:
>
> <snip>
> typedef struct _CFG_struct
> {
> char *db_host;
> char *db_name;
> char *db_pass;
> char *db_user;
> }CFG;
> </snip>
>
> and I am doing the following (pseudo code)
>
> <snip>
> for(i = 0; i <= array_of_config_items; i++)
> {
> keyword = array_of_config_items[i];
>
> while (read_config_file != EOF)
> {
> check_return_value;
> if keyword = 'item1'
> {
> item1 = value;
> }
> elsif keyword = 'item2'
> {
> item2 = value;
> }
> and_so_on();
> }
> }
> </snip>
>
> i can sort it now with the multiple if's but now it segfaults on
> a strcpy for some reason and i'm still learning how to use gdb.
>
> On Thu, Sep 11, 2003 at 11:03:59AM -0300, Mariano Moreyra wrote:
> > > what i'm doing is stepping thru an array of different config options
> > > calling the read_config_var on each one and checking the output.
> > > if the output is good then i want to store the value in a member of
> > > a structure. for example i want the config options to be stored like
> > > this:
> >
> > > config.db_host = 'maiden.genestate.com'
> > > config.db_user = 'root'
> >
> > > you get the idea. my question is how do i dynamically assign a name
> > > to a structure member. I have good_values[i] which contains the
> > > current config directive but if you look at line 8 of the example,
> > > you can see the problem, how do i say config.good_values[i], when
> > > good_values[i] is not itself a member.
> >
> > I think that I get the idea ...
> > But are you sure that is that what you want??
> > I mean, the idea is that in your config option you could have any config
> > directives (or keys) and values??
> > How you will handle that options later without knowing from the begining
> > what options you could have??
> > Anyway...I think that you could do is create your structure like this:
> >
> > typedef struct {
> > char key[255];
> > cahr value[255];
> > } t_config;
> >
> > t_config config[20]; // Change 20 and 255 to adapt to your needs
> >
> >
> > so your code would look like this:
> >
> > for(i = 0; i < n_values; ++i)
> > {
> > strcpy(keyword, good_values[i]);
> >
> > switch (read_config_var(values_file, keyword, value))
> > {
> > case 0:
> > strcpy(config[i].key, keyword);
> > strcpy(config[i].value, value);
> > break;
> > case -1:
> > printf("\nFile Error for [%s] \n",
> > values_file);
> > break;
> > case -2:
> > printf("\nBad User Parm for [%s] \n",
> > keyword);
> > break;
> > default:
> > printf("\nUnknown Error Occurred \n");
> > break;
> > }
> > }
> >
> >
> > I'm not sure if is that what you want...and if I really understood your
> > problem...
> > So, let me know if this mail sucks! :)
> >
> > --
> > Mariano Moreyra <mariano_moreyra@aca.org.ar>
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 15:35 ` Mariano Moreyra
@ 2003-09-11 15:42 ` Matthew Harrison
0 siblings, 0 replies; 15+ messages in thread
From: Matthew Harrison @ 2003-09-11 15:42 UTC (permalink / raw)
To: Mariano Moreyra; +Cc: linux-c-programming
[-- Attachment #1.1: Type: text/plain, Size: 5442 bytes --]
that's very generous of you.
c is not my strong point yet so all help is appreciated.
attached is the sources in question. most of the code is
in src/inventory.c and src/read_config_var.c
thanks again for taking the time
On Thu, Sep 11, 2003 at 12:35:24PM -0300, Mariano Moreyra wrote:
> Are you still getting that segfault??
> Send me the section of the code where the strcpy is (if you want ofcourse
> hehe)
>
>
> -----Mensaje original-----
> De: Matthew Harrison [mailto:matth@3d-computers.co.uk]
> Enviado el: Jueves, 11 de Septiembre de 2003 12:28
> Para: Mariano Moreyra
> CC: linux-c-programming@vger.kernel.org
> Asunto: Re: named structure members
>
>
> On Thu, Sep 11, 2003 at 11:41:34AM -0300, Mariano Moreyra wrote:
> > Ok... I understood that what you wanted was to name your structure members
> > dynamically.
> > But if you will only have those for members, that's a good approach
> > Now, are you allocating memory for your strings before doing a strcpy??
> > Because you defined your members like char pointers, you have to
> dynamically
> > allocate some memory form them before use.
> > You could define your members like arrays instead of char pointers (i.e
> char
> > db_host[256])
> >
>
> actually that was one of the things i tried to get rid of the segfault.
>
> i used to have the struct below like:
>
> typedef struct _CFG_struct
> {
> char db_host[32];
> ...
> }
>
> but then i changed it to the below option while trying to debug.
>
> >
> > ------
> >
> > this is getting very complex.
> >
> > here is the struct that i want my config to go into:
> >
> > <snip>
> > typedef struct _CFG_struct
> > {
> > char *db_host;
> > char *db_name;
> > char *db_pass;
> > char *db_user;
> > }CFG;
> > </snip>
> >
> > and I am doing the following (pseudo code)
> >
> > <snip>
> > for(i = 0; i <= array_of_config_items; i++)
> > {
> > keyword = array_of_config_items[i];
> >
> > while (read_config_file != EOF)
> > {
> > check_return_value;
> > if keyword = 'item1'
> > {
> > item1 = value;
> > }
> > elsif keyword = 'item2'
> > {
> > item2 = value;
> > }
> > and_so_on();
> > }
> > }
> > </snip>
> >
> > i can sort it now with the multiple if's but now it segfaults on
> > a strcpy for some reason and i'm still learning how to use gdb.
> >
> > On Thu, Sep 11, 2003 at 11:03:59AM -0300, Mariano Moreyra wrote:
> > > > what i'm doing is stepping thru an array of different config options
> > > > calling the read_config_var on each one and checking the output.
> > > > if the output is good then i want to store the value in a member of
> > > > a structure. for example i want the config options to be stored like
> > > > this:
> > >
> > > > config.db_host = 'maiden.genestate.com'
> > > > config.db_user = 'root'
> > >
> > > > you get the idea. my question is how do i dynamically assign a name
> > > > to a structure member. I have good_values[i] which contains the
> > > > current config directive but if you look at line 8 of the example,
> > > > you can see the problem, how do i say config.good_values[i], when
> > > > good_values[i] is not itself a member.
> > >
> > > I think that I get the idea ...
> > > But are you sure that is that what you want??
> > > I mean, the idea is that in your config option you could have any config
> > > directives (or keys) and values??
> > > How you will handle that options later without knowing from the begining
> > > what options you could have??
> > > Anyway...I think that you could do is create your structure like this:
> > >
> > > typedef struct {
> > > char key[255];
> > > cahr value[255];
> > > } t_config;
> > >
> > > t_config config[20]; // Change 20 and 255 to adapt to your needs
> > >
> > >
> > > so your code would look like this:
> > >
> > > for(i = 0; i < n_values; ++i)
> > > {
> > > strcpy(keyword, good_values[i]);
> > >
> > > switch (read_config_var(values_file, keyword, value))
> > > {
> > > case 0:
> > > strcpy(config[i].key, keyword);
> > > strcpy(config[i].value, value);
> > > break;
> > > case -1:
> > > printf("\nFile Error for [%s] \n",
> > > values_file);
> > > break;
> > > case -2:
> > > printf("\nBad User Parm for [%s] \n",
> > > keyword);
> > > break;
> > > default:
> > > printf("\nUnknown Error Occurred \n");
> > > break;
> > > }
> > > }
> > >
> > >
> > > I'm not sure if is that what you want...and if I really understood your
> > > problem...
> > > So, let me know if this mail sucks! :)
> > >
> > > --
> > > Mariano Moreyra <mariano_moreyra@aca.org.ar>
> >
> > --
> > Mat Harrison
> > Technical Developer
> > 3d Computer Systems Ltd.
> > matth@3d-computers.co.uk
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #1.2: inventory-0.1.tar.gz --]
[-- Type: application/x-tar-gz, Size: 52687 bytes --]
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 14:26 ` Matthew Harrison
2003-09-11 14:41 ` Mariano Moreyra
@ 2003-09-11 15:50 ` Glynn Clements
2003-09-11 16:52 ` Jan-Benedict Glaw
1 sibling, 1 reply; 15+ messages in thread
From: Glynn Clements @ 2003-09-11 15:50 UTC (permalink / raw)
To: Matthew Harrison; +Cc: Mariano Moreyra, linux-c-programming
Matthew Harrison wrote:
> here is the struct that i want my config to go into:
>
> <snip>
> typedef struct _CFG_struct
> {
> char *db_host;
> char *db_name;
> char *db_pass;
> char *db_user;
> }CFG;
> </snip>
>
> and I am doing the following (pseudo code)
>
> <snip>
> for(i = 0; i <= array_of_config_items; i++)
> {
> keyword = array_of_config_items[i];
>
> while (read_config_file != EOF)
> {
> check_return_value;
> if keyword = 'item1'
> {
> item1 = value;
> }
> elsif keyword = 'item2'
> {
> item2 = value;
> }
> and_so_on();
> }
> }
> </snip>
There are a number of ways which you could improve upon this, but
there isn't anything along the lines of:
config.<something involving "keyword"> = value;
Structure fields have to be specified explicitly; you can't "index" a
structure in the manner of an associative array.
One possible solution is:
enum cfg_option {
cfg_db_host,
cfg_db_name,
cfg_db_pass,
cfg_db_user,
CFG_COUNT
};
static const char * const option_names[CFG_COUNT] = {
"host",
"name",
"pass",
"user"
};
struct cfg {
char *options[CFG_COUNT];
};
static int set_option(struct cfg *config, const char *keyword, const char *value)
{
int i;
for (i = 0; i < CFG_COUNT; i++)
{
if (strcmp(option_names[i], keyword) != 0)
continue;
config->options[i] = strdup(value);
return 0;
}
fprintf(stderr, "option %s not found\n", keyword);
return -1;
}
You would then access the options as e.g. config.options[cfg_db_host]
etc.
> i can sort it now with the multiple if's but now it segfaults on
> a strcpy for some reason
The reason is that the pointers in the structure aren't pointing to
valid memory. You probably want to use strdup(), i.e.
config.db_host = strdup(value);
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 15:50 ` Glynn Clements
@ 2003-09-11 16:52 ` Jan-Benedict Glaw
2003-09-11 17:13 ` Matthew Harrison
0 siblings, 1 reply; 15+ messages in thread
From: Jan-Benedict Glaw @ 2003-09-11 16:52 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1618 bytes --]
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements <glynn.clements@virgin.net>
wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> Matthew Harrison wrote:
> > here is the struct that i want my config to go into:
> There are a number of ways which you could improve upon this, but
> there isn't anything along the lines of:
>
> config.<something involving "keyword"> = value;
>
> Structure fields have to be specified explicitly; you can't "index" a
> structure in the manner of an associative array.
>
> One possible solution is:
>
> enum cfg_option {
> cfg_db_host,
> cfg_db_name,
> cfg_db_pass,
> cfg_db_user,
> CFG_COUNT
> };
[...]
Another approach is to use a union (containing any possible things, in
your case only char pointers) inside a struct (which also contains a
char name[] and an int type). Build up an array with these structs, one
for each config option.
Then have a function which gets on option name as well as an option
value which searches throuch all ->name's of your array and assigns the
(properly converted) value to the proper union type.
To access these options, use multiple access functions (ie.
int get_int(char *option)
) which also searches through the array and returns the int part of the
union.
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak!
ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-11 16:52 ` Jan-Benedict Glaw
@ 2003-09-11 17:13 ` Matthew Harrison
2003-09-12 13:22 ` Mariano Moreyra
0 siblings, 1 reply; 15+ messages in thread
From: Matthew Harrison @ 2003-09-11 17:13 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 2064 bytes --]
i am going thru all these suggestions at a slow rate, but it's time
to finish work now so I won't be able to reply from home.
please don't think i'm ignoring you, i am very grateful.
On Thu, Sep 11, 2003 at 06:52:13PM +0200, Jan-Benedict Glaw wrote:
> On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements <glynn.clements@virgin.net>
> wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> > Matthew Harrison wrote:
> > > here is the struct that i want my config to go into:
> > There are a number of ways which you could improve upon this, but
> > there isn't anything along the lines of:
> >
> > config.<something involving "keyword"> = value;
> >
> > Structure fields have to be specified explicitly; you can't "index" a
> > structure in the manner of an associative array.
> >
> > One possible solution is:
> >
> > enum cfg_option {
> > cfg_db_host,
> > cfg_db_name,
> > cfg_db_pass,
> > cfg_db_user,
> > CFG_COUNT
> > };
> [...]
> Another approach is to use a union (containing any possible things, in
> your case only char pointers) inside a struct (which also contains a
> char name[] and an int type). Build up an array with these structs, one
> for each config option.
>
> Then have a function which gets on option name as well as an option
> value which searches throuch all ->name's of your array and assigns the
> (properly converted) value to the proper union type.
>
> To access these options, use multiple access functions (ie.
>
> int get_int(char *option)
>
> ) which also searches through the array and returns the int part of the
> union.
>
> MfG, JBG
>
> --
> Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
> "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg
> fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak!
> ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: named structure members
2003-09-11 17:13 ` Matthew Harrison
@ 2003-09-12 13:22 ` Mariano Moreyra
2003-09-12 13:47 ` Matthew Harrison
0 siblings, 1 reply; 15+ messages in thread
From: Mariano Moreyra @ 2003-09-12 13:22 UTC (permalink / raw)
To: 'Matthew Harrison', linux-c-programming
Hi Matthew,
First of all, I couldn't install the Inventory. I don't know why the
configure script throws me an error.
But looking at the sources:
- Like I said yesterday, if you define db_host as a "char *db_host "
instead of
"char db_host[NN]" you have to alloc some memory to that string before
the strcpy
But you told me that you had it defined like "char db_host[NN]" and already
had that segfault...am I right??
-----Mensaje original-----
De: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]En nombre de Matthew
Harrison
Enviado el: Jueves, 11 de Septiembre de 2003 14:13
Para: linux-c-programming@vger.kernel.org
Asunto: Re: named structure members
i am going thru all these suggestions at a slow rate, but it's time
to finish work now so I won't be able to reply from home.
please don't think i'm ignoring you, i am very grateful.
On Thu, Sep 11, 2003 at 06:52:13PM +0200, Jan-Benedict Glaw wrote:
> On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
<glynn.clements@virgin.net>
> wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> > Matthew Harrison wrote:
> > > here is the struct that i want my config to go into:
> > There are a number of ways which you could improve upon this, but
> > there isn't anything along the lines of:
> >
> > config.<something involving "keyword"> = value;
> >
> > Structure fields have to be specified explicitly; you can't "index" a
> > structure in the manner of an associative array.
> >
> > One possible solution is:
> >
> > enum cfg_option {
> > cfg_db_host,
> > cfg_db_name,
> > cfg_db_pass,
> > cfg_db_user,
> > CFG_COUNT
> > };
> [...]
> Another approach is to use a union (containing any possible things, in
> your case only char pointers) inside a struct (which also contains a
> char name[] and an int type). Build up an array with these structs, one
> for each config option.
>
> Then have a function which gets on option name as well as an option
> value which searches throuch all ->name's of your array and assigns the
> (properly converted) value to the proper union type.
>
> To access these options, use multiple access functions (ie.
>
> int get_int(char *option)
>
> ) which also searches through the array and returns the int part of the
> union.
>
> MfG, JBG
>
> --
> Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
> "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen
Krieg
> fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
Irak!
> ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-12 13:22 ` Mariano Moreyra
@ 2003-09-12 13:47 ` Matthew Harrison
2003-09-12 13:52 ` Mariano Moreyra
0 siblings, 1 reply; 15+ messages in thread
From: Matthew Harrison @ 2003-09-12 13:47 UTC (permalink / raw)
To: Mariano Moreyra; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 3868 bytes --]
well it's all changed a bit now. i have been learning how to use gdb
and as a result have managed to replace the strcpy's and other bit that
cause segfaults. The app now runs cleanly but i am running into trouble
with my config-file-reading function. it was not my function, i copied
it off the web so i might just write myself a quick one.
attached is a version that should not segfault and only stops working
because the configfile isn't read properly.
what part if the configure script did you have trouble with.
thanks
On Fri, Sep 12, 2003 at 10:22:40AM -0300, Mariano Moreyra wrote:
> Hi Matthew,
> First of all, I couldn't install the Inventory. I don't know why the
> configure script throws me an error.
> But looking at the sources:
> - Like I said yesterday, if you define db_host as a "char *db_host "
> instead of
> "char db_host[NN]" you have to alloc some memory to that string before
> the strcpy
>
> But you told me that you had it defined like "char db_host[NN]" and already
> had that segfault...am I right??
>
> -----Mensaje original-----
> De: linux-c-programming-owner@vger.kernel.org
> [mailto:linux-c-programming-owner@vger.kernel.org]En nombre de Matthew
> Harrison
> Enviado el: Jueves, 11 de Septiembre de 2003 14:13
> Para: linux-c-programming@vger.kernel.org
> Asunto: Re: named structure members
>
>
> i am going thru all these suggestions at a slow rate, but it's time
> to finish work now so I won't be able to reply from home.
>
> please don't think i'm ignoring you, i am very grateful.
>
> On Thu, Sep 11, 2003 at 06:52:13PM +0200, Jan-Benedict Glaw wrote:
> > On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
> <glynn.clements@virgin.net>
> > wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> > > Matthew Harrison wrote:
> > > > here is the struct that i want my config to go into:
> > > There are a number of ways which you could improve upon this, but
> > > there isn't anything along the lines of:
> > >
> > > config.<something involving "keyword"> = value;
> > >
> > > Structure fields have to be specified explicitly; you can't "index" a
> > > structure in the manner of an associative array.
> > >
> > > One possible solution is:
> > >
> > > enum cfg_option {
> > > cfg_db_host,
> > > cfg_db_name,
> > > cfg_db_pass,
> > > cfg_db_user,
> > > CFG_COUNT
> > > };
> > [...]
> > Another approach is to use a union (containing any possible things, in
> > your case only char pointers) inside a struct (which also contains a
> > char name[] and an int type). Build up an array with these structs, one
> > for each config option.
> >
> > Then have a function which gets on option name as well as an option
> > value which searches throuch all ->name's of your array and assigns the
> > (properly converted) value to the proper union type.
> >
> > To access these options, use multiple access functions (ie.
> >
> > int get_int(char *option)
> >
> > ) which also searches through the array and returns the int part of the
> > union.
> >
> > MfG, JBG
> >
> > --
> > Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
> > "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen
> Krieg
> > fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
> Irak!
> > ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));
>
>
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
* RE: named structure members
2003-09-12 13:47 ` Matthew Harrison
@ 2003-09-12 13:52 ` Mariano Moreyra
2003-09-12 14:33 ` Matthew Harrison
0 siblings, 1 reply; 15+ messages in thread
From: Mariano Moreyra @ 2003-09-12 13:52 UTC (permalink / raw)
To: 'Matthew Harrison'; +Cc: linux-c-programming
It seems like there is no attached version :)
-----Mensaje original-----
De: Matthew Harrison [mailto:matth@3d-computers.co.uk]
Enviado el: Viernes, 12 de Septiembre de 2003 10:47
Para: Mariano Moreyra
CC: linux-c-programming@vger.kernel.org
Asunto: Re: named structure members
well it's all changed a bit now. i have been learning how to use gdb
and as a result have managed to replace the strcpy's and other bit that
cause segfaults. The app now runs cleanly but i am running into trouble
with my config-file-reading function. it was not my function, i copied
it off the web so i might just write myself a quick one.
attached is a version that should not segfault and only stops working
because the configfile isn't read properly.
what part if the configure script did you have trouble with.
thanks
On Fri, Sep 12, 2003 at 10:22:40AM -0300, Mariano Moreyra wrote:
> Hi Matthew,
> First of all, I couldn't install the Inventory. I don't know why the
> configure script throws me an error.
> But looking at the sources:
> - Like I said yesterday, if you define db_host as a "char *db_host "
> instead of
> "char db_host[NN]" you have to alloc some memory to that string before
> the strcpy
>
> But you told me that you had it defined like "char db_host[NN]" and
already
> had that segfault...am I right??
>
> -----Mensaje original-----
> De: linux-c-programming-owner@vger.kernel.org
> [mailto:linux-c-programming-owner@vger.kernel.org]En nombre de Matthew
> Harrison
> Enviado el: Jueves, 11 de Septiembre de 2003 14:13
> Para: linux-c-programming@vger.kernel.org
> Asunto: Re: named structure members
>
>
> i am going thru all these suggestions at a slow rate, but it's time
> to finish work now so I won't be able to reply from home.
>
> please don't think i'm ignoring you, i am very grateful.
>
> On Thu, Sep 11, 2003 at 06:52:13PM +0200, Jan-Benedict Glaw wrote:
> > On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
> <glynn.clements@virgin.net>
> > wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> > > Matthew Harrison wrote:
> > > > here is the struct that i want my config to go into:
> > > There are a number of ways which you could improve upon this, but
> > > there isn't anything along the lines of:
> > >
> > > config.<something involving "keyword"> = value;
> > >
> > > Structure fields have to be specified explicitly; you can't "index" a
> > > structure in the manner of an associative array.
> > >
> > > One possible solution is:
> > >
> > > enum cfg_option {
> > > cfg_db_host,
> > > cfg_db_name,
> > > cfg_db_pass,
> > > cfg_db_user,
> > > CFG_COUNT
> > > };
> > [...]
> > Another approach is to use a union (containing any possible things, in
> > your case only char pointers) inside a struct (which also contains a
> > char name[] and an int type). Build up an array with these structs, one
> > for each config option.
> >
> > Then have a function which gets on option name as well as an option
> > value which searches throuch all ->name's of your array and assigns the
> > (properly converted) value to the proper union type.
> >
> > To access these options, use multiple access functions (ie.
> >
> > int get_int(char *option)
> >
> > ) which also searches through the array and returns the int part of the
> > union.
> >
> > MfG, JBG
> >
> > --
> > Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
> > "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen
> Krieg
> > fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
> Irak!
> > ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM |
TCPA));
>
>
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
>
> -
> To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: named structure members
2003-09-12 13:52 ` Mariano Moreyra
@ 2003-09-12 14:33 ` Matthew Harrison
0 siblings, 0 replies; 15+ messages in thread
From: Matthew Harrison @ 2003-09-12 14:33 UTC (permalink / raw)
To: Mariano Moreyra; +Cc: linux-c-programming
[-- Attachment #1.1: Type: text/plain, Size: 4620 bytes --]
my mistake, i'm always doing that.
lets try this one :)
On Fri, Sep 12, 2003 at 10:52:54AM -0300, Mariano Moreyra wrote:
> It seems like there is no attached version :)
>
>
> -----Mensaje original-----
> De: Matthew Harrison [mailto:matth@3d-computers.co.uk]
> Enviado el: Viernes, 12 de Septiembre de 2003 10:47
> Para: Mariano Moreyra
> CC: linux-c-programming@vger.kernel.org
> Asunto: Re: named structure members
>
>
> well it's all changed a bit now. i have been learning how to use gdb
> and as a result have managed to replace the strcpy's and other bit that
> cause segfaults. The app now runs cleanly but i am running into trouble
> with my config-file-reading function. it was not my function, i copied
> it off the web so i might just write myself a quick one.
>
> attached is a version that should not segfault and only stops working
> because the configfile isn't read properly.
>
> what part if the configure script did you have trouble with.
>
> thanks
>
> On Fri, Sep 12, 2003 at 10:22:40AM -0300, Mariano Moreyra wrote:
> > Hi Matthew,
> > First of all, I couldn't install the Inventory. I don't know why the
> > configure script throws me an error.
> > But looking at the sources:
> > - Like I said yesterday, if you define db_host as a "char *db_host "
> > instead of
> > "char db_host[NN]" you have to alloc some memory to that string before
> > the strcpy
> >
> > But you told me that you had it defined like "char db_host[NN]" and
> already
> > had that segfault...am I right??
> >
> > -----Mensaje original-----
> > De: linux-c-programming-owner@vger.kernel.org
> > [mailto:linux-c-programming-owner@vger.kernel.org]En nombre de Matthew
> > Harrison
> > Enviado el: Jueves, 11 de Septiembre de 2003 14:13
> > Para: linux-c-programming@vger.kernel.org
> > Asunto: Re: named structure members
> >
> >
> > i am going thru all these suggestions at a slow rate, but it's time
> > to finish work now so I won't be able to reply from home.
> >
> > please don't think i'm ignoring you, i am very grateful.
> >
> > On Thu, Sep 11, 2003 at 06:52:13PM +0200, Jan-Benedict Glaw wrote:
> > > On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
> > <glynn.clements@virgin.net>
> > > wrote in message <16224.39341.204427.234601@cerise.nosuchdomain.co.uk>:
> > > > Matthew Harrison wrote:
> > > > > here is the struct that i want my config to go into:
> > > > There are a number of ways which you could improve upon this, but
> > > > there isn't anything along the lines of:
> > > >
> > > > config.<something involving "keyword"> = value;
> > > >
> > > > Structure fields have to be specified explicitly; you can't "index" a
> > > > structure in the manner of an associative array.
> > > >
> > > > One possible solution is:
> > > >
> > > > enum cfg_option {
> > > > cfg_db_host,
> > > > cfg_db_name,
> > > > cfg_db_pass,
> > > > cfg_db_user,
> > > > CFG_COUNT
> > > > };
> > > [...]
> > > Another approach is to use a union (containing any possible things, in
> > > your case only char pointers) inside a struct (which also contains a
> > > char name[] and an int type). Build up an array with these structs, one
> > > for each config option.
> > >
> > > Then have a function which gets on option name as well as an option
> > > value which searches throuch all ->name's of your array and assigns the
> > > (properly converted) value to the proper union type.
> > >
> > > To access these options, use multiple access functions (ie.
> > >
> > > int get_int(char *option)
> > >
> > > ) which also searches through the array and returns the int part of the
> > > union.
> > >
> > > MfG, JBG
> > >
> > > --
> > > Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
> > > "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen
> > Krieg
> > > fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
> > Irak!
> > > ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM |
> TCPA));
> >
> >
> >
> > --
> > Mat Harrison
> > Technical Developer
> > 3d Computer Systems Ltd.
> > matth@3d-computers.co.uk
> >
> > -
> > To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at http://vger.kernel.org/majordomo-info.html
>
> --
> Mat Harrison
> Technical Developer
> 3d Computer Systems Ltd.
> matth@3d-computers.co.uk
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
matth@3d-computers.co.uk
[-- Attachment #1.2: inventory-0.1.tar.gz --]
[-- Type: application/x-tar-gz, Size: 52766 bytes --]
[-- Attachment #2: Type: application/pgp-signature, Size: 187 bytes --]
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2003-09-12 14:33 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-09-11 11:07 named structure members Matthew Harrison
2003-09-11 11:50 ` Jeff Woods
2003-09-11 14:03 ` Mariano Moreyra
2003-09-11 14:26 ` Matthew Harrison
2003-09-11 14:41 ` Mariano Moreyra
2003-09-11 15:28 ` Matthew Harrison
2003-09-11 15:35 ` Mariano Moreyra
2003-09-11 15:42 ` Matthew Harrison
2003-09-11 15:50 ` Glynn Clements
2003-09-11 16:52 ` Jan-Benedict Glaw
2003-09-11 17:13 ` Matthew Harrison
2003-09-12 13:22 ` Mariano Moreyra
2003-09-12 13:47 ` Matthew Harrison
2003-09-12 13:52 ` Mariano Moreyra
2003-09-12 14:33 ` Matthew Harrison
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).