]> git.proxmox.com Git - mirror_ovs.git/blame - lib/table.h
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / table.h
CommitLineData
3a3eb9da 1/*
e0edde6f 2 * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
3a3eb9da
BP
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"
db3f700b 23#include "openvswitch/json.h"
3a3eb9da 24
cb139fa8 25struct ds;
3a3eb9da
BP
26struct table_style;
27\f
28/* Manipulating tables and their rows and columns. */
29
30struct 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;
8f46c9bb 37 bool timestamp;
3a3eb9da
BP
38};
39
40void table_init(struct table *);
41void table_destroy(struct table *);
42void table_set_caption(struct table *, char *caption);
8f46c9bb 43void table_set_timestamp(struct table *, bool timestamp);
3a3eb9da
BP
44
45void table_add_column(struct table *, const char *heading, ...)
cab50449 46 OVS_PRINTF_FORMAT(2, 3);
3a3eb9da
BP
47void table_add_row(struct table *);
48\f
49/* Table cells. */
50
51struct cell {
52 /* Literal text. */
53 char *text;
54
55 /* JSON. */
56 struct json *json;
57 const struct ovsdb_type *type;
58};
59
60struct cell *table_add_cell(struct table *);
61\f
62/* Table formatting. */
63
64enum table_format {
65 TF_TABLE, /* 2-d table. */
c6a41252 66 TF_LIST, /* One cell per line, one row per paragraph. */
3a3eb9da
BP
67 TF_HTML, /* HTML table. */
68 TF_CSV, /* Comma-separated lines. */
69 TF_JSON /* JSON. */
70};
71
72enum cell_format {
73 CF_STRING, /* String format. */
c6a41252 74 CF_BARE, /* String format without most punctuation. */
3a3eb9da
BP
75 CF_JSON /* JSON. */
76};
77
78struct 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(). */
80f66ee0 83 int max_column_width; /* CF_STRING: Limit for column width. */
3a3eb9da
BP
84};
85
80f66ee0 86#define TABLE_STYLE_DEFAULT { TF_LIST, CF_STRING, true, JSSF_SORT, 0 }
db3f700b 87static const struct table_style table_style_default = TABLE_STYLE_DEFAULT;
3a3eb9da
BP
88
89#define TABLE_OPTION_ENUMS \
90 OPT_NO_HEADINGS, \
c6a41252 91 OPT_PRETTY, \
80f66ee0
BP
92 OPT_BARE, \
93 OPT_MAX_COLUMN_WIDTH
3a3eb9da 94
e3c17733
BP
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}, \
80f66ee0
BP
100 {"bare", no_argument, NULL, OPT_BARE}, \
101 {"max-column-width", required_argument, NULL, OPT_MAX_COLUMN_WIDTH}
3a3eb9da
BP
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; \
c6a41252
BP
118 break; \
119 \
120 case OPT_BARE: \
121 (STYLE)->format = TF_LIST; \
122 (STYLE)->cell_format = CF_BARE; \
123 (STYLE)->headings = false; \
80f66ee0
BP
124 break; \
125 \
126 case OPT_MAX_COLUMN_WIDTH: \
127 (STYLE)->max_column_width = atoi(optarg); \
3a3eb9da
BP
128 break;
129
130void table_parse_format(struct table_style *, const char *format);
131void table_parse_cell_format(struct table_style *, const char *format);
132
133void table_print(const struct table *, const struct table_style *);
cb139fa8
BP
134void table_format(const struct table *, const struct table_style *,
135 struct ds *);
d9cf9b2d 136void table_format_reset(void);
bcb58ce0 137void table_usage(void);
3a3eb9da
BP
138
139#endif /* table.h */