2
* Copyright (c) 2000, 2002, 2004 Kungliga Tekniska Högskolan
3
* (Royal Institute of Technology, Stockholm, Sweden).
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
10
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
17
* 3. Neither the name of the Institute nor the names of its contributors
18
* may be used to endorse or promote products derived from this software
19
* without specific prior written permission.
21
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
struct column_entry *rows;
52
unsigned int column_id;
59
struct column_data **columns;
61
char *column_separator;
64
rtbl_t ROKEN_LIB_FUNCTION
67
return calloc (1, sizeof (struct rtbl_data));
70
void ROKEN_LIB_FUNCTION
71
rtbl_set_flags (rtbl_t table, unsigned int flags)
76
unsigned int ROKEN_LIB_FUNCTION
77
rtbl_get_flags (rtbl_t table)
82
static struct column_data *
83
rtbl_get_column_by_id (rtbl_t table, unsigned int id)
86
for(i = 0; i < table->num_columns; i++)
87
if(table->columns[i]->column_id == id)
88
return table->columns[i];
92
static struct column_data *
93
rtbl_get_column (rtbl_t table, const char *column)
96
for(i = 0; i < table->num_columns; i++)
97
if(strcmp(table->columns[i]->header, column) == 0)
98
return table->columns[i];
102
void ROKEN_LIB_FUNCTION
103
rtbl_destroy (rtbl_t table)
107
for (i = 0; i < table->num_columns; i++) {
108
struct column_data *c = table->columns[i];
110
for (j = 0; j < c->num_rows; j++)
111
free (c->rows[j].data);
118
free (table->column_prefix);
119
free (table->column_separator);
120
free (table->columns);
124
int ROKEN_LIB_FUNCTION
125
rtbl_add_column_by_id (rtbl_t table, unsigned int id,
126
const char *header, unsigned int flags)
128
struct column_data *col, **tmp;
130
tmp = realloc (table->columns, (table->num_columns + 1) * sizeof (*tmp));
133
table->columns = tmp;
134
col = malloc (sizeof (*col));
137
col->header = strdup (header);
138
if (col->header == NULL) {
149
table->columns[table->num_columns++] = col;
153
int ROKEN_LIB_FUNCTION
154
rtbl_add_column (rtbl_t table, const char *header, unsigned int flags)
156
return rtbl_add_column_by_id(table, 0, header, flags);
159
int ROKEN_LIB_FUNCTION
160
rtbl_new_row(rtbl_t table)
164
for (c = 0; c < table->num_columns; c++)
165
if(table->columns[c]->num_rows > max_rows)
166
max_rows = table->columns[c]->num_rows;
167
for (c = 0; c < table->num_columns; c++) {
168
struct column_entry *tmp;
170
if(table->columns[c]->num_rows == max_rows)
172
tmp = realloc(table->columns[c]->rows,
173
max_rows * sizeof(table->columns[c]->rows));
176
table->columns[c]->rows = tmp;
177
while(table->columns[c]->num_rows < max_rows) {
178
if((tmp[table->columns[c]->num_rows++].data = strdup("")) == NULL)
186
column_compute_width (rtbl_t table, struct column_data *column)
190
if(table->flags & RTBL_HEADER_STYLE_NONE)
193
column->width = strlen (column->header);
194
for (i = 0; i < column->num_rows; i++)
195
column->width = max (column->width, strlen (column->rows[i].data));
199
int ROKEN_LIB_FUNCTION
200
rtbl_set_prefix (rtbl_t table, const char *prefix)
202
if (table->column_prefix)
203
free (table->column_prefix);
204
table->column_prefix = strdup (prefix);
205
if (table->column_prefix == NULL)
210
int ROKEN_LIB_FUNCTION
211
rtbl_set_separator (rtbl_t table, const char *separator)
213
if (table->column_separator)
214
free (table->column_separator);
215
table->column_separator = strdup (separator);
216
if (table->column_separator == NULL)
221
int ROKEN_LIB_FUNCTION
222
rtbl_set_column_prefix (rtbl_t table, const char *column,
225
struct column_data *c = rtbl_get_column (table, column);
231
c->prefix = strdup (prefix);
232
if (c->prefix == NULL)
237
int ROKEN_LIB_FUNCTION
238
rtbl_set_column_affix_by_id(rtbl_t table, unsigned int id,
239
const char *prefix, const char *suffix)
241
struct column_data *c = rtbl_get_column_by_id (table, id);
250
c->prefix = strdup (prefix);
251
if (c->prefix == NULL)
260
c->suffix = strdup (suffix);
261
if (c->suffix == NULL)
269
get_column_prefix (rtbl_t table, struct column_data *c)
275
if (table->column_prefix)
276
return table->column_prefix;
281
get_column_suffix (rtbl_t table, struct column_data *c)
289
add_column_entry (struct column_data *c, const char *data)
291
struct column_entry row, *tmp;
293
row.data = strdup (data);
294
if (row.data == NULL)
296
tmp = realloc (c->rows, (c->num_rows + 1) * sizeof (*tmp));
302
c->rows[c->num_rows++] = row;
306
int ROKEN_LIB_FUNCTION
307
rtbl_add_column_entry_by_id (rtbl_t table, unsigned int id, const char *data)
309
struct column_data *c = rtbl_get_column_by_id (table, id);
314
return add_column_entry(c, data);
317
int ROKEN_LIB_FUNCTION
318
rtbl_add_column_entryv_by_id (rtbl_t table, unsigned int id,
319
const char *fmt, ...)
326
ret = vasprintf(&str, fmt, ap);
330
ret = rtbl_add_column_entry_by_id(table, id, str);
335
int ROKEN_LIB_FUNCTION
336
rtbl_add_column_entry (rtbl_t table, const char *column, const char *data)
338
struct column_data *c = rtbl_get_column (table, column);
343
return add_column_entry(c, data);
346
int ROKEN_LIB_FUNCTION
347
rtbl_add_column_entryv (rtbl_t table, const char *column, const char *fmt, ...)
354
ret = vasprintf(&str, fmt, ap);
358
ret = rtbl_add_column_entry(table, column, str);
364
int ROKEN_LIB_FUNCTION
365
rtbl_format (rtbl_t table, FILE * f)
369
for (i = 0; i < table->num_columns; i++)
370
column_compute_width (table, table->columns[i]);
371
if((table->flags & RTBL_HEADER_STYLE_NONE) == 0) {
372
for (i = 0; i < table->num_columns; i++) {
373
struct column_data *c = table->columns[i];
375
if(table->column_separator != NULL && i > 0)
376
fprintf (f, "%s", table->column_separator);
377
fprintf (f, "%s", get_column_prefix (table, c));
378
if(i == table->num_columns - 1 && c->suffix == NULL)
379
/* last column, so no need to pad with spaces */
380
fprintf (f, "%-*s", 0, c->header);
382
fprintf (f, "%-*s", (int)c->width, c->header);
383
fprintf (f, "%s", get_column_suffix (table, c));
391
/* are there any more rows left? */
392
for (i = 0; flag == 0 && i < table->num_columns; ++i) {
393
struct column_data *c = table->columns[i];
395
if (c->num_rows > j) {
403
for (i = 0; i < table->num_columns; i++) {
405
struct column_data *c = table->columns[i];
407
if(table->column_separator != NULL && i > 0)
408
fprintf (f, "%s", table->column_separator);
412
if ((c->flags & RTBL_ALIGN_RIGHT) == 0) {
413
if(i == table->num_columns - 1 && c->suffix == NULL)
414
/* last column, so no need to pad with spaces */
419
fprintf (f, "%s", get_column_prefix (table, c));
420
if (c->num_rows <= j)
421
fprintf (f, "%*s", w, "");
423
fprintf (f, "%*s", w, c->rows[j].data);
424
fprintf (f, "%s", get_column_suffix (table, c));
433
main (int argc, char **argv)
437
table = rtbl_create ();
438
rtbl_add_column_by_id (table, 0, "Issued", 0);
439
rtbl_add_column_by_id (table, 1, "Expires", 0);
440
rtbl_add_column_by_id (table, 2, "Foo", RTBL_ALIGN_RIGHT);
441
rtbl_add_column_by_id (table, 3, "Principal", 0);
443
rtbl_add_column_entry_by_id (table, 0, "Jul 7 21:19:29");
444
rtbl_add_column_entry_by_id (table, 1, "Jul 8 07:19:29");
445
rtbl_add_column_entry_by_id (table, 2, "73");
446
rtbl_add_column_entry_by_id (table, 2, "0");
447
rtbl_add_column_entry_by_id (table, 2, "-2000");
448
rtbl_add_column_entry_by_id (table, 3, "krbtgt/NADA.KTH.SE@NADA.KTH.SE");
450
rtbl_add_column_entry_by_id (table, 0, "Jul 7 21:19:29");
451
rtbl_add_column_entry_by_id (table, 1, "Jul 8 07:19:29");
452
rtbl_add_column_entry_by_id (table, 3, "afs/pdc.kth.se@NADA.KTH.SE");
454
rtbl_add_column_entry_by_id (table, 0, "Jul 7 21:19:29");
455
rtbl_add_column_entry_by_id (table, 1, "Jul 8 07:19:29");
456
rtbl_add_column_entry_by_id (table, 3, "afs@NADA.KTH.SE");
458
rtbl_set_separator (table, " ");
460
rtbl_format (table, stdout);
462
rtbl_destroy (table);
466
table = rtbl_create ();
467
rtbl_add_column_by_id (table, 0, "Column A", 0);
468
rtbl_set_column_affix_by_id (table, 0, "<", ">");
469
rtbl_add_column_by_id (table, 1, "Column B", 0);
470
rtbl_set_column_affix_by_id (table, 1, "[", "]");
471
rtbl_add_column_by_id (table, 2, "Column C", 0);
472
rtbl_set_column_affix_by_id (table, 2, "(", ")");
474
rtbl_add_column_entry_by_id (table, 0, "1");
476
rtbl_add_column_entry_by_id (table, 1, "2");
478
rtbl_add_column_entry_by_id (table, 2, "3");
481
rtbl_set_separator (table, " ");
482
rtbl_format (table, stdout);
484
rtbl_destroy (table);