1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010 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. */
45 char *file_name; /* Output file name. */
46 FILE *file; /* Output file. */
47 int n_items; /* Number of items output so far. */
50 static struct csv_driver *
51 csv_driver_cast (struct output_driver *driver)
53 assert (driver->class == &csv_class);
54 return UP_CAST (driver, struct csv_driver, driver);
57 static struct driver_option *
58 opt (struct output_driver *d, struct string_map *options, const char *key,
59 const char *default_value)
61 return driver_option_get (d, options, key, default_value);
64 static struct output_driver *
65 csv_create (const char *name, enum output_device_type device_type,
68 struct output_driver *d;
69 struct csv_driver *csv;
71 csv = xzalloc (sizeof *csv);
73 output_driver_init (&csv->driver, &csv_class, name, device_type);
75 csv->separator = parse_string (opt (d, o, "separator", ","));
76 csv->file_name = parse_string (opt (d, o, "output-file", "pspp.csv"));
77 csv->file = fn_open (csv->file_name, "w");
80 if (csv->file == NULL)
82 error (0, errno, _("csv: opening output file \"%s\""), csv->file_name);
83 output_driver_destroy (d);
91 csv_destroy (struct output_driver *driver)
93 struct csv_driver *csv = csv_driver_cast (driver);
95 if (csv->file != NULL)
96 fn_close (csv->file_name, csv->file);
98 free (csv->separator);
99 free (csv->file_name);
104 csv_flush (struct output_driver *driver)
106 struct csv_driver *csv = csv_driver_cast (driver);
107 if (csv->file != NULL)
112 csv_output_field (FILE *file, const char *field)
114 while (*field == ' ')
117 if (field[strcspn (field, "\"\n\r,\t")])
122 for (p = field; *p != '\0'; p++)
135 csv_output_field_format (FILE *file, const char *format, ...)
136 PRINTF_FORMAT (2, 3);
139 csv_output_field_format (FILE *file, const char *format, ...)
144 va_start (args, format);
145 s = xvasprintf (format, args);
148 csv_output_field (file, s);
153 csv_put_separator (struct csv_driver *csv)
155 if (csv->n_items++ > 0)
156 putc ('\n', csv->file);
160 csv_submit (struct output_driver *driver,
161 const struct output_item *output_item)
163 struct csv_driver *csv = csv_driver_cast (driver);
165 if (is_table_item (output_item))
167 struct table_item *table_item = to_table_item (output_item);
168 const char *caption = table_item_get_caption (table_item);
169 const struct table *t = table_item_get_table (table_item);
172 csv_put_separator (csv);
176 csv_output_field_format (csv->file, "Table: %s", caption);
177 putc ('\n', csv->file);
180 for (y = 0; y < table_nr (t); y++)
182 for (x = 0; x < table_nc (t); x++)
184 struct table_cell cell;
186 table_get_cell (t, x, y, &cell);
189 fputs (csv->separator, csv->file);
191 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
192 csv_output_field (csv->file, "");
194 csv_output_field (csv->file, cell.contents);
196 table_cell_free (&cell);
198 putc ('\n', csv->file);
201 else if (is_text_item (output_item))
203 const struct text_item *text_item = to_text_item (output_item);
204 enum text_item_type type = text_item_get_type (text_item);
205 const char *text = text_item_get_text (text_item);
207 if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
208 || type == TEXT_ITEM_SYNTAX)
211 csv_put_separator (csv);
214 case TEXT_ITEM_TITLE:
215 csv_output_field_format (csv->file, "Title: %s", text);
218 case TEXT_ITEM_SUBTITLE:
219 csv_output_field_format (csv->file, "Subtitle: %s", text);
223 csv_output_field (csv->file, text);
226 putc ('\n', csv->file);
230 const struct output_driver_class csv_class =