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
25 #include "termtable.h"
27 DEFINE_MTYPE_STATIC(LIB
, TTABLE
, "ASCII table");
29 /* clang-format off */
30 const struct ttable_style ttable_styles
[] = {
60 }, { // blank, suitable for plaintext alignment
93 void ttable_del(struct ttable
*tt
)
95 for (int i
= tt
->nrows
- 1; i
>= 0; i
--)
96 ttable_del_row(tt
, i
);
98 XFREE(MTYPE_TTABLE
, tt
->table
);
99 XFREE(MTYPE_TTABLE
, tt
);
102 struct ttable
*ttable_new(const struct ttable_style
*style
)
106 tt
= XCALLOC(MTYPE_TTABLE
, sizeof(struct ttable
));
117 * Inserts or appends a new row at the specified index.
119 * If the index is -1, the row is added to the end of the table. Otherwise the
120 * index must be a valid index into tt->table.
122 * If the table already has at least one row (and therefore a determinate
123 * number of columns), a format string specifying a number of columns not equal
124 * to tt->ncols will result in a no-op and a return value of NULL.
126 * @param tt table to insert into
127 * @param i insertion index; inserted row will be (i + 1)'th row
128 * @param format printf format string as in ttable_[add|insert]_row()
129 * @param ap pre-initialized variadic list of arguments for format string
131 * @return pointer to the first cell of allocated row
133 static struct ttable_cell
*ttable_insert_row_va(struct ttable
*tt
, int i
,
134 const char *format
, va_list ap
)
136 assert(i
>= -1 && i
< tt
->nrows
);
139 char *res
, *orig
, *section
;
140 struct ttable_cell
*row
;
144 /* count how many columns we have */
145 for (int j
= 0; format
[j
]; j
++)
146 ncols
+= !!(format
[j
] == '|');
151 else if (ncols
!= tt
->ncols
)
154 /* reallocate chunk if necessary */
155 while (tt
->size
< (tt
->nrows
+ 1) * sizeof(struct ttable_cell
*)) {
156 tt
->size
= MAX(2 * tt
->size
, 2 * sizeof(struct ttable_cell
*));
157 tt
->table
= XREALLOC(MTYPE_TTABLE
, tt
->table
, tt
->size
);
160 /* CALLOC a block of cells */
161 row
= XCALLOC(MTYPE_TTABLE
, tt
->ncols
* sizeof(struct ttable_cell
));
163 res
= vasnprintfrr(MTYPE_TMP
, shortbuf
, sizeof(shortbuf
), format
, ap
);
166 while (res
&& col
< tt
->ncols
) {
167 section
= strsep(&res
, "|");
168 row
[col
].text
= XSTRDUP(MTYPE_TTABLE
, section
);
169 row
[col
].style
= tt
->style
.cell
;
173 if (orig
!= shortbuf
)
174 XFREE(MTYPE_TMP
, orig
);
177 if (i
== -1 || i
== tt
->nrows
)
178 tt
->table
[tt
->nrows
] = row
;
180 memmove(&tt
->table
[i
+ 1], &tt
->table
[i
],
181 (tt
->nrows
- i
) * sizeof(struct ttable_cell
*));
190 struct ttable_cell
*ttable_insert_row(struct ttable
*tt
, unsigned int i
,
191 const char *format
, ...)
193 struct ttable_cell
*ret
;
196 va_start(ap
, format
);
197 ret
= ttable_insert_row_va(tt
, i
, format
, ap
);
203 struct ttable_cell
*ttable_add_row(struct ttable
*tt
, const char *format
, ...)
205 struct ttable_cell
*ret
;
208 va_start(ap
, format
);
209 ret
= ttable_insert_row_va(tt
, -1, format
, ap
);
215 void ttable_del_row(struct ttable
*tt
, unsigned int i
)
217 assert((int)i
< tt
->nrows
);
219 for (int j
= 0; j
< tt
->ncols
; j
++)
220 XFREE(MTYPE_TTABLE
, tt
->table
[i
][j
].text
);
222 XFREE(MTYPE_TTABLE
, tt
->table
[i
]);
224 memmove(&tt
->table
[i
], &tt
->table
[i
+ 1],
225 (tt
->nrows
- i
- 1) * sizeof(struct ttable_cell
*));
233 void ttable_align(struct ttable
*tt
, unsigned int row
, unsigned int col
,
234 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
)
236 assert((int)row
< tt
->nrows
);
237 assert((int)col
< tt
->ncols
);
238 assert((int)row
+ (int)nrow
<= tt
->nrows
);
239 assert((int)col
+ (int)ncol
<= tt
->ncols
);
241 for (unsigned int i
= row
; i
< row
+ nrow
; i
++)
242 for (unsigned int j
= col
; j
< col
+ ncol
; j
++)
243 tt
->table
[i
][j
].style
.align
= align
;
246 static void ttable_cell_pad(struct ttable_cell
*cell
, enum ttable_align align
,
250 cell
->style
.lpad
= pad
;
252 cell
->style
.rpad
= pad
;
255 void ttable_pad(struct ttable
*tt
, unsigned int row
, unsigned int col
,
256 unsigned int nrow
, unsigned int ncol
, enum ttable_align align
,
259 assert((int)row
< tt
->nrows
);
260 assert((int)col
< tt
->ncols
);
261 assert((int)row
+ (int)nrow
<= tt
->nrows
);
262 assert((int)col
+ (int)ncol
<= tt
->ncols
);
264 for (unsigned int i
= row
; i
< row
+ nrow
; i
++)
265 for (unsigned int j
= col
; j
< col
+ ncol
; j
++)
266 ttable_cell_pad(&tt
->table
[i
][j
], align
, pad
);
269 void ttable_restyle(struct ttable
*tt
)
271 for (int i
= 0; i
< tt
->nrows
; i
++)
272 for (int j
= 0; j
< tt
->ncols
; j
++)
273 tt
->table
[i
][j
].style
= tt
->style
.cell
;
276 void ttable_colseps(struct ttable
*tt
, unsigned int col
,
277 enum ttable_align align
, bool on
, char sep
)
279 for (int i
= 0; i
< tt
->nrows
; i
++) {
280 if (align
== RIGHT
) {
281 tt
->table
[i
][col
].style
.border
.right_on
= on
;
282 tt
->table
[i
][col
].style
.border
.right
= sep
;
284 tt
->table
[i
][col
].style
.border
.left_on
= on
;
285 tt
->table
[i
][col
].style
.border
.left
= sep
;
290 void ttable_rowseps(struct ttable
*tt
, unsigned int row
,
291 enum ttable_align align
, bool on
, char sep
)
293 for (int i
= 0; i
< tt
->ncols
; i
++) {
295 tt
->table
[row
][i
].style
.border
.top_on
= on
;
296 tt
->table
[row
][i
].style
.border
.top
= sep
;
298 tt
->table
[row
][i
].style
.border
.bottom_on
= on
;
299 tt
->table
[row
][i
].style
.border
.bottom
= sep
;
304 char *ttable_dump(struct ttable
*tt
, const char *newline
)
306 /* clang-format off */
307 char *buf
; // print buffer
308 size_t pos
; // position in buffer
309 size_t nl_len
; // strlen(newline)
310 int cw
[tt
->ncols
]; // calculated column widths
311 int nlines
; // total number of newlines / table lines
312 size_t width
; // length of one line, with newline
313 int abspad
; // calculated whitespace for sprintf
314 char *left
; // left part of line
315 size_t lsize
; // size of above
316 char *right
; // right part of line
317 size_t rsize
; // size of above
318 struct ttable_cell
*cell
, *row
; // iteration pointers
319 /* clang-format on */
321 nl_len
= strlen(newline
);
323 /* calculate width of each column */
324 memset(cw
, 0x00, sizeof(int) * tt
->ncols
);
326 for (int j
= 0; j
< tt
->ncols
; j
++)
327 for (int i
= 0, cellw
= 0; i
< tt
->nrows
; i
++) {
328 cell
= &tt
->table
[i
][j
];
330 cellw
+= (int)strlen(cell
->text
);
331 cellw
+= cell
->style
.lpad
;
332 cellw
+= cell
->style
.rpad
;
334 cellw
+= cell
->style
.border
.left_on
? 1 : 0;
335 if (j
!= tt
->ncols
- 1)
336 cellw
+= cell
->style
.border
.right_on
? 1 : 0;
337 cw
[j
] = MAX(cw
[j
], cellw
);
340 /* calculate overall line width, including newline */
342 width
+= tt
->style
.indent
;
343 width
+= tt
->style
.border
.left_on
? 1 : 0;
344 width
+= tt
->style
.border
.right_on
? 1 : 0;
345 width
+= strlen(newline
);
346 for (int i
= 0; i
< tt
->ncols
; i
++)
349 /* calculate number of lines en total */
351 nlines
+= tt
->style
.border
.top_on
? 1 : 0;
352 nlines
+= 1; // tt->style.border.bottom_on ? 1 : 1; makes life easier
353 for (int i
= 0; i
< tt
->nrows
; i
++) {
354 /* if leftmost cell has top / bottom border, whole row does */
355 nlines
+= tt
->table
[i
][0].style
.border
.top_on
? 1 : 0;
356 nlines
+= tt
->table
[i
][0].style
.border
.bottom_on
? 1 : 0;
359 /* initialize left & right */
360 lsize
= tt
->style
.indent
+ (tt
->style
.border
.left_on
? 1 : 0);
361 left
= XCALLOC(MTYPE_TTABLE
, lsize
);
362 rsize
= nl_len
+ (tt
->style
.border
.right_on
? 1 : 0);
363 right
= XCALLOC(MTYPE_TTABLE
, rsize
);
365 memset(left
, ' ', lsize
);
367 if (tt
->style
.border
.left_on
)
368 left
[lsize
- 1] = tt
->style
.border
.left
;
370 if (tt
->style
.border
.right_on
) {
371 right
[0] = tt
->style
.border
.right
;
372 memcpy(&right
[1], newline
, nl_len
);
374 memcpy(&right
[0], newline
, nl_len
);
376 /* allocate print buffer */
377 buf
= XCALLOC(MTYPE_TMP
, width
* (nlines
+ 1) + 1);
380 if (tt
->style
.border
.top_on
) {
381 memcpy(&buf
[pos
], left
, lsize
);
384 for (size_t i
= 0; i
< width
- lsize
- rsize
; i
++)
385 buf
[pos
++] = tt
->style
.border
.top
;
387 memcpy(&buf
[pos
], right
, rsize
);
391 for (int i
= 0; i
< tt
->nrows
; i
++) {
394 /* if top border and not first row, print top row border */
395 if (row
[0].style
.border
.top_on
&& i
!= 0) {
396 memcpy(&buf
[pos
], left
, lsize
);
399 for (size_t l
= 0; l
< width
- lsize
- rsize
; l
++)
400 buf
[pos
++] = row
[0].style
.border
.top
;
402 pos
-= width
- lsize
- rsize
;
403 for (int k
= 0; k
< tt
->ncols
; k
++) {
404 if (k
!= 0 && row
[k
].style
.border
.left_on
)
405 buf
[pos
] = tt
->style
.corner
;
407 if (row
[k
].style
.border
.right_on
408 && k
!= tt
->ncols
- 1)
409 buf
[pos
- 1] = tt
->style
.corner
;
412 memcpy(&buf
[pos
], right
, rsize
);
416 memcpy(&buf
[pos
], left
, lsize
);
419 for (int j
= 0; j
< tt
->ncols
; j
++) {
420 /* if left border && not first col print left border */
421 if (row
[j
].style
.border
.left_on
&& j
!= 0)
422 buf
[pos
++] = row
[j
].style
.border
.left
;
424 /* print left padding */
425 for (int k
= 0; k
< row
[j
].style
.lpad
; k
++)
428 /* calculate padding for sprintf */
430 abspad
-= row
[j
].style
.rpad
;
431 abspad
-= row
[j
].style
.lpad
;
433 abspad
-= row
[j
].style
.border
.left_on
? 1 : 0;
434 if (j
!= tt
->ncols
- 1)
435 abspad
-= row
[j
].style
.border
.right_on
? 1 : 0;
439 if (row
[j
].style
.align
== LEFT
)
444 pos
+= sprintf(&buf
[pos
], fmt
, abspad
, row
[j
].text
);
446 /* print right padding */
447 for (int k
= 0; k
< row
[j
].style
.rpad
; k
++)
450 /* if right border && not last col print right border */
451 if (row
[j
].style
.border
.right_on
&& j
!= tt
->ncols
- 1)
452 buf
[pos
++] = row
[j
].style
.border
.right
;
455 memcpy(&buf
[pos
], right
, rsize
);
458 /* if bottom border and not last row, print bottom border */
459 if (row
[0].style
.border
.bottom_on
&& i
!= tt
->nrows
- 1) {
460 memcpy(&buf
[pos
], left
, lsize
);
463 for (size_t l
= 0; l
< width
- lsize
- rsize
; l
++)
464 buf
[pos
++] = row
[0].style
.border
.bottom
;
466 pos
-= width
- lsize
- rsize
;
467 for (int k
= 0; k
< tt
->ncols
; k
++) {
468 if (k
!= 0 && row
[k
].style
.border
.left_on
)
469 buf
[pos
] = tt
->style
.corner
;
471 if (row
[k
].style
.border
.right_on
472 && k
!= tt
->ncols
- 1)
473 buf
[pos
- 1] = tt
->style
.corner
;
476 memcpy(&buf
[pos
], right
, rsize
);
480 assert(!buf
[pos
]); /* pos == & of first \0 in buf */
483 if (tt
->style
.border
.bottom_on
) {
484 memcpy(&buf
[pos
], left
, lsize
);
487 for (size_t l
= 0; l
< width
- lsize
- rsize
; l
++)
488 buf
[pos
++] = tt
->style
.border
.bottom
;
490 memcpy(&buf
[pos
], right
, rsize
);
496 XFREE(MTYPE_TTABLE
, left
);
497 XFREE(MTYPE_TTABLE
, right
);