File: table.h

package info (click to toggle)
nvme-cli 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,664 kB
  • sloc: ansic: 80,727; sh: 2,257; python: 975; makefile: 70; ruby: 25
file content (161 lines) | stat: -rw-r--r-- 3,127 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* SPDX-License-Identifier: GPL-2.0-or-later */
#ifndef _TABLE_H_
#define _TABLE_H_

#include <stdbool.h>

enum fmt_type {
	FMT_STRING,
	FMT_INT,
	FMT_UNSIGNED,
	FMT_LONG,
	FMT_UNSIGNED_LONG,
};

enum alignment {
	RIGHT,
	LEFT,
	CENTERED
};

struct value {
	union {
		char *s;
		int i;
		unsigned int u;
		long ld;
		unsigned long lu;
	};
	enum alignment align;
	enum fmt_type type;
};

struct table_row {
	struct value *val;
};

struct table_column {
	char *name;
	enum alignment align;
	int width;		/* auto populated */
};

struct table {
	struct table_column *columns;
	int num_columns;
	struct table_row *rows;
	int num_rows;
};

static inline int table_set_value_str(struct table *t, int col, int row,
		const char *str, enum alignment align)
{
	struct table_row *r;
	struct value *v;
	char *s;

	if (col >= t->num_columns || row >= t->num_rows)
		return -EINVAL;

	s = strdup(str);
	if (!s)
		return -ENOMEM;

	r = &t->rows[row];
	v = &r->val[col];
	v->s = s;
	v->align = align;
	v->type = FMT_STRING;

	return 0;
}

static inline int table_set_value_int(struct table *t, int col, int row,
		int i, enum alignment align)
{
	struct table_row *r;
	struct value *v;

	if (col >= t->num_columns || row >= t->num_rows)
		return -EINVAL;

	r = &t->rows[row];
	v = &r->val[col];
	v->i = i;
	v->align = align;
	v->type = FMT_INT;

	return 0;
}

static inline int table_set_value_unsigned(struct table *t, int col, int row,
		int u, enum alignment align)
{
	struct table_row *r;
	struct value *v;

	if (col >= t->num_columns || row >= t->num_rows)
		return -EINVAL;

	r = &t->rows[row];
	v = &r->val[col];
	v->u = u;
	v->align = align;
	v->type = FMT_UNSIGNED;

	return 0;
}

static inline int table_set_value_long(struct table *t, int col, int row,
		long ld, enum alignment align)
{
	struct table_row *r;
	struct value *v;

	if (col >= t->num_columns || row >= t->num_rows)
		return -EINVAL;

	r = &t->rows[row];
	v = &r->val[col];
	v->ld = ld;
	v->align = align;
	v->type = FMT_LONG;

	return 0;
}

static inline void table_set_value_unsigned_long(struct table *t, int col,
		int row, long lu, enum alignment align)
{
	struct table_row *r = &t->rows[row];
	struct value *v = &r->val[col];

	v->lu = lu;
	v->align = align;
	v->type = FMT_UNSIGNED_LONG;
}

struct table *table_create(void);
int table_add_columns(struct table *t, struct table_column *c, int num_columns);
int table_add_columns_filter(struct table *t, struct table_column *c,
			int num_columns,
			bool (*filter)(const char *name, void *arg),
			void *arg);
int table_get_row_id(struct table *t);
void table_add_row(struct table *t, int row);
void table_print(struct table *t);
void table_free(struct table *t);

/**
 * table_init_with_columns() - Allocate a table instance with column definitions
 * @c:		Column definitions
 * @num_columns:Number of columns
 *
 * This is a function combined table_create() and table_add_columns().
 *
 * Return: The table instance, or NULL if unsuccessful. If allocated, the caller
 * is responsible to free the table.
 */
struct table *table_init_with_columns(struct table_column *c, int num_columns);

#endif /* _TABLE_H_ */