[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [PATCH v2 1/3] tools/xl: allow split_string_into_pair() to trim values
Most use cases of split_string_into_pair() are requiring the returned strings to be white space trimmed. In order to avoid the same code pattern multiple times, add a predicate parameter to split_string_into_pair() which can be specified to call trim() with that predicate for the string pair returned. Specifying NULL for the predicate will avoid the call of trim(). Signed-off-by: Juergen Gross <jgross@xxxxxxxx> Reviewed-by: Jason Andryuk <jandryuk@xxxxxxxxx> Acked-by: Anthony PERARD <anthony.perard@xxxxxxxxxx> --- tools/xl/xl_parse.c | 42 +++++++++++++++++++----------------------- tools/xl/xl_parse.h | 4 ++-- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index e344d4fda3..0e1b6907fa 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -646,7 +646,7 @@ static void parse_vnuma_config(const XLU_Config *config, conf_count++) { if (xlu_cfg_value_type(conf_option) == XLU_STRING) { - char *buf, *option_untrimmed, *value_untrimmed; + char *buf; char *option, *value; unsigned long val; @@ -654,15 +654,12 @@ static void parse_vnuma_config(const XLU_Config *config, if (!buf) continue; - if (split_string_into_pair(buf, "=", - &option_untrimmed, - &value_untrimmed)) { + if (split_string_into_pair(buf, "=", &option, &value, + isspace)) { fprintf(stderr, "xl: failed to split \"%s\" into pair\n", buf); exit(EXIT_FAILURE); } - trim(isspace, option_untrimmed, &option); - trim(isspace, value_untrimmed, &value); if (!strcmp("pnode", option)) { val = parse_ulong(value); @@ -715,8 +712,6 @@ static void parse_vnuma_config(const XLU_Config *config, } free(option); free(value); - free(option_untrimmed); - free(value_untrimmed); } } } @@ -838,7 +833,7 @@ int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token) rc = split_string_into_pair(connectors[i], ":", &vdispl->connectors[i].unique_id, - &resolution); + &resolution, NULL); rc= sscanf(resolution, "%ux%u", &vdispl->connectors[i].width, &vdispl->connectors[i].height); @@ -2361,18 +2356,15 @@ void parse_config_data(const char *config_source, split_string_into_string_list(buf, ",", &pairs); len = libxl_string_list_length(&pairs); for (i = 0; i < len; i++) { - char *key, *key_untrimmed, *value, *value_untrimmed; + char *key, *value; int rc; - rc = split_string_into_pair(pairs[i], "=", - &key_untrimmed, - &value_untrimmed); + rc = split_string_into_pair(pairs[i], "=", &key, &value, + isspace); if (rc != 0) { fprintf(stderr, "failed to parse channel configuration: %s", pairs[i]); exit(1); } - trim(isspace, key_untrimmed, &key); - trim(isspace, value_untrimmed, &value); if (!strcmp(key, "backend")) { replace_string(&chn->backend_domname, value); @@ -2395,9 +2387,7 @@ void parse_config_data(const char *config_source, " ignoring\n", key); } free(key); - free(key_untrimmed); free(value); - free(value_untrimmed); } switch (chn->connection) { case LIBXL_CHANNEL_CONNECTION_UNKNOWN: @@ -3021,10 +3011,8 @@ void trim(char_predicate_t predicate, const char *input, char **output) *output = result; } -int split_string_into_pair(const char *str, - const char *delim, - char **a, - char **b) +int split_string_into_pair(const char *str, const char *delim, + char **a, char **b, char_predicate_t predicate) { char *s, *p, *saveptr, *aa = NULL, *bb = NULL; int rc = 0; @@ -3036,13 +3024,21 @@ int split_string_into_pair(const char *str, rc = ERROR_INVAL; goto out; } - aa = xstrdup(p); + if (predicate) { + trim(predicate, p, &aa); + } else { + aa = xstrdup(p); + } p = strtok_r(NULL, delim, &saveptr); if (p == NULL) { rc = ERROR_INVAL; goto out; } - bb = xstrdup(p); + if (predicate) { + trim(predicate, p, &bb); + } else { + bb = xstrdup(p); + } *a = aa; aa = NULL; diff --git a/tools/xl/xl_parse.h b/tools/xl/xl_parse.h index bab2861f8c..ab35c68545 100644 --- a/tools/xl/xl_parse.h +++ b/tools/xl/xl_parse.h @@ -45,14 +45,14 @@ int match_option_size(const char *prefix, size_t len, void split_string_into_string_list(const char *str, const char *delim, libxl_string_list *psl); -int split_string_into_pair(const char *str, const char *delim, - char **a, char **b); void replace_string(char **str, const char *val); /* NB: this follows the interface used by <ctype.h>. See 'man 3 ctype' and look for CTYPE in libxl_internal.h */ typedef int (*char_predicate_t)(const int c); void trim(char_predicate_t predicate, const char *input, char **output); +int split_string_into_pair(const char *str, const char *delim, + char **a, char **b, char_predicate_t predicate); const char *get_action_on_shutdown_name(libxl_action_on_shutdown a); -- 2.35.3
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |