]>
Commit | Line | Data |
---|---|---|
1960e736 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | #pragma once | |
3 | ||
4 | #include <stdbool.h> | |
5 | #include <stdio.h> | |
6 | #include <sys/types.h> | |
7 | ||
31ac2357 | 8 | #include "json.h" |
1960e736 LP |
9 | #include "macro.h" |
10 | ||
11 | typedef enum TableDataType { | |
12 | TABLE_EMPTY, | |
13 | TABLE_STRING, | |
14 | TABLE_BOOLEAN, | |
15 | TABLE_TIMESTAMP, | |
c5bbb2b5 YW |
16 | TABLE_TIMESTAMP_UTC, |
17 | TABLE_TIMESTAMP_RELATIVE, | |
1960e736 | 18 | TABLE_TIMESPAN, |
ba99f19c | 19 | TABLE_TIMESPAN_MSEC, |
1960e736 | 20 | TABLE_SIZE, |
9ff27e64 | 21 | TABLE_BPS, |
d5538326 YW |
22 | TABLE_INT, |
23 | TABLE_INT32, | |
24 | TABLE_INT64, | |
25 | TABLE_UINT, | |
1960e736 | 26 | TABLE_UINT32, |
a4661181 LP |
27 | TABLE_UINT64, |
28 | TABLE_PERCENT, | |
8390d391 | 29 | TABLE_IFINDEX, |
a7a257cd YW |
30 | TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */ |
31 | TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */ | |
1960e736 | 32 | _TABLE_DATA_TYPE_MAX, |
6268974f LP |
33 | |
34 | /* The following are not really data types, but commands for table_add_cell_many() to make changes to | |
35 | * a cell just added. */ | |
36 | TABLE_SET_MINIMUM_WIDTH, | |
37 | TABLE_SET_MAXIMUM_WIDTH, | |
38 | TABLE_SET_WEIGHT, | |
39 | TABLE_SET_ALIGN_PERCENT, | |
40 | TABLE_SET_ELLIPSIZE_PERCENT, | |
41 | TABLE_SET_COLOR, | |
42 | TABLE_SET_URL, | |
43 | TABLE_SET_UPPERCASE, | |
44 | ||
1960e736 LP |
45 | _TABLE_DATA_TYPE_INVALID = -1, |
46 | } TableDataType; | |
47 | ||
48 | typedef struct Table Table; | |
49 | typedef struct TableCell TableCell; | |
50 | ||
51 | Table *table_new_internal(const char *first_header, ...) _sentinel_; | |
52 | #define table_new(...) table_new_internal(__VA_ARGS__, NULL) | |
53 | Table *table_new_raw(size_t n_columns); | |
54 | Table *table_unref(Table *t); | |
55 | ||
56 | DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref); | |
57 | ||
58 | int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType type, const void *data, size_t minimum_width, size_t maximum_width, unsigned weight, unsigned align_percent, unsigned ellipsize_percent); | |
59 | static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) { | |
60 | return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1); | |
61 | } | |
5896e03c | 62 | int table_add_cell_stringf(Table *t, TableCell **ret_cell, const char *format, ...) _printf_(3, 4); |
1960e736 LP |
63 | |
64 | int table_dup_cell(Table *t, TableCell *cell); | |
65 | ||
66 | int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width); | |
67 | int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width); | |
68 | int table_set_weight(Table *t, TableCell *cell, unsigned weight); | |
69 | int table_set_align_percent(Table *t, TableCell *cell, unsigned percent); | |
70 | int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent); | |
71 | int table_set_color(Table *t, TableCell *cell, const char *color); | |
dc0e9c9b | 72 | int table_set_url(Table *t, TableCell *cell, const char *url); |
359abf6d | 73 | int table_set_uppercase(Table *t, TableCell *cell, bool b); |
1960e736 | 74 | |
27e730e6 LP |
75 | int table_update(Table *t, TableCell *cell, TableDataType type, const void *data); |
76 | ||
1960e736 LP |
77 | int table_add_many_internal(Table *t, TableDataType first_type, ...); |
78 | #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX) | |
79 | ||
80 | void table_set_header(Table *table, bool b); | |
81 | void table_set_width(Table *t, size_t width); | |
82 | int table_set_display(Table *t, size_t first_column, ...); | |
83 | int table_set_sort(Table *t, size_t first_column, ...); | |
a2c73e2d | 84 | int table_set_reverse(Table *t, size_t column, bool b); |
1960e736 LP |
85 | |
86 | int table_print(Table *t, FILE *f); | |
87 | int table_format(Table *t, char **ret); | |
88 | ||
89 | static inline TableCell* TABLE_HEADER_CELL(size_t i) { | |
90 | return SIZE_TO_PTR(i + 1); | |
91 | } | |
92 | ||
93 | size_t table_get_rows(Table *t); | |
94 | size_t table_get_columns(Table *t); | |
9314ead7 LP |
95 | |
96 | TableCell *table_get_cell(Table *t, size_t row, size_t column); | |
62d99b39 LP |
97 | |
98 | const void *table_get(Table *t, TableCell *cell); | |
99 | const void *table_get_at(Table *t, size_t row, size_t column); | |
31ac2357 LP |
100 | |
101 | int table_to_json(Table *t, JsonVariant **ret); | |
a1c7a1f0 | 102 | int table_print_json(Table *t, FILE *f, JsonFormatFlags json_flags); |