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
49 struct column_entry
*rows
;
50 unsigned int column_id
;
57 struct column_data
**columns
;
59 char *column_separator
;
62 rtbl_t ROKEN_LIB_FUNCTION
65 return calloc (1, sizeof (struct rtbl_data
));
68 void ROKEN_LIB_FUNCTION
69 rtbl_set_flags (rtbl_t table
, unsigned int flags
)
74 unsigned int ROKEN_LIB_FUNCTION
75 rtbl_get_flags (rtbl_t table
)
80 static struct column_data
*
81 rtbl_get_column_by_id (rtbl_t table
, unsigned int id
)
84 for(i
= 0; i
< table
->num_columns
; i
++)
85 if(table
->columns
[i
]->column_id
== id
)
86 return table
->columns
[i
];
90 static struct column_data
*
91 rtbl_get_column (rtbl_t table
, const char *column
)
94 for(i
= 0; i
< table
->num_columns
; i
++)
95 if(strcmp(table
->columns
[i
]->header
, column
) == 0)
96 return table
->columns
[i
];
100 void ROKEN_LIB_FUNCTION
101 rtbl_destroy (rtbl_t table
)
105 for (i
= 0; i
< table
->num_columns
; i
++) {
106 struct column_data
*c
= table
->columns
[i
];
108 for (j
= 0; j
< c
->num_rows
; j
++)
109 free (c
->rows
[j
].data
);
116 free (table
->column_prefix
);
117 free (table
->column_separator
);
118 free (table
->columns
);
122 int ROKEN_LIB_FUNCTION
123 rtbl_add_column_by_id (rtbl_t table
, unsigned int id
,
124 const char *header
, unsigned int flags
)
126 struct column_data
*col
, **tmp
;
128 tmp
= realloc (table
->columns
, (table
->num_columns
+ 1) * sizeof (*tmp
));
131 table
->columns
= tmp
;
132 col
= malloc (sizeof (*col
));
135 col
->header
= strdup (header
);
136 if (col
->header
== NULL
) {
147 table
->columns
[table
->num_columns
++] = col
;
151 int ROKEN_LIB_FUNCTION
152 rtbl_add_column (rtbl_t table
, const char *header
, unsigned int flags
)
154 return rtbl_add_column_by_id(table
, 0, header
, flags
);
157 int ROKEN_LIB_FUNCTION
158 rtbl_new_row(rtbl_t table
)
162 for (c
= 0; c
< table
->num_columns
; c
++)
163 if(table
->columns
[c
]->num_rows
> max_rows
)
164 max_rows
= table
->columns
[c
]->num_rows
;
165 for (c
= 0; c
< table
->num_columns
; c
++) {
166 struct column_entry
*tmp
;
168 if(table
->columns
[c
]->num_rows
== max_rows
)
170 tmp
= realloc(table
->columns
[c
]->rows
,
171 max_rows
* sizeof(table
->columns
[c
]->rows
));
174 table
->columns
[c
]->rows
= tmp
;
175 while(table
->columns
[c
]->num_rows
< max_rows
) {
176 if((tmp
[table
->columns
[c
]->num_rows
++].data
= strdup("")) == NULL
)
184 column_compute_width (rtbl_t table
, struct column_data
*column
)
188 if(table
->flags
& RTBL_HEADER_STYLE_NONE
)
191 column
->width
= strlen (column
->header
);
192 for (i
= 0; i
< column
->num_rows
; i
++)
193 column
->width
= max (column
->width
, strlen (column
->rows
[i
].data
));
197 int ROKEN_LIB_FUNCTION
198 rtbl_set_prefix (rtbl_t table
, const char *prefix
)
200 if (table
->column_prefix
)
201 free (table
->column_prefix
);
202 table
->column_prefix
= strdup (prefix
);
203 if (table
->column_prefix
== NULL
)
208 int ROKEN_LIB_FUNCTION
209 rtbl_set_separator (rtbl_t table
, const char *separator
)
211 if (table
->column_separator
)
212 free (table
->column_separator
);
213 table
->column_separator
= strdup (separator
);
214 if (table
->column_separator
== NULL
)
219 int ROKEN_LIB_FUNCTION
220 rtbl_set_column_prefix (rtbl_t table
, const char *column
,
223 struct column_data
*c
= rtbl_get_column (table
, column
);
229 c
->prefix
= strdup (prefix
);
230 if (c
->prefix
== NULL
)
235 int ROKEN_LIB_FUNCTION
236 rtbl_set_column_affix_by_id(rtbl_t table
, unsigned int id
,
237 const char *prefix
, const char *suffix
)
239 struct column_data
*c
= rtbl_get_column_by_id (table
, id
);
248 c
->prefix
= strdup (prefix
);
249 if (c
->prefix
== NULL
)
258 c
->suffix
= strdup (suffix
);
259 if (c
->suffix
== NULL
)
267 get_column_prefix (rtbl_t table
, struct column_data
*c
)
273 if (table
->column_prefix
)
274 return table
->column_prefix
;
279 get_column_suffix (rtbl_t table
, struct column_data
*c
)
287 add_column_entry (struct column_data
*c
, const char *data
)
289 struct column_entry row
, *tmp
;
291 row
.data
= strdup (data
);
292 if (row
.data
== NULL
)
294 tmp
= realloc (c
->rows
, (c
->num_rows
+ 1) * sizeof (*tmp
));
300 c
->rows
[c
->num_rows
++] = row
;
304 int ROKEN_LIB_FUNCTION
305 rtbl_add_column_entry_by_id (rtbl_t table
, unsigned int id
, const char *data
)
307 struct column_data
*c
= rtbl_get_column_by_id (table
, id
);
312 return add_column_entry(c
, data
);
315 int ROKEN_LIB_FUNCTION
316 rtbl_add_column_entryv_by_id (rtbl_t table
, unsigned int id
,
317 const char *fmt
, ...)
324 ret
= vasprintf(&str
, fmt
, ap
);
328 ret
= rtbl_add_column_entry_by_id(table
, id
, str
);
333 int ROKEN_LIB_FUNCTION
334 rtbl_add_column_entry (rtbl_t table
, const char *column
, const char *data
)
336 struct column_data
*c
= rtbl_get_column (table
, column
);
341 return add_column_entry(c
, data
);
344 int ROKEN_LIB_FUNCTION
345 rtbl_add_column_entryv (rtbl_t table
, const char *column
, const char *fmt
, ...)
352 ret
= vasprintf(&str
, fmt
, ap
);
356 ret
= rtbl_add_column_entry(table
, column
, str
);
362 int ROKEN_LIB_FUNCTION
363 rtbl_format (rtbl_t table
, FILE * f
)
367 for (i
= 0; i
< table
->num_columns
; i
++)
368 column_compute_width (table
, table
->columns
[i
]);
369 if((table
->flags
& RTBL_HEADER_STYLE_NONE
) == 0) {
370 for (i
= 0; i
< table
->num_columns
; i
++) {
371 struct column_data
*c
= table
->columns
[i
];
373 if(table
->column_separator
!= NULL
&& i
> 0)
374 fprintf (f
, "%s", table
->column_separator
);
375 fprintf (f
, "%s", get_column_prefix (table
, c
));
376 if(i
== table
->num_columns
- 1 && c
->suffix
== NULL
)
377 /* last column, so no need to pad with spaces */
378 fprintf (f
, "%-*s", 0, c
->header
);
380 fprintf (f
, "%-*s", (int)c
->width
, c
->header
);
381 fprintf (f
, "%s", get_column_suffix (table
, c
));
389 /* are there any more rows left? */
390 for (i
= 0; flag
== 0 && i
< table
->num_columns
; ++i
) {
391 struct column_data
*c
= table
->columns
[i
];
393 if (c
->num_rows
> j
) {
401 for (i
= 0; i
< table
->num_columns
; i
++) {
403 struct column_data
*c
= table
->columns
[i
];
405 if(table
->column_separator
!= NULL
&& i
> 0)
406 fprintf (f
, "%s", table
->column_separator
);
410 if ((c
->flags
& RTBL_ALIGN_RIGHT
) == 0) {
411 if(i
== table
->num_columns
- 1 && c
->suffix
== NULL
)
412 /* last column, so no need to pad with spaces */
417 fprintf (f
, "%s", get_column_prefix (table
, c
));
418 if (c
->num_rows
<= j
)
419 fprintf (f
, "%*s", w
, "");
421 fprintf (f
, "%*s", w
, c
->rows
[j
].data
);
422 fprintf (f
, "%s", get_column_suffix (table
, c
));
431 main (int argc
, char **argv
)
435 table
= rtbl_create ();
436 rtbl_add_column_by_id (table
, 0, "Issued", 0);
437 rtbl_add_column_by_id (table
, 1, "Expires", 0);
438 rtbl_add_column_by_id (table
, 2, "Foo", RTBL_ALIGN_RIGHT
);
439 rtbl_add_column_by_id (table
, 3, "Principal", 0);
441 rtbl_add_column_entry_by_id (table
, 0, "Jul 7 21:19:29");
442 rtbl_add_column_entry_by_id (table
, 1, "Jul 8 07:19:29");
443 rtbl_add_column_entry_by_id (table
, 2, "73");
444 rtbl_add_column_entry_by_id (table
, 2, "0");
445 rtbl_add_column_entry_by_id (table
, 2, "-2000");
446 rtbl_add_column_entry_by_id (table
, 3, "krbtgt/NADA.KTH.SE@NADA.KTH.SE");
448 rtbl_add_column_entry_by_id (table
, 0, "Jul 7 21:19:29");
449 rtbl_add_column_entry_by_id (table
, 1, "Jul 8 07:19:29");
450 rtbl_add_column_entry_by_id (table
, 3, "afs/pdc.kth.se@NADA.KTH.SE");
452 rtbl_add_column_entry_by_id (table
, 0, "Jul 7 21:19:29");
453 rtbl_add_column_entry_by_id (table
, 1, "Jul 8 07:19:29");
454 rtbl_add_column_entry_by_id (table
, 3, "afs@NADA.KTH.SE");
456 rtbl_set_separator (table
, " ");
458 rtbl_format (table
, stdout
);
460 rtbl_destroy (table
);
464 table
= rtbl_create ();
465 rtbl_add_column_by_id (table
, 0, "Column A", 0);
466 rtbl_set_column_affix_by_id (table
, 0, "<", ">");
467 rtbl_add_column_by_id (table
, 1, "Column B", 0);
468 rtbl_set_column_affix_by_id (table
, 1, "[", "]");
469 rtbl_add_column_by_id (table
, 2, "Column C", 0);
470 rtbl_set_column_affix_by_id (table
, 2, "(", ")");
472 rtbl_add_column_entry_by_id (table
, 0, "1");
474 rtbl_add_column_entry_by_id (table
, 1, "2");
476 rtbl_add_column_entry_by_id (table
, 2, "3");
479 rtbl_set_separator (table
, " ");
480 rtbl_format (table
, stdout
);
482 rtbl_destroy (table
);