1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* ASCII table generator.
* Copyright (C) 2017 Cumulus Networks
* Quentin Young
*/
#include <zebra.h>
#include <termtable.h>
#include <memory.h>
int main(int argc, char **argv)
{
char *table;
struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_ASCII]);
/* test printf compatibility and dimension counters */
ttable_add_row(tt, "%s|%s|%s", "Column 1", "Column 2", "Column 3");
assert(tt->ncols == 3);
assert(tt->nrows == 1);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* add new row with 1 column, assert that it is not added */
assert(ttable_add_row(tt, "%s", "Garbage") == NULL);
assert(tt->ncols == 3);
assert(tt->nrows == 1);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* add new row, assert that it is added */
assert(ttable_add_row(tt, "%s|%s|%s", "a", "b", "c"));
assert(tt->ncols == 3);
assert(tt->nrows == 2);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* add empty row, assert that it is added */
assert(ttable_add_row(tt, "||"));
assert(tt->ncols == 3);
assert(tt->nrows == 3);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* delete 1st row, assert that it is removed */
ttable_del_row(tt, 0);
assert(tt->ncols == 3);
assert(tt->nrows == 2);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* delete last row, assert that it is removed */
ttable_del_row(tt, 0);
assert(tt->ncols == 3);
assert(tt->nrows == 1);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* delete the remaining row, check dumping an empty table */
ttable_del_row(tt, 0);
assert(tt->ncols == 0);
assert(tt->nrows == 0);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* add new row */
ttable_add_row(tt, "%s|%s||%s|%9d", "slick", "black", "triple", 1337);
assert(tt->ncols == 5);
assert(tt->nrows == 1);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* add bigger row */
ttable_add_row(tt, "%s|%s||%s|%s",
"nebula dusk session streets twilight pioneer beats yeah",
"prarie dog", "cornmeal", ":O -*_-*");
assert(tt->ncols == 5);
assert(tt->nrows == 2);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* insert new row at beginning */
ttable_insert_row(tt, 0, "%s|%s||%d|%lf", "converting", "vegetarians",
2, 2015.0);
assert(tt->ncols == 5);
assert(tt->nrows == 3);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* insert new row at end */
ttable_insert_row(tt, tt->nrows - 1, "%s|%s||%d|%ld", "converting",
"vegetarians", 1, 2003L);
assert(tt->ncols == 5);
assert(tt->nrows == 4);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* insert new row at middle */
ttable_insert_row(tt, 1, "%s|%s||%s|%ld", "she", "pioneer", "aki", 1l);
assert(tt->ncols == 5);
assert(tt->nrows == 5);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* set alignment */
ttable_align(tt, 0, 1, 2, 2, LEFT);
assert(tt->ncols == 5);
assert(tt->nrows == 5);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
ttable_align(tt, 0, 1, 5, 1, RIGHT);
assert(tt->ncols == 5);
assert(tt->nrows == 5);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* set padding */
ttable_pad(tt, 0, 1, 1, 1, RIGHT, 2);
assert(tt->ncols == 5);
assert(tt->nrows == 5);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
ttable_pad(tt, 0, 0, 5, 4, LEFT, 2);
assert(tt->ncols == 5);
assert(tt->nrows == 5);
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* restyle */
tt->style.cell.border.bottom_on = false;
tt->style.cell.border.top_on = false;
tt->style.cell.border.right_on = false;
tt->style.cell.border.left_on = false;
ttable_restyle(tt);
/* top & double bottom border for top row */
ttable_rowseps(tt, 0, BOTTOM, true, '-');
ttable_rowseps(tt, 1, TOP, true, '-');
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* column separators for leftmost column */
ttable_colseps(tt, 0, RIGHT, true, '|');
table = ttable_dump(tt, "\n");
fprintf(stdout, "%s\n", table);
XFREE(MTYPE_TMP_TTABLE, table);
/* delete table */
ttable_del(tt);
}
|