new_inst->column_order[i].cell_content = NULL;
int col_idx = new_inst->column_order[i].idx;
new_inst->column_order[i].col_def = new_inst->columns + col_idx;
- new_inst->column_order[i].output_type = tbl_template->columns[col_idx].fmt;
+ new_inst->column_order[i].fmt = tbl_template->columns[col_idx].fmt;
}
new_inst->cols_to_output = tbl_template->cols_to_output;
}
new_inst->col_delimiter = tbl_template->col_delimiter;
- new_inst->print_header = 1;
+ new_inst->print_header = true;
new_inst->out = 0;
new_inst->last_printed_col = -1;
- new_inst->row_printing_started = 0;
+ new_inst->row_printing_started = false;
new_inst->col_out = -1;
new_inst->formatter = tbl_template->formatter;
new_inst->data = NULL;
tbl->formatter = &table_fmt_human_readable;
}
- tbl->print_header = 1; // by default, print header
+ tbl->print_header = true; // by default, print header
return tbl;
}
void table_start(struct table *tbl, struct fastbuf *out)
{
tbl->last_printed_col = -1;
- tbl->row_printing_started = 0;
+ tbl->row_printing_started = false;
tbl->out = out;
ASSERT_MSG(tbl->out, "Output fastbuf not specified.");
void table_end(struct table *tbl)
{
tbl->last_printed_col = -1;
- tbl->row_printing_started = 0;
+ tbl->row_printing_started = false;
mp_restore(tbl->pool, &tbl->pool_state);
tbl->column_order[i].idx = col_idx;
tbl->column_order[i].col_def = tbl->columns + col_idx;
tbl->column_order[i].cell_content = NULL;
- tbl->column_order[i].output_type = tbl->columns[col_idx].fmt;
+ tbl->column_order[i].fmt = tbl->columns[col_idx].fmt;
}
table_update_ll(tbl);
}
return left_br;
}
-/**
- * Setting options for basic table types (as defined in table.h)
- **/
-int table_set_col_opt_default(struct table *tbl, int col_idx, const char *col_arg, char **err)
+const char *table_set_col_opt(struct table *tbl, uint col_idx, const char *col_opt)
{
const struct table_column *col_def = tbl->column_order[col_idx].col_def;
+ if(col_def && col_def->set_col_opt && col_def->set_col_opt != table_set_col_opt) {
+ return col_def->set_col_opt(tbl, col_idx, col_opt);
+ }
- if(col_def->type_def == &xt_double) {
- uint precision = 0;
- const char *tmp_err = str_to_uint(&precision, col_arg, NULL, 0);
- if(tmp_err) {
- *err = mp_printf(tbl->pool, "An error occured while parsing precision: %s.", tmp_err);
- return false;
- }
- tbl->column_order[col_idx].output_type = precision; // FIXME: shift the value of precision
- return true;
+ if(col_def && col_def->type_def && col_def->type_def->parse_fmt) {
+ uint fmt = 0;
+ const char *tmp_err = col_def->type_def->parse_fmt(col_opt, &fmt, tbl->pool);
+ if(tmp_err) return tmp_err;
+ tbl->column_order[col_idx].fmt = fmt;
+ return NULL;
}
- *err = mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_arg, col_idx);
- return false;
+ return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_idx);
}
/**
}
tbl->column_order[curr_col_idx].col_def = tbl->columns + col_idx;
tbl->column_order[curr_col_idx].idx = col_idx;
- tbl->column_order[curr_col_idx].cell_content = NULL;
- tbl->column_order[curr_col_idx].output_type = tbl->columns[col_idx].fmt;
- if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_instance_option) {
- char *err = NULL;
- tbl->columns[col_idx].set_col_instance_option(tbl, curr_col_idx, arg, &err);
+ tbl->column_order[curr_col_idx].fmt = tbl->columns[col_idx].fmt;
+ if(tbl->columns[col_idx].type_def && tbl->columns[col_idx].set_col_opt) {
+ const char *err = NULL;
+ err = tbl->columns[col_idx].set_col_opt(tbl, curr_col_idx, arg);
if(err) return mp_printf(tbl->pool, "Error occured while setting column option: %s.", err);
}
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || expected_type == tbl->columns[col].type_def);
tbl->last_printed_col = col;
- tbl->row_printing_started = 1;
+ tbl->row_printing_started = true;
TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
- enum xtype_fmt fmt = curr_col->output_type;
+ enum xtype_fmt fmt = curr_col->fmt;
curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
} TBL_COL_ITER_END
}
{
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
tbl->last_printed_col = col;
- tbl->row_printing_started = 1;
+ tbl->row_printing_started = true;
va_list args;
va_start(args, fmt);
char *cell_content = mp_vprintf(tbl->pool, fmt, args);
}
mp_restore(tbl->pool, &tbl->pool_state);
tbl->last_printed_col = -1;
- tbl->row_printing_started = 0;
+ tbl->row_printing_started = false;
}
void table_end_row(struct table *tbl)
{
ASSERT(tbl->formatter->row_output);
- if(tbl->row_printing_started == 0) return;
+ if(tbl->row_printing_started == false) return;
tbl->formatter->row_output(tbl);
table_reset_row(tbl);
}
// Options with no value
if(value == NULL || (value != NULL && strlen(value) == 0)) {
if(strcmp(key, "noheader") == 0) {
- tbl->print_header = 0;
+ tbl->print_header = false;
return NULL;
}
}
// Options with a value
if(value) {
if(strcmp(key, "header") == 0) {
- if(value[1] != 0)
- return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
- uint tmp = value[0] - '0';
- if(tmp > 1)
+ bool tmp;
+ const char *err = xt_bool.parse(value, &tmp, tbl->pool);
+ if(err)
return mp_printf(tbl->pool, "Invalid header parameter: '%s' has invalid value: '%s'.", key, value);
+
tbl->print_header = tmp;
+
return NULL;
} else if(strcmp(key, "cols") == 0) {
return table_set_col_order_by_name(tbl, value);
const char *err = xtype_parse_fmt(NULL, value, &fmt, tbl->pool);
if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
for(uint i = 0; i < tbl->cols_to_output; i++) {
- tbl->column_order[i].output_type = fmt;
+ tbl->column_order[i].fmt = fmt;
}
return NULL;
} else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
const char *err = xtype_parse_fmt(NULL, key, &fmt, tbl->pool);
if(err) return mp_printf(tbl->pool, "Invalid cell format: '%s'.", err);
for(uint i = 0; i < tbl->cols_to_output; i++) {
- tbl->column_order[i].output_type = fmt;
+ tbl->column_order[i].fmt = fmt;
}
return NULL;
} else if(strcmp(key, "col-delim") == 0) {
tbl->col_delimiter = " ";
}
- if(tbl->print_header != 0) {
+ if(tbl->print_header != false) {
table_write_header(tbl);
}
}
tbl->col_delimiter = "\t";
}
- if(tbl->print_header != 0 && tbl->cols_to_output > 0) {
+ if(tbl->print_header != false && tbl->cols_to_output > 0) {
bputs(tbl->out, tbl->column_order[0].col_def->name);
for(uint i = 1; i < tbl->cols_to_output; i++) {
bputs(tbl->out, tbl->col_delimiter);
[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_COL3_BOOL] = TBL_COL_BOOL_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
[TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
TBL_COL_END
},
table_end(tbl);
// this also tests whether there is need to call table_set_col_order_by_name after table_end was called
- tbl->print_header = 0;
+ tbl->print_header = false;
table_start(tbl, out);
do_print1(tbl);
table_end(tbl);
- tbl->print_header = 1;
+ tbl->print_header = true;
table_set_col_order_by_name(tbl, "col3_bool");
table_start(tbl, out);