}
// TODO: test default column order
+// FIXME: should we copy the default format from table definition or use TBL_FMT_HUMAN_READABLE?
static void table_make_default_column_order(struct table *tbl)
{
- int *col_order_int = alloca(sizeof(int) * tbl->column_count);
+ struct table_col_instance *col_order = alloca(sizeof(struct table_col_instance) * tbl->column_count);
+ bzero(col_order, sizeof(struct table_col_instance) * tbl->column_count);
for(int i = 0; i < tbl->column_count; i++) {
- col_order_int[i] = i;
+ col_order[i].idx = (uint) i;
+ col_order[i].fmt = tbl->columns[i].fmt;
}
- table_set_col_order(tbl, col_order_int, tbl->column_count);
+
+ table_set_col_order(tbl, col_order, tbl->column_count);
}
void table_start(struct table *tbl, struct fastbuf *out)
}
}
-void table_set_col_order(struct table *tbl, int *col_order, int cols_to_output)
+void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output)
{
- for(int i = 0; i < cols_to_output; i++) {
- ASSERT_MSG(col_order[i] >= 0 && col_order[i] < tbl->column_count, "Column %d does not exist (column number should be between 0 and %d).", col_order[i], tbl->column_count - 1);
+ for(uint i = 0; i < cols_to_output; i++) {
+ ASSERT_MSG(col_order[i].idx < (uint) tbl->column_count, "Column %d does not exist; column number should be between 0 and %d(including).", col_order[i].idx, tbl->column_count - 1);
}
tbl->cols_to_output = cols_to_output;
tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * cols_to_output);
- for(int i = 0; i < cols_to_output; i++) {
- int col_def_idx = col_order[i];
- tbl->column_order[i].idx = col_def_idx;
+ memcpy(tbl->column_order, col_order, sizeof(struct table_col_instance) * cols_to_output);
+ for(uint i = 0; i < cols_to_output; i++) {
+ int col_def_idx = tbl->column_order[i].idx; // this is given in col_order
tbl->column_order[i].col_def = tbl->columns + col_def_idx;
- tbl->column_order[i].cell_content = NULL; // it is in fact initialized by mp_alloc_zero, but for completeness ...
+ tbl->column_order[i].cell_content = NULL; // cell_content is copied from @col_order, so make sure that it is NULL
tbl->column_order[i].next_column = -1;
- tbl->column_order[i].fmt = tbl->columns[col_def_idx].fmt;
+ // tbl->column_order[i].fmt should be untouched (copied from col_order)
}
table_update_ll(tbl);
}
do_print1(tbl);
table_end(tbl);
+
+ // test table_col_order_fmt
+ struct table_col_instance col_order[] = { TBL_COL(TEST_COL0_STR), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_PRETTY), TBL_COL_FMT(TEST_COL4_DOUBLE, XTYPE_FMT_RAW) };
+ table_set_col_order(tbl, col_order, ARRAY_SIZE(col_order));
+ table_start(tbl, out);
+
+ table_col_str(tbl, TEST_COL0_STR, "test");
+ table_col_double(tbl, TEST_COL4_DOUBLE, 1.23456789);
+ table_end_row(tbl);
+
+ table_col_str(tbl, TEST_COL0_STR, "test");
+ table_col_double(tbl, TEST_COL4_DOUBLE, 1.23456789);
+ table_end_row(tbl);
+
+ table_end(tbl);
+
table_cleanup(tbl);
}
#define TBL_FMT_HUMAN_READABLE .formatter = &table_fmt_human_readable
#define TBL_FMT_BLOCKLINE .formatter = &table_fmt_blockline
#define TBL_FMT_MACHINE_READABLE .formatter = &table_fmt_machine_readable
+#define TBL_FMT(_fmt) .formatter = _fmt
/**
* The TBL_COL_ITER_START macro are used for iterating over all instances of a particular column in
**/
int table_get_col_idx(struct table *tbl, const char *col_name);
-
/**
* Sets a string option to an instance of a column type. This is the default version that checks
* whether the xtype::parse_fmt can be called and calls it. However, there are situation in which
const char *table_get_col_list(struct table *tbl);
/**
+ * Sets the order in which the columns are printed. The columns are specified by struct
+ *
* Sets the order in which the columns are printed.
* The table converts the integers in @col_order into an internal representation stored
* in `column_order`. Options to column instances can be set using @table_set_col_opt().
*
- * FIXME: add a function to the interface that accepts a pointer to an
- * array of table_col_instance.
- **/
-void table_set_col_order(struct table *tbl, int *col_order, int col_order_size);
-
-/**
- * Returns true if col_idx will be printed, false otherwise.
- *
- * FIXME: Naming of arguments is confusing. @col_idx sometimes indexes
- * columns, but sometimes their instances.
+ * @table_col_instance. This allows specification of format. The user should make an array of struct
+ * @table_col_instance and fill the array using the TBL_COL and TBL_COL_FMT.
**/
-bool table_col_is_printed(struct table *tbl, uint col_def_idx);
+void table_set_col_order(struct table *tbl, const struct table_col_instance *col_order, uint cols_to_output);
/**
* Sets the order in which the columns are printed. The specification is a string with comma-separated column
**/
const char *table_set_col_order_by_name(struct table *tbl, const char *col_order);
+/**
+ * Returns true if col_idx will be printed, false otherwise.
+ *
+ * FIXME: Naming of arguments is confusing. @col_idx sometimes indexes
+ * columns, but sometimes their instances.
+ **/
+bool table_col_is_printed(struct table *tbl, uint col_def_idx);
+
/**
* Sets table formatter. See below for the list of formatters.
**/