test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double, test_col5_size, test_col6_time
};
-static struct table_col_info test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) };
+static struct table_col_instance test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) };
static struct table_template test_tbl = {
TBL_COLUMNS {
/*** Management of tables ***/
-static struct table *table_make_instance(struct table_template *tbl_template)
+static struct table *table_make_instance(const struct table_template *tbl_template)
{
struct table *new_inst = xmalloc_zero(sizeof(struct table));
- new_inst->column_count = tbl_template->column_count;
new_inst->pool = mp_new(4096);
if(tbl_template->column_order) {
- new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_info) * tbl_template->cols_to_output);
- memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_info) * tbl_template->cols_to_output);
+ new_inst->column_order = mp_alloc_zero(new_inst->pool, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
+ memcpy(new_inst->column_order, tbl_template->column_order, sizeof(struct table_col_instance) * tbl_template->cols_to_output);
for(uint i = 0; i < new_inst->cols_to_output; i++) {
new_inst->column_order[i].cell_content = NULL;
new_inst->column_order[i].col_def = NULL;
new_inst->cols_to_output = tbl_template->cols_to_output;
}
+ int col_count = 0; // count the number of columns in the struct table
+ for(;;) {
+ if(tbl_template->columns[col_count].name == NULL &&
+ tbl_template->columns[col_count].fmt == NULL &&
+ tbl_template->columns[col_count].width == 0 &&
+ tbl_template->columns[col_count].type == COL_TYPE_LAST)
+ break;
+ ASSERT(tbl_template->columns[col_count].name != NULL);
+ ASSERT(tbl_template->columns[col_count].type == COL_TYPE_ANY || tbl_template->columns[col_count].fmt != NULL);
+ ASSERT(tbl_template->columns[col_count].width != 0);
+
+ col_count++;
+ }
+ new_inst->column_count = col_count;
+
+ new_inst->columns = mp_alloc_zero(new_inst->pool, sizeof(struct table_column) * new_inst->column_count);
+ memcpy(new_inst->columns, tbl_template->columns, sizeof(struct table_column) * new_inst->column_count);
new_inst->columns = tbl_template->columns; // FIXME: copy also columns, if there will be two instances of table, then there will be clash between the linked lists!
new_inst->col_delimiter = tbl_template->col_delimiter;
return new_inst;
}
-struct table *table_init(struct table_template *tbl_template)
+struct table *table_init(const struct table_template *tbl_template)
{
- int col_count = 0; // count the number of columns in the struct table
-
struct table *tbl = table_make_instance(tbl_template);
- for(;;) {
- if(tbl->columns[col_count].name == NULL &&
- tbl->columns[col_count].fmt == NULL &&
- tbl->columns[col_count].width == 0 &&
- 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_template->columns[col_count].fmt != NULL);
- ASSERT(tbl->columns[col_count].width != 0);
-
- col_count++;
- }
-
- tbl->column_count = col_count;
-
if(!tbl->formatter) {
tbl->formatter = &table_fmt_human_readable;
}
}
tbl->cols_to_output = cols_to_output;
- tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * 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_idx = col_order[i];
tbl->column_order[i].idx = col_idx;
}
tbl->cols_to_output = col_count;
- tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_info) * col_count);
+ tbl->column_order = mp_alloc_zero(tbl->pool, sizeof(struct table_col_instance) * col_count);
int curr_col_idx = 0;
char *name_start = tmp_col_order;
test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
};
-static struct table_col_info test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) };
+static struct table_col_instance test_column_order[] = { TBL_COL(test_col3_bool), TBL_COL(test_col4_double), TBL_COL(test_col2_uint), TBL_COL(test_col1_int), TBL_COL(test_col0_str) };
static struct table_template test_tbl = {
TBL_COLUMNS {
test_any_col0_int, test_any_col1_any
};
-static struct table_col_info test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
+static struct table_col_instance test_any_column_order[] = { TBL_COL(test_any_col0_int), TBL_COL(test_any_col1_any) };
static struct table_template test_any_tbl = {
TBL_COLUMNS {
const char *fmt; // [*] Default format of each cell in the column
enum column_type type; // [*] Type of the cells in the column
int first_column; // head of linked list of columns of this type
- //int last_column; // tail of linked list of columns of this type
struct table_user_type *type_def;
};
// FIXME: is it correct to have idx and col_def? idx is sufficient and in fact a duplicity of idx
// idx is used only for initialization and col_def is used in other cases
-struct table_col_info {
- uint idx; // idx is a pointer to struct table::columns
+struct table_col_instance {
+ uint idx; // idx is a index into struct table::columns
struct table_column *col_def; // this is pointer to the column definition, located in the array struct table::columns
char *cell_content; // content of the cell of the current row
int next_column; // index of next column in linked list of columns of the same type
**/
struct table_template {
struct table_column *columns; // [*] Definition of columns
- int column_count; // [*] Number of columns (calculated by table_init())
- struct table_col_info *column_order; // [*] Order of the columns in the print-out of the table
+ struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table
uint cols_to_output; // [*] Number of columns that are printed
const char *col_delimiter; // [*] Delimiter that is placed between columns
// Back-end used for table formatting and its private data
struct mempool_state pool_state; // State of the pool after the table is initialized, i.e., before
// per-row data have been allocated.
- struct table_col_info *column_order; // [*] Order of the columns in the print-out of the table
+ struct table_col_instance *column_order; // [*] Order of the columns in the print-out of the table
uint cols_to_output; // [*] Number of columns that are printed
const char *col_delimiter; // [*] Delimiter that is placed between columns
uint print_header; // [*] 0 indicates that table header should not be printed
#define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type = COL_TYPE_LAST }
#define TBL_COLUMNS .columns = (struct table_column [])
-#define TBL_COL_ORDER(order) .column_order = (struct table_col_info *) order, .cols_to_output = ARRAY_SIZE(order)
+#define TBL_COL_ORDER(order) .column_order = (struct table_col_instance *) order, .cols_to_output = ARRAY_SIZE(order)
#define TBL_COL_DELIMITER(_delimiter_) .col_delimiter = _delimiter_
#define TBL_COL(_idx) { .idx = _idx, .output_type = -1, .next_column = -1 }
#define TBL_COL_FMT(_idx, _fmt) { .idx = _idx, .output_type = -1, .next_column = -1, .fmt = _fmt }
#define TBL_OUTPUT_BLOCKLINE .formatter = &table_fmt_blockline
#define TBL_OUTPUT_MACHINE_READABLE .formatter = &table_fmt_machine_readable
-#define TBL_COL_ITER_START(_tbl, _colidx, _var, _idxval) { struct table_col_info *_var = NULL; int _idxval = _tbl->columns[_colidx].first_column; \
+#define TBL_COL_ITER_START(_tbl, _colidx, _var, _idxval) { struct table_col_instance *_var = NULL; int _idxval = _tbl->columns[_colidx].first_column; \
for(_idxval = _tbl->columns[_colidx].first_column, _var = _tbl->column_order + _idxval; _idxval != -1; _idxval = _tbl->column_order[_idxval].next_column, _var = _tbl->column_order + _idxval)
#define TBL_COL_ITER_END }
* Creates a new table from a table template. The template should already contain
* the definitions of columns.
**/
-struct table *table_init(struct table_template *tbl_template);
+struct table *table_init(const struct table_template *tbl_template);
/** Destroy a table definition, freeing all memory used by it. **/
void table_cleanup(struct table *tbl);
void (*table_end)(struct table *tbl); // [*] table_end callback (optional)
bool (*process_option)(struct table *tbl, const char *key, const char *value, const char **err);
// [*] Process table option and possibly return an error message (optional)
- const char *formats[];
};
/** Standard formatter for human-readable output. **/