9be53c8b0f0c92716fc0fe397e23289f7c0a0847
[pspp] / src / output / csv.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2012, 2013 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/str.h"
27 #include "libpspp/string-map.h"
28 #include "output/text-item.h"
29 #include "output/driver-provider.h"
30 #include "output/options.h"
31 #include "output/message-item.h"
32 #include "output/table-item.h"
33 #include "output/table-provider.h"
34
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     int quote;                  /* Quote character (usually ' or ") or 0. */
48     char *quote_set;            /* Characters that force quoting. */
49     bool captions;              /* Print table captions? */
50
51     char *file_name;            /* Output file name. */
52     char *command_name;         /* Current command. */
53     FILE *file;                 /* Output file. */
54     int n_items;                /* Number of items output so far. */
55   };
56
57 static const struct output_driver_class csv_driver_class;
58
59 static struct csv_driver *
60 csv_driver_cast (struct output_driver *driver)
61 {
62   assert (driver->class == &csv_driver_class);
63   return UP_CAST (driver, struct csv_driver, driver);
64 }
65
66 static struct driver_option *
67 opt (struct output_driver *d, struct string_map *options, const char *key,
68      const char *default_value)
69 {
70   return driver_option_get (d, options, key, default_value);
71 }
72
73 static struct output_driver *
74 csv_create (const char *file_name, enum settings_output_devices device_type,
75             struct string_map *o)
76 {
77   struct output_driver *d;
78   struct csv_driver *csv;
79   char *quote;
80
81   csv = xzalloc (sizeof *csv);
82   d = &csv->driver;
83   output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
84
85   csv->separator = parse_string (opt (d, o, "separator", ","));
86   quote = parse_string (opt (d, o, "quote", "\""));
87   csv->quote = quote[0];
88   free (quote);
89   csv->quote_set = xasprintf ("\n\r\t%s%c", csv->separator, csv->quote);
90   csv->captions = parse_boolean (opt (d, o, "captions", "true"));
91   csv->file_name = xstrdup (file_name);
92   csv->file = fn_open (csv->file_name, "w");
93   csv->n_items = 0;
94
95   if (csv->file == NULL)
96     {
97       msg_error (errno, _("error opening output file `%s'"), csv->file_name);
98       output_driver_destroy (d);
99       return NULL;
100     }
101
102   return d;
103 }
104
105 static void
106 csv_destroy (struct output_driver *driver)
107 {
108   struct csv_driver *csv = csv_driver_cast (driver);
109
110   if (csv->file != NULL)
111     fn_close (csv->file_name, csv->file);
112
113   free (csv->separator);
114   free (csv->quote_set);
115   free (csv->file_name);
116   free (csv);
117 }
118
119 static void
120 csv_flush (struct output_driver *driver)
121 {
122   struct csv_driver *csv = csv_driver_cast (driver);
123   if (csv->file != NULL)
124     fflush (csv->file);
125 }
126
127 static void
128 csv_output_field (struct csv_driver *csv, const char *field)
129 {
130   while (*field == ' ')
131     field++;
132
133   if (csv->quote && field[strcspn (field, csv->quote_set)])
134     {
135       const char *p;
136
137       putc (csv->quote, csv->file);
138       for (p = field; *p != '\0'; p++)
139         {
140           if (*p == csv->quote)
141             putc (csv->quote, csv->file);
142           putc (*p, csv->file);
143         }
144       putc (csv->quote, csv->file);
145     }
146   else
147     fputs (field, csv->file);
148 }
149
150 static void
151 csv_put_field (struct csv_driver *csv, struct string *s, const char *field)
152 {
153   while (*field == ' ')
154     field++;
155
156   if (csv->quote && field[strcspn (field, csv->quote_set)])
157     {
158       const char *p;
159
160       ds_put_byte (s, csv->quote);
161       for (p = field; *p != '\0'; p++)
162         {
163           if (*p == csv->quote)
164             ds_put_byte (s, csv->quote);
165           ds_put_byte (s, *p);
166         }
167       ds_put_byte (s, csv->quote);
168     }
169   else
170     ds_put_cstr (s, field);
171 }
172
173 static void
174 csv_output_subtable (struct csv_driver *csv, struct string *s,
175                      const struct table *t)
176 {
177   int y, x;
178
179   for (y = 0; y < table_nr (t); y++)
180     {
181       if (y > 0)
182         ds_put_byte (s, '\n');
183
184       for (x = 0; x < table_nc (t); x++)
185         {
186           struct table_cell cell;
187
188           table_get_cell (t, x, y, &cell);
189
190           if (x > 0)
191             ds_put_cstr (s, csv->separator);
192
193           if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
194             csv_put_field (csv, s, "");
195           else if (cell.n_contents == 1 && cell.contents[0].text != NULL)
196             csv_put_field (csv, s, cell.contents[0].text);
197           else
198             {
199               struct string s2;
200               size_t i;
201
202               ds_init_empty (&s2);
203               for (i = 0; i < cell.n_contents; i++)
204                 {
205                   if (i > 0)
206                     ds_put_cstr (&s2, "\n\n");
207
208                   if (cell.contents[i].text != NULL)
209                     ds_put_cstr (&s2, cell.contents[i].text);
210                   else
211                     csv_output_subtable (csv, &s2, cell.contents[i].table);
212                 }
213               csv_put_field (csv, s, ds_cstr (&s2));
214               ds_destroy (&s2);
215             }
216
217           table_cell_free (&cell);
218         }
219     }
220 }
221
222 static void
223 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
224   PRINTF_FORMAT (2, 3);
225
226 static void
227 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
228 {
229   va_list args;
230   char *s;
231
232   va_start (args, format);
233   s = xvasprintf (format, args);
234   va_end (args);
235
236   csv_output_field (csv, s);
237   free (s);
238 }
239
240 static void
241 csv_put_separator (struct csv_driver *csv)
242 {
243   if (csv->n_items++ > 0)
244     putc ('\n', csv->file);
245 }
246
247 static void
248 csv_submit (struct output_driver *driver,
249             const struct output_item *output_item)
250 {
251   struct csv_driver *csv = csv_driver_cast (driver);
252
253   output_driver_track_current_command (output_item, &csv->command_name);
254
255   if (is_table_item (output_item))
256     {
257       struct table_item *table_item = to_table_item (output_item);
258       const char *caption = table_item_get_caption (table_item);
259       const struct table *t = table_item_get_table (table_item);
260       int x, y;
261
262       csv_put_separator (csv);
263
264       if (csv->captions && caption != NULL)
265         {
266           csv_output_field_format (csv, "Table: %s", caption);
267           putc ('\n', csv->file);
268         }
269
270       for (y = 0; y < table_nr (t); y++)
271         {
272           for (x = 0; x < table_nc (t); x++)
273             {
274               struct table_cell cell;
275
276               table_get_cell (t, x, y, &cell);
277
278               if (x > 0)
279                 fputs (csv->separator, csv->file);
280
281               if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
282                 csv_output_field (csv, "");
283               else if (cell.n_contents == 1 && cell.contents[0].text != NULL)
284                 csv_output_field (csv, cell.contents[0].text);
285               else
286                 {
287                   struct string s;
288                   size_t i;
289
290                   ds_init_empty (&s);
291                   for (i = 0; i < cell.n_contents; i++)
292                     {
293                       if (i > 0)
294                         ds_put_cstr (&s, "\n\n");
295
296                       if (cell.contents[i].text != NULL)
297                         ds_put_cstr (&s, cell.contents[i].text);
298                       else
299                         csv_output_subtable (csv, &s, cell.contents[i].table);
300                     }
301                   csv_output_field (csv, ds_cstr (&s));
302                   ds_destroy (&s);
303                 }
304
305               table_cell_free (&cell);
306             }
307           putc ('\n', csv->file);
308         }
309     }
310   else if (is_text_item (output_item))
311     {
312       const struct text_item *text_item = to_text_item (output_item);
313       enum text_item_type type = text_item_get_type (text_item);
314       const char *text = text_item_get_text (text_item);
315
316       if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
317           || type == TEXT_ITEM_SYNTAX)
318         return;
319
320       csv_put_separator (csv);
321       switch (type)
322         {
323         case TEXT_ITEM_TITLE:
324           csv_output_field_format (csv, "Title: %s", text);
325           break;
326
327         case TEXT_ITEM_SUBTITLE:
328           csv_output_field_format (csv, "Subtitle: %s", text);
329           break;
330
331         default:
332           csv_output_field (csv, text);
333           break;
334         }
335       putc ('\n', csv->file);
336     }
337   else if (is_message_item (output_item))
338     {
339       const struct message_item *message_item = to_message_item (output_item);
340       const struct msg *msg = message_item_get_msg (message_item);
341       char *s = msg_to_string (msg, csv->command_name);
342       csv_put_separator (csv);
343       csv_output_field (csv, s);
344       free (s);
345       putc ('\n', csv->file);
346     }
347 }
348
349 struct output_driver_factory csv_driver_factory = { "csv", "-", csv_create };
350
351 static const struct output_driver_class csv_driver_class =
352   {
353     "csv",
354     csv_destroy,
355     csv_submit,
356     csv_flush,
357   };