[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v2 2/3] tools/xl: make split_string_into_pair() more usable



Today split_string_into_pair() will not really do what its name is
suggesting: instead of splitting a string into a pair of strings using
a delimiter, it will return the first two strings of the initial string
by using the delimiter.

This is never what the callers want, so modify split_string_into_pair()
to split the string only at the first delimiter found, resulting in
something like "x=a=b" to be split into "x" and "a=b" when being called
with "=" as the delimiter. Today the returned strings would be "x" and
"a".

At the same time switch the delimiter from "const char *" (allowing
multiple delimiter characters) to "char" (a single character only), as
this makes the function more simple without breaking any use cases.

Suggested-by: Anthony PERARD <anthony.perard@xxxxxxxxxx>
Signed-off-by: Juergen Gross <jgross@xxxxxxxx>
---
V2:
- new patch
---
 tools/xl/xl_parse.c | 23 ++++++++++++-----------
 tools/xl/xl_parse.h |  4 ++--
 2 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
index 0e1b6907fa..09cabd2732 100644
--- a/tools/xl/xl_parse.c
+++ b/tools/xl/xl_parse.c
@@ -654,7 +654,7 @@ static void parse_vnuma_config(const XLU_Config *config,
 
                 if (!buf) continue;
 
-                if (split_string_into_pair(buf, "=", &option, &value,
+                if (split_string_into_pair(buf, '=', &option, &value,
                                            isspace)) {
                     fprintf(stderr, "xl: failed to split \"%s\" into pair\n",
                             buf);
@@ -831,7 +831,7 @@ int parse_vdispl_config(libxl_device_vdispl *vdispl, char 
*token)
         {
             char *resolution;
 
-            rc = split_string_into_pair(connectors[i], ":",
+            rc = split_string_into_pair(connectors[i], ':',
                                         &vdispl->connectors[i].unique_id,
                                         &resolution, NULL);
 
@@ -2358,7 +2358,7 @@ void parse_config_data(const char *config_source,
             for (i = 0; i < len; i++) {
                 char *key, *value;
                 int rc;
-                rc = split_string_into_pair(pairs[i], "=", &key, &value,
+                rc = split_string_into_pair(pairs[i], '=', &key, &value,
                                             isspace);
                 if (rc != 0) {
                     fprintf(stderr, "failed to parse channel configuration: 
%s",
@@ -3011,26 +3011,27 @@ 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, char_predicate_t predicate)
+int split_string_into_pair(const char *str, char delim, char **a, char **b,
+                           char_predicate_t predicate)
 {
-    char *s, *p, *saveptr, *aa = NULL, *bb = NULL;
+    char *s, *p, *aa = NULL, *bb = NULL;
     int rc = 0;
 
     s = xstrdup(str);
 
-    p = strtok_r(s, delim, &saveptr);
+    p = strchr(s, delim);
     if (p == NULL) {
         rc = ERROR_INVAL;
         goto out;
     }
+    *p = 0;
     if (predicate) {
-        trim(predicate, p, &aa);
+        trim(predicate, s, &aa);
     } else {
-        aa = xstrdup(p);
+        aa = xstrdup(s);
     }
-    p = strtok_r(NULL, delim, &saveptr);
-    if (p == NULL) {
+    p++;
+    if (!*p) {
         rc = ERROR_INVAL;
         goto out;
     }
diff --git a/tools/xl/xl_parse.h b/tools/xl/xl_parse.h
index ab35c68545..fe0d586cdd 100644
--- a/tools/xl/xl_parse.h
+++ b/tools/xl/xl_parse.h
@@ -51,8 +51,8 @@ void replace_string(char **str, const char *val);
    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);
+int split_string_into_pair(const char *str, 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




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.