]>
Commit | Line | Data |
---|---|---|
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" | |
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; | |
8f46c9bb | 35 | bool timestamp; |
3a3eb9da BP |
36 | }; |
37 | ||
38 | void table_init(struct table *); | |
39 | void table_destroy(struct table *); | |
40 | void table_set_caption(struct table *, char *caption); | |
8f46c9bb | 41 | void table_set_timestamp(struct table *, bool timestamp); |
3a3eb9da BP |
42 | |
43 | void table_add_column(struct table *, const char *heading, ...) | |
cab50449 | 44 | OVS_PRINTF_FORMAT(2, 3); |
3a3eb9da BP |
45 | void table_add_row(struct table *); |
46 | \f | |
47 | /* Table cells. */ | |
48 | ||
49 | struct cell { | |
50 | /* Literal text. */ | |
51 | char *text; | |
52 | ||
53 | /* JSON. */ | |
54 | struct json *json; | |
55 | const struct ovsdb_type *type; | |
56 | }; | |
57 | ||
58 | struct cell *table_add_cell(struct table *); | |
59 | \f | |
60 | /* Table formatting. */ | |
61 | ||
62 | enum table_format { | |
63 | TF_TABLE, /* 2-d table. */ | |
c6a41252 | 64 | TF_LIST, /* One cell per line, one row per paragraph. */ |
3a3eb9da BP |
65 | TF_HTML, /* HTML table. */ |
66 | TF_CSV, /* Comma-separated lines. */ | |
67 | TF_JSON /* JSON. */ | |
68 | }; | |
69 | ||
70 | enum cell_format { | |
71 | CF_STRING, /* String format. */ | |
c6a41252 | 72 | CF_BARE, /* String format without most punctuation. */ |
3a3eb9da BP |
73 | CF_JSON /* JSON. */ |
74 | }; | |
75 | ||
76 | struct table_style { | |
77 | enum table_format format; /* TF_*. */ | |
78 | enum cell_format cell_format; /* CF_*. */ | |
79 | bool headings; /* Include headings? */ | |
80 | int json_flags; /* CF_JSON: Flags for json_to_string(). */ | |
81 | }; | |
82 | ||
83 | #define TABLE_STYLE_DEFAULT { TF_TABLE, CF_STRING, true, JSSF_SORT } | |
84 | ||
85 | #define TABLE_OPTION_ENUMS \ | |
86 | OPT_NO_HEADINGS, \ | |
c6a41252 BP |
87 | OPT_PRETTY, \ |
88 | OPT_BARE | |
3a3eb9da | 89 | |
e3c17733 BP |
90 | #define TABLE_LONG_OPTIONS \ |
91 | {"format", required_argument, NULL, 'f'}, \ | |
92 | {"data", required_argument, NULL, 'd'}, \ | |
93 | {"no-headings", no_argument, NULL, OPT_NO_HEADINGS}, \ | |
94 | {"pretty", no_argument, NULL, OPT_PRETTY}, \ | |
95 | {"bare", no_argument, NULL, OPT_BARE} | |
3a3eb9da BP |
96 | |
97 | #define TABLE_OPTION_HANDLERS(STYLE) \ | |
98 | case 'f': \ | |
99 | table_parse_format(STYLE, optarg); \ | |
100 | break; \ | |
101 | \ | |
102 | case 'd': \ | |
103 | table_parse_cell_format(STYLE, optarg); \ | |
104 | break; \ | |
105 | \ | |
106 | case OPT_NO_HEADINGS: \ | |
107 | (STYLE)->headings = false; \ | |
108 | break; \ | |
109 | \ | |
110 | case OPT_PRETTY: \ | |
111 | (STYLE)->json_flags |= JSSF_PRETTY; \ | |
c6a41252 BP |
112 | break; \ |
113 | \ | |
114 | case OPT_BARE: \ | |
115 | (STYLE)->format = TF_LIST; \ | |
116 | (STYLE)->cell_format = CF_BARE; \ | |
117 | (STYLE)->headings = false; \ | |
3a3eb9da BP |
118 | break; |
119 | ||
120 | void table_parse_format(struct table_style *, const char *format); | |
121 | void table_parse_cell_format(struct table_style *, const char *format); | |
122 | ||
123 | void table_print(const struct table *, const struct table_style *); | |
124 | ||
125 | #endif /* table.h */ |