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
50 struct column_entry
*rows
;
51 unsigned int column_id
;
58 struct column_data
**columns
;
60 char *column_separator
;
63 ROKEN_LIB_FUNCTION rtbl_t ROKEN_LIB_CALL
66 return calloc (1, sizeof (struct rtbl_data
));
69 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
70 rtbl_set_flags (rtbl_t table
, unsigned int flags
)
75 ROKEN_LIB_FUNCTION
unsigned int ROKEN_LIB_CALL
76 rtbl_get_flags (rtbl_t table
)
81 static struct column_data
*
82 rtbl_get_column_by_id (rtbl_t table
, unsigned int id
)
85 for(i
= 0; i
< table
->num_columns
; i
++)
86 if(table
->columns
[i
]->column_id
== id
)
87 return table
->columns
[i
];
91 static struct column_data
*
92 rtbl_get_column (rtbl_t table
, const char *column
)
95 for(i
= 0; i
< table
->num_columns
; i
++)
96 if(strcmp(table
->columns
[i
]->header
, column
) == 0)
97 return table
->columns
[i
];
101 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
102 rtbl_destroy (rtbl_t table
)
106 for (i
= 0; i
< table
->num_columns
; i
++) {
107 struct column_data
*c
= table
->columns
[i
];
109 for (j
= 0; j
< c
->num_rows
; j
++)
110 free (c
->rows
[j
].data
);
117 free (table
->column_prefix
);
118 free (table
->column_separator
);
119 free (table
->columns
);
123 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
124 rtbl_add_column_by_id (rtbl_t table
, unsigned int id
,
125 const char *header
, unsigned int flags
)
127 struct column_data
*col
, **tmp
;
129 tmp
= realloc (table
->columns
, (table
->num_columns
+ 1) * sizeof (*tmp
));
132 table
->columns
= tmp
;
133 col
= malloc (sizeof (*col
));
136 col
->header
= strdup (header
);
137 if (col
->header
== NULL
) {
148 table
->columns
[table
->num_columns
++] = col
;
152 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
153 rtbl_add_column (rtbl_t table
, const char *header
, unsigned int flags
)
155 return rtbl_add_column_by_id(table
, 0, header
, flags
);
158 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
159 rtbl_new_row(rtbl_t table
)
163 for (c
= 0; c
< table
->num_columns
; c
++)
164 if(table
->columns
[c
]->num_rows
> max_rows
)
165 max_rows
= table
->columns
[c
]->num_rows
;
166 for (c
= 0; c
< table
->num_columns
; c
++) {
167 struct column_entry
*tmp
;
169 if(table
->columns
[c
]->num_rows
== max_rows
)
171 tmp
= realloc(table
->columns
[c
]->rows
,
172 max_rows
* sizeof(table
->columns
[c
]->rows
));
175 table
->columns
[c
]->rows
= tmp
;
176 while(table
->columns
[c
]->num_rows
< max_rows
) {
177 if((tmp
[table
->columns
[c
]->num_rows
++].data
= strdup("")) == NULL
)
185 column_compute_width (rtbl_t table
, struct column_data
*column
)
189 if(table
->flags
& RTBL_HEADER_STYLE_NONE
)
192 column
->width
= (int)strlen (column
->header
);
193 for (i
= 0; i
< column
->num_rows
; i
++)
194 column
->width
= max (column
->width
, (int) strlen (column
->rows
[i
].data
));
198 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
199 rtbl_set_prefix (rtbl_t table
, const char *prefix
)
201 if (table
->column_prefix
)
202 free (table
->column_prefix
);
203 table
->column_prefix
= strdup (prefix
);
204 if (table
->column_prefix
== NULL
)
209 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
210 rtbl_set_separator (rtbl_t table
, const char *separator
)
212 if (table
->column_separator
)
213 free (table
->column_separator
);
214 table
->column_separator
= strdup (separator
);
215 if (table
->column_separator
== NULL
)
220 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
221 rtbl_set_column_prefix (rtbl_t table
, const char *column
,
224 struct column_data
*c
= rtbl_get_column (table
, column
);
230 c
->prefix
= strdup (prefix
);
231 if (c
->prefix
== NULL
)
236 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
237 rtbl_set_column_affix_by_id(rtbl_t table
, unsigned int id
,
238 const char *prefix
, const char *suffix
)
240 struct column_data
*c
= rtbl_get_column_by_id (table
, id
);
249 c
->prefix
= strdup (prefix
);
250 if (c
->prefix
== NULL
)
259 c
->suffix
= strdup (suffix
);
260 if (c
->suffix
== NULL
)
268 get_column_prefix (rtbl_t table
, struct column_data
*c
)
274 if (table
->column_prefix
)
275 return table
->column_prefix
;
280 get_column_suffix (rtbl_t table
, struct column_data
*c
)
288 add_column_entry (struct column_data
*c
, const char *data
)
290 struct column_entry row
, *tmp
;
292 row
.data
= strdup (data
);
293 if (row
.data
== NULL
)
295 tmp
= realloc (c
->rows
, (c
->num_rows
+ 1) * sizeof (*tmp
));
301 c
->rows
[c
->num_rows
++] = row
;
305 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
306 rtbl_add_column_entry_by_id (rtbl_t table
, unsigned int id
, const char *data
)
308 struct column_data
*c
= rtbl_get_column_by_id (table
, id
);
313 return add_column_entry(c
, data
);
316 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
317 rtbl_add_column_entryv_by_id (rtbl_t table
, unsigned int id
,
318 const char *fmt
, ...)
325 ret
= vasprintf(&str
, fmt
, ap
);
329 ret
= rtbl_add_column_entry_by_id(table
, id
, str
);
334 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
335 rtbl_add_column_entry (rtbl_t table
, const char *column
, const char *data
)
337 struct column_data
*c
= rtbl_get_column (table
, column
);
342 return add_column_entry(c
, data
);
345 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
346 rtbl_add_column_entryv (rtbl_t table
, const char *column
, const char *fmt
, ...)
353 ret
= vasprintf(&str
, fmt
, ap
);
357 ret
= rtbl_add_column_entry(table
, column
, str
);
363 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
364 rtbl_format (rtbl_t table
, FILE * f
)
366 char *str
= rtbl_format_str(table
);
369 fprintf(f
, "%s", str
);
375 rtbl_format_pretty(rtbl_t table
)
377 struct rk_strpool
*p
= NULL
;
380 for (i
= 0; i
< table
->num_columns
; i
++)
381 column_compute_width (table
, table
->columns
[i
]);
382 if((table
->flags
& RTBL_HEADER_STYLE_NONE
) == 0) {
383 for (i
= 0; i
< table
->num_columns
; i
++) {
384 struct column_data
*c
= table
->columns
[i
];
386 if(table
->column_separator
!= NULL
&& i
> 0)
387 p
= rk_strpoolprintf(p
, "%s", table
->column_separator
);
388 p
= rk_strpoolprintf(p
, "%s", get_column_prefix (table
, c
));
390 /* do nothing if no column */
391 } else if(i
== table
->num_columns
- 1 && c
->suffix
== NULL
)
392 /* last column, so no need to pad with spaces */
393 p
= rk_strpoolprintf(p
, "%-*s", 0, c
->header
);
395 p
= rk_strpoolprintf(p
, "%-*s", (int)c
->width
, c
->header
);
396 p
= rk_strpoolprintf(p
, "%s", get_column_suffix (table
, c
));
398 p
= rk_strpoolprintf(p
, "\n");
404 /* are there any more rows left? */
405 for (i
= 0; flag
== 0 && i
< table
->num_columns
; ++i
) {
406 struct column_data
*c
= table
->columns
[i
];
408 if (c
->num_rows
> j
) {
416 for (i
= 0; i
< table
->num_columns
; i
++) {
418 struct column_data
*c
= table
->columns
[i
];
420 if(table
->column_separator
!= NULL
&& i
> 0)
421 p
= rk_strpoolprintf(p
, "%s", table
->column_separator
);
425 if ((c
->flags
& RTBL_ALIGN_RIGHT
) == 0) {
426 if(i
== table
->num_columns
- 1 && c
->suffix
== NULL
)
427 /* last column, so no need to pad with spaces */
432 rk_strpoolprintf(p
, "%s", get_column_prefix (table
, c
));
433 if (c
->num_rows
<= j
)
434 p
= rk_strpoolprintf(p
, "%*s", w
, "");
436 p
= rk_strpoolprintf(p
, "%*s", w
, c
->rows
[j
].data
);
437 p
= rk_strpoolprintf(p
, "%s", get_column_suffix (table
, c
));
439 p
= rk_strpoolprintf(p
, "\n");
442 return rk_strpoolcollect(p
);
446 rtbl_format_json(rtbl_t table
)
448 struct rk_strpool
*p
= NULL
;
452 p
= rk_strpoolprintf(p
, "[");
456 /* are there any more rows left? */
457 for (i
= 0; flag
== 0 && i
< table
->num_columns
; ++i
) {
458 struct column_data
*c
= table
->columns
[i
];
460 if (c
->num_rows
> j
) {
468 p
= rk_strpoolprintf(p
, "%s{", j
> 0 ? "," : "");
471 for (i
= 0; i
< table
->num_columns
; i
++) {
472 struct column_data
*c
= table
->columns
[i
];
474 if (c
->num_rows
> j
) {
475 char *header
= c
->header
;
476 while (isspace((int)header
[0])) /* trim off prefixed whitespace */
478 p
= rk_strpoolprintf(p
, "%s\"%s\" : \"%s\"",
479 comma
? "," : "", header
,
484 p
= rk_strpoolprintf(p
, "}");
486 p
= rk_strpoolprintf(p
, "]");
488 return rk_strpoolcollect(p
);
491 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
492 rtbl_format_str (rtbl_t table
)
494 if (table
->flags
& RTBL_JSON
)
495 return rtbl_format_json(table
);
497 return rtbl_format_pretty(table
);
502 main (int argc
, char **argv
)
506 table
= rtbl_create ();
507 rtbl_add_column_by_id (table
, 0, "Issued", 0);
508 rtbl_add_column_by_id (table
, 1, "Expires", 0);
509 rtbl_add_column_by_id (table
, 2, "Foo", RTBL_ALIGN_RIGHT
);
510 rtbl_add_column_by_id (table
, 3, "Principal", 0);
512 rtbl_add_column_entry_by_id (table
, 0, "Jul 7 21:19:29");
513 rtbl_add_column_entry_by_id (table
, 1, "Jul 8 07:19:29");
514 rtbl_add_column_entry_by_id (table
, 2, "73");
515 rtbl_add_column_entry_by_id (table
, 2, "0");
516 rtbl_add_column_entry_by_id (table
, 2, "-2000");
517 rtbl_add_column_entry_by_id (table
, 3, "krbtgt/NADA.KTH.SE@NADA.KTH.SE");
519 rtbl_add_column_entry_by_id (table
, 0, "Jul 7 21:19:29");
520 rtbl_add_column_entry_by_id (table
, 1, "Jul 8 07:19:29");
521 rtbl_add_column_entry_by_id (table
, 3, "afs/pdc.kth.se@NADA.KTH.SE");
523 rtbl_add_column_entry_by_id (table
, 0, "Jul 7 21:19:29");
524 rtbl_add_column_entry_by_id (table
, 1, "Jul 8 07:19:29");
525 rtbl_add_column_entry_by_id (table
, 3, "afs@NADA.KTH.SE");
527 rtbl_set_separator (table
, " ");
529 rtbl_format (table
, stdout
);
531 rtbl_destroy (table
);
535 table
= rtbl_create ();
536 rtbl_add_column_by_id (table
, 0, "Column A", 0);
537 rtbl_set_column_affix_by_id (table
, 0, "<", ">");
538 rtbl_add_column_by_id (table
, 1, "Column B", 0);
539 rtbl_set_column_affix_by_id (table
, 1, "[", "]");
540 rtbl_add_column_by_id (table
, 2, "Column C", 0);
541 rtbl_set_column_affix_by_id (table
, 2, "(", ")");
543 rtbl_add_column_entry_by_id (table
, 0, "1");
545 rtbl_add_column_entry_by_id (table
, 1, "2");
547 rtbl_add_column_entry_by_id (table
, 2, "3");
550 rtbl_set_separator (table
, " ");
551 rtbl_format (table
, stdout
);
553 rtbl_destroy (table
);