Skip to content

Commit

Permalink
ovsdb: Add functions for formatting column sets and data in columns s…
Browse files Browse the repository at this point in the history
…ets.

These will be used for formatting error messages in an upcoming commit.
  • Loading branch information
blp committed Jun 6, 2011
1 parent 44b4d05 commit 25d4983
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
22 changes: 22 additions & 0 deletions ovsdb/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions ovsdb/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand Down
20 changes: 19 additions & 1 deletion ovsdb/row.c
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,6 +20,7 @@
#include <assert.h>
#include <stddef.h>

#include "dynamic-string.h"
#include "json.h"
#include "ovsdb-error.h"
#include "shash.h"
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions ovsdb/row.h
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 *,
Expand Down

0 comments on commit 25d4983

Please sign in to comment.