From 6badd98747316cb0d82195e8022e30ece202be1d Mon Sep 17 00:00:00 2001 From: Robert Kessl Date: Mon, 21 Jul 2014 09:23:16 +0200 Subject: [PATCH] tableprinter: formatters are now const --- ucw/table-test.c | 9 ++++++++- ucw/table.c | 8 ++++---- ucw/table.h | 14 +++++++------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/ucw/table-test.c b/ucw/table-test.c index c41dbf28..f23c176d 100644 --- a/ucw/table-test.c +++ b/ucw/table-test.c @@ -143,7 +143,12 @@ static bool user_defined_option(struct table *tbl UNUSED, const char *key, const static void test_option_parser(struct table *tbl) { - tbl->formatter->process_option = user_defined_option; + struct table_formatter test_option_fmtr = table_fmt_human_readable; + test_option_fmtr.process_option = user_defined_option; + + const struct table_formatter *tmp_fmtr = tbl->formatter; + tbl->formatter = &test_option_fmtr; + const char *rv = table_set_option(tbl, "invalid:option"); if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv); @@ -155,6 +160,8 @@ static void test_option_parser(struct table *tbl) rv = table_set_option(tbl, "valuekey:value"); if(rv) printf("Tableprinter option parser returned error: \"%s\".\n", rv); + + tbl->formatter = tmp_fmtr; } int main(int argc UNUSED, char **argv) diff --git a/ucw/table.c b/ucw/table.c index 5260a565..272b694c 100644 --- a/ucw/table.c +++ b/ucw/table.c @@ -131,7 +131,7 @@ void table_end(struct table *tbl) /*** 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; } @@ -503,7 +503,7 @@ static void table_start_human_readable(struct table *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, }; @@ -537,7 +537,7 @@ static void table_start_machine_readable(struct table *tbl) } } -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, }; @@ -561,7 +561,7 @@ static void table_start_blockline(struct table *tbl) } } -struct table_formatter table_fmt_blockline = { +const struct table_formatter table_fmt_blockline = { .row_output = table_row_blockline_output, .table_start = table_start_blockline }; diff --git a/ucw/table.h b/ucw/table.h index 4f5d98a2..81d1aa64 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -77,7 +77,7 @@ // FIXME: update documentation according to the changes made in recent commits! -/** Types of columns. These are seldom used explicitly, using a column definition macro is preferred. **/ +/** The COL_TYPE_ANY macro specifies a column type which can be filled with arbitrary type. **/ #define COL_TYPE_ANY NULL @@ -132,7 +132,7 @@ struct table_template { uint cols_to_output; // [*] Number of columns that are printed const char *col_delimiter; // [*] Delimiter that is placed between columns // Back-end used for table formatting and its private data - struct table_formatter *formatter; // FIXME: should be const? + const struct table_formatter *formatter; // FIXME: should be const? }; /** @@ -162,7 +162,7 @@ struct table { int col_out; // Index of the column that is currently printed using fb_col_out // Back-end used for table formatting and its private data - struct table_formatter *formatter; + const struct table_formatter *formatter; void *data; }; @@ -387,7 +387,7 @@ const char *table_set_col_order_by_name(struct table *tbl, const char *col_order /** * Sets table formatter. See below for the list of formatters. **/ -void table_set_formatter(struct table *tbl, struct table_formatter *fmt); +void table_set_formatter(struct table *tbl, const struct table_formatter *fmt); /** * Set a table option. All options have a key and a value. Currently, @@ -446,15 +446,15 @@ struct table_formatter { }; /** Standard formatter for human-readable output. **/ -extern struct table_formatter table_fmt_human_readable; +extern const struct table_formatter table_fmt_human_readable; /** Standard formatter for machine-readable output (tab-separated values). **/ -extern struct table_formatter table_fmt_machine_readable; +extern const struct table_formatter table_fmt_machine_readable; /** * Standard formatter for block output. Each cell is output on its own line * of the form `column_name: value`. Rows are separated by blank lines. **/ -extern struct table_formatter table_fmt_blockline; +extern const struct table_formatter table_fmt_blockline; #endif -- 2.39.2