]> mj.ucw.cz Git - libucw.git/commitdiff
Table: update of column initialization macros
authorRobert Kessl <kesslr@centrum.cz>
Tue, 3 Jun 2014 08:22:20 +0000 (10:22 +0200)
committerRobert Kessl <kesslr@centrum.cz>
Tue, 3 Jun 2014 08:22:20 +0000 (10:22 +0200)
ucw/table-test.c
ucw/table.c
ucw/table.h

index b033720838e5d83964660057600b85051e82e4c5..c3522cc89dcc8db80b4cb0132eddca59a7686053 100644 (file)
@@ -17,11 +17,11 @@ static uint test_column_order[] = { test_col3_bool, test_col4_double, test_col2_
 
 static struct table test_tbl = {
   TBL_COLUMNS {
-    TBL_COL_STR(test, col0_str, 20),
-    TBL_COL_INT(test, col1_int, 8),
-    TBL_COL_UINT(test, col2_uint, 9),
-    TBL_COL_BOOL(test, col3_bool, 9),
-    TBL_COL_DOUBLE(test, 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("col3_bool", 9),
+    [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
     TBL_COL_END
   },
   TBL_COL_ORDER(test_column_order),
@@ -35,9 +35,9 @@ enum test_default_order_cols {
 
 static struct table test_default_order_tbl = {
   TBL_COLUMNS {
-    TBL_COL_INT(test_default_order, col0_int, 8),
-    TBL_COL_INT(test_default_order, col1_int, 9),
-    TBL_COL_INT(test_default_order, col2_int, 9),
+    [test_default_order_col0_int] = TBL_COL_INT("col0_int", 8),
+    [test_default_order_col1_int] = TBL_COL_INT("col1_int", 9),
+    [test_default_order_col2_int] = TBL_COL_INT("col2_int", 9),
     TBL_COL_END
   },
   TBL_OUTPUT_HUMAN_READABLE,
@@ -46,7 +46,9 @@ static struct table test_default_order_tbl = {
 
 static void do_default_order_test(struct fastbuf *out)
 {
-  table_init(&test_default_order_tbl, out);
+  table_init(&test_default_order_tbl);
+  test_default_order_tbl.out = out;
+
   table_start(&test_default_order_tbl);
 
   table_set_int(&test_default_order_tbl, test_default_order_col0_int, 0);
@@ -152,7 +154,8 @@ int main(int argc UNUSED, char **argv)
   struct fastbuf *out;
   out = bfdopen_shared(1, 4096);
 
-  table_init(&test_tbl, out);
+  table_init(&test_tbl);
+  test_tbl.out = out;
 
   process_command_line_opts(argv, &test_tbl);
 
index a21f08aaba4055968001263b51485e00948edf8a..2549eec8d17bd326accafc3115db24a371e079af 100644 (file)
 
 /*** Management of tables ***/
 
-void table_init(struct table *tbl, struct fastbuf *out)
+void table_init(struct table *tbl)
 {
-  tbl->out = out;
-
   int col_count = 0; // count the number of columns in the struct table
 
   for(;;) {
@@ -64,6 +62,8 @@ void table_start(struct table *tbl)
   tbl->last_printed_col = -1;
   tbl->row_printing_started = 0;
 
+  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);
   }
@@ -506,11 +506,11 @@ static uint test_column_order[] = {test_col3_bool, test_col4_double, test_col2_u
 
 static struct table test_tbl = {
   TBL_COLUMNS {
-    TBL_COL_STR(test, col0_str, 20),
-    TBL_COL_INT(test, col1_int, 8),
-    TBL_COL_UINT(test, col2_uint, 9),
-    TBL_COL_BOOL(test, col3_bool, 9),
-    TBL_COL_DOUBLE(test, 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("col3_bool", 9),
+    [test_col4_double] = TBL_COL_DOUBLE("col4_double", 11, 2),
     TBL_COL_END
   },
   TBL_COL_ORDER(test_column_order),
@@ -546,7 +546,10 @@ static void do_print1(struct table *test_tbl)
 
 static void test_simple1(struct fastbuf *out)
 {
-  table_init(&test_tbl, out);
+  table_init(&test_tbl);
+
+  test_tbl.out = out;
+
   // print table with header
   table_col_order_by_name(&test_tbl, "col3_bool");
   table_start(&test_tbl);
@@ -602,8 +605,8 @@ static uint test_any_column_order[] = { test_any_col0_int, test_any_col1_any };
 
 static struct table test_any_tbl = {
   TBL_COLUMNS {
-    TBL_COL_INT(test_any, col0_int, 8),
-    TBL_COL_ANY(test_any, col1_any, 9),
+    [test_any_col0_int] = TBL_COL_INT("col0_int", 8),
+    [test_any_col1_any] = TBL_COL_ANY("col1_any", 9),
     TBL_COL_END
   },
   TBL_COL_ORDER(test_any_column_order),
@@ -614,7 +617,9 @@ static struct table test_any_tbl = {
 
 static void test_any_type(struct fastbuf *out)
 {
-  table_init(&test_any_tbl, out);
+  table_init(&test_any_tbl);
+  test_any_tbl.out = out;
+
   table_start(&test_any_tbl);
 
   table_set_int(&test_any_tbl, test_any_col0_int, -10);
index 62340fe6572619debbcc431a8954ebbce9ab7eea..d6888e10e8053ef329dccc325a7deecf06f28f62 100644 (file)
@@ -22,23 +22,23 @@ enum column_type {
   COL_TYPE_LAST
 };
 
-#define TBL_COL_STR(_enum_prefix, _name, _width)            [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%s", .type = COL_TYPE_STR }
-#define TBL_COL_INT(_enum_prefix, _name, _width)            [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%d", .type = COL_TYPE_INT }
-#define TBL_COL_UINT(_enum_prefix, _name, _width)           [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%u", .type = COL_TYPE_UINT }
-#define TBL_COL_INTMAX(_enum_prefix, _name, _width)         [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%jd", .type = COL_TYPE_INTMAX }
-#define TBL_COL_UINTMAX(_enum_prefix, _name, _width)        [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%ju", .type = COL_TYPE_UINTMAX }
-#define TBL_COL_HEXUINT(_enum_prefix, _name, _width)        [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "0x%x", .type = COL_TYPE_UINT }
-#define TBL_COL_DOUBLE(_enum_prefix, _name, _width, _prec)  [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%." #_prec "lf", .type = COL_TYPE_DOUBLE }
-#define TBL_COL_BOOL(_enum_prefix, _name, _width)           [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = "%s", .type = COL_TYPE_BOOL }
-#define TBL_COL_ANY(_enum_prefix, _name, _width)            [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = 0, .type = COL_TYPE_ANY }
-
-#define TBL_COL_STR_FMT(_enum_prefix, _name, _width, _fmt)            [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_STR }
-#define TBL_COL_INT_FMT(_enum_prefix, _name, _width, _fmt)            [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INT }
-#define TBL_COL_UINT_FMT(_enum_prefix, _name, _width, _fmt)           [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
-#define TBL_COL_INTMAX_FMT(_enum_prefix, _name, _width, _fmt)         [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INTMAX }
-#define TBL_COL_UINTMAX_FMT(_enum_prefix, _name, _width, _fmt)        [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINTMAX }
-#define TBL_COL_HEXUINT_FMT(_enum_prefix, _name, _width, _fmt)        [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
-#define TBL_COL_BOOL_FMT(_enum_prefix, _name, _width, _fmt)           [_enum_prefix##_##_name] = { .name = #_name, .width = _width, .fmt = _fmt, .type = COL_TYPE_BOOL }
+#define TBL_COL_STR(_name, _width)            { .name = _name, .width = _width, .fmt = "%s", .type = COL_TYPE_STR }
+#define TBL_COL_INT(_name, _width)            { .name = _name, .width = _width, .fmt = "%d", .type = COL_TYPE_INT }
+#define TBL_COL_UINT(_name, _width)           { .name = _name, .width = _width, .fmt = "%u", .type = COL_TYPE_UINT }
+#define TBL_COL_INTMAX(_name, _width)         { .name = _name, .width = _width, .fmt = "%jd", .type = COL_TYPE_INTMAX }
+#define TBL_COL_UINTMAX(_name, _width)        { .name = _name, .width = _width, .fmt = "%ju", .type = COL_TYPE_UINTMAX }
+#define TBL_COL_HEXUINT(_name, _width)        { .name = _name, .width = _width, .fmt = "0x%x", .type = COL_TYPE_UINT }
+#define TBL_COL_DOUBLE(_name, _width, _prec)  { .name = _name, .width = _width, .fmt = "%." #_prec "lf", .type = COL_TYPE_DOUBLE }
+#define TBL_COL_BOOL(_name, _width)           { .name = _name, .width = _width, .fmt = "%s", .type = COL_TYPE_BOOL }
+#define TBL_COL_ANY(_name, _width)            { .name = _name, .width = _width, .fmt = 0, .type = COL_TYPE_ANY }
+
+#define TBL_COL_STR_FMT(_name, _width, _fmt)            { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_STR }
+#define TBL_COL_INT_FMT(_name, _width, _fmt)            { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INT }
+#define TBL_COL_UINT_FMT(_name, _width, _fmt)           { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
+#define TBL_COL_INTMAX_FMT(_name, _width, _fmt)         { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_INTMAX }
+#define TBL_COL_UINTMAX_FMT(_name, _width, _fmt)        { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINTMAX }
+#define TBL_COL_HEXUINT_FMT(_name, _width, _fmt)        { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_UINT }
+#define TBL_COL_BOOL_FMT(_name, _width, _fmt)           { .name = _name, .width = _width, .fmt = _fmt, .type = COL_TYPE_BOOL }
 
 #define TBL_COL_END { .name = 0, .width = 0, .fmt = 0, .type = COL_TYPE_LAST }
 
@@ -173,7 +173,7 @@ struct table {
  * about deallocation does not make much sense, the fastbuf is definitely not copied, only
  * a pointer to it.
  **/
-void table_init(struct table *tbl, struct fastbuf *out);
+void table_init(struct table *tbl);
 void table_cleanup(struct table *tbl);
 
 /**