X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=lib%2Ftable.c;h=f24ddc62e5cde843334c9dd117e37000157b78b1;hb=6e797088c0ff76843137c3ce0642b319b6404b7c;hp=d150b2924f5a5ff721796c6cd12c673bd45acec5;hpb=3a3eb9daef012e58d6604a6fc726a5115b25945f;p=openvswitch diff --git a/lib/table.c b/lib/table.c index d150b292..f24ddc62 100644 --- a/lib/table.c +++ b/lib/table.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011 Nicira Networks. + * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,8 @@ #include "dynamic-string.h" #include "json.h" #include "ovsdb-data.h" +#include "ovsdb-error.h" +#include "timeval.h" #include "util.h" struct column { @@ -36,7 +38,7 @@ cell_to_text(struct cell *cell, const struct table_style *style) if (cell->json) { if (style->cell_format == CF_JSON || !cell->type) { cell->text = json_to_string(cell->json, JSSF_SORT); - } else if (style->cell_format == CF_STRING) { + } else { struct ovsdb_datum datum; struct ovsdb_error *error; struct ds s; @@ -45,14 +47,17 @@ cell_to_text(struct cell *cell, const struct table_style *style) NULL); if (!error) { ds_init(&s); - ovsdb_datum_to_string(&datum, cell->type, &s); + if (style->cell_format == CF_STRING) { + ovsdb_datum_to_string(&datum, cell->type, &s); + } else { + ovsdb_datum_to_bare(&datum, cell->type, &s); + } ovsdb_datum_destroy(&datum, cell->type); cell->text = ds_steal_cstr(&s); } else { cell->text = json_to_string(cell->json, JSSF_SORT); + ovsdb_error_destroy(error); } - } else { - NOT_REACHED(); } } else { cell->text = xstrdup(""); @@ -119,6 +124,14 @@ table_set_caption(struct table *table, char *caption) table->caption = caption; } +/* Turns printing a timestamp along with 'table' on or off, according to + * 'timestamp'. */ +void +table_set_timestamp(struct table *table, bool timestamp) +{ + table->timestamp = timestamp; +} + /* Adds a new column to 'table' just to the right of any existing column, with * 'heading' as a title for the column. 'heading' must be a valid printf() * format specifier. @@ -207,6 +220,24 @@ table_print_table_line__(struct ds *line) ds_clear(line); } +static void +table_format_timestamp__(char *s, size_t size) +{ + time_t now = time_wall(); + strftime(s, size, "%Y-%m-%d %H:%M:%S", gmtime(&now)); +} + +static void +table_print_timestamp__(const struct table *table) +{ + if (table->timestamp) { + char s[32]; + + table_format_timestamp__(s, sizeof s); + puts(s); + } +} + static void table_print_table__(const struct table *table, const struct table_style *style) { @@ -219,6 +250,8 @@ table_print_table__(const struct table *table, const struct table_style *style) putchar('\n'); } + table_print_timestamp__(table); + if (table->caption) { puts(table->caption); } @@ -272,6 +305,36 @@ table_print_table__(const struct table *table, const struct table_style *style) free(widths); } +static void +table_print_list__(const struct table *table, const struct table_style *style) +{ + static int n = 0; + size_t x, y; + + if (n++ > 0) { + putchar('\n'); + } + + table_print_timestamp__(table); + + if (table->caption) { + puts(table->caption); + } + + for (y = 0; y < table->n_rows; y++) { + if (y > 0) { + putchar('\n'); + } + for (x = 0; x < table->n_columns; x++) { + const char *text = cell_to_text(table_cell__(table, y, x), style); + if (style->headings) { + printf("%-20s: ", table->columns[x].heading); + } + puts(text); + } + } +} + static void table_escape_html_text__(const char *s, size_t n) { @@ -325,6 +388,8 @@ table_print_html__(const struct table *table, const struct table_style *style) { size_t x, y; + table_print_timestamp__(table); + fputs("