From: Ben Pfaff Date: Mon, 6 Feb 2023 16:17:45 +0000 (-0800) Subject: output: Modernize how drivers are initialized. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c607eb8737f25e44121450527ab50a315841580;p=pspp output: Modernize how drivers are initialized. --- diff --git a/src/output/ascii.c b/src/output/ascii.c index e654691cd6..b94d4941c2 100644 --- a/src/output/ascii.c +++ b/src/output/ascii.c @@ -278,13 +278,20 @@ box_get (const struct box_chars *box, return box->c[right][bottom][left][top]; } +/* How the page width is determined. */ +enum ascii_width_mode + { + FIXED_WIDTH, /* Specified by configuration. */ + VIEW_WIDTH, /* From SET WIDTH. */ + TERMINAL_WIDTH /* From the terminal's width. */ + }; + /* ASCII output driver. */ struct ascii_driver { struct output_driver driver; /* User parameters. */ - bool append; /* Append if output file already exists? */ bool emphasis; /* Enable bold and underline in output? */ char *chart_file_name; /* Name of files used for charts. */ @@ -293,11 +300,7 @@ struct ascii_driver struct cell_color bg; /* How the page width is determined: */ - enum { - FIXED_WIDTH, /* Specified by configuration. */ - VIEW_WIDTH, /* From SET WIDTH. */ - TERMINAL_WIDTH /* From the terminal's width. */ - } width_mode; + enum ascii_width_mode width_mode; int width; /* Page width. */ int min_hbreak; /* Min cell size to break across pages. */ @@ -344,10 +347,9 @@ ascii_driver_cast (struct output_driver *driver) } static struct driver_option * -opt (struct output_driver *d, struct string_map *options, const char *key, - const char *default_value) +opt (struct string_map *options, const char *key, const char *default_value) { - return driver_option_get (d, options, key, default_value); + return driver_option_get ("ascii", options, key, default_value); } /* Return true iff the terminal appears to be an xterm with @@ -367,43 +369,25 @@ static struct output_driver * ascii_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o) { - enum { BOX_ASCII, BOX_UNICODE } box; - struct output_driver *d; - struct ascii_driver *a = XZALLOC (struct ascii_driver); - d = &a->driver; - output_driver_init (&a->driver, &ascii_driver_class, fh_get_file_name (fh), device_type); - a->append = parse_boolean (opt (d, o, "append", "false")); - a->emphasis = parse_boolean (opt (d, o, "emphasis", "false")); - - a->chart_file_name = parse_chart_file_name (opt (d, o, "charts", fh_get_file_name (fh))); - a->handle = fh; - + bool append = parse_boolean (opt (o, "append", "false")); + FILE *file = fn_open (fh, append ? "a" : "w"); + if (!file) + { + msg_error (errno, _("ascii: opening output file `%s'"), + fh_get_file_name (fh)); + return NULL; + } + int width = parse_page_size (opt (o, "width", "-1")); bool terminal = !strcmp (fh_get_file_name (fh), "-") && isatty (1); - a->width = parse_page_size (opt (d, o, "width", "-1")); - a->width_mode = (a->width > 0 ? FIXED_WIDTH - : terminal ? TERMINAL_WIDTH - : VIEW_WIDTH); - a->min_hbreak = parse_int (opt (d, o, "min-hbreak", "-1"), -1, INT_MAX); - - a->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF")); - a->fg = parse_color (opt (d, o, "foreground-color", "#000000000000")); const char *default_box = (terminal && (!strcmp (locale_charset (), "UTF-8") || term_is_utf8_xterm ()) ? "unicode" : "ascii"); - box = parse_enum (opt (d, o, "box", default_box), - "ascii", BOX_ASCII, - "unicode", BOX_UNICODE, - NULL_SENTINEL); - a->box = box == BOX_ASCII ? get_ascii_box () : get_unicode_box (); - - a->file = NULL; - a->error = false; - a->lines = NULL; - a->allocated_lines = 0; - a->n_charts = 0; - a->n_objects = 0; + enum { BOX_ASCII, BOX_UNICODE } box = parse_enum (opt (o, "box", default_box), + "ascii", BOX_ASCII, + "unicode", BOX_UNICODE, + NULL_SENTINEL); static const struct render_ops ascii_render_ops = { .draw_line = ascii_draw_line, @@ -412,12 +396,6 @@ ascii_create (struct file_handle *fh, enum settings_output_devices device_type, .adjust_break = NULL, .draw_cell = ascii_draw_cell, }; - a->params.ops = &ascii_render_ops; - a->params.aux = a; - a->params.size[H] = a->width; - a->params.size[V] = INT_MAX; - a->params.font_size[H] = 1; - a->params.font_size[V] = 1; static const int ascii_line_widths[TABLE_N_STROKES] = { [TABLE_STROKE_NONE] = 0, @@ -427,26 +405,52 @@ ascii_create (struct file_handle *fh, enum settings_output_devices device_type, [TABLE_STROKE_THIN] = 1, [TABLE_STROKE_DOUBLE] = 1, }; - a->params.line_widths = ascii_line_widths; - a->params.supports_margins = false; - a->params.rtl = render_direction_rtl (); - a->params.printing = true; + + struct ascii_driver *a = xmalloc (sizeof *a); + *a = (struct ascii_driver) { + .driver = { + .class = &ascii_driver_class, + .name = xstrdup (fh_get_file_name (fh)), + .device_type = device_type + }, + + .emphasis = parse_boolean (opt (o, "emphasis", "false")), + .chart_file_name = parse_chart_file_name (opt (o, "charts", + fh_get_file_name (fh))), + + .fg = parse_color (opt (o, "foreground-color", "#000000000000")), + .bg = parse_color (opt (o, "background-color", "#FFFFFFFFFFFF")), + + .width_mode = (width > 0 ? FIXED_WIDTH + : terminal ? TERMINAL_WIDTH + : VIEW_WIDTH), + .width = width, + + .min_hbreak = parse_int (opt (o, "min-hbreak", "-1"), -1, INT_MAX), + + .box = box == BOX_ASCII ? get_ascii_box () : get_unicode_box (), + + .handle = fh, + .file = file, + + .params = (struct render_params) { + .ops = &ascii_render_ops, + .aux = a, + .size = { [H] = a->width, [V] = INT_MAX }, + .font_size = { [H] = 1, [V] = 1 }, + .line_widths = ascii_line_widths, + .rtl = render_direction_rtl (), + .printing = true, + }, + }; if (!update_page_size (a, true)) goto error; - a->file = fn_open (a->handle, a->append ? "a" : "w"); - if (!a->file) - { - msg_error (errno, _("ascii: opening output file `%s'"), - fh_get_file_name (a->handle)); - goto error; - } - - return d; + return &a->driver; error: - output_driver_destroy (d); + output_driver_destroy (&a->driver); return NULL; } diff --git a/src/output/cairo.c b/src/output/cairo.c index a08771bb44..da224346f8 100644 --- a/src/output/cairo.c +++ b/src/output/cairo.c @@ -130,10 +130,9 @@ xr_driver_cast (struct output_driver *driver) } static struct driver_option * -opt (struct output_driver *d, struct string_map *options, const char *key, - const char *default_value) +opt (struct string_map *options, const char *key, const char *default_value) { - return driver_option_get (d, options, key, default_value); + return driver_option_get ("cairo", options, key, default_value); } static PangoFontDescription * @@ -163,11 +162,11 @@ parse_font (const char *font, int default_size, bool bold, bool italic) } static PangoFontDescription * -parse_font_option (struct output_driver *d, struct string_map *options, +parse_font_option (struct string_map *options, const char *key, const char *default_value, int default_size, bool bold, bool italic) { - char *string = parse_string (opt (d, options, key, default_value)); + char *string = parse_string (opt (options, key, default_value)); PangoFontDescription *desc = parse_font (string, default_size, bold, italic); if (!desc) { @@ -187,55 +186,49 @@ static struct xr_driver * xr_allocate (const char *name, int device_type, enum xr_output_type output_type, struct string_map *o) { - struct xr_driver *xr = XZALLOC (struct xr_driver); - struct output_driver *d = &xr->driver; - - output_driver_init (d, &cairo_driver_class, name, device_type); - xr->output_type = output_type; - /* Scale factor from inch/72000 to inch/(72 * XR_POINT). */ const double scale = XR_POINT / 1000.; int paper[TABLE_N_AXES]; - parse_paper_size (opt (d, o, "paper-size", ""), &paper[H], &paper[V]); + parse_paper_size (opt (o, "paper-size", ""), &paper[H], &paper[V]); for (int a = 0; a < TABLE_N_AXES; a++) paper[a] *= scale; int margins[TABLE_N_AXES][2]; - margins[H][0] = parse_dimension (opt (d, o, "left-margin", ".5in")) * scale; - margins[H][1] = parse_dimension (opt (d, o, "right-margin", ".5in")) * scale; - margins[V][0] = parse_dimension (opt (d, o, "top-margin", ".5in")) * scale; - margins[V][1] = parse_dimension (opt (d, o, "bottom-margin", ".5in")) * scale; + margins[H][0] = parse_dimension (opt (o, "left-margin", ".5in")) * scale; + margins[H][1] = parse_dimension (opt (o, "right-margin", ".5in")) * scale; + margins[V][0] = parse_dimension (opt (o, "top-margin", ".5in")) * scale; + margins[V][1] = parse_dimension (opt (o, "bottom-margin", ".5in")) * scale; int size[TABLE_N_AXES]; for (int a = 0; a < TABLE_N_AXES; a++) size[a] = paper[a] - margins[a][0] - margins[a][1]; int min_break[TABLE_N_AXES]; - min_break[H] = parse_dimension (opt (d, o, "min-hbreak", NULL)) * scale; - min_break[V] = parse_dimension (opt (d, o, "min-vbreak", NULL)) * scale; + min_break[H] = parse_dimension (opt (o, "min-hbreak", NULL)) * scale; + min_break[V] = parse_dimension (opt (o, "min-vbreak", NULL)) * scale; for (int a = 0; a < TABLE_N_AXES; a++) if (min_break[a] <= 0) min_break[a] = size[a] / 2; - int font_size = parse_int (opt (d, o, "font-size", "10000"), 1000, 1000000); + int font_size = parse_int (opt (o, "font-size", "10000"), 1000, 1000000); PangoFontDescription *font = parse_font_option ( - d, o, "prop-font", "Sans Serif", font_size, false, false); + o, "prop-font", "Sans Serif", font_size, false, false); - struct cell_color fg = parse_color (opt (d, o, "foreground-color", "black")); + struct cell_color fg = parse_color (opt (o, "foreground-color", "black")); - bool systemcolors = parse_boolean (opt (d, o, "systemcolors", "false")); + bool systemcolors = parse_boolean (opt (o, "systemcolors", "false")); int object_spacing - = parse_dimension (opt (d, o, "object-spacing", NULL)) * scale; + = parse_dimension (opt (o, "object-spacing", NULL)) * scale; if (object_spacing <= 0) object_spacing = XR_POINT * 12; const char *default_resolution = (output_type == XR_PNG ? "96" : "72"); - int font_resolution = parse_int (opt (d, o, "font-resolution", + int font_resolution = parse_int (opt (o, "font-resolution", default_resolution), 10, 1000); - xr->trim = parse_boolean (opt (d, o, "trim", "false")); + bool trim = parse_boolean (opt (o, "trim", "false")); /* Cairo 1.16.0 has a bug that causes crashes if outlines are enabled at the same time as trimming: @@ -243,10 +236,10 @@ xr_allocate (const char *name, int device_type, For now, just disable the outline if trimming is enabled. */ bool include_outline = (output_type == XR_PDF - && parse_boolean (opt (d, o, "outline", xr->trim ? "false" : "true"))); + && parse_boolean (opt (o, "outline", trim ? "false" : "true"))); - xr->page_style = xmalloc (sizeof *xr->page_style); - *xr->page_style = (struct xr_page_style) { + struct xr_page_style *page_style = xmalloc (sizeof *page_style); + *page_style = (struct xr_page_style) { .ref_cnt = 1, .margins = { @@ -258,8 +251,8 @@ xr_allocate (const char *name, int device_type, .include_outline = include_outline, }; - xr->fsm_style = xmalloc (sizeof *xr->fsm_style); - *xr->fsm_style = (struct xr_fsm_style) { + struct xr_fsm_style *fsm_style = xmalloc (sizeof *fsm_style); + *fsm_style = (struct xr_fsm_style) { .ref_cnt = 1, .size = { [H] = size[H], [V] = size[V] }, .min_break = { [H] = min_break[H], [V] = min_break[V] }, @@ -270,6 +263,19 @@ xr_allocate (const char *name, int device_type, .font_resolution = font_resolution, }; + struct xr_driver *xr = xmalloc (sizeof *xr); + *xr = (struct xr_driver) { + .driver = { + .class = &cairo_driver_class, + .name = xstrdup (name), + .device_type = device_type, + }, + .output_type = output_type, + .fsm_style = fsm_style, + .page_style = page_style, + .trim = trim, + }; + return xr; } diff --git a/src/output/csv.c b/src/output/csv.c index ba1481c9a0..e63e828d4e 100644 --- a/src/output/csv.c +++ b/src/output/csv.c @@ -66,42 +66,45 @@ csv_driver_cast (struct output_driver *driver) } static struct driver_option * -opt (struct output_driver *d, struct string_map *options, const char *key, - const char *default_value) +opt (struct string_map *options, const char *key, const char *default_value) { - return driver_option_get (d, options, key, default_value); + return driver_option_get ("csv", options, key, default_value); } static struct output_driver * csv_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o) { - struct output_driver *d; - char *quote; - - struct csv_driver *csv = XZALLOC (struct csv_driver); - d = &csv->driver; - output_driver_init (&csv->driver, &csv_driver_class, fh_get_file_name (fh), device_type); - - csv->separator = parse_string (opt (d, o, "separator", ",")); - quote = parse_string (opt (d, o, "quote", "\"")); - csv->quote = quote[0]; - free (quote); - csv->quote_set = xasprintf ("\n\r\t%s%c", csv->separator, csv->quote); - csv->titles = parse_boolean (opt (d, o, "titles", "true")); - csv->captions = parse_boolean (opt (d, o, "captions", "true")); - csv->handle = fh; - csv->file = fn_open (fh, "w"); - csv->n_items = 0; - - if (csv->file == NULL) + FILE *file = fn_open (fh, "w"); + if (!file) { msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (fh)); - output_driver_destroy (d); return NULL; } - return d; + char *quote_s = parse_string (opt (o, "quote", "\"")); + int quote = quote_s[0]; + free (quote_s); + + char *separator = parse_string (opt (o, "separator", ",")); + + struct csv_driver *csv = xmalloc (sizeof *csv); + *csv = (struct csv_driver) { + .driver = { + .class = &csv_driver_class, + .name = xstrdup (fh_get_file_name (fh)), + .device_type = device_type, + }, + .separator = separator, + .quote = quote, + .quote_set = xasprintf ("\n\r\t%s%c", separator, quote), + .titles = parse_boolean (opt (o, "titles", "true")), + .captions = parse_boolean (opt (o, "captions", "true")), + .handle = fh, + .file = file, + }; + + return &csv->driver; } static void diff --git a/src/output/driver-provider.h b/src/output/driver-provider.h index 1f3f726949..d0a054e8e0 100644 --- a/src/output/driver-provider.h +++ b/src/output/driver-provider.h @@ -37,10 +37,6 @@ struct output_driver enum settings_output_devices device_type; /* One of SETTINGS_DEVICE_*. */ }; -void output_driver_init (struct output_driver *, - const struct output_driver_class *, - const char *, enum settings_output_devices); - void output_driver_destroy (struct output_driver *); const char *output_driver_get_name (const struct output_driver *); diff --git a/src/output/driver.c b/src/output/driver.c index f627e31248..92de2f5f17 100644 --- a/src/output/driver.c +++ b/src/output/driver.c @@ -411,16 +411,6 @@ output_set_filename (const char *filename) string_map_replace (&e->heading_vars, "Filename", filename); } -void -output_driver_init (struct output_driver *driver, - const struct output_driver_class *class, - const char *name, enum settings_output_devices type) -{ - driver->class = class; - driver->name = xstrdup (name); - driver->device_type = type; -} - void output_driver_destroy (struct output_driver *driver) { diff --git a/src/output/html.c b/src/output/html.c index 6c5348a871..0e6e8d822d 100644 --- a/src/output/html.c +++ b/src/output/html.c @@ -85,10 +85,9 @@ html_driver_cast (struct output_driver *driver) } static struct driver_option * -opt (struct output_driver *d, struct string_map *options, const char *key, - const char *default_value) +opt (struct string_map *options, const char *key, const char *default_value) { - return driver_option_get (d, options, key, default_value); + return driver_option_get ("html", options, key, default_value); } static void @@ -179,37 +178,37 @@ static struct output_driver * html_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o) { - struct output_driver *d; - struct html_driver *html = XZALLOC (struct html_driver); - d = &html->driver; - output_driver_init (&html->driver, &html_driver_class, fh_get_file_name (fh), - device_type); - html->bare = parse_boolean (opt (d, o, "bare", "false")); - html->css = parse_boolean (opt (d, o, "css", "true")); - html->borders = parse_boolean (opt (d, o, "borders", "true")); - - html->handle = fh; - html->chart_file_name = parse_chart_file_name (opt (d, o, "charts", - fh_get_file_name (fh))); - html->file = NULL; - html->n_charts = 1; - html->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF")); - html->fg = parse_color (opt (d, o, "foreground-color", "#000000000000")); - html->file = fn_open (html->handle, "w"); - if (html->file == NULL) + FILE *file = fn_open (fh, "w"); + if (!file) { - msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (html->handle)); - goto error; + msg_error (errno, _("error opening output file `%s'"), + fh_get_file_name (fh)); + return NULL; } + struct html_driver *html = xmalloc (sizeof *html); + *html = (struct html_driver) { + .driver = { + .class = &html_driver_class, + .name = xstrdup (fh_get_file_name (fh)), + .device_type = device_type + }, + .bare = parse_boolean (opt (o, "bare", "false")), + .css = parse_boolean (opt (o, "css", "true")), + .borders = parse_boolean (opt (o, "borders", "true")), + .handle = fh, + .chart_file_name = parse_chart_file_name (opt (o, "charts", + fh_get_file_name (fh))), + .file = file, + .n_charts = 1, + .bg = parse_color (opt (o, "background-color", "#FFFFFFFFFFFF")), + .fg = parse_color (opt (o, "foreground-color", "#000000000000")), + }; + if (!html->bare) put_header (html); - return d; - - error: - output_driver_destroy (d); - return NULL; + return &html->driver; } /* Emits CONTENT to the output, escaping CONTENT as diff --git a/src/output/journal.c b/src/output/journal.c index a07fd4a094..f708e6d787 100644 --- a/src/output/journal.c +++ b/src/output/journal.c @@ -140,16 +140,16 @@ static const struct output_driver_class journal_class = void journal_init (void) { - /* Create journal driver. */ - output_driver_init (&journal.driver, &journal_class, "journal", - SETTINGS_DEVICE_UNFILTERED); - journal.file = NULL; + journal = (struct journal_driver) { + .driver = { + .class = &journal_class, + .name = xstrdup ("journal"), + .device_type = SETTINGS_DEVICE_UNFILTERED, + } + }; - /* Register journal driver. */ output_driver_register (&journal.driver); - journal_enable (); - journal.destroyed = false; } /* Disables journaling. */ diff --git a/src/output/msglog.c b/src/output/msglog.c index 20cfe3a3b2..3cb4dc38e9 100644 --- a/src/output/msglog.c +++ b/src/output/msglog.c @@ -72,13 +72,17 @@ msglog_create (const char *file_name) ? SETTINGS_DEVICE_TERMINAL : SETTINGS_DEVICE_UNFILTERED); - struct msglog_driver *ml = XZALLOC (struct msglog_driver); - ml->handle = handle; - output_driver_init (&ml->driver, &msglog_class, file_name, type); - ml->file = file; - + struct msglog_driver *ml = xmalloc (sizeof *ml); + *ml = (struct msglog_driver) { + .driver = { + .class = &msglog_class, + .name = xstrdup (file_name), + .device_type = type, + }, + .file = file, + .handle = handle, + }; output_driver_register (&ml->driver); - return &ml->driver; } diff --git a/src/output/odt.c b/src/output/odt.c index abb30cb530..2f58586530 100644 --- a/src/output/odt.c +++ b/src/output/odt.c @@ -284,22 +284,22 @@ static struct output_driver * odt_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o UNUSED) { - struct output_driver *d; - struct zip_writer *zip; const char *file_name = fh_get_file_name (fh); - - zip = zip_writer_create (file_name); + struct zip_writer *zip = zip_writer_create (file_name); if (zip == NULL) return NULL; - struct odt_driver *odt = XZALLOC (struct odt_driver); - d = &odt->driver; - - output_driver_init (d, &odt_driver_class, file_name, device_type); - - odt->zip = zip; - odt->handle = fh; - odt->file_name = xstrdup (file_name); + struct odt_driver *odt = xmalloc (sizeof *odt); + *odt = (struct odt_driver) { + .driver = { + .class = &odt_driver_class, + .name = xstrdup (file_name), + .device_type = device_type, + }, + .zip = zip, + .handle = fh, + .file_name = xstrdup (file_name), + }; zip_writer_add_string (zip, "mimetype", "application/vnd.oasis.opendocument.text"); @@ -351,7 +351,7 @@ odt_create (struct file_handle *fh, enum settings_output_devices device_type, zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml"); close_temp_file (odt->manifest_file); - return d; + return &odt->driver; } static void diff --git a/src/output/options.c b/src/output/options.c index a212d1c63e..23715c6932 100644 --- a/src/output/options.c +++ b/src/output/options.c @@ -38,40 +38,19 @@ #include "gettext.h" #define _(msgid) gettext (msgid) -/* Creates and returns a new struct driver_option that contains copies of - all of the supplied arguments. All of the arguments must be nonnull, - except that VALUE may be NULL (if the user did not supply a value for this - option). - - Refer to struct driver_option for the meaning of each argument. */ -struct driver_option * -driver_option_create (const char *driver_name, const char *name, - const char *value, const char *default_value) -{ - struct driver_option *o = xmalloc (sizeof *o); - o->driver_name = xstrdup (driver_name); - o->name = xstrdup (name); - o->value = xstrdup_if_nonnull (value); - o->default_value = xstrdup_if_nonnull (default_value); - return o; -} - -/* Creates and returns a new struct driver_option for output driver DRIVER - (which is needed only to the extent that its name will be used in error - messages). The option named NAME is extracted from OPTIONS. DEFAULT_VALUE - is the default value of the option, used if the given option was not - supplied or was invalid. */ +/* Creates and returns a new struct driver_option for driver DRIVER_NAME (which + is used only in error messages). The option named NAME is extracted from + OPTIONS. DEFAULT_VALUE is the default value of the option, used if the + given option was not supplied or was invalid. */ struct driver_option * -driver_option_get (struct output_driver *driver, struct string_map *options, +driver_option_get (const char *driver_name, struct string_map *options, const char *name, const char *default_value) { - struct driver_option *option; - char *value; - - value = string_map_find_and_delete (options, name); - option = driver_option_create (output_driver_get_name (driver), name, value, - default_value); - free (value); + struct driver_option *option = xmalloc (sizeof *option); + option->driver_name = xstrdup (driver_name); + option->name = xstrdup (name); + option->value = string_map_find_and_delete (options, name); + option->default_value = xstrdup_if_nonnull (default_value); return option; } diff --git a/src/output/options.h b/src/output/options.h index 57bd2aea66..36d7bac9fb 100644 --- a/src/output/options.h +++ b/src/output/options.h @@ -34,11 +34,7 @@ struct driver_option char *default_value; /* Default value supplied by driver. */ }; -struct driver_option *driver_option_create (const char *driver_name, - const char *name, - const char *value, - const char *default_value); -struct driver_option *driver_option_get (struct output_driver *, +struct driver_option *driver_option_get (const char *driver_name, struct string_map *, const char *name, const char *default_value); diff --git a/src/output/spv-driver.c b/src/output/spv-driver.c index 7e22cf0c1b..5dcb9fee21 100644 --- a/src/output/spv-driver.c +++ b/src/output/spv-driver.c @@ -51,26 +51,26 @@ static struct output_driver * spv_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o UNUSED) { - struct output_driver *d; - struct spv_driver *spv = XZALLOC (struct spv_driver); - d = &spv->driver; - spv->handle = fh; - output_driver_init (&spv->driver, &spv_driver_class, fh_get_file_name (fh), - device_type); - - char *error = spv_writer_open (fh_get_file_name (fh), &spv->writer); - if (spv->writer == NULL) + struct spv_writer *writer; + char *error = spv_writer_open (fh_get_file_name (fh), &writer); + if (!writer) { msg (ME, "%s", error); free (error); - goto error; + return NULL; } - return d; - -error: - output_driver_destroy (d); - return NULL; + struct spv_driver *spv = xmalloc (sizeof *spv); + *spv = (struct spv_driver) { + .driver = { + .class = &spv_driver_class, + .name = xstrdup (fh_get_file_name (fh)), + .device_type = device_type, + }, + .handle = fh, + .writer = writer, + }; + return &spv->driver; } static void diff --git a/src/output/tex.c b/src/output/tex.c index de111ec13f..4ffe33b1f9 100644 --- a/src/output/tex.c +++ b/src/output/tex.c @@ -109,45 +109,42 @@ tex_driver_cast (struct output_driver *driver) } static struct driver_option * -opt (struct output_driver *d, struct string_map *options, const char *key, - const char *default_value) +opt (struct string_map *options, const char *key, const char *default_value) { - return driver_option_get (d, options, key, default_value); + return driver_option_get ("tex", options, key, default_value); } static struct output_driver * tex_create (struct file_handle *fh, enum settings_output_devices device_type, struct string_map *o) { - struct output_driver *d; - struct tex_driver *tex = XZALLOC (struct tex_driver); - hmap_init (&tex->macros); - ll_init (&tex->preamble_list); - ll_init (&tex->token_list); - - d = &tex->driver; - output_driver_init (&tex->driver, &tex_driver_class, fh_get_file_name (fh), - device_type); - tex->handle = fh; - tex->chart_file_name = parse_chart_file_name (opt (d, o, "charts", - fh_get_file_name (fh))); - tex->n_charts = 1; - tex->bg = parse_color (opt (d, o, "background-color", "#FFFFFFFFFFFF")); - tex->fg = parse_color (opt (d, o, "foreground-color", "#000000000000")); - - tex->file = fn_open (tex->handle, "w"); - if (tex->file == NULL) + FILE *file = fn_open (fh, "w"); + if (!file) { msg_error (errno, _("error opening output file `%s'"), - fh_get_file_name (tex->handle)); - goto error; + fh_get_file_name (fh)); + return NULL; } - return d; - - error: - output_driver_destroy (d); - return NULL; + struct tex_driver *tex = xmalloc (sizeof *tex); + *tex = (struct tex_driver) { + .driver = { + .class = &tex_driver_class, + .name = xstrdup (fh_get_file_name (fh)), + .device_type = device_type, + }, + .macros = HMAP_INITIALIZER (tex->macros), + .bg = parse_color (opt (o, "background-color", "#FFFFFFFFFFFF")), + .fg = parse_color (opt (o, "foreground-color", "#000000000000")), + .handle = fh, + .chart_file_name = parse_chart_file_name (opt (o, "charts", + fh_get_file_name (fh))), + .file = file, + .n_charts = 1, + .preamble_list = LL_INITIALIZER (tex->preamble_list), + .token_list = LL_INITIALIZER (tex->token_list), + }; + return &tex->driver; } diff --git a/src/ui/gui/psppire-output-window.c b/src/ui/gui/psppire-output-window.c index 46535b0f23..14fc1fd0dc 100644 --- a/src/ui/gui/psppire-output-window.c +++ b/src/ui/gui/psppire-output-window.c @@ -166,13 +166,15 @@ static struct output_driver_class psppire_output_class = void psppire_output_window_setup (void) { - struct psppire_output_driver *pod = XZALLOC (struct psppire_output_driver); - struct output_driver *d; - - d = &pod->driver; - output_driver_init (d, &psppire_output_class, "PSPPIRE", - SETTINGS_DEVICE_UNFILTERED); - output_driver_register (d); + struct psppire_output_driver *pod = xmalloc (sizeof *pod); + *pod = (struct psppire_output_driver) { + .driver = { + .class = &psppire_output_class, + .name = xstrdup ("PSPPIRE"), + .device_type = SETTINGS_DEVICE_UNFILTERED, + }, + }; + output_driver_register (&pod->driver); }