Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / lib / table.h
1 /*
2  * Copyright (c) 2009, 2010, 2011 Nicira Networks.
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
24 struct table_style;
25 \f
26 /* Manipulating tables and their rows and columns. */
27
28 struct table {
29     struct cell *cells;
30     struct column *columns;
31     size_t n_columns, allocated_columns;
32     size_t n_rows, allocated_rows;
33     size_t current_column;
34     char *caption;
35 };
36
37 void table_init(struct table *);
38 void table_destroy(struct table *);
39 void table_set_caption(struct table *, char *caption);
40
41 void table_add_column(struct table *, const char *heading, ...)
42     PRINTF_FORMAT(2, 3);
43 void table_add_row(struct table *);
44 \f
45 /* Table cells. */
46
47 struct cell {
48     /* Literal text. */
49     char *text;
50
51     /* JSON. */
52     struct json *json;
53     const struct ovsdb_type *type;
54 };
55
56 struct cell *table_add_cell(struct table *);
57 \f
58 /* Table formatting. */
59
60 enum table_format {
61     TF_TABLE,                   /* 2-d table. */
62     TF_LIST,                    /* One cell per line, one row per paragraph. */
63     TF_HTML,                    /* HTML table. */
64     TF_CSV,                     /* Comma-separated lines. */
65     TF_JSON                     /* JSON. */
66 };
67
68 enum cell_format {
69     CF_STRING,                  /* String format. */
70     CF_BARE,                    /* String format without most punctuation. */
71     CF_JSON                     /* JSON. */
72 };
73
74 struct table_style {
75     enum table_format format;   /* TF_*. */
76     enum cell_format cell_format; /* CF_*. */
77     bool headings;              /* Include headings? */
78     int json_flags;             /* CF_JSON: Flags for json_to_string(). */
79 };
80
81 #define TABLE_STYLE_DEFAULT { TF_TABLE, CF_STRING, true, JSSF_SORT }
82
83 #define TABLE_OPTION_ENUMS                      \
84     OPT_NO_HEADINGS,                            \
85     OPT_PRETTY,                                 \
86     OPT_BARE
87
88 #define TABLE_LONG_OPTIONS                                  \
89         {"format", required_argument, 0, 'f'},              \
90         {"data", required_argument, 0, 'd'},                \
91         {"no-headings", no_argument, 0, OPT_NO_HEADINGS},   \
92         {"pretty", no_argument, 0, OPT_PRETTY},             \
93         {"bare", no_argument, 0, OPT_BARE}
94
95 #define TABLE_OPTION_HANDLERS(STYLE)                \
96         case 'f':                                   \
97             table_parse_format(STYLE, optarg);      \
98             break;                                  \
99                                                     \
100         case 'd':                                   \
101             table_parse_cell_format(STYLE, optarg); \
102             break;                                  \
103                                                     \
104         case OPT_NO_HEADINGS:                       \
105             (STYLE)->headings = false;              \
106             break;                                  \
107                                                     \
108         case OPT_PRETTY:                            \
109             (STYLE)->json_flags |= JSSF_PRETTY;     \
110             break;                                  \
111                                                     \
112         case OPT_BARE:                              \
113             (STYLE)->format = TF_LIST;              \
114             (STYLE)->cell_format = CF_BARE;         \
115             (STYLE)->headings = false;              \
116             break;
117
118 void table_parse_format(struct table_style *, const char *format);
119 void table_parse_cell_format(struct table_style *, const char *format);
120
121 void table_print(const struct table *, const struct table_style *);
122
123 #endif /* table.h */