1eb57f7f16001a6befb951e8817c9010a6b21d94
[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/spv/spv-writer.h"
28
29 #include "gl/xalloc.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33
34 struct spv_driver
35   {
36     struct output_driver driver;
37     struct spv_writer *writer;
38     struct file_handle *handle;
39   };
40
41 static const struct output_driver_class spv_driver_class;
42
43 static struct spv_driver *
44 spv_driver_cast (struct output_driver *driver)
45 {
46   assert (driver->class == &spv_driver_class);
47   return UP_CAST (driver, struct spv_driver, driver);
48 }
49
50 static struct output_driver *
51 spv_create (struct file_handle *fh, enum settings_output_devices device_type,
52             struct string_map *o UNUSED)
53 {
54   struct output_driver *d;
55   struct spv_driver *spv;
56
57   spv = xzalloc (sizeof *spv);
58   d = &spv->driver;
59   spv->handle = fh;
60   output_driver_init (&spv->driver, &spv_driver_class, fh_get_file_name (fh),
61                       device_type);
62
63   char *error = spv_writer_open (fh_get_file_name (fh), &spv->writer);
64   if (spv->writer == NULL)
65     {
66       msg (ME, "%s", error);
67       goto error;
68     }
69
70   return d;
71
72 error:
73   output_driver_destroy (d);
74   return NULL;
75 }
76
77 static void
78 spv_destroy (struct output_driver *driver)
79 {
80   struct spv_driver *spv = spv_driver_cast (driver);
81
82   char *error = spv_writer_close (spv->writer);
83   if (error)
84     msg (ME, "%s", error);
85   fh_unref (spv->handle);
86   free (spv);
87 }
88
89 static void
90 spv_submit (struct output_driver *driver,
91             const struct output_item *output_item)
92 {
93   struct spv_driver *spv = spv_driver_cast (driver);
94
95   spv_writer_write (spv->writer, output_item);
96 }
97
98 static void
99 spv_setup (struct output_driver *driver,
100            const struct page_setup *ps)
101 {
102   struct spv_driver *spv = spv_driver_cast (driver);
103
104   spv_writer_set_page_setup (spv->writer, ps);
105 }
106
107 struct output_driver_factory spv_driver_factory =
108   { "spv", "pspp.spv", spv_create };
109
110 static const struct output_driver_class spv_driver_class =
111   {
112     .name = "spv",
113     .destroy = spv_destroy,
114     .submit = spv_submit,
115     .setup = spv_setup,
116     .handles_show = true,
117     .handles_groups = true,
118   };