A
Aaron Walker
At the beginning of my program, I open a config file and load the contents
into a structure (please disregard the non-portable sockaddr_in struct as it is
irrelevant to the problem):
struct conf_data {
char *config_file;
char *root; /* SERVER_ROOT */
char *pid_file; /* PID_FILE */
char *log_dir; /* LOG_DIR */
struct sockaddr_in local_addr;
unsigned int port; /* PORT */
unsigned short child_min; /* CHILD_MIN */
unsigned short child_max; /* CHILD_MAX */
};
typedef struct conf_data Config;
My config file is in the format of:
KEY1 value1
KEY2 value2
....
Once I have the key and value (per line) I do:
if(!strcmp(key, "SERVER_ROOT")) {
conf->root = ec_malloc(strlen(value) + 1);
strcpy(conf->root, value);
}
else if(!strcmp(key, "PID_FILE")) {
conf->pid_file = ec_malloc(strlen(value) + 1);
strcpy(conf->pid_file, value);
}
....
is there a better way to do this other than a whole sleu of if...else
if's?
I thought about doing:
char *possible_values[] = {
"SERVER_ROOT",
"PID_FILE",
...
NULL
};
for(i = 0; possible_values != NULL; i++) {
if(!strcmp(key, possible_values)) {
but this is where I am uncertain of what to do, because how do I know
which Config structure variable to strcpy value to (for example
SERVER_ROOT needs to reside in conf->root, PID_FILE in conf->pid_file,
etc)?
Sorry for the long post.
Thanks,
Aaron
into a structure (please disregard the non-portable sockaddr_in struct as it is
irrelevant to the problem):
struct conf_data {
char *config_file;
char *root; /* SERVER_ROOT */
char *pid_file; /* PID_FILE */
char *log_dir; /* LOG_DIR */
struct sockaddr_in local_addr;
unsigned int port; /* PORT */
unsigned short child_min; /* CHILD_MIN */
unsigned short child_max; /* CHILD_MAX */
};
typedef struct conf_data Config;
My config file is in the format of:
KEY1 value1
KEY2 value2
....
Once I have the key and value (per line) I do:
if(!strcmp(key, "SERVER_ROOT")) {
conf->root = ec_malloc(strlen(value) + 1);
strcpy(conf->root, value);
}
else if(!strcmp(key, "PID_FILE")) {
conf->pid_file = ec_malloc(strlen(value) + 1);
strcpy(conf->pid_file, value);
}
....
is there a better way to do this other than a whole sleu of if...else
if's?
I thought about doing:
char *possible_values[] = {
"SERVER_ROOT",
"PID_FILE",
...
NULL
};
for(i = 0; possible_values != NULL; i++) {
if(!strcmp(key, possible_values)) {
but this is where I am uncertain of what to do, because how do I know
which Config structure variable to strcpy value to (for example
SERVER_ROOT needs to reside in conf->root, PID_FILE in conf->pid_file,
etc)?
Sorry for the long post.
Thanks,
Aaron