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/driver-provider.h"
30 #include "output/options.h"
31 #include "output/output-item.h"
32 #include "output/pivot-output.h"
33 #include "output/pivot-table.h"
34 #include "output/table-provider.h"
36 #include "gl/minmax.h"
37 #include "gl/xalloc.h"
38 #include "gl/xvasprintf.h"
41 #define _(msgid) gettext (msgid)
43 /* Comma-separated value output driver. */
46 struct output_driver driver;
48 char *separator; /* Field separator (usually comma or tab). */
49 int quote; /* Quote character (usually ' or ") or 0. */
50 char *quote_set; /* Characters that force quoting. */
51 bool titles; /* Print table titles? */
52 bool captions; /* Print table captions? */
54 struct file_handle *handle;
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;
82 struct csv_driver *csv = XZALLOC (struct csv_driver);
84 output_driver_init (&csv->driver, &csv_driver_class, fh_get_file_name (fh), device_type);
86 csv->separator = parse_string (opt (d, o, "separator", ","));
87 quote = parse_string (opt (d, o, "quote", "\""));
88 csv->quote = quote[0];
90 csv->quote_set = xasprintf ("\n\r\t%s%c", csv->separator, csv->quote);
91 csv->titles = parse_boolean (opt (d, o, "titles", "true"));
92 csv->captions = parse_boolean (opt (d, o, "captions", "true"));
94 csv->file = fn_open (fh, "w");
97 if (csv->file == NULL)
99 msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (fh));
100 output_driver_destroy (d);
108 csv_destroy (struct output_driver *driver)
110 struct csv_driver *csv = csv_driver_cast (driver);
112 if (csv->file != NULL)
113 fn_close (csv->handle, csv->file);
115 free (csv->separator);
116 free (csv->quote_set);
117 fh_unref (csv->handle);
122 csv_flush (struct output_driver *driver)
124 struct csv_driver *csv = csv_driver_cast (driver);
125 if (csv->file != NULL)
130 csv_output_field__ (struct csv_driver *csv, struct substring field)
132 ss_ltrim (&field, ss_cstr (" "));
134 if (csv->quote && ss_cspan (field, ss_cstr (csv->quote_set)) < field.length)
136 putc (csv->quote, csv->file);
137 for (size_t i = 0; i < field.length; i++)
139 if (field.string[i] == csv->quote)
140 putc (csv->quote, csv->file);
141 putc (field.string[i], csv->file);
143 putc (csv->quote, csv->file);
146 fwrite (field.string, field.length, 1, csv->file);
150 csv_output_field (struct csv_driver *csv, const char *field)
152 csv_output_field__ (csv, ss_cstr (field));
156 csv_put_separator (struct csv_driver *csv)
158 if (csv->n_items++ > 0)
159 putc ('\n', csv->file);
163 csv_output_lines (struct csv_driver *csv, const char *text_)
165 struct substring text = ss_cstr (text_);
166 struct substring line;
168 while (ss_separate (text, ss_cstr ("\n"), &save_idx, &line))
170 csv_output_field__ (csv, line);
171 putc ('\n', csv->file);
176 csv_output_table_cell (struct csv_driver *csv, const struct pivot_table *pt,
177 const struct table_cell *cell, const char *leader)
179 struct string s = DS_EMPTY_INITIALIZER;
181 ds_put_format (&s, "%s: ", leader);
182 pivot_value_format (cell->value, pt, &s);
183 csv_output_field (csv, ds_cstr (&s));
188 csv_output_table__ (struct csv_driver *csv, const struct pivot_table *pt,
189 const struct table *t, const char *leader)
194 for (int y = 0; y < t->n[TABLE_VERT]; y++)
196 for (int x = 0; x < t->n[TABLE_HORZ]; x++)
198 struct table_cell cell;
200 table_get_cell (t, x, y, &cell);
203 fputs (csv->separator, csv->file);
205 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
206 csv_output_field (csv, "");
208 csv_output_table_cell (csv, pt, &cell, !x ? leader : NULL);
210 putc ('\n', csv->file);
215 csv_output_table_layer (struct csv_driver *csv, const struct pivot_table *pt,
216 const size_t *layer_indexes)
218 struct table *title, *layers, *body, *caption, *footnotes;
219 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
220 &caption, &footnotes, NULL, NULL);
222 csv_put_separator (csv);
223 csv_output_table__ (csv, pt, title, "Table");
224 csv_output_table__ (csv, pt, layers, "Layer");
225 csv_output_table__ (csv, pt, body, NULL);
226 csv_output_table__ (csv, pt, caption, "Caption");
227 csv_output_table__ (csv, pt, footnotes, "Footnote");
230 table_unref (layers);
232 table_unref (caption);
233 table_unref (footnotes);
237 csv_submit (struct output_driver *driver,
238 const struct output_item *item)
240 struct csv_driver *csv = csv_driver_cast (driver);
244 case OUTPUT_ITEM_CHART:
247 case OUTPUT_ITEM_GROUP:
250 case OUTPUT_ITEM_IMAGE:
253 case OUTPUT_ITEM_MESSAGE:
254 csv_put_separator (csv);
255 char *s = msg_to_string (item->message);
256 csv_output_field (csv, s);
258 putc ('\n', csv->file);
261 case OUTPUT_ITEM_PAGE_BREAK:
262 csv_put_separator (csv);
263 csv_output_lines (csv, "");
266 case OUTPUT_ITEM_TABLE:
268 size_t *layer_indexes;
269 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->table, true)
270 csv_output_table_layer (csv, item->table, layer_indexes);
274 case OUTPUT_ITEM_TEXT:
275 if (item->text.subtype == TEXT_ITEM_SYNTAX
276 || item->text.subtype == TEXT_ITEM_PAGE_TITLE)
279 csv_put_separator (csv);
281 char *text = text_item_get_plain_text (item);
282 csv_output_lines (csv, text);
288 struct output_driver_factory csv_driver_factory = { "csv", "-", csv_create };
290 static const struct output_driver_class csv_driver_class =
293 .destroy = csv_destroy,
294 .submit = csv_submit,