static struct table test_tbl = {
TBL_COLUMNS {
- TBL_COL_STR(test, col0_str, 20),
- TBL_COL_INT(test, col1_int, 8),
- TBL_COL_UINT(test, col2_uint, 9),
- TBL_COL_BOOL(test, col3_bool, 9),
- TBL_COL_DOUBLE(test, col4_double, 11, 2),
+ [test_col0_str] = TBL_COL_STR("col0_str", 20),
+ [test_col1_int] = TBL_COL_INT("col1_int", 8),
+ [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
+ [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
+ [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
TBL_COL_END
},
TBL_COL_ORDER(test_column_order),
static struct table test_default_order_tbl = {
TBL_COLUMNS {
- TBL_COL_INT(test_default_order, col0_int, 8),
- TBL_COL_INT(test_default_order, col1_int, 9),
- TBL_COL_INT(test_default_order, col2_int, 9),
+ [test_default_order_col0_int] = TBL_COL_INT("col0_int", 8),
+ [test_default_order_col1_int] = TBL_COL_INT("col1_int", 9),
+ [test_default_order_col2_int] = TBL_COL_INT("col2_int", 9),
TBL_COL_END
},
TBL_OUTPUT_HUMAN_READABLE,
static void do_default_order_test(struct fastbuf *out)
{
- table_init(&test_default_order_tbl, out);
+ table_init(&test_default_order_tbl);
+ test_default_order_tbl.out = out;
+
table_start(&test_default_order_tbl);
table_set_int(&test_default_order_tbl, test_default_order_col0_int, 0);
struct fastbuf *out;
out = bfdopen_shared(1, 4096);
- table_init(&test_tbl, out);
+ table_init(&test_tbl);
+ test_tbl.out = out;
process_command_line_opts(argv, &test_tbl);
/*** Management of tables ***/
-void table_init(struct table *tbl, struct fastbuf *out)
+void table_init(struct table *tbl)
{
- tbl->out = out;
-
int col_count = 0; // count the number of columns in the struct table
for(;;) {
tbl->last_printed_col = -1;
tbl->row_printing_started = 0;
+ ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
+
if(!tbl->col_str_ptrs) {
tbl->col_str_ptrs = mp_alloc_zero(tbl->pool, sizeof(char *) * tbl->column_count);
}
static struct table test_tbl = {
TBL_COLUMNS {
- TBL_COL_STR(test, col0_str, 20),
- TBL_COL_INT(test, col1_int, 8),
- TBL_COL_UINT(test, col2_uint, 9),
- TBL_COL_BOOL(test, col3_bool, 9),
- TBL_COL_DOUBLE(test, col4_double, 11, 2),
+ [test_col0_str] = TBL_COL_STR("col0_str", 20),
+ [test_col1_int] = TBL_COL_INT("col1_int", 8),
+ [test_col2_uint] = TBL_COL_UINT("col2_uint", 9),
+ [test_col3_bool] = TBL_COL_BOOL("col3_bool", 9),
+ [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
TBL_COL_END
},
TBL_COL_ORDER(test_column_order),
static void test_simple1(struct fastbuf *out)
{
- table_init(&test_tbl, out);
+ table_init(&test_tbl);
+
+ test_tbl.out = out;
+
// print table with header
table_col_order_by_name(&test_tbl, "col3_bool");
table_start(&test_tbl);
static struct table test_any_tbl = {
TBL_COLUMNS {
- TBL_COL_INT(test_any, col0_int, 8),
- TBL_COL_ANY(test_any, col1_any, 9),
+ [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
+ [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
TBL_COL_END
},
TBL_COL_ORDER(test_any_column_order),
static void test_any_type(struct fastbuf *out)
{
- table_init(&test_any_tbl, out);
+ table_init(&test_any_tbl);
+ test_any_tbl.out = out;
+
table_start(&test_any_tbl);
table_set_int(&test_any_tbl, test_any_col0_int, -10);
COL_TYPE_LAST
};
-#define TBL_COL_STR(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%s", .type = COL_TYPE_STR }
-#define TBL_COL_INT(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%d", .type = COL_TYPE_INT }
-#define TBL_COL_UINT(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%u", .type = COL_TYPE_UINT }
-#define TBL_COL_INTMAX(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%jd", .type = COL_TYPE_INTMAX }
-#define TBL_COL_UINTMAX(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%ju", .type = COL_TYPE_UINTMAX }
-#define TBL_COL_HEXUINT(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "0x%x", .type = COL_TYPE_UINT }
-#define TBL_COL_DOUBLE(_enum_prefix, _name, _width, _prec) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%." #_prec "lf", .type = COL_TYPE_DOUBLE }
-#define TBL_COL_BOOL(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%s", .type = COL_TYPE_BOOL }
-#define TBL_COL_ANY(_enum_prefix, _name, _width) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = 0, .type = COL_TYPE_ANY }
-
-#define TBL_COL_STR_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_STR }
-#define TBL_COL_INT_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INT }
-#define TBL_COL_UINT_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
-#define TBL_COL_INTMAX_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INTMAX }
-#define TBL_COL_UINTMAX_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINTMAX }
-#define TBL_COL_HEXUINT_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
-#define TBL_COL_BOOL_FMT(_enum_prefix, _name, _width, _fmt) [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_BOOL }
+#define TBL_COL_STR(_name, _width) { .name = _name, .width = _width, .fmt = "%s", .type = COL_TYPE_STR }
+#define TBL_COL_INT(_name, _width) { .name = _name, .width = _width, .fmt = "%d", .type = COL_TYPE_INT }
+#define TBL_COL_UINT(_name, _width) { .name = _name, .width = _width, .fmt = "%u", .type = COL_TYPE_UINT }
+#define TBL_COL_INTMAX(_name, _width) { .name = _name, .width = _width, .fmt = "%jd", .type = COL_TYPE_INTMAX }
+#define TBL_COL_UINTMAX(_name, _width) { .name = _name, .width = _width, .fmt = "%ju", .type = COL_TYPE_UINTMAX }
+#define TBL_COL_HEXUINT(_name, _width) { .name = _name, .width = _width, .fmt = "0x%x", .type = COL_TYPE_UINT }
+#define TBL_COL_DOUBLE(_name, _width, _prec) { .name = _name, .width = _width, .fmt = "%." #_prec "lf", .type = COL_TYPE_DOUBLE }
+#define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = "%s", .type = COL_TYPE_BOOL }
+#define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = 0, .type = COL_TYPE_ANY }
+
+#define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_STR }
+#define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INT }
+#define TBL_COL_UINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
+#define TBL_COL_INTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INTMAX }
+#define TBL_COL_UINTMAX_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINTMAX }
+#define TBL_COL_HEXUINT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
+#define TBL_COL_BOOL_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_BOOL }
#define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type = COL_TYPE_LAST }
* about deallocation does not make much sense, the fastbuf is definitely not copied, only
* a pointer to it.
**/
-void table_init(struct table *tbl, struct fastbuf *out);
+void table_init(struct table *tbl);
void table_cleanup(struct table *tbl);
/**