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