]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/format-table.h
table: add TABLE_UINT8 or friends
[thirdparty/systemd.git] / src / shared / format-table.h
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
8 #include "json.h"
9 #include "macro.h"
10
11 typedef enum TableDataType {
12 TABLE_EMPTY,
13 TABLE_STRING,
14 TABLE_BOOLEAN,
15 TABLE_TIMESTAMP,
16 TABLE_TIMESTAMP_UTC,
17 TABLE_TIMESTAMP_RELATIVE,
18 TABLE_TIMESPAN,
19 TABLE_TIMESPAN_MSEC,
20 TABLE_SIZE,
21 TABLE_BPS,
22 TABLE_INT,
23 TABLE_INT8,
24 TABLE_INT16,
25 TABLE_INT32,
26 TABLE_INT64,
27 TABLE_UINT,
28 TABLE_UINT8,
29 TABLE_UINT16,
30 TABLE_UINT32,
31 TABLE_UINT64,
32 TABLE_PERCENT,
33 TABLE_IFINDEX,
34 TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */
35 TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
36 _TABLE_DATA_TYPE_MAX,
37
38 /* The following are not really data types, but commands for table_add_cell_many() to make changes to
39 * a cell just added. */
40 TABLE_SET_MINIMUM_WIDTH,
41 TABLE_SET_MAXIMUM_WIDTH,
42 TABLE_SET_WEIGHT,
43 TABLE_SET_ALIGN_PERCENT,
44 TABLE_SET_ELLIPSIZE_PERCENT,
45 TABLE_SET_COLOR,
46 TABLE_SET_URL,
47 TABLE_SET_UPPERCASE,
48
49 _TABLE_DATA_TYPE_INVALID = -1,
50 } TableDataType;
51
52 typedef struct Table Table;
53 typedef struct TableCell TableCell;
54
55 Table *table_new_internal(const char *first_header, ...) _sentinel_;
56 #define table_new(...) table_new_internal(__VA_ARGS__, NULL)
57 Table *table_new_raw(size_t n_columns);
58 Table *table_unref(Table *t);
59
60 DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
61
62 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);
63 static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
64 return table_add_cell_full(t, ret_cell, type, data, (size_t) -1, (size_t) -1, (unsigned) -1, (unsigned) -1, (unsigned) -1);
65 }
66 int table_add_cell_stringf(Table *t, TableCell **ret_cell, const char *format, ...) _printf_(3, 4);
67
68 int table_dup_cell(Table *t, TableCell *cell);
69
70 int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
71 int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
72 int table_set_weight(Table *t, TableCell *cell, unsigned weight);
73 int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
74 int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
75 int table_set_color(Table *t, TableCell *cell, const char *color);
76 int table_set_url(Table *t, TableCell *cell, const char *url);
77 int table_set_uppercase(Table *t, TableCell *cell, bool b);
78
79 int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
80
81 int table_add_many_internal(Table *t, TableDataType first_type, ...);
82 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
83
84 void table_set_header(Table *table, bool b);
85 void table_set_width(Table *t, size_t width);
86 int table_set_display(Table *t, size_t first_column, ...);
87 int table_set_sort(Table *t, size_t first_column, ...);
88 int table_set_reverse(Table *t, size_t column, bool b);
89
90 int table_print(Table *t, FILE *f);
91 int table_format(Table *t, char **ret);
92
93 static inline TableCell* TABLE_HEADER_CELL(size_t i) {
94 return SIZE_TO_PTR(i + 1);
95 }
96
97 size_t table_get_rows(Table *t);
98 size_t table_get_columns(Table *t);
99
100 TableCell *table_get_cell(Table *t, size_t row, size_t column);
101
102 const void *table_get(Table *t, TableCell *cell);
103 const void *table_get_at(Table *t, size_t row, size_t column);
104
105 int table_to_json(Table *t, JsonVariant **ret);
106 int table_print_json(Table *t, FILE *f, JsonFormatFlags json_flags);