static void table_set_all_inst_content(struct table *tbl, int col_templ, const char *col_content)
{
TBL_COL_ITER_START(tbl, col_templ, curr_col_ptr, curr_col) {
- //if( override == 0 ) {
- //die("Error while setting content of all cells of a single type column, cell format should not be overriden.");
- //}
curr_col_ptr->cell_content = col_content;
} TBL_COL_ITER_END
}
va_end(args);
}
-#define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
- {\
- enum xtype_fmt fmt = tbl->columns[col].fmt;\
- 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_, _override) void table_col_##_name_##_fmt(struct table *tbl, int col, enum xtype_fmt fmt, _type_ val) \
- {\
- ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
- ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type_def);\
- tbl->last_printed_col = col;\
- tbl->row_printing_started = 1;\
- const char *cell_content = NULL;\
- if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\
- else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool); \
- table_set_all_inst_content(tbl, col, cell_content);\
- }
-
-#define TABLE_COL_BODIES(_name_, _type_, _typeconst_, _override) TABLE_COL(_name_, _type_, _typeconst_); \
- TABLE_COL_STR(_name_, _type_, _typeconst_);\
- TABLE_COL_FMT(_name_, _type_, _typeconst_, _override);
-
-TABLE_COL_BODIES(int, int, COL_TYPE_INT, 0)
-TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT, 0)
-TABLE_COL_BODIES(str, const char *, COL_TYPE_STR, 1)
-TABLE_COL_BODIES(intmax, intmax_t, COL_TYPE_INTMAX, 0)
-TABLE_COL_BODIES(uintmax, uintmax_t, COL_TYPE_UINTMAX, 0)
-TABLE_COL_BODIES(s64, s64, COL_TYPE_S64, 0)
-TABLE_COL_BODIES(u64, u64, COL_TYPE_U64, 0)
-TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE, 0)
-//TABLE_COL_BODIES(bool, bool, COL_TYPE_BOOL, 0)
-
-// column type double is a special case
-//TABLE_COL(double, double, COL_TYPE_DOUBLE);
-//TABLE_COL_STR(double, double, COL_TYPE_DOUBLE);
+TABLE_COL_BODIES(int, int, COL_TYPE_INT)
+TABLE_COL_BODIES(uint, uint, COL_TYPE_UINT)
+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)
+TABLE_COL_BODIES(double, double, COL_TYPE_DOUBLE)
TABLE_COL(bool, bool, COL_TYPE_BOOL)
TABLE_COL_STR(bool, bool, COL_TYPE_BOOL)
-TABLE_COL_FMT(bool, bool, COL_TYPE_BOOL, 0)
+TABLE_COL_FMT(bool, bool, COL_TYPE_BOOL)
#undef TABLE_COL
#undef TABLE_COL_FMT
#undef TABLE_COL_STR
#undef TABLE_COL_BODIES
-
void table_reset_row(struct table *tbl)
{
for(uint i = 0; i < tbl->cols_to_output; i++) {
#define TBL_COL_DOUBLE(_name, _width, _prec) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_DOUBLE, TBL_COL_LIST_INIT }
#define TBL_COL_BOOL(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_BOOL, TBL_COL_LIST_INIT }
#define TBL_COL_ANY(_name, _width) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = COL_TYPE_ANY, TBL_COL_LIST_INIT }
+#define TBL_COL_CUSTOM(_name, _width, _xtype) { .name = _name, .width = _width, .fmt = XTYPE_FMT_DEFAULT, .type_def = _xtype, TBL_COL_LIST_INIT }
#define TBL_COL_STR_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_STR, TBL_COL_LIST_INIT }
#define TBL_COL_INT_FMT(_name, _width, _fmt) { .name = _name, .width = _width, .fmt = _fmt, .type_def = COL_TYPE_INT, TBL_COL_LIST_INIT }
TABLE_COL_PROTO(u64, u64);
TABLE_COL_PROTO(bool, bool);
-#undef TABLE_COL_PROTO
+/** macros that enables easy definitions of bodies of table_col_<something> functions **/
+
+#define TABLE_COL(_name_, _type_, _typeconst_) void table_col_##_name_(struct table *tbl, int col, _type_ val)\
+ {\
+ enum xtype_fmt fmt = tbl->columns[col].fmt;\
+ 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, enum xtype_fmt fmt, _type_ val) \
+ {\
+ ASSERT_MSG(col < tbl->column_count && col >= 0, "Table column %d does not exist.", col);\
+ ASSERT(tbl->columns[col].type_def == COL_TYPE_ANY || _typeconst_ == tbl->columns[col].type_def);\
+ tbl->last_printed_col = col;\
+ tbl->row_printing_started = 1;\
+ const char *cell_content = NULL;\
+ if(tbl->columns[col].type_def != COL_TYPE_ANY) cell_content = tbl->columns[col].type_def->format(&val, fmt, tbl->pool);\
+ else cell_content = (_typeconst_)->format(&val, fmt, tbl->pool);\
+ table_set_all_inst_content(tbl, col, cell_content);\
+ }
+
+#define TABLE_COL_BODIES(_name_, _type_, _typeconst_) TABLE_COL(_name_, _type_, _typeconst_); \
+ TABLE_COL_STR(_name_, _type_, _typeconst_);\
+ TABLE_COL_FMT(_name_, _type_, _typeconst_);
+
+// FIXME: the next line was removed, the question is whether it should
+// be there currently it is impossible to undef the macro.
+// #undef TABLE_COL_PROTO
/**
* Set a particular cell of the current row to a string formatted
#include <ucw/xtypes.h>
#include <errno.h>
#include <stdlib.h>
+#include <inttypes.h>
#define XTYPE_NUM_FORMAT(_type, _fmt, _typename) static const char *xt_##_typename##_format(void *src, u32 fmt UNUSED, struct mempool *pool) \
{\
XTYPE_NUM_STRUCT(_type, _typename)
XTYPE_NUM_DEF(int, "%d", int)
-XTYPE_NUM_DEF(s64, PRId64, s64)
+XTYPE_NUM_DEF(s64, "%" PRId64, s64)
XTYPE_NUM_DEF(intmax_t, "%jd", intmax)
XTYPE_NUM_DEF(uint, "%u", uint)
-XTYPE_NUM_DEF(u64, PRIu64, u64)
+XTYPE_NUM_DEF(u64, "%" PRIu64, u64)
XTYPE_NUM_DEF(uintmax_t, "%ju", uintmax)
/* double */
.format = xt_bool_format,
};
-
/* str */
-static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool) // (struct table *tbl, int col, enum xtype_fmt fmt, bool val)
+
+static const char *xt_str_format(void *src, u32 fmt UNUSED, struct mempool *pool)
{
return mp_printf(pool, "%s", *((char **) src));
}