]> mj.ucw.cz Git - libucw.git/commitdiff
tableprinter: update of TABLE_COL_BODIES macro, code cleanup
authorRobert Kessl <kesslr@centrum.cz>
Tue, 15 Jul 2014 11:06:24 +0000 (13:06 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Tue, 15 Jul 2014 11:06:24 +0000 (13:06 +0200)
 - TABLE_COL_BODIES macros moved to table.h
 - removed undef of TABLE_COL_PROTO in table.h
 - code cleanup
 - added xtypes.h to header list in Makefile

ucw/Makefile
ucw/table.c
ucw/table.h
ucw/xtypes-basic.c

index 6bc4f26fe68c760a0e90f626ed6432113b67ea2c..9a716946a51f667ba71887fd4e9beae24a5879a2 100644 (file)
@@ -69,7 +69,8 @@ LIBUCW_MAIN_INCLUDES= \
        signames.h \
        sighandler.h \
        opt.h \
-       table.h table-types.h
+       table.h table-types.h \
+       xtypes.h
 
 ifdef CONFIG_UCW_THREADS
 # Some modules require threading
index c5d59a5830feba5ce775664861361d3c904f2342..4896b6f310388913dc4026ccb34791cf0252c3fd 100644 (file)
@@ -311,9 +311,6 @@ const char * table_set_col_order_by_name(struct table *tbl, const char *col_orde
 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
 }
@@ -330,58 +327,24 @@ void table_col_printf(struct table *tbl, int col, const char *fmt, ...)
   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++) {
index 89684c75c75c89dce5e4f40c8fe1470341bb468a..49b7fbb30e90b2b3003418b11e300a6f95a8481a 100644 (file)
@@ -197,6 +197,7 @@ struct table {
 #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 }
@@ -284,7 +285,39 @@ TABLE_COL_PROTO(s64, s64);
 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
index 0438045709dcb1084f39e57d482f8957a21fbe29..cf3010b5f3d5afafae4471d9af2094a1fed45d8f 100644 (file)
@@ -13,6 +13,7 @@
 #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 */
@@ -127,9 +128,9 @@ const struct xtype xt_bool = {
   .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));
 }