1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2012, 2013, 2014 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
39 #define _(msgid) gettext (msgid)
41 /* Comma-separated value output driver. */
44 struct output_driver driver;
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? */
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. */
57 static const struct output_driver_class csv_driver_class;
59 static struct csv_driver *
60 csv_driver_cast (struct output_driver *driver)
62 assert (driver->class == &csv_driver_class);
63 return UP_CAST (driver, struct csv_driver, driver);
66 static struct driver_option *
67 opt (struct output_driver *d, struct string_map *options, const char *key,
68 const char *default_value)
70 return driver_option_get (d, options, key, default_value);
73 static struct output_driver *
74 csv_create (const char *file_name, enum settings_output_devices device_type,
77 struct output_driver *d;
78 struct csv_driver *csv;
81 csv = xzalloc (sizeof *csv);
83 output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
85 csv->separator = parse_string (opt (d, o, "separator", ","));
86 quote = parse_string (opt (d, o, "quote", "\""));
87 csv->quote = quote[0];
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");
95 if (csv->file == NULL)
97 msg_error (errno, _("error opening output file `%s'"), csv->file_name);
98 output_driver_destroy (d);
106 csv_destroy (struct output_driver *driver)
108 struct csv_driver *csv = csv_driver_cast (driver);
110 if (csv->file != NULL)
111 fn_close (csv->file_name, csv->file);
113 free (csv->separator);
114 free (csv->quote_set);
115 free (csv->file_name);
120 csv_flush (struct output_driver *driver)
122 struct csv_driver *csv = csv_driver_cast (driver);
123 if (csv->file != NULL)
128 csv_output_field (struct csv_driver *csv, const char *field)
130 while (*field == ' ')
133 if (csv->quote && field[strcspn (field, csv->quote_set)])
137 putc (csv->quote, csv->file);
138 for (p = field; *p != '\0'; p++)
140 if (*p == csv->quote)
141 putc (csv->quote, csv->file);
142 putc (*p, csv->file);
144 putc (csv->quote, csv->file);
147 fputs (field, csv->file);
150 static void PRINTF_FORMAT (2, 3)
151 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
156 va_start (args, format);
157 s = xvasprintf (format, args);
160 csv_output_field (csv, s);
165 csv_put_field (struct csv_driver *csv, struct string *s, const char *field)
167 while (*field == ' ')
170 if (csv->quote && field[strcspn (field, csv->quote_set)])
174 ds_put_byte (s, csv->quote);
175 for (p = field; *p != '\0'; p++)
177 if (*p == csv->quote)
178 ds_put_byte (s, csv->quote);
181 ds_put_byte (s, csv->quote);
184 ds_put_cstr (s, field);
188 csv_output_subtable (struct csv_driver *csv, struct string *s,
189 const struct table_item *item)
191 const struct table *t = table_item_get_table (item);
192 const char *caption = table_item_get_caption (item);
195 if (csv->captions && caption != NULL)
197 csv_output_field_format (csv, "Table: %s", caption);
198 putc ('\n', csv->file);
201 for (y = 0; y < table_nr (t); y++)
204 ds_put_byte (s, '\n');
206 for (x = 0; x < table_nc (t); x++)
208 struct table_cell cell;
210 table_get_cell (t, x, y, &cell);
213 ds_put_cstr (s, csv->separator);
215 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
216 csv_put_field (csv, s, "");
217 else if (cell.n_contents == 1 && cell.contents[0].text != NULL)
218 csv_put_field (csv, s, cell.contents[0].text);
225 for (i = 0; i < cell.n_contents; i++)
228 ds_put_cstr (&s2, "\n\n");
230 if (cell.contents[i].text != NULL)
231 ds_put_cstr (&s2, cell.contents[i].text);
233 csv_output_subtable (csv, &s2, cell.contents[i].table);
235 csv_put_field (csv, s, ds_cstr (&s2));
239 table_cell_free (&cell);
245 csv_put_separator (struct csv_driver *csv)
247 if (csv->n_items++ > 0)
248 putc ('\n', csv->file);
252 csv_submit (struct output_driver *driver,
253 const struct output_item *output_item)
255 struct csv_driver *csv = csv_driver_cast (driver);
257 output_driver_track_current_command (output_item, &csv->command_name);
259 if (is_table_item (output_item))
261 struct table_item *table_item = to_table_item (output_item);
262 const char *caption = table_item_get_caption (table_item);
263 const struct table *t = table_item_get_table (table_item);
266 csv_put_separator (csv);
268 if (csv->captions && caption != NULL)
270 csv_output_field_format (csv, "Table: %s", caption);
271 putc ('\n', csv->file);
274 for (y = 0; y < table_nr (t); y++)
276 for (x = 0; x < table_nc (t); x++)
278 struct table_cell cell;
280 table_get_cell (t, x, y, &cell);
283 fputs (csv->separator, csv->file);
285 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
286 csv_output_field (csv, "");
287 else if (cell.n_contents == 1 && cell.contents[0].text != NULL)
288 csv_output_field (csv, cell.contents[0].text);
295 for (i = 0; i < cell.n_contents; i++)
298 ds_put_cstr (&s, "\n\n");
300 if (cell.contents[i].text != NULL)
301 ds_put_cstr (&s, cell.contents[i].text);
303 csv_output_subtable (csv, &s, cell.contents[i].table);
305 csv_output_field (csv, ds_cstr (&s));
309 table_cell_free (&cell);
311 putc ('\n', csv->file);
314 else if (is_text_item (output_item))
316 const struct text_item *text_item = to_text_item (output_item);
317 enum text_item_type type = text_item_get_type (text_item);
318 const char *text = text_item_get_text (text_item);
320 if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
321 || type == TEXT_ITEM_SYNTAX)
324 csv_put_separator (csv);
327 case TEXT_ITEM_TITLE:
328 csv_output_field_format (csv, "Title: %s", text);
331 case TEXT_ITEM_SUBTITLE:
332 csv_output_field_format (csv, "Subtitle: %s", text);
336 csv_output_field (csv, text);
339 putc ('\n', csv->file);
341 else if (is_message_item (output_item))
343 const struct message_item *message_item = to_message_item (output_item);
344 const struct msg *msg = message_item_get_msg (message_item);
345 char *s = msg_to_string (msg, csv->command_name);
346 csv_put_separator (csv);
347 csv_output_field (csv, s);
349 putc ('\n', csv->file);
353 struct output_driver_factory csv_driver_factory = { "csv", "-", csv_create };
355 static const struct output_driver_class csv_driver_class =