{
struct fastbuf *out;
out = bfdopen_shared(1, 4096);
- table_init(&test_tbl);
- table_start(&test_tbl, out);
+ struct table *tbl = table_init(&test_tbl);
+ table_start(tbl, out);
u64 test_time = 1403685533;
s64 test_size = 4LU*(1024LU * 1024LU * 1024LU);
- table_col_size(&test_tbl, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl);
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
- test_tbl.column_order[TEST_COL0_SIZE].output_type = UNIT_KILOBYTE;
- table_col_size(&test_tbl, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl);
+ tbl->column_order[TEST_COL0_SIZE].output_type = UNIT_KILOBYTE;
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
- test_tbl.column_order[TEST_COL0_SIZE].output_type = UNIT_MEGABYTE;
- table_col_size(&test_tbl, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl);
+ tbl->column_order[TEST_COL0_SIZE].output_type = UNIT_MEGABYTE;
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
- test_tbl.column_order[TEST_COL0_SIZE].output_type = UNIT_GIGABYTE;
- test_tbl.column_order[TEST_COL1_TS].output_type = TIMESTAMP_DATETIME;
- table_col_size(&test_tbl, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl);
+ tbl->column_order[TEST_COL0_SIZE].output_type = UNIT_GIGABYTE;
+ tbl->column_order[TEST_COL1_TS].output_type = TIMESTAMP_DATETIME;
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
test_size = test_size * 1024LU;
- test_tbl.column_order[TEST_COL0_SIZE].output_type = UNIT_TERABYTE;
- test_tbl.column_order[TEST_COL1_TS].output_type = TIMESTAMP_DATETIME;
- table_col_size(&test_tbl, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl);
+ tbl->column_order[TEST_COL0_SIZE].output_type = UNIT_TERABYTE;
+ tbl->column_order[TEST_COL1_TS].output_type = TIMESTAMP_DATETIME;
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
- table_end(&test_tbl);
- table_cleanup(&test_tbl);
+ table_end(tbl);
+ table_cleanup(tbl);
bclose(out);
}
{
struct fastbuf *out;
out = bfdopen_shared(1, 4096);
- table_init(&test_tbl2);
- table_set_col_order_by_name(&test_tbl2, "");
- const char *err = table_set_option_value(&test_tbl2, "cols", "size[kb],size[mb],size[gb],size[tb],ts[datetime],ts[timestamp]");
+ struct table *tbl = table_init(&test_tbl2);
+ table_set_col_order_by_name(tbl, "");
+ const char *err = table_set_option_value(tbl, "cols", "size[kb],size[mb],size[gb],size[tb],ts[datetime],ts[timestamp]");
if(err) {
opt_failure("err in table_set_option_value: '%s'.", err);
abort();
}
- table_start(&test_tbl2, out);
+ table_start(tbl, out);
u64 test_time = 1403685533;
s64 test_size = 4LU*(1024LU * 1024LU * 1024LU);
- table_col_size(&test_tbl2, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl2, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl2);
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
- table_col_size(&test_tbl2, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl2, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl2);
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
test_size = test_size * 1024LU;
- table_col_size(&test_tbl2, TEST_COL0_SIZE, test_size);
- table_col_timestamp(&test_tbl2, TEST_COL1_TS, test_time);
- table_end_row(&test_tbl2);
+ table_col_size(tbl, TEST_COL0_SIZE, test_size);
+ table_col_timestamp(tbl, TEST_COL1_TS, test_time);
+ table_end_row(tbl);
- table_end(&test_tbl2);
- table_cleanup(&test_tbl2);
+ table_end(tbl);
+ table_cleanup(tbl);
bclose(out);
}
struct fastbuf *out;
out = bfdopen_shared(1, 4096);
- table_init(&test_tbl);
- process_command_line_opts(argv, &test_tbl);
+ struct table *tbl = table_init(&test_tbl);
+ process_command_line_opts(argv, tbl);
- print_table(&test_tbl, out);
- table_cleanup(&test_tbl);
+ print_table(tbl, out);
+ table_cleanup(tbl);
bclose(out);
return 0;
static void do_default_order_test(struct fastbuf *out)
{
- table_init(&test_default_order_tbl);
+ struct table *tbl = table_init(&test_default_order_tbl);
- table_start(&test_default_order_tbl, out);
+ table_start(tbl, out);
- 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_col_int(tbl, test_default_order_col0_int, 0);
+ table_col_int(tbl, test_default_order_col1_int, 1);
+ table_col_int(tbl, test_default_order_col2_int, 2);
+ table_end_row(tbl);
- 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_col_int(tbl, test_default_order_col0_int, 10);
+ table_col_int(tbl, test_default_order_col1_int, 11);
+ table_col_int(tbl, test_default_order_col2_int, 12);
+ table_end_row(tbl);
- table_end(&test_default_order_tbl);
- table_cleanup(&test_default_order_tbl);
+ table_end(tbl);
+ table_cleanup(tbl);
}
/**
struct fastbuf *out;
out = bfdopen_shared(1, 4096);
- table_init(&test_tbl);
+ struct table *tbl = table_init(&test_tbl);
- process_command_line_opts(argv, &test_tbl);
+ process_command_line_opts(argv, tbl);
const char *rv = NULL;
switch(test_to_perform) {
case TEST_INVALID_ORDER:
- rv = table_set_option(&test_tbl, "cols:test_col0_str,test_col1_int,xxx");
+ rv = table_set_option(tbl, "cols:test_col0_str,test_col1_int,xxx");
if(rv) printf("Tableprinter option parser returned: '%s'.\n", rv);
return 0;
case TEST_DEFAULT_COLUMN_ORDER:
bclose(out);
return 0;
case TEST_INVALID_OPTION:
- test_option_parser(&test_tbl);
+ test_option_parser(tbl);
bclose(out);
return 0;
};
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
- table_cleanup(&test_tbl);
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
+ table_cleanup(tbl);
bclose(out);
/*** Management of tables ***/
-void table_init(struct table *tbl)
+static void table_template_init(struct table *tbl_template)
+{
+ if(!tbl_template->pool) {
+ tbl_template->pool = mp_new(4096);
+ }
+}
+
+static struct table *table_template_copy(struct table *tbl_template)
+{
+ struct table *copy = mp_alloc_zero(tbl_template->pool, sizeof(struct table));
+
+ copy->column_count = tbl_template->column_count;
+ copy->pool = mp_new(4096);
+ if(tbl_template->column_order) {
+ copy->column_order = mp_alloc_zero(copy->pool, sizeof(struct table_col_info) * tbl_template->cols_to_output); //tbl_template->; // FIXME: more complicated copying
+ memcpy(copy->column_order, tbl_template->column_order, sizeof(struct table_col_info) * tbl_template->cols_to_output);
+ for(uint i = 0; i < copy->cols_to_output; i++) {
+ copy->column_order[i].cell_content = NULL;
+ copy->column_order[i].col_def = NULL;
+ copy->column_order[i].output_type = tbl_template->column_order[i].output_type;
+ }
+
+ copy->cols_to_output = tbl_template->cols_to_output;
+ }
+
+ copy->columns = tbl_template->columns;
+
+ copy->col_delimiter = tbl_template->col_delimiter;
+ copy->print_header = tbl_template->print_header;
+ copy->out = 0;
+ copy->last_printed_col = -1;
+ copy->row_printing_started = 0;
+ copy->col_out = -1;
+ copy->formatter = tbl_template->formatter;
+ copy->data = NULL;
+ return copy;
+}
+
+struct table *table_init(struct table *tbl_template)
{
int col_count = 0; // count the number of columns in the struct table
+ table_template_init(tbl_template);
+
+ struct table *tbl = table_template_copy(tbl_template);
+
for(;;) {
if(tbl->columns[col_count].name == NULL &&
tbl->columns[col_count].fmt == NULL &&
tbl->columns[col_count].type == COL_TYPE_LAST)
break;
ASSERT(tbl->columns[col_count].name != NULL);
- ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl->columns[col_count].fmt != NULL);
+ ASSERT(tbl->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL);
ASSERT(tbl->columns[col_count].width != 0);
col_count++;
}
- tbl->pool = mp_new(4096);
tbl->column_count = col_count;
}
tbl->print_header = 1; // by default, print header
+ return tbl;
}
void table_cleanup(struct table *tbl)
// TODO: test default column order
static void table_make_default_column_order(struct table *tbl)
{
- int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count);
+ int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * tbl->column_count); // FIXME: use stack instead of memory pool
for(int i = 0; i < tbl->column_count; i++) {
col_order_int[i] = i;
}
static void test_simple1(struct fastbuf *out)
{
- table_init(&test_tbl);
+ struct table *tbl = table_init(&test_tbl);
// print table with header
- table_set_col_order_by_name(&test_tbl, "col3_bool");
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col3_bool");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
// print the same table as in the previous case without header
- table_set_col_order_by_name(&test_tbl, "col0_str,col2_uint,col1_int,col3_bool");
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col0_str,col2_uint,col1_int,col3_bool");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
// this also tests whether there is need to call table_set_col_order_by_name after table_end was called
- test_tbl.print_header = 0;
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
- test_tbl.print_header = 1;
+ tbl->print_header = 0;
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
+ tbl->print_header = 1;
- table_set_col_order_by_name(&test_tbl, "col3_bool");
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col3_bool");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
- table_set_col_order_by_name(&test_tbl, "col3_bool,col0_str");
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col3_bool,col0_str");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
- table_set_col_order_by_name(&test_tbl, "col0_str,col3_bool,col2_uint");
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
- table_set_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, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint,col0_str,col3_bool,col2_uint");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
- table_set_col_order_by_name(&test_tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
- table_start(&test_tbl, out);
- do_print1(&test_tbl);
- table_end(&test_tbl);
+ table_set_col_order_by_name(tbl, "col0_str,col1_int,col2_uint,col3_bool,col4_double");
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
- table_cleanup(&test_tbl);
+ table_cleanup(tbl);
}
enum test_any_table_cols {
static void test_any_type(struct fastbuf *out)
{
- table_init(&test_any_tbl);
+ struct table *tbl = table_init(&test_any_tbl);
- table_start(&test_any_tbl, out);
+ table_start(tbl, out);
- 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_col_int(tbl, test_any_col0_int, -10);
+ table_col_int(tbl, test_any_col1_any, 10000);
+ table_end_row(tbl);
- 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_col_int(tbl, test_any_col0_int, -10);
+ table_col_double(tbl, test_any_col1_any, 1.4);
+ table_end_row(tbl);
- table_col_printf(&test_any_tbl, test_any_col0_int, "%d", 10);
- table_col_double(&test_any_tbl, test_any_col1_any, 1.4);
- table_end_row(&test_any_tbl);
+ table_col_printf(tbl, test_any_col0_int, "%d", 10);
+ table_col_double(tbl, test_any_col1_any, 1.4);
+ table_end_row(tbl);
- table_end(&test_any_tbl);
- table_cleanup(&test_any_tbl);
+ table_end(tbl);
+ table_cleanup(tbl);
}
int main(int argc UNUSED, char **argv UNUSED)
* -----------------
***/
+// FIXME: update documentation according to the changes made in recent comments!
+
/** Types of columns. These are seldom used explicitly, using a column definition macro is preferred. **/
enum column_type {
COL_TYPE_STR, // String
* Initialize a table definition. The structure should already contain
* the definitions of columns.
**/
-void table_init(struct table *tbl);
+struct table *table_init(struct table *tbl_template);
/** Destroy a table definition, freeing all memory used by it. **/
void table_cleanup(struct table *tbl);