1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010, 2012 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/string-map.h"
27 #include "output/text-item.h"
28 #include "output/driver-provider.h"
29 #include "output/options.h"
30 #include "output/message-item.h"
31 #include "output/table-item.h"
32 #include "output/table-provider.h"
34 #include "gl/xalloc.h"
35 #include "gl/xvasprintf.h"
38 #define _(msgid) gettext (msgid)
40 /* Comma-separated value output driver. */
43 struct output_driver driver;
45 char *separator; /* Field separator (usually comma or tab). */
46 int quote; /* Quote character (usually ' or ") or 0. */
47 char *quote_set; /* Characters that force quoting. */
48 bool captions; /* Print table captions? */
50 char *file_name; /* Output file name. */
51 char *command_name; /* Current command. */
52 FILE *file; /* Output file. */
53 int n_items; /* Number of items output so far. */
56 static const struct output_driver_class csv_driver_class;
58 static struct csv_driver *
59 csv_driver_cast (struct output_driver *driver)
61 assert (driver->class == &csv_driver_class);
62 return UP_CAST (driver, struct csv_driver, driver);
65 static struct driver_option *
66 opt (struct output_driver *d, struct string_map *options, const char *key,
67 const char *default_value)
69 return driver_option_get (d, options, key, default_value);
72 static struct output_driver *
73 csv_create (const char *file_name, enum settings_output_devices device_type,
76 struct output_driver *d;
77 struct csv_driver *csv;
80 csv = xzalloc (sizeof *csv);
82 output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
84 csv->separator = parse_string (opt (d, o, "separator", ","));
85 quote = parse_string (opt (d, o, "quote", "\""));
86 csv->quote = quote[0];
88 csv->quote_set = xasprintf ("\n\r\t%s%c", csv->separator, csv->quote);
89 csv->captions = parse_boolean (opt (d, o, "captions", "true"));
90 csv->file_name = xstrdup (file_name);
91 csv->file = fn_open (csv->file_name, "w");
94 if (csv->file == NULL)
96 msg_error (errno, _("error opening output file `%s'"), csv->file_name);
97 output_driver_destroy (d);
105 csv_destroy (struct output_driver *driver)
107 struct csv_driver *csv = csv_driver_cast (driver);
109 if (csv->file != NULL)
110 fn_close (csv->file_name, csv->file);
112 free (csv->separator);
113 free (csv->quote_set);
114 free (csv->file_name);
119 csv_flush (struct output_driver *driver)
121 struct csv_driver *csv = csv_driver_cast (driver);
122 if (csv->file != NULL)
127 csv_output_field (struct csv_driver *csv, const char *field)
129 while (*field == ' ')
132 if (csv->quote && field[strcspn (field, csv->quote_set)])
136 putc (csv->quote, csv->file);
137 for (p = field; *p != '\0'; p++)
139 if (*p == csv->quote)
140 putc (csv->quote, csv->file);
141 putc (*p, csv->file);
143 putc (csv->quote, csv->file);
146 fputs (field, csv->file);
150 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
151 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 *caption = table_item_get_caption (table_item);
186 const struct table *t = table_item_get_table (table_item);
189 csv_put_separator (csv);
191 if (csv->captions && caption != NULL)
193 csv_output_field_format (csv, "Table: %s", caption);
194 putc ('\n', csv->file);
197 for (y = 0; y < table_nr (t); y++)
199 for (x = 0; x < table_nc (t); x++)
201 struct table_cell cell;
203 table_get_cell (t, x, y, &cell);
206 fputs (csv->separator, csv->file);
208 if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
209 csv_output_field (csv, "");
211 csv_output_field (csv, cell.contents);
213 table_cell_free (&cell);
215 putc ('\n', csv->file);
218 else if (is_text_item (output_item))
220 const struct text_item *text_item = to_text_item (output_item);
221 enum text_item_type type = text_item_get_type (text_item);
222 const char *text = text_item_get_text (text_item);
224 if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
225 || type == TEXT_ITEM_SYNTAX)
228 csv_put_separator (csv);
231 case TEXT_ITEM_TITLE:
232 csv_output_field_format (csv, "Title: %s", text);
235 case TEXT_ITEM_SUBTITLE:
236 csv_output_field_format (csv, "Subtitle: %s", text);
240 csv_output_field (csv, text);
243 putc ('\n', csv->file);
245 else if (is_message_item (output_item))
247 const struct message_item *message_item = to_message_item (output_item);
248 const struct msg *msg = message_item_get_msg (message_item);
249 char *s = msg_to_string (msg, csv->command_name);
250 csv_put_separator (csv);
251 csv_output_field (csv, s);
253 putc ('\n', csv->file);
257 struct output_driver_factory csv_driver_factory = { "csv", "-", csv_create };
259 static const struct output_driver_class csv_driver_class =