File: string-table.c

package info (click to toggle)
btrfs-progs 6.2-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 17,244 kB
  • sloc: ansic: 114,376; sh: 9,576; python: 1,242; makefile: 820
file content (194 lines) | stat: -rw-r--r-- 4,480 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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License v2 as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 021110-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include "common/messages.h"
#include "common/string-table.h"
#include "common/internal.h"

/*
 * Create an array of char* which will point to table cell strings
 */
struct string_table *table_create(unsigned int columns, unsigned int rows)
{
	struct string_table *tab;
	size_t size;

	size = sizeof(struct string_table) + rows * columns * sizeof(char*);
	tab = calloc(1, size);

	if (!tab)
		return NULL;

	tab->ncols = columns;
	tab->nrows = rows;
	tab->spacing = STRING_TABLE_SPACING_1;

	return tab;
}

/*
 * This is like a vprintf, but stores the results in a cell of the table.
 */
__attribute__ ((format (printf, 4, 0)))
char *table_vprintf(struct string_table *tab, unsigned int column, unsigned int row,
			  const char *fmt, va_list ap)
{
	unsigned int idx = tab->ncols * row + column;
	char *msg = calloc(100, 1);

	if (!msg)
		return NULL;

	if (column >= tab->ncols || row >= tab->nrows) {
		error("attempt to write outside of table: col %u row %u fmt %s",
			column, row, fmt);
		return NULL;
	}

	if (tab->cells[idx])
		free(tab->cells[idx]);
	tab->cells[idx] = msg;
	vsnprintf(msg, 99, fmt, ap);

	return msg;
}

/*
 * This is like a printf, but stores the results in a cell of the table.
 */
__attribute__ ((format (printf, 4, 5)))
char *table_printf(struct string_table *tab, unsigned int column, unsigned int row,
		   const char *fmt, ...)
{
	va_list ap;
	char *ret;

	va_start(ap, fmt);
	ret = table_vprintf(tab, column, row, fmt, ap);
	va_end(ap);

	return ret;
}

/*
 * Print the table to stdout, interpret the alignment and expand specifiers.
 * @from:    row from which to start
 * @to:      upper row limit (not inclusive), 0 for the whole table
 *
 * Formatting:
 * <TEXT - the TEXT is left aligned
 * >TEXT - the TEXT is right aligned
 * =     - the cell text will be filled by ===== (column width)
 * *C    - the cell text will be filled by character C (column width)
 */
void table_dump_range(struct string_table *tab, unsigned int from, unsigned int to)
{
	unsigned int sizes[tab->ncols];
	unsigned int i, j;
	unsigned int prescan;

	if (to == 0)
		to = tab->nrows;

	if (from > to) {
		error("invalid range for table dump %u > %u", from, to);
		return;
	}

	prescan = max_t(unsigned int, 100, to);
	prescan = min_t(unsigned int, tab->nrows, prescan);

	for (i = 0; i < tab->ncols; i++) {
		sizes[i] = 0;
		for (j = 0; j < prescan; j++) {
			int idx = i + j * tab->ncols;
			int len;

			if (!tab->cells[idx])
				continue;

			len = strlen(tab->cells[idx]) - 1;
			if (len == 0 || tab->cells[idx][0] == '*')
				continue;

			if (len > sizes[i])
				sizes[i] = len;
		}
	}

	for (j = from; j < to; j++) {
		for (i = 0; i < tab->ncols; i++) {
			int idx = i + j * tab->ncols;
			char *cell = tab->cells[idx];

			if (!cell || !strlen(cell)) {
				printf("%*s", sizes[i], "");
			} else if (cell && cell[0] == '*' && cell[1]) {
				int k = sizes[i];

				while (k--)
					putchar(cell[1]);
			} else {
				printf("%*s",
					cell[0] == '<' ? -sizes[i] : sizes[i],
					cell + 1);
			}
			if (i != (tab->ncols - 1)) {
				putchar(' ');
				if (tab->spacing == STRING_TABLE_SPACING_2)
					putchar(' ');
			}
		}
		putchar('\n');
	}
}

/*
 *  Deallocate a table and all of its content
 */
void table_free(struct string_table *tab)
{
	unsigned int i, count;

	count = tab->ncols * tab->nrows;

	for (i = 0; i < count; i++)
		if (tab->cells[i])
			free(tab->cells[i]);

	free(tab);
}

void table_clear_range(struct string_table *tab, unsigned int from, unsigned int to)
{
	unsigned int row, col;

	if (to == 0)
		to = tab->nrows;

	for (row = from; row < to; row++) {
		char **rowstart = &tab->cells[row * tab->ncols];

		for (col = 0; col < tab->ncols; col++) {
			free(rowstart[col]);
			rowstart[col] = NULL;
		}
	}
}