#include <zebra.h>
#include <stdio.h>
+#include "printfrr.h"
#include "memory.h"
#include "termtable.h"
DEFINE_MTYPE_STATIC(LIB, TTABLE, "ASCII table")
/* clang-format off */
-struct ttable_style ttable_styles[] = {
+const struct ttable_style ttable_styles[] = {
{ // default ascii
.corner = '+',
.rownums_on = false,
.indent = 1,
- .border.top = '-',
- .border.bottom = '-',
- .border.left = '|',
- .border.right = '|',
- .border.top_on = true,
- .border.bottom_on = true,
- .border.left_on = true,
- .border.right_on = true,
- .cell.lpad = 1,
- .cell.rpad = 1,
- .cell.align = LEFT,
- .cell.border.bottom = '-',
- .cell.border.bottom_on = true,
- .cell.border.top = '-',
- .cell.border.top_on = false,
- .cell.border.right = '|',
- .cell.border.right_on = true,
- .cell.border.left = '|',
- .cell.border.left_on = false,
+ .border = {
+ .top = '-',
+ .bottom = '-',
+ .left = '|',
+ .right = '|',
+ .top_on = true,
+ .bottom_on = true,
+ .left_on = true,
+ .right_on = true,
+ },
+ .cell = {
+ .lpad = 1,
+ .rpad = 1,
+ .align = LEFT,
+ .border = {
+ .bottom = '-',
+ .bottom_on = true,
+ .top = '-',
+ .top_on = false,
+ .right = '|',
+ .right_on = true,
+ .left = '|',
+ .left_on = false,
+ },
+ },
}, { // blank, suitable for plaintext alignment
.corner = ' ',
.rownums_on = false,
.indent = 1,
- .border.top = ' ',
- .border.bottom = ' ',
- .border.left = ' ',
- .border.right = ' ',
- .border.top_on = false,
- .border.bottom_on = false,
- .border.left_on = false,
- .border.right_on = false,
- .cell.lpad = 0,
- .cell.rpad = 3,
- .cell.align = LEFT,
- .cell.border.bottom = ' ',
- .cell.border.bottom_on = false,
- .cell.border.top = ' ',
- .cell.border.top_on = false,
- .cell.border.right = ' ',
- .cell.border.right_on = false,
- .cell.border.left = ' ',
- .cell.border.left_on = false,
+ .border = {
+ .top = ' ',
+ .bottom = ' ',
+ .left = ' ',
+ .right = ' ',
+ .top_on = false,
+ .bottom_on = false,
+ .left_on = false,
+ .right_on = false,
+ },
+ .cell = {
+ .lpad = 0,
+ .rpad = 3,
+ .align = LEFT,
+ .border = {
+ .bottom = ' ',
+ .bottom_on = false,
+ .top = ' ',
+ .top_on = false,
+ .right = ' ',
+ .right_on = false,
+ .left = ' ',
+ .left_on = false,
+ },
+ }
}
};
/* clang-format on */
XFREE(MTYPE_TTABLE, tt);
}
-struct ttable *ttable_new(struct ttable_style *style)
+struct ttable *ttable_new(const struct ttable_style *style)
{
struct ttable *tt;
{
assert(i >= -1 && i < tt->nrows);
+ char shortbuf[256];
char *res, *orig, *section;
struct ttable_cell *row;
int col = 0;
int ncols = 0;
/* count how many columns we have */
- for (int i = 0; format[i]; i++)
- ncols += !!(format[i] == '|');
+ for (int j = 0; format[j]; j++)
+ ncols += !!(format[j] == '|');
ncols++;
if (tt->ncols == 0)
/* CALLOC a block of cells */
row = XCALLOC(MTYPE_TTABLE, tt->ncols * sizeof(struct ttable_cell));
- res = NULL;
- vasprintf(&res, format, ap);
-
+ res = vasnprintfrr(MTYPE_TMP, shortbuf, sizeof(shortbuf), format, ap);
orig = res;
- while (res) {
+ while (res && col < tt->ncols) {
section = strsep(&res, "|");
row[col].text = XSTRDUP(MTYPE_TTABLE, section);
row[col].style = tt->style.cell;
col++;
}
- free(orig);
+ if (orig != shortbuf)
+ XFREE(MTYPE_TMP, orig);
/* insert row */
if (i == -1 || i == tt->nrows)
rsize = nl_len + (tt->style.border.right_on ? 1 : 0);
right = XCALLOC(MTYPE_TTABLE, rsize);
- memset (left, ' ', lsize);
+ memset(left, ' ', lsize);
if (tt->style.border.left_on)
left[lsize - 1] = tt->style.border.left;
memcpy(&buf[pos], left, lsize);
pos += lsize;
- for (size_t i = 0; i < width - lsize - rsize; i++)
+ for (size_t l = 0; l < width - lsize - rsize; l++)
buf[pos++] = row[0].style.border.top;
pos -= width - lsize - rsize;
buf[pos++] = row[j].style.border.left;
/* print left padding */
- for (int i = 0; i < row[j].style.lpad; i++)
+ for (int k = 0; k < row[j].style.lpad; k++)
buf[pos++] = ' ';
/* calculate padding for sprintf */
pos += sprintf(&buf[pos], fmt, abspad, row[j].text);
/* print right padding */
- for (int i = 0; i < row[j].style.rpad; i++)
+ for (int k = 0; k < row[j].style.rpad; k++)
buf[pos++] = ' ';
/* if right border && not last col print right border */
memcpy(&buf[pos], left, lsize);
pos += lsize;
- for (size_t i = 0; i < width - lsize - rsize; i++)
+ for (size_t l = 0; l < width - lsize - rsize; l++)
buf[pos++] = row[0].style.border.bottom;
pos -= width - lsize - rsize;
memcpy(&buf[pos], left, lsize);
pos += lsize;
- for (size_t i = 0; i < width - lsize - rsize; i++)
+ for (size_t l = 0; l < width - lsize - rsize; l++)
buf[pos++] = tt->style.border.bottom;
memcpy(&buf[pos], right, rsize);