2 * ASCII table generator.
3 * Copyright (C) 2017 Cumulus Networks
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "termtable.h"
26 DEFINE_MTYPE_STATIC(LIB
, TTABLE
, "ASCII table")
28 /* clang-format off */
29 struct ttable_style ttable_styles
[] = {
59 }, { // blank, suitable for plaintext alignment
92 void ttable_del(struct ttable
*tt
)
94 for (int i
= tt
->nrows
- 1; i
>= 0; i
--)
95 ttable_del_row(tt
, i
);
97 XFREE(MTYPE_TTABLE
, tt
->table
);
98 XFREE(MTYPE_TTABLE
, tt
);
101 struct ttable
*ttable_new(struct ttable_style
*style
)
105 tt
= XCALLOC(MTYPE_TTABLE
, sizeof(struct ttable
));
116 * Inserts or appends a new row at the specified index.
118 * If the index is -1, the row is added to the end of the table. Otherwise the
119 * index must be a valid index into tt->table.
121 * If the table already has at least one row (and therefore a determinate
122 * number of columns), a format string specifying a number of columns not equal
123 * to tt->ncols will result in a no-op and a return value of NULL.
125 * @param tt table to insert into
126 * @param i insertion index; inserted row will be (i + 1)'th row
127 * @param format printf format string as in ttable_[add|insert]_row()
128 * @param ap pre-initialized variadic list of arguments for format string
130 * @return pointer to the first cell of allocated row
132 static struct ttable_cell
*ttable_insert_row_va(struct ttable
*tt
, int i
,
133 const char *format
, va_list ap
)
135 assert(i
>= -1 && i
< tt
->nrows
);
137 char *res
, *orig
, *section
;
138 struct ttable_cell
*row
;
142 /* count how many columns we have */
143 for (int i
= 0; format
[i
]; i
++)
144 ncols
+= !!(format
[i
] == '|');
149 else if (ncols
!= tt
->ncols
)
152 /* reallocate chunk if necessary */
153 while (tt
->size
< (tt
->nrows
+ 1) * sizeof(struct ttable_cell
*)) {
154 tt
->size
= MAX(2 * tt
->size
, 2 * sizeof(struct ttable_cell
*));
155 tt
->table
= XREALLOC(MTYPE_TTABLE
, tt
->table
, tt
->size
);
158 /* CALLOC a block of cells */
159 row
= XCALLOC(MTYPE_TTABLE
, tt
->ncols
* sizeof(struct ttable_cell
));
162 vasprintf(&res
, format
, ap
);
167 section
= strsep(&res
, "|");
168 row
[col
].text
= XSTRDUP(MTYPE_TTABLE
, section
);
169 row
[col
].style
= tt
->style
.cell
;
176 if (i
== -1 || i
== tt
->nrows
)
177 tt
->table
[tt
->nrows
] = row
;
179 memmove(&tt
->table
[i
+ 1], &tt
->table
[i
],
180 (tt
->nrows
- i
) * sizeof(struct ttable_cell
*));
189 struct ttable_cell
*ttable_insert_row(struct ttable
*tt
, unsigned int i
,
190 const char *format
, ...)
192 struct ttable_cell
*ret
;
195 va_start(ap
, format
);
196 ret
= ttable_insert_row_va(tt
, i
, format
, ap
);
202 struct ttable_cell
*ttable_add_row(struct ttable
*tt
, const char *format
, ...)
204 struct ttable_cell
*ret
;
207 va_start(ap
, format
);
208 ret
= ttable_insert_row_va(tt
, -1, format
, ap
);
214 void ttable_del_row(struct ttable
*tt
, unsigned int i
)
216 assert((int)i
< tt
->nrows
);
218 for (int j
= 0; j
< tt
->ncols
; j
++)
219 XFREE(MTYPE_TTABLE
, tt
->table
[i
][j
].text
);
221 XFREE(MTYPE_TTABLE
, tt
->table
[i
]);
223 memmove(&tt
->table
[i
], &tt
->table
[i
+ 1],
224 (tt
->nrows
- i
- 1) * sizeof(struct ttable_cell
*));
232 void ttable_align(struct ttable
*tt
, unsigned int row
, unsigned int col
,
233 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
)
235 assert((int)row
< tt
->nrows
);
236 assert((int)col
< tt
->ncols
);
237 assert((int)row
+ (int)nrow
<= tt
->nrows
);
238 assert((int)col
+ (int)ncol
<= tt
->ncols
);
240 for (unsigned int i
= row
; i
< row
+ nrow
; i
++)
241 for (unsigned int j
= col
; j
< col
+ ncol
; j
++)
242 tt
->table
[i
][j
].style
.align
= align
;
245 static void ttable_cell_pad(struct ttable_cell
*cell
, enum ttable_align align
,
249 cell
->style
.lpad
= pad
;
251 cell
->style
.rpad
= pad
;
254 void ttable_pad(struct ttable
*tt
, unsigned int row
, unsigned int col
,
255 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
,
258 assert((int)row
< tt
->nrows
);
259 assert((int)col
< tt
->ncols
);
260 assert((int)row
+ (int)nrow
<= tt
->nrows
);
261 assert((int)col
+ (int)ncol
<= tt
->ncols
);
263 for (unsigned int i
= row
; i
< row
+ nrow
; i
++)
264 for (unsigned int j
= col
; j
< col
+ ncol
; j
++)
265 ttable_cell_pad(&tt
->table
[i
][j
], align
, pad
);
268 void ttable_restyle(struct ttable
*tt
)
270 for (int i
= 0; i
< tt
->nrows
; i
++)
271 for (int j
= 0; j
< tt
->ncols
; j
++)
272 tt
->table
[i
][j
].style
= tt
->style
.cell
;
275 void ttable_colseps(struct ttable
*tt
, unsigned int col
,
276 enum ttable_align align
, bool on
, char sep
)
278 for (int i
= 0; i
< tt
->nrows
; i
++) {
279 if (align
== RIGHT
) {
280 tt
->table
[i
][col
].style
.border
.right_on
= on
;
281 tt
->table
[i
][col
].style
.border
.right
= sep
;
283 tt
->table
[i
][col
].style
.border
.left_on
= on
;
284 tt
->table
[i
][col
].style
.border
.left
= sep
;
289 void ttable_rowseps(struct ttable
*tt
, unsigned int row
,
290 enum ttable_align align
, bool on
, char sep
)
292 for (int i
= 0; i
< tt
->ncols
; i
++) {
294 tt
->table
[row
][i
].style
.border
.top_on
= on
;
295 tt
->table
[row
][i
].style
.border
.top
= sep
;
297 tt
->table
[row
][i
].style
.border
.bottom_on
= on
;
298 tt
->table
[row
][i
].style
.border
.bottom
= sep
;
303 char *ttable_dump(struct ttable
*tt
, const char *newline
)
305 /* clang-format off */
306 char *buf
; // print buffer
307 size_t pos
; // position in buffer
308 size_t nl_len
; // strlen(newline)
309 int cw
[tt
->ncols
]; // calculated column widths
310 int nlines
; // total number of newlines / table lines
311 size_t width
; // length of one line, with newline
312 int abspad
; // calculated whitespace for sprintf
313 char *left
; // left part of line
314 size_t lsize
; // size of above
315 char *right
; // right part of line
316 size_t rsize
; // size of above
317 struct ttable_cell
*cell
, *row
; // iteration pointers
318 /* clang-format on */
320 nl_len
= strlen(newline
);
322 /* calculate width of each column */
323 memset(cw
, 0x00, sizeof(int) * tt
->ncols
);
325 for (int j
= 0; j
< tt
->ncols
; j
++)
326 for (int i
= 0, cellw
= 0; i
< tt
->nrows
; i
++) {
327 cell
= &tt
->table
[i
][j
];
329 cellw
+= (int)strlen(cell
->text
);
330 cellw
+= cell
->style
.lpad
;
331 cellw
+= cell
->style
.rpad
;
333 cellw
+= cell
->style
.border
.left_on
? 1 : 0;
334 if (j
!= tt
->ncols
- 1)
335 cellw
+= cell
->style
.border
.right_on
? 1 : 0;
336 cw
[j
] = MAX(cw
[j
], cellw
);
339 /* calculate overall line width, including newline */
341 width
+= tt
->style
.indent
;
342 width
+= tt
->style
.border
.left_on
? 1 : 0;
343 width
+= tt
->style
.border
.right_on
? 1 : 0;
344 width
+= strlen(newline
);
345 for (int i
= 0; i
< tt
->ncols
; i
++)
348 /* calculate number of lines en total */
350 nlines
+= tt
->style
.border
.top_on
? 1 : 0;
351 nlines
+= 1; // tt->style.border.bottom_on ? 1 : 1; makes life easier
352 for (int i
= 0; i
< tt
->nrows
; i
++) {
353 /* if leftmost cell has top / bottom border, whole row does */
354 nlines
+= tt
->table
[i
][0].style
.border
.top_on
? 1 : 0;
355 nlines
+= tt
->table
[i
][0].style
.border
.bottom_on
? 1 : 0;
358 /* initialize left & right */
359 lsize
= tt
->style
.indent
+ (tt
->style
.border
.left_on
? 1 : 0);
360 left
= XCALLOC(MTYPE_TTABLE
, lsize
);
361 rsize
= nl_len
+ (tt
->style
.border
.right_on
? 1 : 0);
362 right
= XCALLOC(MTYPE_TTABLE
, rsize
);
364 memset(left
, ' ', lsize
);
366 if (tt
->style
.border
.left_on
)
367 left
[lsize
- 1] = tt
->style
.border
.left
;
369 if (tt
->style
.border
.right_on
) {
370 right
[0] = tt
->style
.border
.right
;
371 memcpy(&right
[1], newline
, nl_len
);
373 memcpy(&right
[0], newline
, nl_len
);
375 /* allocate print buffer */
376 buf
= XCALLOC(MTYPE_TMP
, width
* (nlines
+ 1) + 1);
379 if (tt
->style
.border
.top_on
) {
380 memcpy(&buf
[pos
], left
, lsize
);
383 for (size_t i
= 0; i
< width
- lsize
- rsize
; i
++)
384 buf
[pos
++] = tt
->style
.border
.top
;
386 memcpy(&buf
[pos
], right
, rsize
);
390 for (int i
= 0; i
< tt
->nrows
; i
++) {
393 /* if top border and not first row, print top row border */
394 if (row
[0].style
.border
.top_on
&& i
!= 0) {
395 memcpy(&buf
[pos
], left
, lsize
);
398 for (size_t i
= 0; i
< width
- lsize
- rsize
; i
++)
399 buf
[pos
++] = row
[0].style
.border
.top
;
401 pos
-= width
- lsize
- rsize
;
402 for (int k
= 0; k
< tt
->ncols
; k
++) {
403 if (k
!= 0 && row
[k
].style
.border
.left_on
)
404 buf
[pos
] = tt
->style
.corner
;
406 if (row
[k
].style
.border
.right_on
407 && k
!= tt
->ncols
- 1)
408 buf
[pos
- 1] = tt
->style
.corner
;
411 memcpy(&buf
[pos
], right
, rsize
);
415 memcpy(&buf
[pos
], left
, lsize
);
418 for (int j
= 0; j
< tt
->ncols
; j
++) {
419 /* if left border && not first col print left border */
420 if (row
[j
].style
.border
.left_on
&& j
!= 0)
421 buf
[pos
++] = row
[j
].style
.border
.left
;
423 /* print left padding */
424 for (int i
= 0; i
< row
[j
].style
.lpad
; i
++)
427 /* calculate padding for sprintf */
429 abspad
-= row
[j
].style
.rpad
;
430 abspad
-= row
[j
].style
.lpad
;
432 abspad
-= row
[j
].style
.border
.left_on
? 1 : 0;
433 if (j
!= tt
->ncols
- 1)
434 abspad
-= row
[j
].style
.border
.right_on
? 1 : 0;
438 if (row
[j
].style
.align
== LEFT
)
443 pos
+= sprintf(&buf
[pos
], fmt
, abspad
, row
[j
].text
);
445 /* print right padding */
446 for (int i
= 0; i
< row
[j
].style
.rpad
; i
++)
449 /* if right border && not last col print right border */
450 if (row
[j
].style
.border
.right_on
&& j
!= tt
->ncols
- 1)
451 buf
[pos
++] = row
[j
].style
.border
.right
;
454 memcpy(&buf
[pos
], right
, rsize
);
457 /* if bottom border and not last row, print bottom border */
458 if (row
[0].style
.border
.bottom_on
&& i
!= tt
->nrows
- 1) {
459 memcpy(&buf
[pos
], left
, lsize
);
462 for (size_t i
= 0; i
< width
- lsize
- rsize
; i
++)
463 buf
[pos
++] = row
[0].style
.border
.bottom
;
465 pos
-= width
- lsize
- rsize
;
466 for (int k
= 0; k
< tt
->ncols
; k
++) {
467 if (k
!= 0 && row
[k
].style
.border
.left_on
)
468 buf
[pos
] = tt
->style
.corner
;
470 if (row
[k
].style
.border
.right_on
471 && k
!= tt
->ncols
- 1)
472 buf
[pos
- 1] = tt
->style
.corner
;
475 memcpy(&buf
[pos
], right
, rsize
);
479 assert(!buf
[pos
]); /* pos == & of first \0 in buf */
482 if (tt
->style
.border
.bottom_on
) {
483 memcpy(&buf
[pos
], left
, lsize
);
486 for (size_t i
= 0; i
< width
- lsize
- rsize
; i
++)
487 buf
[pos
++] = tt
->style
.border
.bottom
;
489 memcpy(&buf
[pos
], right
, rsize
);
495 XFREE(MTYPE_TTABLE
, left
);
496 XFREE(MTYPE_TTABLE
, right
);