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 "data/file-handle-def.h"
24 #include "libpspp/assertion.h"
25 #include "libpspp/compiler.h"
26 #include "libpspp/message.h"
27 #include "libpspp/str.h"
28 #include "libpspp/string-map.h"
29 #include "output/text-item.h"
30 #include "output/driver-provider.h"
31 #include "output/options.h"
32 #include "output/message-item.h"
33 #include "output/table-item.h"
34 #include "output/table-provider.h"
36 #include "gl/xalloc.h"
37 #include "gl/xvasprintf.h"
40 #define _(msgid) gettext (msgid)
42 /* Comma-separated value output driver. */
45 struct output_driver driver;
47 char *separator; /* Field separator (usually comma or tab). */
48 int quote; /* Quote character (usually ' or ") or 0. */
49 char *quote_set; /* Characters that force quoting. */
50 bool titles; /* Print table titles? */
51 bool captions; /* Print table captions? */
53 struct file_handle *handle;
54 char *command_name; /* Current command. */
55 FILE *file; /* Output file. */
56 int n_items; /* Number of items output so far. */
59 static const struct output_driver_class csv_driver_class;
61 static struct csv_driver *
62 csv_driver_cast (struct output_driver *driver)
64 assert (driver->class == &csv_driver_class);
65 return UP_CAST (driver, struct csv_driver, driver);
68 static struct driver_option *
69 opt (struct output_driver *d, struct string_map *options, const char *key,
70 const char *default_value)
72 return driver_option_get (d, options, key, default_value);
75 static struct output_driver *
76 csv_create (struct file_handle *fh, enum settings_output_devices device_type,
79 struct output_driver *d;
80 struct csv_driver *csv;
83 csv = xzalloc (sizeof *csv);
85 output_driver_init (&csv->driver, &csv_driver_class, fh_get_file_name (fh), device_type);
87 csv->separator = parse_string (opt (d, o, "separator", ","));
88 quote = parse_string (opt (d, o, "quote", "\""));
89 csv->quote = quote[0];
91 csv->quote_set = xasprintf ("\n\r\t%s%c", csv->separator, csv->quote);
92 csv->titles = parse_boolean (opt (d, o, "titles", "true"));
93 csv->captions = parse_boolean (opt (d, o, "captions", "true"));
95 csv->file = fn_open (fh, "w");
98 if (csv->file == NULL)
100 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (fh));
101 output_driver_destroy (d);
109 csv_destroy (struct output_driver *driver)
111 struct csv_driver *csv = csv_driver_cast (driver);
113 if (csv->file != NULL)
114 fn_close (csv->handle, csv->file);
116 free (csv->separator);
117 free (csv->quote_set);
118 fh_unref (csv->handle);
123 csv_flush (struct output_driver *driver)
125 struct csv_driver *csv = csv_driver_cast (driver);
126 if (csv->file != NULL)
131 csv_output_field (struct csv_driver *csv, const char *field)
133 while (*field == ' ')
136 if (csv->quote && field[strcspn (field, csv->quote_set)])
140 putc (csv->quote, csv->file);
141 for (p = field; *p != '\0'; p++)
143 if (*p == csv->quote)
144 putc (csv->quote, csv->file);
145 putc (*p, csv->file);
147 putc (csv->quote, csv->file);
150 fputs (field, csv->file);
153 static void PRINTF_FORMAT (2, 3)
154 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
159 va_start (args, format);
160 s = xvasprintf (format, args);
163 csv_output_field (csv, s);
168 csv_put_separator (struct csv_driver *csv)
170 if (csv->n_items++ > 0)
171 putc ('\n', csv->file);
175 csv_submit (struct output_driver *driver,
176 const struct output_item *output_item)
178 struct csv_driver *csv = csv_driver_cast (driver);
180 output_driver_track_current_command (output_item, &csv->command_name);
182 if (is_table_item (output_item))
184 struct table_item *table_item = to_table_item (output_item);
185 const char *title = table_item_get_title (table_item);
186 const char *caption = table_item_get_caption (table_item);
187 const struct table *t = table_item_get_table (table_item);
191 csv_put_separator (csv);
193 if (csv->titles && title != NULL)
195 csv_output_field_format (csv, "Table: %s", title);
196 putc ('\n', csv->file);
200 for (y = 0; y < table_nr (t); y++)
202 for (x = 0; x < table_nc (t); x++)
204 struct table_cell cell;
206 table_get_cell (t, x, y, &cell);
209 fputs (csv->separator, csv->file);
211 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
212 csv_output_field (csv, "");
213 else if (cell.n_contents == 1
214 && cell.contents[0].text != NULL
215 && cell.contents[0].n_footnotes == 0)
216 csv_output_field (csv, cell.contents[0].text);
223 for (i = 0; i < cell.n_contents; i++)
225 const struct cell_contents *c = &cell.contents[i];
229 ds_put_cstr (&s, "\n\n");
231 ds_put_cstr (&s, c->text);
233 for (j = 0; j < c->n_footnotes; j++)
237 str_format_26adic (++footnote_idx, false,
238 marker, sizeof marker);
239 ds_put_format (&s, "[%s]", marker);
242 csv_output_field (csv, ds_cstr (&s));
246 table_cell_free (&cell);
248 putc ('\n', csv->file);
251 if (csv->captions && caption != NULL)
253 csv_output_field_format (csv, "Caption: %s", caption);
254 putc ('\n', csv->file);
261 fputs ("\nFootnotes:\n", csv->file);
264 for (y = 0; y < table_nr (t); y++)
266 struct table_cell cell;
267 for (x = 0; x < table_nc (t); x = cell.d[TABLE_HORZ][1])
269 table_get_cell (t, x, y, &cell);
271 if (x == cell.d[TABLE_HORZ][0] && y == cell.d[TABLE_VERT][0])
272 for (i = 0; i < cell.n_contents; i++)
274 const struct cell_contents *c = &cell.contents[i];
277 for (j = 0; j < c->n_footnotes; j++)
281 str_format_26adic (++footnote_idx, false,
282 marker, sizeof marker);
283 csv_output_field (csv, marker);
284 fputs (csv->separator, csv->file);
285 csv_output_field (csv, c->footnotes[j]);
286 putc ('\n', csv->file);
289 table_cell_free (&cell);
294 else if (is_text_item (output_item))
296 const struct text_item *text_item = to_text_item (output_item);
297 enum text_item_type type = text_item_get_type (text_item);
298 const char *text = text_item_get_text (text_item);
300 if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
301 || type == TEXT_ITEM_SYNTAX)
304 csv_put_separator (csv);
307 case TEXT_ITEM_TITLE:
308 csv_output_field_format (csv, "Title: %s", text);
311 case TEXT_ITEM_SUBTITLE:
312 csv_output_field_format (csv, "Subtitle: %s", text);
316 csv_output_field (csv, text);
319 putc ('\n', csv->file);
321 else if (is_message_item (output_item))
323 const struct message_item *message_item = to_message_item (output_item);
324 const struct msg *msg = message_item_get_msg (message_item);
325 char *s = msg_to_string (msg, csv->command_name);
326 csv_put_separator (csv);
327 csv_output_field (csv, s);
329 putc ('\n', csv->file);
333 struct output_driver_factory csv_driver_factory = { "csv", "-", csv_create };
335 static const struct output_driver_class csv_driver_class =