From: Ben Pfaff Date: Mon, 6 Jun 2011 16:02:01 +0000 (-0700) Subject: ovsdb: Add functions for formatting column sets and data in columns sets. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=25d4983554f6cf8ce5e169f7c5bb5a08c981d27e;p=openvswitch ovsdb: Add functions for formatting column sets and data in columns sets. These will be used for formatting error messages in an upcoming commit. --- diff --git a/ovsdb/column.c b/ovsdb/column.c index be346e4d..576e4638 100644 --- a/ovsdb/column.c +++ b/ovsdb/column.c @@ -20,6 +20,7 @@ #include #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 d90c12ad..429818aa 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 ba00bb9f..dece90fd 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 #include +#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 6c249a18..ba0f9fbe 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 *,