1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 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/string-map.h>
26 #include <output/text-item.h>
27 #include <output/driver-provider.h>
28 #include <output/options.h>
29 #include <output/table-item.h>
30 #include <output/table-provider.h>
33 #include "gl/xalloc.h"
34 #include "gl/xvasprintf.h"
37 #define _(msgid) gettext (msgid)
39 /* Comma-separated value output driver. */
42 struct output_driver driver;
44 char *separator; /* Comma or tab. */
46 char *file_name; /* Output file name. */
47 FILE *file; /* Output file. */
48 bool reported_error; /* Reported file open error? */
51 static struct csv_driver *
52 csv_driver_cast (struct output_driver *driver)
54 assert (driver->class == &csv_class);
55 return UP_CAST (driver, struct csv_driver, driver);
58 static struct driver_option *
59 opt (struct output_driver *d, struct string_map *options, const char *key,
60 const char *default_value)
62 return driver_option_get (d, options, key, default_value);
65 static struct output_driver *
66 csv_create (const char *name, enum output_device_type device_type,
69 struct output_driver *d;
70 struct csv_driver *csv;
72 csv = xzalloc (sizeof *csv);
74 output_driver_init (&csv->driver, &csv_class, name, device_type);
76 csv->separator = parse_string (opt (d, o, "separator", ","));
77 csv->file_name = parse_string (opt (d, o, "output-file", "pspp.csv"));
79 csv->reported_error = false;
85 csv_destroy (struct output_driver *driver)
87 struct csv_driver *csv = csv_driver_cast (driver);
89 free (csv->separator);
90 free (csv->file_name);
91 if (csv->file != NULL)
97 csv_flush (struct output_driver *driver)
99 struct csv_driver *csv = csv_driver_cast (driver);
100 if (csv->file != NULL)
105 csv_output_field (FILE *file, const char *field)
107 while (*field == ' ')
110 if (field[strcspn (field, "\"\n\r,\t")])
115 for (p = field; *p != '\0'; p++)
128 csv_output_field_format (FILE *file, const char *format, ...)
129 PRINTF_FORMAT (2, 3);
132 csv_output_field_format (FILE *file, const char *format, ...)
137 va_start (args, format);
138 s = xvasprintf (format, args);
141 csv_output_field (file, s);
146 csv_open_file (struct csv_driver *csv)
148 if (csv->file == NULL)
150 csv->file = fn_open (csv->file_name, "w");
151 if (csv->file == NULL)
153 if (!csv->reported_error)
154 error (0, errno, _("csv: opening output file \"%s\""),
156 csv->reported_error = true;
161 putc ('\n', csv->file);
167 csv_submit (struct output_driver *driver,
168 const struct output_item *output_item)
170 struct csv_driver *csv = csv_driver_cast (driver);
172 if (is_table_item (output_item))
174 struct table_item *table_item = to_table_item (output_item);
175 const char *caption = table_item_get_caption (table_item);
176 const struct table *t = table_item_get_table (table_item);
179 if (!csv_open_file (csv))
184 csv_output_field_format (csv->file, "Table: %s", caption);
185 putc ('\n', csv->file);
188 for (y = 0; y < table_nr (t); y++)
190 for (x = 0; x < table_nc (t); x++)
192 struct table_cell cell;
194 table_get_cell (t, x, y, &cell);
197 fputs (csv->separator, csv->file);
199 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
200 csv_output_field (csv->file, "");
202 csv_output_field (csv->file, cell.contents);
204 table_cell_free (&cell);
206 putc ('\n', csv->file);
209 else if (is_text_item (output_item))
211 const struct text_item *text_item = to_text_item (output_item);
212 enum text_item_type type = text_item_get_type (text_item);
213 const char *text = text_item_get_text (text_item);
215 if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
216 || type == TEXT_ITEM_SYNTAX)
222 case TEXT_ITEM_TITLE:
223 csv_output_field_format (csv->file, "Title: %s", text);
226 case TEXT_ITEM_SUBTITLE:
227 csv_output_field_format (csv->file, "Subtitle: %s", text);
231 csv_output_field (csv->file, text);
234 putc ('\n', csv->file);
238 const struct output_driver_class csv_class =