3fbf53d963448b041d403c81f2e4cd13fe6ad227
[pspp] / src / output / csv.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010 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 <errno.h>
20 #include <stdlib.h>
21
22 #include "data/file-name.h"
23 #include "libpspp/assertion.h"
24 #include "libpspp/compiler.h"
25 #include "libpspp/message.h"
26 #include "libpspp/string-map.h"
27 #include "output/text-item.h"
28 #include "output/driver-provider.h"
29 #include "output/options.h"
30 #include "output/message-item.h"
31 #include "output/table-item.h"
32 #include "output/table-provider.h"
33
34 #include "gl/error.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 /* Comma-separated value output driver. */
42 struct csv_driver
43   {
44     struct output_driver driver;
45
46     char *separator;            /* Field separator (usually comma or tab). */
47     char *quote_set;            /* Characters that force quoting. */
48     char *file_name;            /* Output file name. */
49     char *command_name;         /* Current command. */
50     FILE *file;                 /* Output file. */
51     int n_items;                /* Number of items output so far. */
52   };
53
54 static const struct output_driver_class csv_driver_class;
55
56 static struct csv_driver *
57 csv_driver_cast (struct output_driver *driver)
58 {
59   assert (driver->class == &csv_driver_class);
60   return UP_CAST (driver, struct csv_driver, driver);
61 }
62
63 static struct driver_option *
64 opt (struct output_driver *d, struct string_map *options, const char *key,
65      const char *default_value)
66 {
67   return driver_option_get (d, options, key, default_value);
68 }
69
70 static struct output_driver *
71 csv_create (const char *file_name, enum settings_output_devices device_type,
72             struct string_map *o)
73 {
74   struct output_driver *d;
75   struct csv_driver *csv;
76
77   csv = xzalloc (sizeof *csv);
78   d = &csv->driver;
79   output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
80
81   csv->separator = parse_string (opt (d, o, "separator", ","));
82   csv->quote_set = xasprintf ("\"\n\r\t%s", csv->separator);
83   csv->file_name = xstrdup (file_name);
84   csv->file = fn_open (csv->file_name, "w");
85   csv->n_items = 0;
86
87   if (csv->file == NULL)
88     {
89       error (0, errno, _("error opening output file `%s'"), csv->file_name);
90       output_driver_destroy (d);
91       return NULL;
92     }
93
94   return d;
95 }
96
97 static void
98 csv_destroy (struct output_driver *driver)
99 {
100   struct csv_driver *csv = csv_driver_cast (driver);
101
102   if (csv->file != NULL)
103     fn_close (csv->file_name, csv->file);
104
105   free (csv->separator);
106   free (csv->quote_set);
107   free (csv->file_name);
108   free (csv);
109 }
110
111 static void
112 csv_flush (struct output_driver *driver)
113 {
114   struct csv_driver *csv = csv_driver_cast (driver);
115   if (csv->file != NULL)
116     fflush (csv->file);
117 }
118
119 static void
120 csv_output_field (struct csv_driver *csv, const char *field)
121 {
122   while (*field == ' ')
123     field++;
124
125   if (field[strcspn (field, csv->quote_set)])
126     {
127       const char *p;
128
129       putc ('"', csv->file);
130       for (p = field; *p != '\0'; p++)
131         {
132           if (*p == '"')
133             putc ('"', csv->file);
134           putc (*p, csv->file);
135         }
136       putc ('"', csv->file);
137     }
138   else
139     fputs (field, csv->file);
140 }
141
142 static void
143 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
144   PRINTF_FORMAT (2, 3);
145
146 static void
147 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
148 {
149   va_list args;
150   char *s;
151
152   va_start (args, format);
153   s = xvasprintf (format, args);
154   va_end (args);
155
156   csv_output_field (csv, s);
157   free (s);
158 }
159
160 static void
161 csv_put_separator (struct csv_driver *csv)
162 {
163   if (csv->n_items++ > 0)
164     putc ('\n', csv->file);
165 }
166
167 static void
168 csv_submit (struct output_driver *driver,
169             const struct output_item *output_item)
170 {
171   struct csv_driver *csv = csv_driver_cast (driver);
172
173   output_driver_track_current_command (output_item, &csv->command_name);
174
175   if (is_table_item (output_item))
176     {
177       struct table_item *table_item = to_table_item (output_item);
178       const char *caption = table_item_get_caption (table_item);
179       const struct table *t = table_item_get_table (table_item);
180       int x, y;
181
182       csv_put_separator (csv);
183
184       if (caption != NULL)
185         {
186           csv_output_field_format (csv, "Table: %s", caption);
187           putc ('\n', csv->file);
188         }
189
190       for (y = 0; y < table_nr (t); y++)
191         {
192           for (x = 0; x < table_nc (t); x++)
193             {
194               struct table_cell cell;
195
196               table_get_cell (t, x, y, &cell);
197
198               if (x > 0)
199                 fputs (csv->separator, csv->file);
200
201               if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
202                 csv_output_field (csv, "");
203               else
204                 csv_output_field (csv, cell.contents);
205
206               table_cell_free (&cell);
207             }
208           putc ('\n', csv->file);
209         }
210     }
211   else if (is_text_item (output_item))
212     {
213       const struct text_item *text_item = to_text_item (output_item);
214       enum text_item_type type = text_item_get_type (text_item);
215       const char *text = text_item_get_text (text_item);
216
217       if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
218           || type == TEXT_ITEM_SYNTAX)
219         return;
220
221       csv_put_separator (csv);
222       switch (type)
223         {
224         case TEXT_ITEM_TITLE:
225           csv_output_field_format (csv, "Title: %s", text);
226           break;
227
228         case TEXT_ITEM_SUBTITLE:
229           csv_output_field_format (csv, "Subtitle: %s", text);
230           break;
231
232         default:
233           csv_output_field (csv, text);
234           break;
235         }
236       putc ('\n', csv->file);
237     }
238   else if (is_message_item (output_item))
239     {
240       const struct message_item *message_item = to_message_item (output_item);
241       const struct msg *msg = message_item_get_msg (message_item);
242       char *s = msg_to_string (msg, csv->command_name);
243       csv_put_separator (csv);
244       csv_output_field (csv, s);
245       free (s);
246       putc ('\n', csv->file);
247     }
248 }
249
250 struct output_driver_factory csv_driver_factory = { "csv", csv_create };
251
252 static const struct output_driver_class csv_driver_class =
253   {
254     "csv",
255     csv_destroy,
256     csv_submit,
257     csv_flush,
258   };