Rework the configuration file interface.

This change will make it easier to extend the configuration file contents
to include arbitrary new data structures.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2012-03-20 20:09:40 +01:00
parent 32d461d4ed
commit da69203517
3 changed files with 17 additions and 5 deletions

View File

@ -20,13 +20,16 @@
#include <string.h> #include <string.h>
#include "config.h" #include "config.h"
static void scan_line(char *s, struct defaultDS *dds, struct port_defaults *pod) static void scan_line(char *s, struct config *cfg)
{ {
int val; int val;
Integer8 i8; Integer8 i8;
UInteger16 u16; UInteger16 u16;
UInteger8 u8; UInteger8 u8;
struct defaultDS *dds = cfg->dds;
struct port_defaults *pod = cfg->pod;
if (1 == sscanf(s, " twoStepFlag %d", &val)) { if (1 == sscanf(s, " twoStepFlag %d", &val)) {
if (val) /* TODO - implement one step */ if (val) /* TODO - implement one step */
@ -79,7 +82,7 @@ static void scan_line(char *s, struct defaultDS *dds, struct port_defaults *pod)
} }
} }
int config_read(char *name, struct defaultDS *dds, struct port_defaults *pod) int config_read(char *name, struct config *cfg)
{ {
FILE *fp; FILE *fp;
char line[1024]; char line[1024];
@ -92,7 +95,7 @@ int config_read(char *name, struct defaultDS *dds, struct port_defaults *pod)
} }
while (fgets(line, sizeof(line), fp)) { while (fgets(line, sizeof(line), fp)) {
scan_line(line, dds, pod); scan_line(line, cfg);
} }
fclose(fp); fclose(fp);

View File

@ -22,6 +22,11 @@
#include "ds.h" #include "ds.h"
int config_read(char *name, struct defaultDS *dds, struct port_defaults *pod); struct config {
struct defaultDS *dds;
struct port_defaults *pod;
};
int config_read(char *name, struct config *cfg);
#endif #endif

View File

@ -32,6 +32,7 @@
static int running = 1; static int running = 1;
static struct defaultDS ds; static struct defaultDS ds;
static struct port_defaults pod; static struct port_defaults pod;
static struct config cfg_settings;
static int generate_clock_identity(struct ClockIdentity *ci, char *name) static int generate_clock_identity(struct ClockIdentity *ci, char *name)
{ {
@ -175,7 +176,10 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
if (config && config_read(config, &ds, &pod)) { cfg_settings.dds = &ds;
cfg_settings.pod = &pod;
if (config && config_read(config, &cfg_settings)) {
fprintf(stderr, "failed to read configuration file\n"); fprintf(stderr, "failed to read configuration file\n");
return -1; return -1;
} }