summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2011-06-06 09:02:01 -0700
committerBen Pfaff <blp@nicira.com>2011-06-06 09:02:01 -0700
commit25d4983554f6cf8ce5e169f7c5bb5a08c981d27e (patch)
treeff6fc1082d1ea60a022c0b564d7d4666c14f44b6
parent44b4d050d4a2c966ed6f4aef2e523f70b6463648 (diff)
downloadopenvswitch-25d4983554f6cf8ce5e169f7c5bb5a08c981d27e.tar.gz
ovsdb: Add functions for formatting column sets and data in columns sets.
These will be used for formatting error messages in an upcoming commit.
-rw-r--r--ovsdb/column.c22
-rw-r--r--ovsdb/column.h1
-rw-r--r--ovsdb/row.c20
-rw-r--r--ovsdb/row.h5
4 files changed, 45 insertions, 3 deletions
diff --git a/ovsdb/column.c b/ovsdb/column.c
index be346e4d6..576e46387 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 d90c12adb..429818aa1 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 ba00bb9f3..dece90fd9 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 6c249a184..ba0f9fbee 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 *,