Skip to content

Commit

Permalink
config: add methods to set values taken from the command line.
Browse files Browse the repository at this point in the history
This patch adds functions that will set and lock a certain value.  The
intended use of these methods is to give command line options priority
over the configuration file.

Signed-off-by: Richard Cochran <[email protected]>
  • Loading branch information
richardcochran committed Aug 23, 2015
1 parent bde8ef4 commit 28fbc39
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
50 changes: 50 additions & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1039,3 +1039,53 @@ int config_get_int(struct config *cfg, const char *section, const char *option)
pr_debug("config item %s.%s is %d", section, option, ci->val.i);
return ci->val.i;
}

int config_set_double(struct config *cfg, const char *option, double val)
{
struct config_item *ci = config_find_item(cfg, NULL, option);

if (!ci || ci->type != CFG_TYPE_DOUBLE) {
pr_err("bug: config option %s missing or invalid!", option);
return -1;
}
ci->flags |= CFG_ITEM_LOCKED;
ci->val.d = val;
pr_debug("locked item global.%s as %f", option, ci->val.d);
return 0;
}

int config_set_section_int(struct config *cfg, const char *section,
const char *option, int val)
{
struct config_item *cgi, *dst;

cgi = config_find_item(cfg, NULL, option);
if (!cgi) {
pr_err("bug: config option %s missing!", option);
return -1;
}
switch (cgi->type) {
case CFG_TYPE_DOUBLE:
pr_err("bug: config option %s type mismatch!", option);
return -1;
case CFG_TYPE_INT:
break;
}
if (!section) {
cgi->flags |= CFG_ITEM_LOCKED;
cgi->val.i = val;
pr_debug("locked item global.%s as %d", option, cgi->val.i);
return 0;
}
/* Create or update this port specific item. */
dst = config_section_item(cfg, section, option);
if (!dst) {
dst = config_item_alloc(cfg, section, option, cgi->type);
if (!dst) {
return -1;
}
}
dst->val.i = val;
pr_debug("section item %s.%s now %d", section, option, dst->val.i);
return 0;
}
11 changes: 11 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,15 @@ double config_get_double(struct config *cfg, const char *section,
int config_get_int(struct config *cfg, const char *section,
const char *option);

int config_set_double(struct config *cfg, const char *option, double val);

int config_set_section_int(struct config *cfg, const char *section,
const char *option, int val);

static inline int config_set_int(struct config *cfg,
const char *option, int val)
{
return config_set_section_int(cfg, NULL, option, val);
}

#endif

0 comments on commit 28fbc39

Please sign in to comment.