Browse Source

= parse_config()

master
Alex 'AdUser' Z 9 years ago
parent
commit
6519d14962
  1. 18
      src/config.c

18
src/config.c

@ -23,20 +23,24 @@ int parse_config(oal_config_t * const config, const char *file) {
while (fgets(buf, bufsize, f)) { while (fgets(buf, bufsize, f)) {
linenum++; linenum++;
/* find start of key */
key = buf; key = buf;
while (isspace(*key)) while (isspace(*key))
key++; key++;
if (*key == '#') if (*key == '#')
continue; /* ignore comments */ continue; /* ignore comments */
if (strlen(key) == 0)
continue; /* ignore empty lines */
if (!isalpha(*key)) { if (!isalpha(*key)) {
snprintf(err, bufsize, "can't parse line %d", linenum); snprintf(err, bufsize, "can't parse line %d", linenum);
config->error = strdup(err); config->error = strdup(err);
return 1; return 1;
} }
/* find start of value */
value = key; value = key;
while(*value && !isspace(*value)) while(*value && !isspace(*value))
value++; value++;
if (!isspace(value)) { if (!isspace(*value)) {
snprintf(err, bufsize, "can't find value at line %d", linenum); snprintf(err, bufsize, "can't find value at line %d", linenum);
config->error = strndup(err, bufsize); config->error = strndup(err, bufsize);
return 1; return 1;
@ -44,8 +48,18 @@ int parse_config(oal_config_t * const config, const char *file) {
*value = '\0', value += 1; *value = '\0', value += 1;
while (isspace(*value)) while (isspace(*value))
value++; value++;
if (!*value) {
snprintf(err, bufsize, "can't find value at line %d", linenum);
config->error = strndup(err, bufsize);
return 1;
}
/* strip trailing spaces and newline */
valsize = strnlen(value, bufsize - (value - buf)); valsize = strnlen(value, bufsize - (value - buf));
while (valsize && isspace(value[valsize - 1])) {
value[valsize - 1] = '\0';
valsize--;
}
/* check & copy valid keys */
if (strncmp(key, "bindurls", 6) == 0) { if (strncmp(key, "bindurls", 6) == 0) {
config->bindurls = strndup(value, valsize); config->bindurls = strndup(value, valsize);
} else } else

Loading…
Cancel
Save