diff mbox

[ovs-dev,v2] sset: New function sset_from_delimited_string().

Message ID 1466829039-20877-1-git-send-email-blp@ovn.org
State Accepted
Headers show

Commit Message

Ben Pfaff June 25, 2016, 4:30 a.m. UTC
This simplifies code in a couple of places.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
v1->v2: Fix compile errors.

 lib/sset.c                | 21 ++++++++++++++++++++-
 lib/sset.h                |  3 +++
 ovn/utilities/ovn-sbctl.c | 10 ++--------
 ovsdb/replication.c       | 12 +-----------
 4 files changed, 26 insertions(+), 20 deletions(-)

Comments

Ryan Moats July 6, 2016, 2:32 a.m. UTC | #1
"dev" <dev-bounces@openvswitch.org> wrote on 06/24/2016 11:30:39 PM:

> From: Ben Pfaff <blp@ovn.org>
> To: dev@openvswitch.org
> Cc: Ben Pfaff <blp@ovn.org>
> Date: 06/24/2016 11:31 PM
> Subject: [ovs-dev] [PATCH v2] sset: New function
sset_from_delimited_string().
> Sent by: "dev" <dev-bounces@openvswitch.org>
>
> This simplifies code in a couple of places.
>
> Signed-off-by: Ben Pfaff <blp@ovn.org>
> ---

Looks straightforward enough (and it does build)

Acked-By: Ryan Moats <rmoats@us.ibm.com>
Ben Pfaff July 13, 2016, 8:43 p.m. UTC | #2
On Tue, Jul 05, 2016 at 09:32:31PM -0500, Ryan Moats wrote:
> "dev" <dev-bounces@openvswitch.org> wrote on 06/24/2016 11:30:39 PM:
> 
> > From: Ben Pfaff <blp@ovn.org>
> > To: dev@openvswitch.org
> > Cc: Ben Pfaff <blp@ovn.org>
> > Date: 06/24/2016 11:31 PM
> > Subject: [ovs-dev] [PATCH v2] sset: New function
> sset_from_delimited_string().
> > Sent by: "dev" <dev-bounces@openvswitch.org>
> >
> > This simplifies code in a couple of places.
> >
> > Signed-off-by: Ben Pfaff <blp@ovn.org>
> > ---
> 
> Looks straightforward enough (and it does build)
> 
> Acked-By: Ryan Moats <rmoats@us.ibm.com>

Thanks, applied to master.
diff mbox

Patch

diff --git a/lib/sset.c b/lib/sset.c
index 4fd3fae..be29cc7 100644
--- a/lib/sset.c
+++ b/lib/sset.c
@@ -1,5 +1,5 @@ 
 /*
- * Copyright (c) 2011, 2012, 2013, 2015 Nicira, Inc.
+ * Copyright (c) 2011, 2012, 2013, 2015, 2016 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -99,6 +99,25 @@  sset_moved(struct sset *set)
     hmap_moved(&set->map);
 }
 
+/* Initializes 'set' with substrings of 's' that are delimited by any of the
+ * characters in 'delimiters'.  For example,
+ *     sset_from_delimited_string(&set, "a b,c", " ,");
+ * initializes 'set' with three strings "a", "b", and "c". */
+void
+sset_from_delimited_string(struct sset *set, const char *s_,
+                           const char *delimiters)
+{
+    sset_init(set);
+
+    char *s = xstrdup(s_);
+    char *token, *save_ptr = NULL;
+    for (token = strtok_r(s, delimiters, &save_ptr); token != NULL;
+         token = strtok_r(NULL, delimiters, &save_ptr)) {
+        sset_add(set, token);
+    }
+    free(s);
+}
+
 /* Returns true if 'set' contains no strings, false if it contains at least one
  * string. */
 bool
diff --git a/lib/sset.h b/lib/sset.h
index 9c2f703..c3b5e97 100644
--- a/lib/sset.h
+++ b/lib/sset.h
@@ -43,6 +43,9 @@  void sset_clone(struct sset *, const struct sset *);
 void sset_swap(struct sset *, struct sset *);
 void sset_moved(struct sset *);
 
+void sset_from_delimited_string(struct sset *, const char *s,
+                                const char *delimiters);
+
 /* Count. */
 bool sset_is_empty(const struct sset *);
 size_t sset_count(const struct sset *);
diff --git a/ovn/utilities/ovn-sbctl.c b/ovn/utilities/ovn-sbctl.c
index c0ee518..40e1797 100644
--- a/ovn/utilities/ovn-sbctl.c
+++ b/ovn/utilities/ovn-sbctl.c
@@ -548,14 +548,8 @@  cmd_chassis_add(struct ctl_context *ctx)
     check_conflicts(sbctl_ctx, ch_name,
                     xasprintf("cannot create a chassis named %s", ch_name));
 
-    char *tokstr = xstrdup(encap_types);
-    char *token, *save_ptr = NULL;
-    struct sset encap_set = SSET_INITIALIZER(&encap_set);
-    for (token = strtok_r(tokstr, ",", &save_ptr); token != NULL;
-         token = strtok_r(NULL, ",", &save_ptr)) {
-        sset_add(&encap_set, token);
-    }
-    free(tokstr);
+    struct sset encap_set;
+    sset_from_delimited_string(&encap_set, encap_types, ",");
 
     size_t n_encaps = sset_count(&encap_set);
     struct sbrec_encap **encaps = xmalloc(n_encaps * sizeof *encaps);
diff --git a/ovsdb/replication.c b/ovsdb/replication.c
index 1fa0f25..aa6b9e2 100644
--- a/ovsdb/replication.c
+++ b/ovsdb/replication.c
@@ -121,19 +121,9 @@  set_remote_ovsdb_server(const char *remote_server)
 void
 set_tables_blacklist(const char *blacklist)
 {
-    char *save_ptr = NULL;
-    char *blacklist_item;
-
     replication_init();
-
     if (blacklist) {
-        char *t_blacklist = xstrdup(blacklist);
-        for (blacklist_item = strtok_r(t_blacklist, ",", &save_ptr);
-             blacklist_item != NULL;
-             blacklist_item = strtok_r(NULL, ",", &save_ptr)) {
-            sset_add(&tables_blacklist, blacklist_item);
-        }
-        free(t_blacklist);
+        sset_from_delimited_string(&tables_blacklist, blacklist, ",");
     }
 }