[ovs-dev] [indexes 4/6] ovsdb: Add functions for formatting column sets and data in columns sets.

Ben Pfaff blp at nicira.com
Thu Jun 2 23:19:22 UTC 2011


These will be used for formatting error messages in an upcoming commit.
---
 ovsdb/column.c |   22 ++++++++++++++++++++++
 ovsdb/column.h |    1 +
 ovsdb/row.c    |   20 +++++++++++++++++++-
 ovsdb/row.h    |    5 +++--
 4 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/ovsdb/column.c b/ovsdb/column.c
index be346e4..751c81f 100644
--- a/ovsdb/column.c
+++ b/ovsdb/column.c
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 
 #include "column.h"
+#include "dynamic-string.h"
 #include "json.h"
 #include "ovsdb-error.h"
 #include "ovsdb-parser.h"
@@ -203,6 +204,27 @@ ovsdb_column_set_to_json(const struct ovsdb_column_set *set)
     return json;
 }
 
+/* Returns an English string listing the contents of 'set', e.g. "columns
+ * \"a\", \"b\", and \"c\"."  The caller must free the string. */
+char *
+ovsdb_column_set_to_string(const struct ovsdb_column_set *set)
+{
+    if (!set->n_columns) {
+        return xstrdup("no columns");
+    } else {
+        struct ds s;
+        size_t i;
+
+        ds_init(&s);
+        ds_put_format(&s, "column%s ", set->n_columns > 1 ? "s" : "");
+        for (i = 0; i < set->n_columns; i++) {
+            const char *delimiter = english_list_delimiter(i, set->n_columns);
+            ds_put_format(&s, "%s\"%s\"", delimiter, set->columns[i]->name);
+        }
+        return ds_steal_cstr(&s);
+    }
+}
+
 void
 ovsdb_column_set_add(struct ovsdb_column_set *set,
                      const struct ovsdb_column *column)
diff --git a/ovsdb/column.h b/ovsdb/column.h
index d90c12a..429818a 100644
--- a/ovsdb/column.h
+++ b/ovsdb/column.h
@@ -73,6 +73,7 @@ struct ovsdb_error *ovsdb_column_set_from_json(
     const struct json *, const struct ovsdb_table_schema *,
     struct ovsdb_column_set *);
 struct json *ovsdb_column_set_to_json(const struct ovsdb_column_set *);
+char *ovsdb_column_set_to_string(const struct ovsdb_column_set *);
 
 void ovsdb_column_set_add(struct ovsdb_column_set *,
                           const struct ovsdb_column *);
diff --git a/ovsdb/row.c b/ovsdb/row.c
index ba00bb9..dece90f 100644
--- a/ovsdb/row.c
+++ b/ovsdb/row.c
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010 Nicira Networks
+/* Copyright (c) 2009, 2010, 2011 Nicira Networks
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
 #include <assert.h>
 #include <stddef.h>
 
+#include "dynamic-string.h"
 #include "json.h"
 #include "ovsdb-error.h"
 #include "shash.h"
@@ -173,6 +174,23 @@ ovsdb_row_update_columns(struct ovsdb_row *dst,
     }
 }
 
+/* Appends the string form of the value in 'row' of each of the columns in
+ * 'columns' to 'out', e.g. "1, \"xyz\", and [1, 2, 3]". */
+void
+ovsdb_row_columns_to_string(const struct ovsdb_row *row,
+                            const struct ovsdb_column_set *columns,
+                            struct ds *out)
+{
+    size_t i;
+
+    for (i = 0; i < columns->n_columns; i++) {
+        const struct ovsdb_column *column = columns->columns[i];
+
+        ds_put_cstr(out, english_list_delimiter(i, columns->n_columns));
+        ovsdb_datum_to_string(&row->fields[column->index], &column->type, out);
+    }
+}
+
 struct ovsdb_error *
 ovsdb_row_from_json(struct ovsdb_row *row, const struct json *json,
                     struct ovsdb_symbol_table *symtab,
diff --git a/ovsdb/row.h b/ovsdb/row.h
index 6c249a1..ba0f9fb 100644
--- a/ovsdb/row.h
+++ b/ovsdb/row.h
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010 Nicira Networks
+/* Copyright (c) 2009, 2010, 2011 Nicira Networks
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -76,7 +76,8 @@ int ovsdb_row_compare_columns_3way(const struct ovsdb_row *,
                                    const struct ovsdb_column_set *);
 void ovsdb_row_update_columns(struct ovsdb_row *, const struct ovsdb_row *,
                               const struct ovsdb_column_set *);
-
+void ovsdb_row_columns_to_string(const struct ovsdb_row *,
+                                 const struct ovsdb_column_set *, struct ds *);
 struct ovsdb_error *ovsdb_row_from_json(struct ovsdb_row *,
                                         const struct json *,
                                         struct ovsdb_symbol_table *,
-- 
1.7.4.4




More information about the dev mailing list