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