ovsdb-client: Break table formatting into new library.
[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_HTML,                    /* HTML table. */
63     TF_CSV,                     /* Comma-separated lines. */
64     TF_JSON                     /* JSON. */
65 };
66
67 enum cell_format {
68     CF_STRING,                  /* String format. */
69     CF_JSON                     /* JSON. */
70 };
71
72 struct table_style {
73     enum table_format format;   /* TF_*. */
74     enum cell_format cell_format; /* CF_*. */
75     bool headings;              /* Include headings? */
76     int json_flags;             /* CF_JSON: Flags for json_to_string(). */
77 };
78
79 #define TABLE_STYLE_DEFAULT { TF_TABLE, CF_STRING, true, JSSF_SORT }
80
81 #define TABLE_OPTION_ENUMS                      \
82     OPT_NO_HEADINGS,                            \
83     OPT_PRETTY
84
85 #define TABLE_LONG_OPTIONS                                  \
86         {"format", required_argument, 0, 'f'},              \
87         {"data", required_argument, 0, 'd'},                \
88         {"no-headings", no_argument, 0, OPT_NO_HEADINGS},   \
89         {"pretty", no_argument, 0, OPT_PRETTY},
90
91 #define TABLE_OPTION_HANDLERS(STYLE)                \
92         case 'f':                                   \
93             table_parse_format(STYLE, optarg);      \
94             break;                                  \
95                                                     \
96         case 'd':                                   \
97             table_parse_cell_format(STYLE, optarg); \
98             break;                                  \
99                                                     \
100         case OPT_NO_HEADINGS:                       \
101             (STYLE)->headings = false;              \
102             break;                                  \
103                                                     \
104         case OPT_PRETTY:                            \
105             (STYLE)->json_flags |= JSSF_PRETTY;     \
106             break;
107
108 void table_parse_format(struct table_style *, const char *format);
109 void table_parse_cell_format(struct table_style *, const char *format);
110
111 void table_print(const struct table *, const struct table_style *);
112
113 #endif /* table.h */