File: table.c

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 (353 lines) | stat: -rw-r--r-- 7,179 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * table.c : Common APIs for printing tabular format output.
 *
 * Copyright (c) 2025 Nilay Shroff, IBM
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "table.h"

static int table_get_value_width(struct value *v)
{
	char buf[64];
	int len = 0;

	switch (v->type) {
	case FMT_STRING:
		len = strlen((const char *)v->s);
		break;
	case FMT_INT:
		len = snprintf(buf, sizeof(buf), "%d", v->i);
		break;
	default:
		printf("Invalid print format!\n");
		break;
	}
	return len;
}

static void table_print_centered(struct value *val, int width, enum fmt_type type)
{
	int i, len, left_pad, right_pad;
	char buf[64];

	switch (type) {
	case FMT_STRING:
		len = strlen(val->s);
		break;
	case FMT_INT:
		len = snprintf(buf, sizeof(buf), "%d", val->i);
		break;
	case FMT_UNSIGNED:
		len = snprintf(buf, sizeof(buf), "%u", val->u);
		break;
	case FMT_LONG:
		len = snprintf(buf, sizeof(buf), "%ld", val->ld);
		break;
	case FMT_UNSIGNED_LONG:
		len = snprintf(buf, sizeof(buf), "%lu", val->lu);
		break;
	default:
		fprintf(stderr, "Invalid format!\n");
		return;
	}

	left_pad = (width - len) / 2;
	right_pad = width - len - left_pad;

	/* add left padding */
	for (i = 0; i < left_pad; i++)
		putchar(' ');

	/* print value */
	switch (type) {
	case FMT_STRING:
		printf("%s", val->s);
		break;
	case FMT_INT:
		printf("%d", val->i);
		break;
	case FMT_UNSIGNED:
		printf("%u", val->u);
		break;
	case FMT_LONG:
		printf("%ld", val->ld);
		break;
	case FMT_UNSIGNED_LONG:
		printf("%lu", val->lu);
		break;
	default:
		break;
	}

	/* add right padding */
	for (i = 0; i < right_pad; i++)
		putchar(' ');
}

static void table_print_columns(const struct table *t)
{
	int col, j, width;
	struct table_column *c;
	struct value v;

	for (col = 0; col < t->num_columns; col++) {
		c = &t->columns[col];
		width = c->width;
		switch (c->align) {
		case CENTERED:
			v.s = c->name;
			v.align = c->align;
			table_print_centered(&v, width, FMT_STRING);
			break;
		case LEFT:
			width *= -1;
			fallthrough;
		default:
			printf("%*s", width, c->name);
			break;
		}
		if (col + 1 != t->num_columns)
			putchar(' ');
	}

	printf("\n");

	for (col = 0; col < t->num_columns; col++) {
		for (j = 0; j < t->columns[col].width; j++)
			putchar('-');
		if (col + 1 != t->num_columns)
			putchar(' ');
	}

	printf("\n");
}

static void table_print_rows(const struct table *t)
{
	int row, col;
	struct table_column *c;
	struct table_row *r;
	int width;
	struct value *v;

	for (row = 0; row < t->num_rows; row++) {
		for (col = 0; col < t->num_columns; col++) {
			c = &t->columns[col];
			r = &t->rows[row];
			v = &r->val[col];

			width = c->width;
			switch (v->align) {
			case CENTERED:
				table_print_centered(v, width, v->type);
				break;
			case LEFT:
				width *= -1;
				fallthrough;
			default:
				switch (v->type) {
				case FMT_STRING:
					printf("%*s", width, v->s);
					break;
				case FMT_INT:
					printf("%*d", width, v->i);
					break;
				case FMT_UNSIGNED:
					printf("%*u", width, v->u);
					break;
				case FMT_LONG:
					printf("%*ld", width, v->ld);
					break;
				case FMT_UNSIGNED_LONG:
					printf("%*lu", width, v->lu);
					break;
				default:
					fprintf(stderr, "Invalid format!\n");
					break;
				}
				break;
			}
			if (col + 1 != t->num_columns)
				putchar(' ');
		}
		printf("\n");
	}
}

void table_print(struct table *t)
{
	/* first print columns */
	table_print_columns(t);

	/* next print rows */
	table_print_rows(t);
}

int table_get_row_id(struct table *t)
{
	struct table_row *new_rows;
	int row = t->num_rows;

	new_rows = reallocarray(t->rows, (row + 1), sizeof(struct table_row));
	if (!new_rows)
		return -ENOMEM;

	t->rows = new_rows;
	t->rows[row].val = calloc(t->num_columns, sizeof(struct value));
	if (!t->rows->val)
		return -ENOMEM;

	t->num_rows++;
	return row;
}

void table_add_row(struct table *t, int row_id)
{
	int col, max_width, width;
	struct table_row *row = &t->rows[row_id];

	/* Adjust the column width based on the row value. */
	for (col = 0; col < t->num_columns; col++) {
		max_width = t->columns[col].width;
		width = table_get_value_width(&row->val[col]);
		if (width > max_width)
			t->columns[col].width = width;
	}
}

struct table *table_create(void)
{
	return calloc(1, sizeof(struct table));
}

struct table *table_init_with_columns(struct table_column *c, int num_columns)
{
	struct table *t = table_create();

	if (!t)
		return NULL;

	if (table_add_columns(t, c, num_columns)) {
		table_free(t);
		return NULL;
	}

	return t;
}

static int table_add_column(struct table *t, struct table_column *c)
{
	struct table_column *new_columns;
	int col = t->num_columns;

	new_columns = reallocarray(t->columns, t->num_columns + 1,
			sizeof(struct table_column));
	if (!new_columns)
		return -ENOMEM;

	t->columns = new_columns;
	t->columns[col].name = strdup(c->name);
	if (!t->columns[col].name)
		return -ENOMEM;
	t->columns[col].align = c->align;
	t->columns[col].width = strlen(c->name);
	t->num_columns++;

	return 0;
}

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 col;

	if (!filter)
		return table_add_columns(t, c, num_columns);

	for (col = 0; col < num_columns; col++) {
		if (!filter(c[col].name, arg))
			continue;	/* skip this column */

		if (table_add_column(t, &c[col]))
			goto out;
	}
	return 0;
out:
	return -ENOMEM;
}

int table_add_columns(struct table *t, struct table_column *c, int num_columns)
{
	int col;

	t->columns = calloc(num_columns, sizeof(struct table_column));
	if (!t->columns)
		return -ENOMEM;

	for (col = 0; col < num_columns; col++) {
		t->columns[col].name = strdup(c[col].name);
		if (!t->columns[col].name)
			goto free_col;

		t->columns[col].align = c[col].align;
		if (c[col].width > strlen(t->columns[col].name))
			t->columns[col].width = c[col].width;
		else
			t->columns[col].width = strlen(t->columns[col].name);
	}
	t->num_columns = num_columns;

	return 0;
free_col:
	while (--col >= 0)
		free(t->columns[col].name);
	free(t->columns);
	t->columns = NULL;
	return -ENOMEM;
}

void table_free(struct table *t)
{
	int row, col;
	struct table_row *r;
	struct value *v;

	/* free rows */
	for (row = 0; row < t->num_rows; row++) {
		r = &t->rows[row];
		for (col = 0; col < t->num_columns; col++) {
			v = &r->val[col];

			if (v->type == FMT_STRING)
				free(v->s);
		}
		free(r->val);
	}
	free(t->rows);

	/* free columns */
	for (col = 0; col < t->num_columns; col++)
		free(t->columns[col].name);
	free(t->columns);

	/* free table */
	free(t);
}