Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: support different separators for parameter parsing #117

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/re_fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ typedef void (fmt_param_h)(const struct pl *name, const struct pl *val,
void *arg);

bool fmt_param_exists(const struct pl *pl, const char *pname);
bool fmt_param_sep_get(const struct pl *pl, const char *pname, char sep,
struct pl *val);
bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val);
void fmt_param_apply(const struct pl *pl, fmt_param_h *ph, void *arg);

Expand Down
25 changes: 21 additions & 4 deletions src/fmt/prm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ bool fmt_param_exists(const struct pl *pl, const char *pname)


/**
* Fetch a semicolon separated parameter from a PL string
* Fetch parameter from a PL string. The separator can be specified
*
* @param pl PL string to search
* @param pname Parameter name
* @param sep Separator
* @param val Parameter value, set on return
*
* @return true if found, false if not found
*/
bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val)
bool fmt_param_sep_get(const struct pl *pl, const char *pname, char sep,
struct pl *val)
{
struct pl semi;
char expr[128];
Expand All @@ -55,8 +57,8 @@ bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val)
return false;

(void)re_snprintf(expr, sizeof(expr),
"[;]*[ \t\r\n]*%s[ \t\r\n]*=[ \t\r\n]*[~ \t\r\n;]+",
pname);
"[%c]*[ \t\r\n]*%s[ \t\r\n]*=[ \t\r\n]*[~ \t\r\n%c]+",
sep, pname, sep);

if (re_regex(pl->p, pl->l, expr, &semi, NULL, NULL, NULL, val))
return false;
Expand All @@ -65,6 +67,21 @@ bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val)
}


/**
* Fetch a semicolon separated parameter from a PL string
*
* @param pl PL string to search
* @param pname Parameter name
* @param val Parameter value, set on return
*
* @return true if found, false if not found
*/
bool fmt_param_get(const struct pl *pl, const char *pname, struct pl *val)
{
return fmt_param_sep_get(pl, pname, ';', val);
}


/**
* Apply a function handler for each semicolon separated parameter
*
Expand Down