1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ASCII table generator.
4 * Copyright (C) 2017 Cumulus Networks
12 #include "termtable.h"
14 DEFINE_MTYPE_STATIC(LIB
, TTABLE
, "ASCII table");
16 /* clang-format off */
17 const struct ttable_style ttable_styles
[] = {
47 }, { // blank, suitable for plaintext alignment
80 void ttable_del(struct ttable
*tt
)
82 for (int i
= tt
->nrows
- 1; i
>= 0; i
--)
83 ttable_del_row(tt
, i
);
85 XFREE(MTYPE_TTABLE
, tt
->table
);
86 XFREE(MTYPE_TTABLE
, tt
);
89 struct ttable
*ttable_new(const struct ttable_style
*style
)
93 tt
= XCALLOC(MTYPE_TTABLE
, sizeof(struct ttable
));
104 * Inserts or appends a new row at the specified index.
106 * If the index is -1, the row is added to the end of the table. Otherwise the
107 * index must be a valid index into tt->table.
109 * If the table already has at least one row (and therefore a determinate
110 * number of columns), a format string specifying a number of columns not equal
111 * to tt->ncols will result in a no-op and a return value of NULL.
113 * @param tt table to insert into
114 * @param i insertion index; inserted row will be (i + 1)'th row
115 * @param format printf format string as in ttable_[add|insert]_row()
116 * @param ap pre-initialized variadic list of arguments for format string
118 * @return pointer to the first cell of allocated row
121 static struct ttable_cell
*ttable_insert_row_va(struct ttable
*tt
, int i
,
122 const char *format
, va_list ap
)
124 assert(i
>= -1 && i
< tt
->nrows
);
127 char *res
, *orig
, *section
;
128 struct ttable_cell
*row
;
132 /* count how many columns we have */
133 for (int j
= 0; format
[j
]; j
++)
134 ncols
+= !!(format
[j
] == '|');
139 else if (ncols
!= tt
->ncols
)
142 /* reallocate chunk if necessary */
143 while (tt
->size
< (tt
->nrows
+ 1) * sizeof(struct ttable_cell
*)) {
144 tt
->size
= MAX(2 * tt
->size
, 2 * sizeof(struct ttable_cell
*));
145 tt
->table
= XREALLOC(MTYPE_TTABLE
, tt
->table
, tt
->size
);
148 /* CALLOC a block of cells */
149 row
= XCALLOC(MTYPE_TTABLE
, tt
->ncols
* sizeof(struct ttable_cell
));
151 res
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
154 while (res
&& col
< tt
->ncols
) {
155 section
= strsep(&res
, "|");
156 row
[col
].text
= XSTRDUP(MTYPE_TTABLE
, section
);
157 row
[col
].style
= tt
->style
.cell
;
161 if (orig
!= shortbuf
)
162 XFREE(MTYPE_TMP
, orig
);
165 if (i
== -1 || i
== tt
->nrows
)
166 tt
->table
[tt
->nrows
] = row
;
168 memmove(&tt
->table
[i
+ 1], &tt
->table
[i
],
169 (tt
->nrows
- i
) * sizeof(struct ttable_cell
*));
178 struct ttable_cell
*ttable_insert_row(struct ttable
*tt
, unsigned int i
,
179 const char *format
, ...)
181 struct ttable_cell
*ret
;
184 va_start(ap
, format
);
185 ret
= ttable_insert_row_va(tt
, i
, format
, ap
);
191 struct ttable_cell
*ttable_add_row(struct ttable
*tt
, const char *format
, ...)
193 struct ttable_cell
*ret
;
196 va_start(ap
, format
);
197 ret
= ttable_insert_row_va(tt
, -1, format
, ap
);
203 void ttable_del_row(struct ttable
*tt
, unsigned int i
)
205 assert((int)i
< tt
->nrows
);
207 for (int j
= 0; j
< tt
->ncols
; j
++)
208 XFREE(MTYPE_TTABLE
, tt
->table
[i
][j
].text
);
210 XFREE(MTYPE_TTABLE
, tt
->table
[i
]);
212 memmove(&tt
->table
[i
], &tt
->table
[i
+ 1],
213 (tt
->nrows
- i
- 1) * sizeof(struct ttable_cell
*));
221 void ttable_align(struct ttable
*tt
, unsigned int row
, unsigned int col
,
222 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
)
224 assert((int)row
< tt
->nrows
);
225 assert((int)col
< tt
->ncols
);
226 assert((int)row
+ (int)nrow
<= tt
->nrows
);
227 assert((int)col
+ (int)ncol
<= tt
->ncols
);
229 for (unsigned int i
= row
; i
< row
+ nrow
; i
++)
230 for (unsigned int j
= col
; j
< col
+ ncol
; j
++)
231 tt
->table
[i
][j
].style
.align
= align
;
234 static void ttable_cell_pad(struct ttable_cell
*cell
, enum ttable_align align
,
238 cell
->style
.lpad
= pad
;
240 cell
->style
.rpad
= pad
;
243 void ttable_pad(struct ttable
*tt
, unsigned int row
, unsigned int col
,
244 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
,
247 assert((int)row
< tt
->nrows
);
248 assert((int)col
< tt
->ncols
);
249 assert((int)row
+ (int)nrow
<= tt
->nrows
);
250 assert((int)col
+ (int)ncol
<= tt
->ncols
);
252 for (unsigned int i
= row
; i
< row
+ nrow
; i
++)
253 for (unsigned int j
= col
; j
< col
+ ncol
; j
++)
254 ttable_cell_pad(&tt
->table
[i
][j
], align
, pad
);
257 void ttable_restyle(struct ttable
*tt
)
259 for (int i
= 0; i
< tt
->nrows
; i
++)
260 for (int j
= 0; j
< tt
->ncols
; j
++)
261 tt
->table
[i
][j
].style
= tt
->style
.cell
;
264 void ttable_colseps(struct ttable
*tt
, unsigned int col
,
265 enum ttable_align align
, bool on
, char sep
)
267 for (int i
= 0; i
< tt
->nrows
; i
++) {
268 if (align
== RIGHT
) {
269 tt
->table
[i
][col
].style
.border
.right_on
= on
;
270 tt
->table
[i
][col
].style
.border
.right
= sep
;
272 tt
->table
[i
][col
].style
.border
.left_on
= on
;
273 tt
->table
[i
][col
].style
.border
.left
= sep
;
278 void ttable_rowseps(struct ttable
*tt
, unsigned int row
,
279 enum ttable_align align
, bool on
, char sep
)
281 for (int i
= 0; i
< tt
->ncols
; i
++) {
283 tt
->table
[row
][i
].style
.border
.top_on
= on
;
284 tt
->table
[row
][i
].style
.border
.top
= sep
;
286 tt
->table
[row
][i
].style
.border
.bottom_on
= on
;
287 tt
->table
[row
][i
].style
.border
.bottom
= sep
;
292 char *ttable_dump(struct ttable
*tt
, const char *newline
)
294 /* clang-format off */
295 char *buf
; // print buffer
296 size_t pos
; // position in buffer
297 size_t nl_len
; // strlen(newline)
298 int cw
[tt
->ncols
]; // calculated column widths
299 int nlines
; // total number of newlines / table lines
300 size_t width
; // length of one line, with newline
301 int abspad
; // calculated whitespace for sprintf
302 char *left
; // left part of line
303 size_t lsize
; // size of above
304 char *right
; // right part of line
305 size_t rsize
; // size of above
306 struct ttable_cell
*cell
, *row
; // iteration pointers
307 /* clang-format on */
309 nl_len
= strlen(newline
);
311 /* calculate width of each column */
312 memset(cw
, 0x00, sizeof(int) * tt
->ncols
);
314 for (int j
= 0; j
< tt
->ncols
; j
++)
315 for (int i
= 0, cellw
= 0; i
< tt
->nrows
; i
++) {
316 cell
= &tt
->table
[i
][j
];
318 cellw
+= (int)strlen(cell
->text
);
319 cellw
+= cell
->style
.lpad
;
320 cellw
+= cell
->style
.rpad
;
322 cellw
+= cell
->style
.border
.left_on
? 1 : 0;
323 if (j
!= tt
->ncols
- 1)
324 cellw
+= cell
->style
.border
.right_on
? 1 : 0;
325 cw
[j
] = MAX(cw
[j
], cellw
);
328 /* calculate overall line width, including newline */
330 width
+= tt
->style
.indent
;
331 width
+= tt
->style
.border
.left_on
? 1 : 0;
332 width
+= tt
->style
.border
.right_on
? 1 : 0;
333 width
+= strlen(newline
);
334 for (int i
= 0; i
< tt
->ncols
; i
++)
337 /* calculate number of lines en total */
339 nlines
+= tt
->style
.border
.top_on
? 1 : 0;
340 nlines
+= 1; // tt->style.border.bottom_on ? 1 : 1; makes life easier
341 for (int i
= 0; i
< tt
->nrows
; i
++) {
342 /* if leftmost cell has top / bottom border, whole row does */
343 nlines
+= tt
->table
[i
][0].style
.border
.top_on
? 1 : 0;
344 nlines
+= tt
->table
[i
][0].style
.border
.bottom_on
? 1 : 0;
347 /* initialize left & right */
348 lsize
= tt
->style
.indent
+ (tt
->style
.border
.left_on
? 1 : 0);
349 left
= XCALLOC(MTYPE_TTABLE
, lsize
);
350 rsize
= nl_len
+ (tt
->style
.border
.right_on
? 1 : 0);
351 right
= XCALLOC(MTYPE_TTABLE
, rsize
);
353 memset(left
, ' ', lsize
);
355 if (tt
->style
.border
.left_on
)
356 left
[lsize
- 1] = tt
->style
.border
.left
;
358 if (tt
->style
.border
.right_on
) {
359 right
[0] = tt
->style
.border
.right
;
360 memcpy(&right
[1], newline
, nl_len
);
362 memcpy(&right
[0], newline
, nl_len
);
364 /* allocate print buffer */
365 buf
= XCALLOC(MTYPE_TMP
, width
* (nlines
+ 1) + 1);
368 if (tt
->style
.border
.top_on
) {
369 memcpy(&buf
[pos
], left
, lsize
);
372 for (size_t i
= 0; i
< width
- lsize
- rsize
; i
++)
373 buf
[pos
++] = tt
->style
.border
.top
;
375 memcpy(&buf
[pos
], right
, rsize
);
379 for (int i
= 0; i
< tt
->nrows
; i
++) {
382 /* if top border and not first row, print top row border */
383 if (row
[0].style
.border
.top_on
&& i
!= 0) {
384 memcpy(&buf
[pos
], left
, lsize
);
387 for (size_t l
= 0; l
< width
- lsize
- rsize
; l
++)
388 buf
[pos
++] = row
[0].style
.border
.top
;
390 pos
-= width
- lsize
- rsize
;
391 for (int k
= 0; k
< tt
->ncols
; k
++) {
392 if (k
!= 0 && row
[k
].style
.border
.left_on
)
393 buf
[pos
] = tt
->style
.corner
;
395 if (row
[k
].style
.border
.right_on
396 && k
!= tt
->ncols
- 1)
397 buf
[pos
- 1] = tt
->style
.corner
;
400 memcpy(&buf
[pos
], right
, rsize
);
404 memcpy(&buf
[pos
], left
, lsize
);
407 for (int j
= 0; j
< tt
->ncols
; j
++) {
408 /* if left border && not first col print left border */
409 if (row
[j
].style
.border
.left_on
&& j
!= 0)
410 buf
[pos
++] = row
[j
].style
.border
.left
;
412 /* print left padding */
413 for (int k
= 0; k
< row
[j
].style
.lpad
; k
++)
416 /* calculate padding for sprintf */
418 abspad
-= row
[j
].style
.rpad
;
419 abspad
-= row
[j
].style
.lpad
;
421 abspad
-= row
[j
].style
.border
.left_on
? 1 : 0;
422 if (j
!= tt
->ncols
- 1)
423 abspad
-= row
[j
].style
.border
.right_on
? 1 : 0;
426 if (row
[j
].style
.align
== LEFT
)
427 pos
+= sprintf(&buf
[pos
], "%-*s", abspad
,
430 pos
+= sprintf(&buf
[pos
], "%*s", abspad
,
433 /* print right padding */
434 for (int k
= 0; k
< row
[j
].style
.rpad
; k
++)
437 /* if right border && not last col print right border */
438 if (row
[j
].style
.border
.right_on
&& j
!= tt
->ncols
- 1)
439 buf
[pos
++] = row
[j
].style
.border
.right
;
442 memcpy(&buf
[pos
], right
, rsize
);
445 /* if bottom border and not last row, print bottom border */
446 if (row
[0].style
.border
.bottom_on
&& i
!= tt
->nrows
- 1) {
447 memcpy(&buf
[pos
], left
, lsize
);
450 for (size_t l
= 0; l
< width
- lsize
- rsize
; l
++)
451 buf
[pos
++] = row
[0].style
.border
.bottom
;
453 pos
-= width
- lsize
- rsize
;
454 for (int k
= 0; k
< tt
->ncols
; k
++) {
455 if (k
!= 0 && row
[k
].style
.border
.left_on
)
456 buf
[pos
] = tt
->style
.corner
;
458 if (row
[k
].style
.border
.right_on
459 && k
!= tt
->ncols
- 1)
460 buf
[pos
- 1] = tt
->style
.corner
;
463 memcpy(&buf
[pos
], right
, rsize
);
467 assert(!buf
[pos
]); /* pos == & of first \0 in buf */
470 if (tt
->style
.border
.bottom_on
) {
471 memcpy(&buf
[pos
], left
, lsize
);
474 for (size_t l
= 0; l
< width
- lsize
- rsize
; l
++)
475 buf
[pos
++] = tt
->style
.border
.bottom
;
477 memcpy(&buf
[pos
], right
, rsize
);
483 XFREE(MTYPE_TTABLE
, left
);
484 XFREE(MTYPE_TTABLE
, right
);