summaryrefslogtreecommitdiff
path: root/string-table.c
blob: d20e6a82d87590d4c0940c6e501a498cabe58d78 (plain)
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
/*
 * 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 "string-table.h"

/*
 *  This function create an array of char * which will represent a table
 */
struct string_table *table_create(int columns, int rows)
{
	struct string_table *p;
	int size;

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

	if (!p)
		return NULL;

	p->ncols = columns;
	p->nrows = rows;

	return p;
}

/*
 * This function is like a vprintf, but store the results in a cell of
 * the table.
 * If fmt  starts with '<', the text is left aligned; if fmt starts with
 * '>' the text is right aligned. If fmt is equal to '=' the text will
 * be replaced by a '=====' dimensioned on the basis of the column width
 */
char *table_vprintf(struct string_table *tab, int column, int row,
			  char *fmt, va_list ap)
{
	int idx = tab->ncols * row + column;
	char *msg = calloc(100, 1);

	if (!msg)
		return NULL;

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

	return msg;
}

/*
 * This function is like a printf, but store the results in a cell of
 * the table.
 */
char *table_printf(struct string_table *tab, int column, int row,
			  char *fmt, ...)
{
	va_list ap;
	char *ret;

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

	return ret;
}

/*
 * This function dumps the table. Every "=" string will be replaced by
 * a "=======" length as the column
 */
void table_dump(struct string_table *tab)
{
	int sizes[tab->ncols];
	int i, j;

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

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

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

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

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

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

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

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

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

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

	free(tab);
}