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