static void do_default_order_test(struct fastbuf *out)
{
table_init(&test_default_order_tbl);
- test_default_order_tbl.out = out;
- table_start(&test_default_order_tbl);
+ table_start(&test_default_order_tbl, out);
- table_set_int(&test_default_order_tbl, test_default_order_col0_int, 0);
- table_set_int(&test_default_order_tbl, test_default_order_col1_int, 1);
- table_set_int(&test_default_order_tbl, test_default_order_col2_int, 2);
+ table_col_int(&test_default_order_tbl, test_default_order_col0_int, 0);
+ table_col_int(&test_default_order_tbl, test_default_order_col1_int, 1);
+ table_col_int(&test_default_order_tbl, test_default_order_col2_int, 2);
table_end_row(&test_default_order_tbl);
- table_set_int(&test_default_order_tbl, test_default_order_col0_int, 10);
- table_set_int(&test_default_order_tbl, test_default_order_col1_int, 11);
- table_set_int(&test_default_order_tbl, test_default_order_col2_int, 12);
+ table_col_int(&test_default_order_tbl, test_default_order_col0_int, 10);
+ table_col_int(&test_default_order_tbl, test_default_order_col1_int, 11);
+ table_col_int(&test_default_order_tbl, test_default_order_col2_int, 12);
table_end_row(&test_default_order_tbl);
table_end(&test_default_order_tbl);
}
/**
- * tests: table_set_nt, table_set_uint, table_set_bool, table_set_double, table_set_printf
+ * tests: table_col_int, table_col_uint, table_col_bool, table_col_double, table_col_printf
**/
static void do_print1(struct table *test_tbl)
{
- table_set_str(test_tbl, test_col0_str, "sdsdf");
+ table_col_str(test_tbl, test_col0_str, "sdsdf");
table_append_str(test_tbl, "aaaaa");
- table_set_int(test_tbl, test_col1_int, -10);
- table_set_int(test_tbl, test_col1_int, 10000);
- table_set_uint(test_tbl, test_col2_uint, 10);
- table_set_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
- table_set_bool(test_tbl, test_col3_bool, 1);
- table_set_double(test_tbl, test_col4_double, 1.5);
+ table_col_int(test_tbl, test_col1_int, -10);
+ table_col_int(test_tbl, test_col1_int, 10000);
+ table_col_uint(test_tbl, test_col2_uint, 10);
+ table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
+ table_col_bool(test_tbl, test_col3_bool, 1);
+ table_col_double(test_tbl, test_col4_double, 1.5);
table_end_row(test_tbl);
- table_set_str(test_tbl, test_col0_str, "test");
+ table_col_str(test_tbl, test_col0_str, "test");
table_append_str(test_tbl, "bbbbb");
- table_set_int(test_tbl, test_col1_int, -100);
- table_set_uint(test_tbl, test_col2_uint, 100);
- table_set_bool(test_tbl, test_col3_bool, 0);
- table_set_double(test_tbl, test_col4_double, 1.5);
+ table_col_int(test_tbl, test_col1_int, -100);
+ table_col_uint(test_tbl, test_col2_uint, 100);
+ table_col_bool(test_tbl, test_col3_bool, 0);
+ table_col_double(test_tbl, test_col4_double, 1.5);
table_end_row(test_tbl);
}
out = bfdopen_shared(1, 4096);
table_init(&test_tbl);
- test_tbl.out = out;
process_command_line_opts(argv, &test_tbl);
return 0;
}
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
table_cleanup(&test_tbl);
table_col_order(tbl, col_order_int, tbl->column_count);
}
-void table_start(struct table *tbl)
+void table_start(struct table *tbl, struct fastbuf *out)
{
tbl->last_printed_col = -1;
tbl->row_printing_started = 0;
+ tbl->out = out;
ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
/*** Table cells ***/
-void table_set_printf(struct table *tbl, int col, const char *fmt, ...)
+void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
{
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
tbl->last_printed_col = col;
va_end(args);
}
-static const char *table_set_col_default_fmts[] = {
+static const char *table_col_default_fmts[] = {
[COL_TYPE_STR] = "%s",
[COL_TYPE_INT] = "%d",
[COL_TYPE_INTMAX] = "%jd",
[COL_TYPE_LAST] = NULL
};
-#define TABLE_SET_COL(_name_, _type_, _typeconst_) void table_set_##_name_(struct table *tbl, int col, _type_ val) \
+#define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val) \
{\
const char *fmt = tbl->columns[col].fmt;\
if(tbl->columns[col].type == COL_TYPE_ANY) {\
- fmt = table_set_col_default_fmts[_typeconst_];\
+ fmt = table_col_default_fmts[_typeconst_];\
}\
- table_set_##_name_##_fmt(tbl, col, fmt, val);\
+ table_col_##_name_##_fmt(tbl, col, fmt, val);\
}
-#define TABLE_SET_COL_STR(_name_, _type_, _typeconst_) void table_set_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
+#define TABLE_COL_STR(_name_, _type_, _typeconst_) void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val) \
{\
int col = table_get_col_idx(tbl, col_name);\
- table_set_##_name_(tbl, col, val);\
+ table_col_##_name_(tbl, col, val);\
}
-#define TABLE_SET_COL_FMT(_name_, _type_, _typeconst_) void table_set_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
+#define TABLE_COL_FMT(_name_, _type_, _typeconst_) void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val)\
{\
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
ASSERT(tbl->columns[col].type == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type);\
tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val);\
}
-#define TABLE_SET(_name_, _type_, _typeconst_) TABLE_SET_COL(_name_, _type_, _typeconst_);\
- TABLE_SET_COL_STR(_name_, _type_, _typeconst_);\
- TABLE_SET_COL_FMT(_name_, _type_, _typeconst_);
-
-TABLE_SET(int, int, COL_TYPE_INT)
-TABLE_SET(uint, uint, COL_TYPE_UINT)
-TABLE_SET(double, double, COL_TYPE_DOUBLE)
-TABLE_SET(str, const char *, COL_TYPE_STR)
-TABLE_SET(intmax, intmax_t, COL_TYPE_INTMAX)
-TABLE_SET(uintmax, uintmax_t, COL_TYPE_UINTMAX)
-#undef TABLE_SET_COL_FMT
-#undef TABLE_SET_COL_STR
-#undef TABLE_SET_COL
-#undef TABLE_SET
-
-void table_set_bool(struct table *tbl, int col, uint val)
+#define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_);\
+ TABLE_COL_STR(_name_, _type_, _typeconst_);\
+ TABLE_COL_FMT(_name_, _type_, _typeconst_);
+
+TABLE_COL_BODIES(int, int, COL_TYPE_INT)
+TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
+TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
+TABLE_COL_BODIES(str, const char *, COL_TYPE_STR)
+TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX)
+TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX)
+#undef TABLE_COL
+#undef TABLE_COL_FMT
+#undef TABLE_COL_STR
+#undef TABLE_COL_BODIES
+
+void table_col_bool(struct table *tbl, int col, uint val)
{
- table_set_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
+ table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
}
-void table_set_bool_name(struct table *tbl, const char *col_name, uint val)
+void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
{
int col = table_get_col_idx(tbl, col_name);
- table_set_bool(tbl, col, val);
+ table_col_bool(tbl, col, val);
}
-void table_set_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
+void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
{
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
**/
static void do_print1(struct table *test_tbl)
{
- table_set_str(test_tbl, test_col0_str, "sdsdf");
+ table_col_str(test_tbl, test_col0_str, "sdsdf");
table_append_str(test_tbl, "aaaaa");
- table_set_int(test_tbl, test_col1_int, -10);
- table_set_int(test_tbl, test_col1_int, 10000);
- table_set_uint(test_tbl, test_col2_uint, 10);
- table_set_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
- table_set_bool(test_tbl, test_col3_bool, 1);
- table_set_double(test_tbl, test_col4_double, 1.5);
- table_set_printf(test_tbl, test_col4_double, "AAA");
+ table_col_int(test_tbl, test_col1_int, -10);
+ table_col_int(test_tbl, test_col1_int, 10000);
+ table_col_uint(test_tbl, test_col2_uint, 10);
+ table_col_printf(test_tbl, test_col2_uint, "XXX-%u", 22222);
+ table_col_bool(test_tbl, test_col3_bool, 1);
+ table_col_double(test_tbl, test_col4_double, 1.5);
+ table_col_printf(test_tbl, test_col4_double, "AAA");
table_end_row(test_tbl);
- table_set_str(test_tbl, test_col0_str, "test");
+ table_col_str(test_tbl, test_col0_str, "test");
table_append_str(test_tbl, "bbbbb");
- table_set_int(test_tbl, test_col1_int, -100);
- table_set_uint(test_tbl, test_col2_uint, 100);
- table_set_bool(test_tbl, test_col3_bool, 0);
- table_set_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
+ table_col_int(test_tbl, test_col1_int, -100);
+ table_col_uint(test_tbl, test_col2_uint, 100);
+ table_col_bool(test_tbl, test_col3_bool, 0);
+ table_col_printf(test_tbl, test_col4_double, "%.2lf", 1.5);
table_end_row(test_tbl);
}
{
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);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
// print the same table as in the previous case without header
table_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
// this also tests whether there is need to call table_col_order_by_name after table_end was called
test_tbl.print_header = 0;
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
test_tbl.print_header = 1;
table_col_order_by_name(&test_tbl, "col3_bool");
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
table_col_order_by_name(&test_tbl, "col3_bool,col0_str");
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
table_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
table_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
- table_start(&test_tbl);
+ table_start(&test_tbl, out);
do_print1(&test_tbl);
table_end(&test_tbl);
static void test_any_type(struct fastbuf *out)
{
table_init(&test_any_tbl);
- test_any_tbl.out = out;
- table_start(&test_any_tbl);
+ table_start(&test_any_tbl, out);
- table_set_int(&test_any_tbl, test_any_col0_int, -10);
- table_set_int(&test_any_tbl, test_any_col1_any, 10000);
+ table_col_int(&test_any_tbl, test_any_col0_int, -10);
+ table_col_int(&test_any_tbl, test_any_col1_any, 10000);
table_end_row(&test_any_tbl);
- table_set_int(&test_any_tbl, test_any_col0_int, -10);
- table_set_double(&test_any_tbl, test_any_col1_any, 1.4);
+ table_col_int(&test_any_tbl, test_any_col0_int, -10);
+ table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
table_end_row(&test_any_tbl);
- table_set_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
+ table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
table_append_printf(&test_any_tbl, "%d", 20);
table_append_printf(&test_any_tbl, "%d", 30);
- table_set_double(&test_any_tbl, test_any_col1_any, 1.4);
+ table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
table_append_printf(&test_any_tbl, "%.2lf", 1.5);
table_append_printf(&test_any_tbl, "%.2lf", 1.6);
table_end_row(&test_any_tbl);
* call the table_set_* functions. The table_end_row function can be called after the table_start is called
* (but before the table_end is called)
**/
-void table_start(struct table *tbl);
+void table_start(struct table *tbl, struct fastbuf *out);
/**
* This function must be called after all the rows of the current table are printed. The table_set_*
* Prints a string that is printf-like formated into a particular column. This function does not check the
* type of the column, i.e., it can be used to print double into an int column
**/
-void table_set_printf(struct table *tbl, int col, const char *fmt, ...) FORMAT_CHECK(printf, 3, 4);
+void table_col_printf(struct table *tbl, int col, const char *fmt, ...) FORMAT_CHECK(printf, 3, 4);
/**
* Appends a string that is printf-like formated to the last printed column. This function does not check the
const char *table_set_option_value(struct table *tbl, const char *key, const char *value);
const char *table_set_gary_options(struct table *tbl, char **gary_table_opts);
-#define TABLE_SET_COL_PROTO(_name_, _type_) void table_set_##_name_(struct table *tbl, int col, _type_ val);\
- void table_set_##_name_##_name(struct table *tbl, const char *col_name, _type_ val);\
- void table_set_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) FORMAT_CHECK(printf, 3, 0);
+#define TABLE_COL_PROTO(_name_, _type_) void table_col_##_name_(struct table *tbl, int col, _type_ val);\
+ void table_col_##_name_##_name(struct table *tbl, const char *col_name, _type_ val);\
+ void table_col_##_name_##_fmt(struct table *tbl, int col, const char *fmt, _type_ val) FORMAT_CHECK(printf, 3, 0);
-// table_set_<type>_fmt has one disadvantage: it is not possible to
+// table_col_<type>_fmt has one disadvantage: it is not possible to
// check whether fmt contains format that contains formatting that is
// compatible with _type_
-TABLE_SET_COL_PROTO(int, int);
-TABLE_SET_COL_PROTO(uint, uint);
-TABLE_SET_COL_PROTO(double, double);
-TABLE_SET_COL_PROTO(str, const char *);
-TABLE_SET_COL_PROTO(intmax, intmax_t);
-TABLE_SET_COL_PROTO(uintmax, uintmax_t);
-
-void table_set_bool(struct table *tbl, int col, uint val);
-void table_set_bool_name(struct table *tbl, const char *col_name, uint val);
-void table_set_bool_fmt(struct table *tbl, int col, const char *fmt, uint val);
-#undef TABLE_SET_COL_PROTO
+TABLE_COL_PROTO(int, int);
+TABLE_COL_PROTO(uint, uint);
+TABLE_COL_PROTO(double, double);
+TABLE_COL_PROTO(str, const char *);
+TABLE_COL_PROTO(intmax, intmax_t);
+TABLE_COL_PROTO(uintmax, uintmax_t);
+
+void table_col_bool(struct table *tbl, int col, uint val);
+void table_col_bool_name(struct table *tbl, const char *col_name, uint val);
+void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val);
+#undef TABLE_COL_PROTO
#define TABLE_APPEND_PROTO(_name_, _type_) void table_append_##_name_(struct table *tbl, _type_ val)
TABLE_APPEND_PROTO(int, int);