named structure members
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
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
Quoted 23 lines · Matthew Harrison
<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; } }
Quoted 8 lines
</snip>
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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.
Quoted 1 line
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 <[email protected]>
Quoted 6 lines
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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:
Quoted 8 lines
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 <[email protected]>
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.
Quoted 64 lines · Mariano Moreyra
Quoted 6 lines
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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:Quoted 8 lines
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_configconfig[20]; // Change 20 and 255 to adapt to your needsso 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! :)Signature · 1 line
Mariano Moreyra <[email protected]>
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
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.
Quoted 58 lines · Mariano Moreyra
Quoted 6 lines
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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:Quoted 8 lines
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_configconfig[20]; // Change 20 and 255 to adapt to your needsso 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; } }
Quoted 6 lines
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 <[email protected]>
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]
Quoted 9 lines · Mariano Moreyra
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.
Quoted 114 lines
------
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.
Quoted 64 lines · Mariano Moreyra
Quoted 6 lines
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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:Quoted 8 lines
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_configconfig[20]; // Change 20 and 255 to adapt to your needsso 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 <[email protected]>Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
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:[email protected]] Enviado el: Jueves, 11 de Septiembre de 2003 12:28
Para: Mariano Moreyra
CC: [email protected]
Asunto: Re: named structure members
Quoted 5 lines · Mariano Moreyra
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
Quoted 2 lines
allocate some memory form them before use.
You could define your members like arrays instead of char pointers (i.e
char
Quoted 2 lines
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.
Quoted 106 lines
------
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.
Quoted 62 lines · Mariano Moreyra
Quoted 6 lines
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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:Quoted 8 lines
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_configconfig[20]; // Change 20 and 255 to adapt to your needsso 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! :)
Quoted 8 lines
Quoted 2 lines
--
Mariano Moreyra <[email protected]>--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]
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
Quoted 158 lines · Mariano Moreyra
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:[email protected]] Enviado el: Jueves, 11 de Septiembre de 2003 12:28
Para: Mariano Moreyra
CC: [email protected]
Asunto: Re: named structure membersQuoted 5 lines · Mariano Moreyra
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 todynamically
Quoted 2 lines
allocate some memory form them before use.
You could define your members like arrays instead of char pointers (i.echar
Quoted 2 lines
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.
Quoted 114 lines
------
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.
Quoted 58 lines · Mariano Moreyra
Quoted 6 lines
what i'm doing is stepping thru an array of different config options calling the
read_config_varon 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:Quoted 8 lines
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_configconfig[20]; // Change 20 and 255 to adapt to your needsso 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; } }Quoted 6 lines
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 <[email protected]>--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
Quoted 33 lines · Matthew Harrison
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(); } }
Quoted 1 line
</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.
Quoted 2 lines
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);
Signature · 1 line
Glynn Clements <[email protected]>
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements <[email protected]> wrote in message <[email protected]>:
Quoted 12 lines
Quoted 1 line · Matthew Harrison
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:
Quoted 7 lines
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
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +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));
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.
Quoted 45 lines · Jan-Benedict Glaw
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements <[email protected]> wrote in message <[email protected]>:
Quoted 12 lines
Quoted 1 line · Matthew Harrison
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:
Quoted 7 lines
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
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +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));
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
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: [email protected] [mailto:[email protected]]En nombre de Matthew Harrison
Enviado el: Jueves, 11 de Septiembre de 2003 14:13
Para: [email protected]
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.
Quoted 1 line · Jan-Benedict Glaw
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
Quoted 34 lines
wrote in message <[email protected]>:
Quoted 12 lines
Quoted 1 line · Matthew Harrison
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:
Quoted 7 lines
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)
Quoted 8 lines
) which also searches through the array and returns the int part of the union.
MfG, JBG
--
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen
Krieg
Quoted 1 line
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
Irak!
Quoted 1 line
ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
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
Quoted 88 lines · Mariano Moreyra
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 definedb_hostas a "char*db_host"
instead of
"char db_host[NN]" you have to alloc some memory to that string before
the strcpyBut you told me that you had it defined like "char db_host[NN]" and already had that segfault...am I right??
-----Mensaje original-----
De: [email protected] [mailto:[email protected]]En nombre de Matthew Harrison
Enviado el: Jueves, 11 de Septiembre de 2003 14:13
Para: [email protected]
Asunto: Re: named structure membersi 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.
Quoted 1 line · Jan-Benedict Glaw
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
Quoted 34 lines
wrote in message <[email protected]>:
Quoted 12 lines
Quoted 1 line · Matthew Harrison
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:
Quoted 7 lines
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)Quoted 8 lines
) which also searches through the array and returns the int part of the union.
MfG, JBG
--
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | GegenKrieg
Quoted 1 line
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
Irak!
Quoted 1 line
ret = do_actions((curr | FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM | TCPA));Signature · 9 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected] - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
It seems like there is no attached version :)
-----Mensaje original-----
De: Matthew Harrison [mailto:[email protected]] Enviado el: Viernes, 12 de Septiembre de 2003 10:47
Para: Mariano Moreyra
CC: [email protected]
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
Quoted 10 lines · Mariano Moreyra
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 definedb_hostas a "char*db_host"
instead of
"char db_host[NN]" you have to alloc some memory to that string before
the strcpyBut you told me that you had it defined like "char db_host[NN]" and
already
Quoted 56 lines
had that segfault...am I right??
-----Mensaje original-----
De: [email protected] [mailto:[email protected]]En nombre de Matthew Harrison
Enviado el: Jueves, 11 de Septiembre de 2003 14:13
Para: [email protected]
Asunto: Re: named structure membersi 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.
Quoted 1 line · Jan-Benedict Glaw
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
Quoted 37 lines
wrote in message <[email protected]>:
Quoted 12 lines
Quoted 1 line · Matthew Harrison
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:
Quoted 7 lines
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.
Quoted 9 lines
Quoted 5 lines
MfG, JBG
--
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | GegenKrieg
Quoted 1 line
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
Irak!
Quoted 1 line
ret = do_actions((curr |
FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM |
TCPA));
Quoted 11 lines
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
Quoted 2 lines
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
my mistake, i'm always doing that.
lets try this one :)
Quoted 122 lines · Mariano Moreyra
It seems like there is no attached version :)
-----Mensaje original-----
De: Matthew Harrison [mailto:[email protected]] Enviado el: Viernes, 12 de Septiembre de 2003 10:47
Para: Mariano Moreyra
CC: [email protected]
Asunto: Re: named structure memberswell 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
Quoted 10 lines · Mariano Moreyra
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 definedb_hostas a "char*db_host"
instead of
"char db_host[NN]" you have to alloc some memory to that string before
the strcpyBut you told me that you had it defined like "char db_host[NN]" and
already
Quoted 56 lines
had that segfault...am I right??
-----Mensaje original-----
De: [email protected] [mailto:[email protected]]En nombre de Matthew Harrison
Enviado el: Jueves, 11 de Septiembre de 2003 14:13
Para: [email protected]
Asunto: Re: named structure membersi 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.
Quoted 1 line · Jan-Benedict Glaw
On Thu, 2003-09-11 16:50:05 +0100, Glynn Clements
Quoted 37 lines
wrote in message <[email protected]>:
Quoted 12 lines
Quoted 1 line · Matthew Harrison
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:
Quoted 7 lines
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.
Quoted 9 lines
Quoted 5 lines
MfG, JBG
--
Jan-Benedict Glaw [email protected] . +49-172-7608481 "Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | GegenKrieg
Quoted 1 line
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im
Irak!
Quoted 1 line
ret = do_actions((curr |
FREE_SPEECH) & ~(IRAQ_WAR_2 | DRM |TCPA));
Quoted 11 lines
--
Mat Harrison
Technical Developer
3d Computer Systems Ltd.
[email protected]-
To unsubscribe from this list: send the line "unsubscribelinux-c-programming" in
Quoted 2 lines
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.htmlSignature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]
Signature · 4 lines
Mat Harrison Technical Developer 3d Computer Systems Ltd. [email protected]