From f4bd801640ccaf1d41f45e0206e01e71d3e18604 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Thu, 29 Nov 2018 21:04:55 -0800 Subject: [PATCH] output: Factor common code for command name tracking out of all drivers. This reduces redundancy. --- src/output/ascii.c | 7 +---- src/output/cairo.c | 5 +--- src/output/csv.c | 5 +--- src/output/driver-provider.h | 4 +-- src/output/driver.c | 55 ++++++++++++++++++++---------------- src/output/html.c | 6 +--- src/output/journal.c | 11 ++------ src/output/message-item.c | 2 ++ src/output/message-item.h | 1 + src/output/msglog.c | 7 +---- src/output/odt.c | 8 +----- 11 files changed, 42 insertions(+), 69 deletions(-) diff --git a/src/output/ascii.c b/src/output/ascii.c index c6a8ee51de..bb776d0c2a 100644 --- a/src/output/ascii.c +++ b/src/output/ascii.c @@ -171,7 +171,6 @@ struct ascii_driver const ucs4_t *box; /* Line & box drawing characters. */ /* Internal state. */ - char *command_name; struct file_handle *handle; FILE *file; /* Output file. */ bool error; /* Output error? */ @@ -250,7 +249,6 @@ ascii_create (struct file_handle *fh, enum settings_output_devices device_type, NULL_SENTINEL); a->box = box == BOX_ASCII ? ascii_box_chars : unicode_box_chars; - a->command_name = NULL; a->file = NULL; a->error = false; a->lines = NULL; @@ -334,7 +332,6 @@ ascii_destroy (struct output_driver *driver) if (a->file != NULL) fn_close (a->handle, a->file); fh_unref (a->handle); - free (a->command_name); free (a->chart_file_name); for (i = 0; i < a->allocated_lines; i++) u8_line_destroy (&a->lines[i]); @@ -428,8 +425,6 @@ ascii_submit (struct output_driver *driver, { struct ascii_driver *a = ascii_driver_cast (driver); - output_driver_track_current_command (output_item, &a->command_name); - if (a->error) return; @@ -487,7 +482,7 @@ ascii_submit (struct output_driver *driver, { const struct message_item *message_item = to_message_item (output_item); const struct msg *msg = message_item_get_msg (message_item); - char *s = msg_to_string (msg, a->command_name); + char *s = msg_to_string (msg, message_item->command_name); ascii_output_text (a, s); free (s); } diff --git a/src/output/cairo.c b/src/output/cairo.c index 3b87aeb64d..745c16e1eb 100644 --- a/src/output/cairo.c +++ b/src/output/cairo.c @@ -524,7 +524,6 @@ xr_destroy (struct output_driver *driver) cairo_destroy (xr->cairo); } - free (xr->command_name); for (i = 0; i < XR_N_FONTS; i++) { struct xr_font *font = &xr->fonts[i]; @@ -552,8 +551,6 @@ xr_submit (struct output_driver *driver, const struct output_item *output_item) { struct xr_driver *xr = xr_driver_cast (driver); - output_driver_track_current_command (output_item, &xr->command_name); - xr_driver_output_item (xr, output_item); while (xr_driver_need_new_page (xr)) { @@ -1588,7 +1585,7 @@ xr_render_message (struct xr_driver *xr, struct xr_render_fsm *fsm; char *s; - s = msg_to_string (msg, xr->command_name); + s = msg_to_string (msg, message_item->command_name); fsm = xr_create_text_renderer (xr, s); free (s); diff --git a/src/output/csv.c b/src/output/csv.c index d463a3f6de..ef49759566 100644 --- a/src/output/csv.c +++ b/src/output/csv.c @@ -51,7 +51,6 @@ struct csv_driver bool captions; /* Print table captions? */ struct file_handle *handle; - char *command_name; /* Current command. */ FILE *file; /* Output file. */ int n_items; /* Number of items output so far. */ }; @@ -177,8 +176,6 @@ csv_submit (struct output_driver *driver, { struct csv_driver *csv = csv_driver_cast (driver); - output_driver_track_current_command (output_item, &csv->command_name); - if (is_table_item (output_item)) { struct table_item *table_item = to_table_item (output_item); @@ -322,7 +319,7 @@ csv_submit (struct output_driver *driver, { const struct message_item *message_item = to_message_item (output_item); const struct msg *msg = message_item_get_msg (message_item); - char *s = msg_to_string (msg, csv->command_name); + char *s = msg_to_string (msg, message_item->command_name); csv_put_separator (csv); csv_output_field (csv, s); free (s); diff --git a/src/output/driver-provider.h b/src/output/driver-provider.h index aa70bb454a..31503f2175 100644 --- a/src/output/driver-provider.h +++ b/src/output/driver-provider.h @@ -73,9 +73,7 @@ struct output_driver_class void (*flush) (struct output_driver *driver); }; -/* Useful for output driver implementation. */ -void output_driver_track_current_command (const struct output_item *, char **); - + /* An abstract way for the output subsystem to create an output driver. */ struct output_driver_factory { diff --git a/src/output/driver.c b/src/output/driver.c index bb5aa3a8b9..653aa8d9c3 100644 --- a/src/output/driver.c +++ b/src/output/driver.c @@ -48,6 +48,7 @@ struct output_engine { struct llx_list drivers; /* Contains "struct output_driver"s. */ struct string deferred_syntax; /* TEXT_ITEM_SYNTAX being accumulated. */ + char *command_name; /* Name of command being processed. */ }; static const struct output_driver_factory *factories[]; @@ -75,6 +76,7 @@ output_engine_push (void) e = &engine_stack[n_stack++]; llx_init (&e->drivers); ds_init_empty (&e->deferred_syntax); + e->command_name = NULL; } void @@ -90,6 +92,7 @@ output_engine_pop (void) output_driver_destroy (d); } ds_destroy (&e->deferred_syntax); + free (e->command_name); } void @@ -174,6 +177,33 @@ output_submit (struct output_item *item) } flush_deferred_syntax (e); + + if (is_text_item (item)) + { + const struct text_item *text_item = to_text_item (item); + const char *text = text_item_get_text (text_item); + enum text_item_type type = text_item_get_type (text_item); + + if (type == TEXT_ITEM_COMMAND_OPEN) + { + free (e->command_name); + e->command_name = xstrdup (text); + } + else if (type == TEXT_ITEM_COMMAND_CLOSE) + { + free (e->command_name); + e->command_name = NULL; + } + } + else if (is_message_item (item)) + { + struct message_item *message_item = to_message_item (item); + free (message_item->command_name); + message_item->command_name = (e->command_name + ? xstrdup (e->command_name) + : NULL); + } + output_submit__ (e, item); } @@ -262,31 +292,6 @@ output_driver_is_registered (const struct output_driver *driver) return output_driver_get_engine (driver) != NULL; } -/* Useful functions for output driver implementation. */ - -void -output_driver_track_current_command (const struct output_item *output_item, - char **command_namep) -{ - if (is_text_item (output_item)) - { - const struct text_item *item = to_text_item (output_item); - const char *text = text_item_get_text (item); - enum text_item_type type = text_item_get_type (item); - - if (type == TEXT_ITEM_COMMAND_OPEN) - { - free (*command_namep); - *command_namep = xstrdup (text); - } - else if (type == TEXT_ITEM_COMMAND_CLOSE) - { - free (*command_namep); - *command_namep = NULL; - } - } -} - extern const struct output_driver_factory txt_driver_factory; extern const struct output_driver_factory list_driver_factory; extern const struct output_driver_factory html_driver_factory; diff --git a/src/output/html.c b/src/output/html.c index 6e5181d7da..e79cd8afea 100644 --- a/src/output/html.c +++ b/src/output/html.c @@ -55,7 +55,6 @@ struct html_driver struct file_handle *handle; char *chart_file_name; - char *command_name; FILE *file; size_t chart_cnt; @@ -221,7 +220,6 @@ html_destroy (struct output_driver *driver) } free (html->chart_file_name); fh_unref (html->handle); - free (html->command_name); free (html); } @@ -231,8 +229,6 @@ html_submit (struct output_driver *driver, { struct html_driver *html = html_driver_cast (driver); - output_driver_track_current_command (output_item, &html->command_name); - if (is_table_item (output_item)) { struct table_item *table_item = to_table_item (output_item); @@ -320,7 +316,7 @@ html_submit (struct output_driver *driver, { const struct message_item *message_item = to_message_item (output_item); const struct msg *msg = message_item_get_msg (message_item); - char *s = msg_to_string (msg, html->command_name); + char *s = msg_to_string (msg, message_item->command_name); print_title_tag (html->file, "P", s); free (s); } diff --git a/src/output/journal.c b/src/output/journal.c index d4ad5a75fb..5ace0db216 100644 --- a/src/output/journal.c +++ b/src/output/journal.c @@ -41,7 +41,6 @@ struct journal_driver { struct output_driver driver; FILE *file; - char *command_name; /* Name of journal file. */ char *file_name; @@ -80,10 +79,7 @@ journal_destroy (struct output_driver *driver) struct journal_driver *j = journal_driver_cast (driver); if ( !j->destroyed) - { - journal_close (); - free (j->command_name); - } + journal_close (); j->destroyed = true; } @@ -107,8 +103,6 @@ journal_submit (struct output_driver *driver, const struct output_item *item) { struct journal_driver *j = journal_driver_cast (driver); - output_driver_track_current_command (item, &j->command_name); - if (is_text_item (item)) { const struct text_item *text_item = to_text_item (item); @@ -121,7 +115,7 @@ journal_submit (struct output_driver *driver, const struct output_item *item) { const struct message_item *message_item = to_message_item (item); const struct msg *msg = message_item_get_msg (message_item); - char *s = msg_to_string (msg, j->command_name); + char *s = msg_to_string (msg, message_item->command_name); journal_output (j, s); free (s); } @@ -145,7 +139,6 @@ journal_init (void) output_driver_init (&journal.driver, &journal_class, "journal", SETTINGS_DEVICE_UNFILTERED); journal.file = NULL; - journal.command_name = NULL; /* Register journal driver. */ output_driver_register (&journal.driver); diff --git a/src/output/message-item.c b/src/output/message-item.c index feb1d32c02..a44784fe2e 100644 --- a/src/output/message-item.c +++ b/src/output/message-item.c @@ -34,6 +34,7 @@ message_item_create (const struct msg *msg) item = xmalloc (sizeof *msg); output_item_init (&item->output_item, &message_item_class); item->msg = msg_dup (msg); + item->command_name = NULL; return item; } @@ -49,6 +50,7 @@ message_item_destroy (struct output_item *output_item) { struct message_item *item = to_message_item (output_item); msg_destroy (item->msg); + free (item->command_name); free (item); } diff --git a/src/output/message-item.h b/src/output/message-item.h index 88450f1ade..8f0befe350 100644 --- a/src/output/message-item.h +++ b/src/output/message-item.h @@ -36,6 +36,7 @@ struct message_item { struct output_item output_item; struct msg *msg; + char *command_name; }; struct message_item *message_item_create (const struct msg *); diff --git a/src/output/msglog.c b/src/output/msglog.c index 0d03295710..9b606b4f18 100644 --- a/src/output/msglog.c +++ b/src/output/msglog.c @@ -42,7 +42,6 @@ struct msglog_driver struct output_driver driver; FILE *file; struct file_handle *handle; - char *command_name; }; static const struct output_driver_class msglog_class; @@ -78,7 +77,6 @@ msglog_create (const char *file_name) ml->handle = handle; output_driver_init (&ml->driver, &msglog_class, file_name, type); ml->file = file; - ml->command_name = NULL; output_driver_register (&ml->driver); @@ -91,7 +89,6 @@ msglog_destroy (struct output_driver *driver) struct msglog_driver *ml = msglog_driver_cast (driver); fn_close (ml->handle, ml->file); - free (ml->command_name); fh_unref (ml->handle); free (ml); } @@ -101,13 +98,11 @@ msglog_submit (struct output_driver *driver, const struct output_item *item) { struct msglog_driver *ml = msglog_driver_cast (driver); - output_driver_track_current_command (item, &ml->command_name); - if (is_message_item (item)) { const struct message_item *message_item = to_message_item (item); const struct msg *msg = message_item_get_msg (message_item); - char *s = msg_to_string (msg, ml->command_name); + char *s = msg_to_string (msg, message_item->command_name); fprintf (ml->file, "%s\n", s); free (s); } diff --git a/src/output/odt.c b/src/output/odt.c index 63c10054f9..1d11594a15 100644 --- a/src/output/odt.c +++ b/src/output/odt.c @@ -70,9 +70,6 @@ struct odt_driver /* Number of tables so far. */ int table_num; - /* Name of current command. */ - char *command_name; - /* Number of footnotes so far. */ int n_footnotes; }; @@ -387,7 +384,6 @@ odt_destroy (struct output_driver *driver) } free (odt->file_name); - free (odt->command_name); free (odt); } @@ -587,8 +583,6 @@ odt_submit (struct output_driver *driver, { struct odt_driver *odt = odt_driver_cast (driver); - output_driver_track_current_command (output_item, &odt->command_name); - if (is_table_item (output_item)) write_table (odt, to_table_item (output_item)); else if (is_text_item (output_item)) @@ -602,7 +596,7 @@ odt_submit (struct output_driver *driver, { const struct message_item *message_item = to_message_item (output_item); const struct msg *msg = message_item_get_msg (message_item); - char *s = msg_to_string (msg, odt->command_name); + char *s = msg_to_string (msg, message_item->command_name); odt_output_text (odt, s); free (s); } -- 2.30.2