]> git.proxmox.com Git - mirror_ovs.git/blob - lib/table.h
ovsdb-idl: Fix iteration over tracked rows with no actual data.
[mirror_ovs.git] / lib / table.h
1 /*
2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef TABLE_H
18 #define TABLE_H 1
19
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include "compiler.h"
23 #include "openvswitch/json.h"
24
25 struct ds;
26 struct table_style;
27 \f
28 /* Manipulating tables and their rows and columns. */
29
30 struct table {
31 struct cell *cells;
32 struct column *columns;
33 size_t n_columns, allocated_columns;
34 size_t n_rows, allocated_rows;
35 size_t current_column;
36 char *caption;
37 bool timestamp;
38 };
39
40 void table_init(struct table *);
41 void table_destroy(struct table *);
42 void table_set_caption(struct table *, char *caption);
43 void table_set_timestamp(struct table *, bool timestamp);
44
45 void table_add_column(struct table *, const char *heading, ...)
46 OVS_PRINTF_FORMAT(2, 3);
47 void table_add_row(struct table *);
48 \f
49 /* Table cells. */
50
51 struct cell {
52 /* Literal text. */
53 char *text;
54
55 /* JSON. */
56 struct json *json;
57 const struct ovsdb_type *type;
58 };
59
60 struct cell *table_add_cell(struct table *);
61 \f
62 /* Table formatting. */
63
64 enum table_format {
65 TF_TABLE, /* 2-d table. */
66 TF_LIST, /* One cell per line, one row per paragraph. */
67 TF_HTML, /* HTML table. */
68 TF_CSV, /* Comma-separated lines. */
69 TF_JSON /* JSON. */
70 };
71
72 enum cell_format {
73 CF_STRING, /* String format. */
74 CF_BARE, /* String format without most punctuation. */
75 CF_JSON /* JSON. */
76 };
77
78 struct table_style {
79 enum table_format format; /* TF_*. */
80 enum cell_format cell_format; /* CF_*. */
81 bool headings; /* Include headings? */
82 int json_flags; /* CF_JSON: Flags for json_to_string(). */
83 int max_column_width; /* CF_STRING: Limit for column width. */
84 };
85
86 #define TABLE_STYLE_DEFAULT { TF_LIST, CF_STRING, true, JSSF_SORT, 0 }
87 static const struct table_style table_style_default = TABLE_STYLE_DEFAULT;
88
89 #define TABLE_OPTION_ENUMS \
90 OPT_NO_HEADINGS, \
91 OPT_PRETTY, \
92 OPT_BARE, \
93 OPT_MAX_COLUMN_WIDTH
94
95 #define TABLE_LONG_OPTIONS \
96 {"format", required_argument, NULL, 'f'}, \
97 {"data", required_argument, NULL, 'd'}, \
98 {"no-headings", no_argument, NULL, OPT_NO_HEADINGS}, \
99 {"pretty", no_argument, NULL, OPT_PRETTY}, \
100 {"bare", no_argument, NULL, OPT_BARE}, \
101 {"max-column-width", required_argument, NULL, OPT_MAX_COLUMN_WIDTH}
102
103 #define TABLE_OPTION_HANDLERS(STYLE) \
104 case 'f': \
105 table_parse_format(STYLE, optarg); \
106 break; \
107 \
108 case 'd': \
109 table_parse_cell_format(STYLE, optarg); \
110 break; \
111 \
112 case OPT_NO_HEADINGS: \
113 (STYLE)->headings = false; \
114 break; \
115 \
116 case OPT_PRETTY: \
117 (STYLE)->json_flags |= JSSF_PRETTY; \
118 break; \
119 \
120 case OPT_BARE: \
121 (STYLE)->format = TF_LIST; \
122 (STYLE)->cell_format = CF_BARE; \
123 (STYLE)->headings = false; \
124 break; \
125 \
126 case OPT_MAX_COLUMN_WIDTH: \
127 (STYLE)->max_column_width = atoi(optarg); \
128 break;
129
130 void table_parse_format(struct table_style *, const char *format);
131 void table_parse_cell_format(struct table_style *, const char *format);
132
133 void table_print(const struct table *, const struct table_style *);
134 void table_format(const struct table *, const struct table_style *,
135 struct ds *);
136 void table_format_reset(void);
137 void table_usage(void);
138
139 #endif /* table.h */