spv-driver: Add page_setup to output files and allow user to configure them.
[pspp] / src / output / spv-driver.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2019 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "output/driver-provider.h"
20
21 #include <stdlib.h>
22
23 #include "data/file-handle-def.h"
24 #include "libpspp/cast.h"
25 #include "output/cairo-chart.h"
26 #include "output/output-item.h"
27 #include "output/page-setup.h"
28 #include "output/spv/spv-writer.h"
29
30 #include "gl/xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 struct spv_driver
36   {
37     struct output_driver driver;
38     struct spv_writer *writer;
39     struct file_handle *handle;
40   };
41
42 static const struct output_driver_class spv_driver_class;
43
44 static struct spv_driver *
45 spv_driver_cast (struct output_driver *driver)
46 {
47   assert (driver->class == &spv_driver_class);
48   return UP_CAST (driver, struct spv_driver, driver);
49 }
50
51 static struct output_driver *
52 spv_create (struct file_handle *fh, enum settings_output_devices device_type,
53             struct driver_options *o)
54 {
55   struct spv_writer *writer;
56   char *error = spv_writer_open (fh_get_file_name (fh), &writer);
57   if (!writer)
58     {
59       msg (ME, "%s", error);
60       free (error);
61       return NULL;
62     }
63
64   struct page_setup *ps = page_setup_parse (o);
65   spv_writer_set_page_setup (writer, ps);
66   page_setup_destroy (ps);
67
68   struct spv_driver *spv = xmalloc (sizeof *spv);
69   *spv = (struct spv_driver) {
70     .driver = {
71       .class = &spv_driver_class,
72       .name = xstrdup (fh_get_file_name (fh)),
73       .device_type = device_type,
74     },
75     .handle = fh,
76     .writer = writer,
77   };
78   return &spv->driver;
79 }
80
81 static void
82 spv_destroy (struct output_driver *driver)
83 {
84   struct spv_driver *spv = spv_driver_cast (driver);
85
86   char *error = spv_writer_close (spv->writer);
87   if (error)
88     msg (ME, "%s", error);
89   fh_unref (spv->handle);
90   free (spv);
91 }
92
93 static void
94 spv_submit (struct output_driver *driver,
95             const struct output_item *output_item)
96 {
97   struct spv_driver *spv = spv_driver_cast (driver);
98
99   spv_writer_write (spv->writer, output_item);
100 }
101
102 static void
103 spv_setup (struct output_driver *driver,
104            const struct page_setup *ps)
105 {
106   struct spv_driver *spv = spv_driver_cast (driver);
107
108   spv_writer_set_page_setup (spv->writer, ps);
109 }
110
111 struct output_driver_factory spv_driver_factory =
112   { "spv", "pspp.spv", spv_create };
113
114 static const struct output_driver_class spv_driver_class =
115   {
116     .name = "spv",
117     .destroy = spv_destroy,
118     .submit = spv_submit,
119     .setup = spv_setup,
120     .handles_show = true,
121     .handles_groups = true,
122   };