From: Robert Kessl Date: Tue, 3 Jun 2014 08:22:20 +0000 (+0200) Subject: Table: update of column initialization macros X-Git-Tag: v6.0~13^2~4 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=407df89d1a31056c3c00a40968d3c3092eef9f04;p=libucw.git Table: update of column initialization macros --- diff --git a/ucw/table-test.c b/ucw/table-test.c index b0337208..c3522cc8 100644 --- a/ucw/table-test.c +++ b/ucw/table-test.c @@ -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); diff --git a/ucw/table.c b/ucw/table.c index a21f08aa..2549eec8 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -14,10 +14,8 @@ /*** 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); diff --git a/ucw/table.h b/ucw/table.h index 62340fe6..d6888e10 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -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); /**