/* * config.c * * Summary: Read config file. * * Author: David Hardy */ #include #include #include #include "config.h" #include "error.h" int config_init(Config *c) { c->f = NULL; return 0; } void config_destroy(Config *c) { c->f = NULL; } int config_open(Config *c, const char *fname) { c->f = fopen(fname, "r"); MDIO_ASSERT(c->f != NULL); return 0; } int config_close(Config *c) { int ret; ret = fclose(c->f); MDIO_ASSERT(ret == 0); return 0; } static void parse_line(register char *buf, char **keywd, char **value) { char *cp; /* get rid of any leading white space */ while (isspace((int)*buf)) buf++; /* get rid of any trailing comments, delimited by '#' */ cp = strchr(buf, '#'); if (cp != NULL) *cp = '\0'; /* save keyword, get rid of (one) '=' if present */ *keywd = buf; if (*buf == '\0') { *value = buf; return; } while (!isspace((int)*buf) && *buf != '=' && *buf != '\0') buf++; if (*buf == '\0') { *value = buf; return; } else if (*buf == '=') { *buf = '\0'; *value = buf + 1; return; } *buf = '\0'; buf++; while (isspace((int)*buf)) buf++; if (*buf == '=') buf++; *value = buf; } int config_read(Config *c, char *buf, int buflen, char **keywd, char **value) { /* find beginning keyword */ *value = NULL; *keywd = NULL; do { if (fgets(buf, buflen, c->f) == NULL) return 0; parse_line(buf, keywd, value); } while (**keywd == '\0' && **value == '\0'); /* skip blank lines */ return 1; }