table-item: Change title from table_item_text to table_cell.
[pspp] / src / output / csv.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2012, 2013, 2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <stdlib.h>
21
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/page-eject-item.h"
34 #include "output/table-item.h"
35 #include "output/table-provider.h"
36
37 #include "gl/minmax.h"
38 #include "gl/xalloc.h"
39 #include "gl/xvasprintf.h"
40
41 #include "gettext.h"
42 #define _(msgid) gettext (msgid)
43
44 /* Comma-separated value output driver. */
45 struct csv_driver
46   {
47     struct output_driver driver;
48
49     char *separator;            /* Field separator (usually comma or tab). */
50     int quote;                  /* Quote character (usually ' or ") or 0. */
51     char *quote_set;            /* Characters that force quoting. */
52     bool titles;                /* Print table titles? */
53     bool captions;              /* Print table captions? */
54
55     struct file_handle *handle;
56     FILE *file;                 /* Output file. */
57     int n_items;                /* Number of items output so far. */
58   };
59
60 static const struct output_driver_class csv_driver_class;
61
62 static struct csv_driver *
63 csv_driver_cast (struct output_driver *driver)
64 {
65   assert (driver->class == &csv_driver_class);
66   return UP_CAST (driver, struct csv_driver, driver);
67 }
68
69 static struct driver_option *
70 opt (struct output_driver *d, struct string_map *options, const char *key,
71      const char *default_value)
72 {
73   return driver_option_get (d, options, key, default_value);
74 }
75
76 static struct output_driver *
77 csv_create (struct file_handle *fh, enum settings_output_devices device_type,
78             struct string_map *o)
79 {
80   struct output_driver *d;
81   struct csv_driver *csv;
82   char *quote;
83
84   csv = xzalloc (sizeof *csv);
85   d = &csv->driver;
86   output_driver_init (&csv->driver, &csv_driver_class, fh_get_file_name (fh), device_type);
87
88   csv->separator = parse_string (opt (d, o, "separator", ","));
89   quote = parse_string (opt (d, o, "quote", "\""));
90   csv->quote = quote[0];
91   free (quote);
92   csv->quote_set = xasprintf ("\n\r\t%s%c", csv->separator, csv->quote);
93   csv->titles = parse_boolean (opt (d, o, "titles", "true"));
94   csv->captions = parse_boolean (opt (d, o, "captions", "true"));
95   csv->handle = fh;
96   csv->file = fn_open (fh, "w");
97   csv->n_items = 0;
98
99   if (csv->file == NULL)
100     {
101       msg_error (errno, _("error opening output file `%s'"), fh_get_file_name (fh));
102       output_driver_destroy (d);
103       return NULL;
104     }
105
106   return d;
107 }
108
109 static void
110 csv_destroy (struct output_driver *driver)
111 {
112   struct csv_driver *csv = csv_driver_cast (driver);
113
114   if (csv->file != NULL)
115     fn_close (csv->handle, csv->file);
116
117   free (csv->separator);
118   free (csv->quote_set);
119   fh_unref (csv->handle);
120   free (csv);
121 }
122
123 static void
124 csv_flush (struct output_driver *driver)
125 {
126   struct csv_driver *csv = csv_driver_cast (driver);
127   if (csv->file != NULL)
128     fflush (csv->file);
129 }
130
131 static void
132 csv_output_field__ (struct csv_driver *csv, struct substring field)
133 {
134   ss_ltrim (&field, ss_cstr (" "));
135
136   if (csv->quote && ss_cspan (field, ss_cstr (csv->quote_set)) < field.length)
137     {
138       putc (csv->quote, csv->file);
139       for (size_t i = 0; i < field.length; i++)
140         {
141           if (field.string[i] == csv->quote)
142             putc (csv->quote, csv->file);
143           putc (field.string[i], csv->file);
144         }
145       putc (csv->quote, csv->file);
146     }
147   else
148     fwrite (field.string, field.length, 1, csv->file);
149 }
150
151 static void
152 csv_output_field (struct csv_driver *csv, const char *field)
153 {
154   csv_output_field__ (csv, ss_cstr (field));
155 }
156
157 static void
158 csv_put_separator (struct csv_driver *csv)
159 {
160   if (csv->n_items++ > 0)
161     putc ('\n', csv->file);
162 }
163
164 static void
165 csv_output_lines (struct csv_driver *csv, const char *text_)
166 {
167   struct substring text = ss_cstr (text_);
168   struct substring line;
169   size_t save_idx = 0;
170   while (ss_separate (text, ss_cstr ("\n"), &save_idx, &line))
171     {
172       csv_output_field__ (csv, line);
173       putc ('\n', csv->file);
174     }
175 }
176
177 static void
178 csv_format_footnotes (struct footnote **f, size_t n, struct string *s)
179 {
180   for (size_t i = 0; i < n; i++)
181     ds_put_format (s, "[%s]", f[i]->marker);
182 }
183
184 static void
185 csv_output_table_item_text (struct csv_driver *csv,
186                             const struct table_item_text *text,
187                             const char *leader)
188 {
189   if (!text)
190     return;
191
192   struct string s = DS_EMPTY_INITIALIZER;
193   ds_put_format (&s, "%s: %s", leader, text->content);
194   csv_format_footnotes (text->footnotes, text->n_footnotes, &s);
195   csv_output_field (csv, ds_cstr (&s));
196   ds_destroy (&s);
197   putc ('\n', csv->file);
198 }
199
200 static void
201 csv_output_table_cell (struct csv_driver *csv,
202                        const struct table_cell *cell,
203                        const char *leader)
204 {
205   if (!cell)
206     return;
207
208   if (!(cell->options & TAB_MARKUP) && !cell->n_footnotes
209       && !cell->n_subscripts && !leader)
210     csv_output_field (csv, cell->text);
211   else
212     {
213       struct string s = DS_EMPTY_INITIALIZER;
214
215       if (leader)
216         ds_put_format (&s, "%s: ", leader);
217
218       if (cell->options & TAB_MARKUP)
219         {
220           char *t = output_get_text_from_markup (cell->text);
221           ds_put_cstr (&s, t);
222           free (t);
223         }
224       else
225         ds_put_cstr (&s, cell->text);
226
227       if (cell->n_subscripts)
228         for (size_t i = 0; i < cell->n_subscripts; i++)
229           ds_put_format (&s, "%c%s",
230                          i ? ',' : '_', cell->subscripts[i]);
231       csv_format_footnotes (cell->footnotes, cell->n_footnotes, &s);
232       csv_output_field (csv, ds_cstr (&s));
233       ds_destroy (&s);
234
235       if (leader)
236         putc ('\n', csv->file);
237     }
238 }
239
240 static void
241 csv_submit (struct output_driver *driver,
242             const struct output_item *output_item)
243 {
244   struct csv_driver *csv = csv_driver_cast (driver);
245
246   if (is_table_item (output_item))
247     {
248       struct table_item *table_item = to_table_item (output_item);
249       const struct table *t = table_item_get_table (table_item);
250       int x, y;
251
252       csv_put_separator (csv);
253
254       if (csv->titles)
255         csv_output_table_cell (csv, table_item_get_title (table_item), "Table");
256
257       for (y = 0; y < t->n[TABLE_VERT]; y++)
258         {
259           for (x = 0; x < t->n[TABLE_HORZ]; x++)
260             {
261               struct table_cell cell;
262
263               table_get_cell (t, x, y, &cell);
264
265               if (x > 0)
266                 fputs (csv->separator, csv->file);
267
268               if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
269                 csv_output_field (csv, "");
270               else
271                 csv_output_table_cell (csv, &cell, NULL);
272             }
273           putc ('\n', csv->file);
274         }
275
276       if (csv->captions)
277         csv_output_table_item_text (csv, table_item_get_caption (table_item),
278                                     "Caption");
279
280       struct footnote **f;
281       size_t n_footnotes = table_collect_footnotes (table_item, &f);
282       if (n_footnotes)
283         {
284           fputs ("\nFootnotes:\n", csv->file);
285
286           for (size_t i = 0; i < n_footnotes; i++)
287             {
288               csv_output_field (csv, f[i]->marker);
289               fputs (csv->separator, csv->file);
290               csv_output_field (csv, f[i]->content);
291               putc ('\n', csv->file);
292             }
293
294           free (f);
295         }
296     }
297   else if (is_text_item (output_item))
298     {
299       const struct text_item *text_item = to_text_item (output_item);
300       enum text_item_type type = text_item_get_type (text_item);
301       const char *text = text_item_get_text (text_item);
302
303       if (type == TEXT_ITEM_SYNTAX || type == TEXT_ITEM_PAGE_TITLE)
304         return;
305
306       csv_put_separator (csv);
307
308       if (text_item->markup)
309         {
310           char *plain_text = output_get_text_from_markup (text);
311           csv_output_lines (csv, plain_text);
312           free (plain_text);
313         }
314       else
315         csv_output_lines (csv, text);
316     }
317   else if (is_page_eject_item (output_item))
318     {
319       csv_put_separator (csv);
320       csv_output_lines (csv, "");
321     }
322   else if (is_message_item (output_item))
323     {
324       const struct message_item *message_item = to_message_item (output_item);
325       char *s = msg_to_string (message_item_get_msg (message_item));
326       csv_put_separator (csv);
327       csv_output_field (csv, s);
328       free (s);
329       putc ('\n', csv->file);
330     }
331 }
332
333 struct output_driver_factory csv_driver_factory = { "csv", "-", csv_create };
334
335 static const struct output_driver_class csv_driver_class =
336   {
337     "csv",
338     csv_destroy,
339     csv_submit,
340     csv_flush,
341   };