#include <ucw/stkstring.h>
#include <ucw/gary.h>
#include <ucw/table.h>
+#include <ucw/strtonum.h>
#include <stdlib.h>
+#include <stdio.h>
+
+/* Forward declarations */
+
+static void table_update_ll(struct table *tbl);
/*** Management of tables ***/
-void table_init(struct table *tbl)
+static struct table *table_make_instance(const struct table_template *tbl_template)
{
- int col_count = 0; // count the number of columns in the struct table
+ struct mempool *pool = mp_new(4096);
+ struct table *new_inst = mp_alloc_zero(pool, sizeof(struct table));
+
+ new_inst->pool = pool;
+ // initialize column definitions
+ uint col_count = 0; // count the number of columns in the struct table
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)
+ if(tbl_template->columns[col_count].name == NULL &&
+ tbl_template->columns[col_count].width == 0 &&
+ tbl_template->columns[col_count].type_def == COL_TYPE_ANY)
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].width != 0);
- ASSERT(tbl->columns[col_count].type < COL_TYPE_LAST);
+ ASSERT(tbl_template->columns[col_count].name != NULL);
+ ASSERT(tbl_template->columns[col_count].width != 0);
+
col_count++;
}
- tbl->pool = mp_new(4096);
+ new_inst->column_count = col_count;
+
+ new_inst->columns = tbl_template->columns;
+ new_inst->ll_headers = mp_alloc(new_inst->pool, sizeof(int) * col_count);
+ for(uint i = 0; i < col_count; i++) {
+ new_inst->ll_headers[i] = -1;
+ }
- tbl->column_count = col_count;
+ // initialize column_order
+ if(tbl_template->column_order) {
+ 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;
+ 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].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 = true;
+ new_inst->out = 0;
+ new_inst->last_printed_col = -1;
+ new_inst->row_printing_started = false;
+ new_inst->col_out = -1;
+ new_inst->formatter = tbl_template->formatter;
+ new_inst->data = NULL;
+ return new_inst;
+}
+
+struct table *table_init(const struct table_template *tbl_template)
+{
+ struct table *tbl = table_make_instance(tbl_template);
if(!tbl->formatter) {
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_cleanup(struct table *tbl)
{
mp_delete(tbl->pool);
- memset(tbl, 0, sizeof(struct table));
}
// 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;
}
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.");
- if(!tbl->col_str_ptrs) {
- tbl->col_str_ptrs = mp_alloc_zero(tbl->pool, sizeof(char *) * tbl->column_count);
- }
-
if(tbl->column_order == NULL) table_make_default_column_order(tbl);
-
+ else {
+ // update linked lists
+ table_update_ll(tbl);
+ }
if(tbl->formatter->table_start != NULL) tbl->formatter->table_start(tbl);
mp_save(tbl->pool, &tbl->pool_state);
ASSERT_MSG(tbl->col_delimiter, "In-between column delimiter not specified.");
- ASSERT_MSG(tbl->append_delimiter, "Append delimiter 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);
/*** Configuration ***/
-void table_set_formatter(struct table *tbl, struct table_formatter *fmt)
+void table_set_formatter(struct table *tbl, const struct table_formatter *fmt)
{
tbl->formatter = fmt;
}
return tmp;
}
+static void table_update_ll(struct table *tbl)
+{
+ int cols_to_output = tbl->cols_to_output;
+
+ for(int i = 0; i < tbl->column_count; i++) {
+ tbl->ll_headers[i] = -1;
+ }
+
+ for(int i = 0; i < cols_to_output; i++) {
+ int idx = tbl->column_order[i].idx;
+ tbl->column_order[i].col_def = tbl->columns + idx;
+ }
+
+ for(int i = 0; i < cols_to_output; i++) {
+ int col_def_idx = tbl->column_order[i].idx;
+ int first = tbl->ll_headers[col_def_idx];
+ tbl->ll_headers[col_def_idx] = i;
+ tbl->column_order[i].next_column = first;
+ }
+}
+
void table_set_col_order(struct table *tbl, int *col_order, int 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);
+ 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);
}
- tbl->column_order = col_order;
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_idx = col_order[i];
+ 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].fmt = tbl->columns[col_idx].fmt;
+ }
+ table_update_ll(tbl);
+}
+
+bool table_col_is_printed(struct table *tbl, uint col_idx)
+{
+ if(tbl->ll_headers[col_idx] == -1) return 0;
+
+ return 1;
+}
+
+static char * table_parse_col_arg(char *col_def)
+{
+ // FIXME: should be switched to str_sepsplit
+ char * left_br = strchr(col_def, '[');
+ if(left_br == NULL) return NULL;
+ *left_br = 0;
+ left_br++;
+ char *right_br = strchr(left_br, ']');
+ *right_br = 0;
+ return left_br;
+}
+
+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 && 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;
+ }
+
+ return mp_printf(tbl->pool, "Invalid column format option: '%s' for column %d.", col_opt, col_idx);
}
/**
const char * table_set_col_order_by_name(struct table *tbl, const char *col_order_str)
{
if(col_order_str[0] == '*') {
- tbl->column_order = mp_alloc(tbl->pool, sizeof(int) * tbl->column_count);
- tbl->cols_to_output = tbl->column_count;
- for(uint i = 0; i < tbl->cols_to_output; i++) tbl->column_order[i] = i;
-
+ table_make_default_column_order(tbl);
return NULL;
}
}
}
- int *col_order_int = mp_alloc_zero(tbl->pool, sizeof(int) * col_count);
- int curr_col_order_int = 0;
- const char *name_start = tmp_col_order;
+ tbl->cols_to_output = 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;
while(name_start) {
char *next = strchr(name_start, ',');
if(next) {
*next++ = 0;
}
- int idx = table_get_col_idx(tbl, name_start);
- if(idx == -1) {
- return mp_printf(tbl->pool, "Unknown table column '%s'", name_start);
+ char *arg = table_parse_col_arg(name_start); // this sets 0 on the '['
+ int col_idx = table_get_col_idx(tbl, name_start);
+
+ if(col_idx == -1) {
+ return mp_printf(tbl->pool, "Unknown table column '%s', possible column names are: %s.", name_start, table_get_col_list(tbl));
+ }
+ 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].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);
}
- col_order_int[curr_col_order_int++] = idx;
name_start = next;
+ curr_col_idx++;
}
- tbl->column_order = col_order_int;
- tbl->cols_to_output = curr_col_order_int;
+ table_update_ll(tbl);
+
return NULL;
}
/*** Table cells ***/
-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;
- tbl->row_printing_started = 1;
- va_list args;
- va_start(args, fmt);
- tbl->col_str_ptrs[col] = mp_vprintf(tbl->pool, fmt, args);
- va_end(args);
-}
-
-static const char *table_col_default_fmts[] = {
- [COL_TYPE_STR] = "%s",
- [COL_TYPE_INT] = "%d",
- [COL_TYPE_INTMAX] = "%jd",
- [COL_TYPE_UINT] = "%u",
- [COL_TYPE_UINTMAX] = "%ju",
- [COL_TYPE_BOOL] = "%d",
- [COL_TYPE_DOUBLE] = "%.2lf",
- [COL_TYPE_ANY] = NULL,
- [COL_TYPE_LAST] = NULL
-};
-
-#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_col_default_fmts[_typeconst_];\
- }\
- table_col_##_name_##_fmt(tbl, col, fmt, 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_col_##_name_(tbl, col, 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);\
- ASSERT(fmt != NULL);\
- tbl->last_printed_col = col;\
- tbl->row_printing_started = 1;\
- tbl->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, 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)
-TABLE_COL_BODIES(s64, s64, COL_TYPE_S64)
-TABLE_COL_BODIES(u64, u64, COL_TYPE_U64)
-#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)
+static void table_set_all_inst_content(struct table *tbl, int col_templ, const char *col_content)
{
- table_col_bool_fmt(tbl, col, tbl->columns[col].fmt, val);
+ TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
+ curr_col_ptr->cell_content = col_content;
+ } TBL_COL_ITER_END
}
-void table_col_bool_name(struct table *tbl, const char *col_name, uint val)
-{
- int col = table_get_col_idx(tbl, col_name);
- table_col_bool(tbl, col, val);
-}
-
-void table_col_bool_fmt(struct table *tbl, int col, const char *fmt, uint val)
+void table_col_generic_format(struct table *tbl, int col, void *value, const struct xtype *expected_type)
{
ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
- ASSERT(COL_TYPE_BOOL == tbl->columns[col].type);
-
+ 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->col_str_ptrs[col] = mp_printf(tbl->pool, fmt, val ? "true" : "false");
+ tbl->row_printing_started = true;
+ TBL_COL_ITER_START(tbl, col, curr_col, curr_col_idx) {
+ enum xtype_fmt fmt = curr_col->fmt;
+ curr_col->cell_content = expected_type->format(value, fmt, tbl->pool);
+ } TBL_COL_ITER_END
}
-#define TABLE_APPEND(_name_, _type_, _typeconst_) void table_append_##_name_(struct table *tbl, _type_ val) \
- {\
- ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);\
- ASSERT(_typeconst_ == tbl->columns[tbl->last_printed_col].type);\
- int col = tbl->last_printed_col;\
- mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);\
- tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val);\
- }
-
-TABLE_APPEND(int, int, COL_TYPE_INT)
-TABLE_APPEND(uint, uint, COL_TYPE_UINT)
-TABLE_APPEND(double, double, COL_TYPE_DOUBLE)
-TABLE_APPEND(str, const char *, COL_TYPE_STR)
-TABLE_APPEND(intmax, intmax_t, COL_TYPE_INTMAX)
-TABLE_APPEND(uintmax, uintmax_t, COL_TYPE_UINTMAX)
-TABLE_APPEND(u64, u64, COL_TYPE_U64)
-#undef TABLE_APPEND
-
-void table_append_bool(struct table *tbl, int val)
-{
- ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
- ASSERT(COL_TYPE_BOOL == tbl->columns[tbl->last_printed_col].type);
-
- int col = tbl->last_printed_col;
-
- mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
-
- tbl->col_str_ptrs[col] = mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], tbl->columns[col].fmt, val ? "true" : "false");
-}
-
-void table_append_printf(struct table *tbl, const char *fmt, ...)
+void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
{
- ASSERT(tbl->last_printed_col != -1 || tbl->row_printing_started != 0);
- int col = tbl->last_printed_col;
-
+ ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);
+ tbl->last_printed_col = col;
+ tbl->row_printing_started = true;
va_list args;
va_start(args, fmt);
-
- mp_printf_append(tbl->pool, tbl->col_str_ptrs[col], "%s", tbl->append_delimiter);
- tbl->col_str_ptrs[col] = mp_vprintf_append(tbl->pool, tbl->col_str_ptrs[col], fmt, args);
-
+ char *cell_content = mp_vprintf(tbl->pool, fmt, args);
+ table_set_all_inst_content(tbl, col, cell_content);
va_end(args);
}
-void table_clean_row(struct table *tbl)
+TABLE_COL_BODY(int, int)
+TABLE_COL_BODY(uint, uint)
+TABLE_COL_BODY(double, double)
+TABLE_COL_BODY(intmax, intmax_t)
+TABLE_COL_BODY(uintmax, uintmax_t)
+TABLE_COL_BODY(s64, s64)
+TABLE_COL_BODY(u64, u64)
+TABLE_COL_BODY(bool, bool)
+TABLE_COL_BODY(str, const char *)
+
+void table_reset_row(struct table *tbl)
{
- memset(tbl->col_str_ptrs, 0, sizeof(char *) * tbl->column_count);
+ for(uint i = 0; i < tbl->cols_to_output; i++) {
+ tbl->column_order[i].cell_content = NULL;
+ }
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_clean_row(tbl);
+ table_reset_row(tbl);
}
/* Construction of a cell using a fastbuf */
void table_col_fbend(struct table *tbl)
{
- tbl->col_str_ptrs[tbl->col_out] = fbpool_end(&tbl->fb_col_out);
+ char *cell_content = fbpool_end(&tbl->fb_col_out);
+ table_set_all_inst_content(tbl, tbl->col_out, cell_content);
tbl->col_out = -1;
}
// 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, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
- uint tmp = value[0] - '0';
- if(tmp > 1)
- return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s' has invalid value: '%s'.", key, value);
+ 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) {
- const char *err = table_set_col_order_by_name(tbl, value);
- if(err != NULL) {
- return mp_printf(tbl->pool, "%s, possible column names are: %s.", err, table_get_col_list(tbl));
- }
- return NULL;
+ return table_set_col_order_by_name(tbl, value);
} else if(strcmp(key, "fmt") == 0) {
if(strcmp(value, "human") == 0) table_set_formatter(tbl, &table_fmt_human_readable);
else if(strcmp(value, "machine") == 0) table_set_formatter(tbl, &table_fmt_machine_readable);
else if(strcmp(value, "blockline") == 0) table_set_formatter(tbl, &table_fmt_blockline);
else {
- return "Tableprinter: invalid argument to output-type option.";
+ return "Invalid argument to output-type option.";
+ }
+ return NULL;
+ } else if(strcmp(key, "cells") == 0) {
+ u32 fmt = 0;
+ 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].fmt = fmt;
+ }
+ return NULL;
+ } else if(strcmp(key, "raw") == 0 || strcmp(key, "pretty") == 0) {
+ u32 fmt = 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].fmt = fmt;
}
return NULL;
} else if(strcmp(key, "col-delim") == 0) {
// Formatter options
if(tbl->formatter && tbl->formatter->process_option) {
const char *err = NULL;
- if (tbl->formatter->process_option(tbl, key, value, &err)) {
+ if(tbl->formatter->process_option(tbl, key, value, &err)) {
return err;
}
}
// Unrecognized option
- return mp_printf(tbl->pool, "Tableprinter: invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
+ return mp_printf(tbl->pool, "Invalid option: '%s%s%s'.", key, (value ? ":" : ""), (value ? : ""));
}
const char *table_set_option(struct table *tbl, const char *opt)
static void table_row_human_readable(struct table *tbl)
{
for(uint i = 0; i < tbl->cols_to_output; i++) {
- int col_idx = tbl->column_order[i];
+ const struct table_column *col_def = tbl->column_order[i].col_def;
if(i) {
bputs(tbl->out, tbl->col_delimiter);
}
- int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
- if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
- bprintf(tbl->out, "%*s", col_width, tbl->col_str_ptrs[col_idx]);
+ int col_width = col_def->width & CELL_WIDTH_MASK;
+ if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
+ bprintf(tbl->out, "%*s", col_width, tbl->column_order[i].cell_content);
}
bputc(tbl->out, '\n');
}
static void table_write_header(struct table *tbl)
{
for(uint i = 0; i < tbl->cols_to_output; i++) {
- int col_idx = tbl->column_order[i];
+ const struct table_column *col_def = tbl->column_order[i].col_def;
if(i) {
bputs(tbl->out, tbl->col_delimiter);
}
- int col_width = tbl->columns[col_idx].width & CELL_WIDTH_MASK;
- if(tbl->columns[col_idx].width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
- bprintf(tbl->out, "%*s", col_width, tbl->columns[col_idx].name);
+ int col_width = col_def->width & CELL_WIDTH_MASK;
+ if(col_def->width & CELL_ALIGN_LEFT) col_width = -1 * col_width;
+ bprintf(tbl->out, "%*s", col_width, col_def->name);
}
bputc(tbl->out, '\n');
}
tbl->col_delimiter = " ";
}
- if(tbl->append_delimiter == NULL) {
- tbl->append_delimiter = ",";
- }
-
- if(tbl->print_header != 0) {
+ if(tbl->print_header != false) {
table_write_header(tbl);
}
}
-struct table_formatter table_fmt_human_readable = {
+const struct table_formatter table_fmt_human_readable = {
.row_output = table_row_human_readable,
.table_start = table_start_human_readable,
};
static void table_row_machine_readable(struct table *tbl)
{
for(uint i = 0; i < tbl->cols_to_output; i++) {
- int col_idx = tbl->column_order[i];
if(i) {
bputs(tbl->out, tbl->col_delimiter);
}
- bputs(tbl->out, tbl->col_str_ptrs[col_idx]);
+ bputs(tbl->out, tbl->column_order[i].cell_content);
}
bputc(tbl->out, '\n');
}
static void table_start_machine_readable(struct table *tbl)
{
if(tbl->col_delimiter == NULL) {
- tbl->col_delimiter = ";";
+ tbl->col_delimiter = "\t";
}
- if(tbl->append_delimiter == NULL) {
- tbl->append_delimiter = ",";
- }
-
- if(tbl->print_header != 0) {
- uint col_idx = tbl->column_order[0];
- bputs(tbl->out, tbl->columns[col_idx].name);
+ 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++) {
- col_idx = tbl->column_order[i];
bputs(tbl->out, tbl->col_delimiter);
- bputs(tbl->out, tbl->columns[col_idx].name);
+ bputs(tbl->out, tbl->column_order[i].col_def->name);
}
bputc(tbl->out, '\n');
}
}
-struct table_formatter table_fmt_machine_readable = {
+const struct table_formatter table_fmt_machine_readable = {
.row_output = table_row_machine_readable,
.table_start = table_start_machine_readable,
};
static void table_row_blockline_output(struct table *tbl)
{
for(uint i = 0; i < tbl->cols_to_output; i++) {
- int col_idx = tbl->column_order[i];
- bprintf(tbl->out, "%s: %s\n", tbl->columns[col_idx].name, tbl->col_str_ptrs[col_idx]);
+ const struct table_column *col_def = tbl->column_order[i].col_def;
+ bprintf(tbl->out, "%s: %s\n", col_def->name, tbl->column_order[i].cell_content);
}
bputc(tbl->out, '\n');
}
static void table_start_blockline(struct table *tbl)
{
if(tbl->col_delimiter == NULL) {
- tbl->col_delimiter = " ";
- }
-
- if(tbl->append_delimiter == NULL) {
- tbl->append_delimiter = ",";
+ tbl->col_delimiter = "\n";
}
}
-struct table_formatter table_fmt_blockline = {
+const struct table_formatter table_fmt_blockline = {
.row_output = table_row_blockline_output,
.table_start = table_start_blockline
};
-
-
/*** Tests ***/
#ifdef TEST
#include <stdio.h>
enum test_table_cols {
- test_col0_str, test_col1_int, test_col2_uint, test_col3_bool, test_col4_double
+ TEST_COL0_STR, TEST_COL1_INT, TEST_COL2_UINT, TEST_COL3_BOOL, TEST_COL4_DOUBLE
};
-static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_uint,test_col1_int, 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 test_tbl = {
+static struct table_template test_tbl = {
TBL_COLUMNS {
- [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_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
+ [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_FMT("col3_bool", 9, XTYPE_FMT_PRETTY),
+ [TEST_COL4_DOUBLE] = TBL_COL_DOUBLE("col4_double", 11),
TBL_COL_END
},
TBL_COL_ORDER(test_column_order),
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
- TBL_APPEND_DELIMITER(",")
};
/**
**/
static void do_print1(struct table *test_tbl)
{
- table_col_str(test_tbl, test_col0_str, "sdsdf");
- table_append_str(test_tbl, "aaaaa");
- 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_col_str(test_tbl, TEST_COL0_STR, "sdsdf");
+ 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, true);
+ 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_col_str(test_tbl, test_col0_str, "test");
- table_append_str(test_tbl, "bbbbb");
- 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_col_str(test_tbl, TEST_COL0_STR, "test");
+ 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, false);
+ table_col_printf(test_tbl, TEST_COL4_DOUBLE, "%.2lf", 1.5);
table_end_row(test_tbl);
}
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 = false;
+ table_start(tbl, out);
+ do_print1(tbl);
+ table_end(tbl);
+ tbl->print_header = true;
- 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 {
- test_any_col0_int, test_any_col1_any
+ TEST_ANY_COL0_INT, TEST_ANY_COL1_ANY
};
-static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
+static struct table_col_instance test_any_column_order[] = { TBL_COL(TEST_ANY_COL0_INT), TBL_COL_FMT(TEST_ANY_COL1_ANY, XTYPE_FMT_PRETTY) };
-static struct table test_any_tbl = {
+static struct table_template test_any_tbl = {
TBL_COLUMNS {
- [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
- [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
+ [TEST_ANY_COL0_INT] = TBL_COL_INT("col0_int", 8),
+ [TEST_ANY_COL1_ANY] = TBL_COL_ANY_FMT("col1_any", 9, XTYPE_FMT_PRETTY),
TBL_COL_END
},
TBL_COL_ORDER(test_any_column_order),
- TBL_OUTPUT_HUMAN_READABLE,
+ TBL_FMT_HUMAN_READABLE,
TBL_COL_DELIMITER("\t"),
- TBL_APPEND_DELIMITER(",")
};
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_append_printf(&test_any_tbl, "%d", 20);
- table_append_printf(&test_any_tbl, "%d", 30);
- 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);
+ 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)