d168fd9534140230f2e4fb9c15646aa779c0a7e2
[pspp-builds.git] / src / output / csv.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010 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 "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"
33
34 #include "gl/error.h"
35 #include "gl/xalloc.h"
36 #include "gl/xvasprintf.h"
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 /* Comma-separated value output driver. */
42 struct csv_driver
43   {
44     struct output_driver driver;
45
46     char *separator;            /* Field separator (usually comma or tab). */
47     char *quote_set;            /* Characters that force quoting. */
48     bool captions;              /* Print table captions? */
49
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. */
54   };
55
56 static const struct output_driver_class csv_driver_class;
57
58 static struct csv_driver *
59 csv_driver_cast (struct output_driver *driver)
60 {
61   assert (driver->class == &csv_driver_class);
62   return UP_CAST (driver, struct csv_driver, driver);
63 }
64
65 static struct driver_option *
66 opt (struct output_driver *d, struct string_map *options, const char *key,
67      const char *default_value)
68 {
69   return driver_option_get (d, options, key, default_value);
70 }
71
72 static struct output_driver *
73 csv_create (const char *file_name, enum settings_output_devices device_type,
74             struct string_map *o)
75 {
76   struct output_driver *d;
77   struct csv_driver *csv;
78
79   csv = xzalloc (sizeof *csv);
80   d = &csv->driver;
81   output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
82
83   csv->separator = parse_string (opt (d, o, "separator", ","));
84   csv->quote_set = xasprintf ("\"\n\r\t%s", csv->separator);
85   csv->captions = parse_boolean (opt (d, o, "captions", "true"));
86   csv->file_name = xstrdup (file_name);
87   csv->file = fn_open (csv->file_name, "w");
88   csv->n_items = 0;
89
90   if (csv->file == NULL)
91     {
92       error (0, errno, _("error opening output file `%s'"), csv->file_name);
93       output_driver_destroy (d);
94       return NULL;
95     }
96
97   return d;
98 }
99
100 static void
101 csv_destroy (struct output_driver *driver)
102 {
103   struct csv_driver *csv = csv_driver_cast (driver);
104
105   if (csv->file != NULL)
106     fn_close (csv->file_name, csv->file);
107
108   free (csv->separator);
109   free (csv->quote_set);
110   free (csv->file_name);
111   free (csv);
112 }
113
114 static void
115 csv_flush (struct output_driver *driver)
116 {
117   struct csv_driver *csv = csv_driver_cast (driver);
118   if (csv->file != NULL)
119     fflush (csv->file);
120 }
121
122 static void
123 csv_output_field (struct csv_driver *csv, const char *field)
124 {
125   while (*field == ' ')
126     field++;
127
128   if (field[strcspn (field, csv->quote_set)])
129     {
130       const char *p;
131
132       putc ('"', csv->file);
133       for (p = field; *p != '\0'; p++)
134         {
135           if (*p == '"')
136             putc ('"', csv->file);
137           putc (*p, csv->file);
138         }
139       putc ('"', csv->file);
140     }
141   else
142     fputs (field, csv->file);
143 }
144
145 static void
146 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
147   PRINTF_FORMAT (2, 3);
148
149 static void
150 csv_output_field_format (struct csv_driver *csv, const char *format, ...)
151 {
152   va_list args;
153   char *s;
154
155   va_start (args, format);
156   s = xvasprintf (format, args);
157   va_end (args);
158
159   csv_output_field (csv, s);
160   free (s);
161 }
162
163 static void
164 csv_put_separator (struct csv_driver *csv)
165 {
166   if (csv->n_items++ > 0)
167     putc ('\n', csv->file);
168 }
169
170 static void
171 csv_submit (struct output_driver *driver,
172             const struct output_item *output_item)
173 {
174   struct csv_driver *csv = csv_driver_cast (driver);
175
176   output_driver_track_current_command (output_item, &csv->command_name);
177
178   if (is_table_item (output_item))
179     {
180       struct table_item *table_item = to_table_item (output_item);
181       const char *caption = table_item_get_caption (table_item);
182       const struct table *t = table_item_get_table (table_item);
183       int x, y;
184
185       csv_put_separator (csv);
186
187       if (csv->captions && caption != NULL)
188         {
189           csv_output_field_format (csv, "Table: %s", caption);
190           putc ('\n', csv->file);
191         }
192
193       for (y = 0; y < table_nr (t); y++)
194         {
195           for (x = 0; x < table_nc (t); x++)
196             {
197               struct table_cell cell;
198
199               table_get_cell (t, x, y, &cell);
200
201               if (x > 0)
202                 fputs (csv->separator, csv->file);
203
204               if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
205                 csv_output_field (csv, "");
206               else
207                 csv_output_field (csv, cell.contents);
208
209               table_cell_free (&cell);
210             }
211           putc ('\n', csv->file);
212         }
213     }
214   else if (is_text_item (output_item))
215     {
216       const struct text_item *text_item = to_text_item (output_item);
217       enum text_item_type type = text_item_get_type (text_item);
218       const char *text = text_item_get_text (text_item);
219
220       if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
221           || type == TEXT_ITEM_SYNTAX)
222         return;
223
224       csv_put_separator (csv);
225       switch (type)
226         {
227         case TEXT_ITEM_TITLE:
228           csv_output_field_format (csv, "Title: %s", text);
229           break;
230
231         case TEXT_ITEM_SUBTITLE:
232           csv_output_field_format (csv, "Subtitle: %s", text);
233           break;
234
235         default:
236           csv_output_field (csv, text);
237           break;
238         }
239       putc ('\n', csv->file);
240     }
241   else if (is_message_item (output_item))
242     {
243       const struct message_item *message_item = to_message_item (output_item);
244       const struct msg *msg = message_item_get_msg (message_item);
245       char *s = msg_to_string (msg, csv->command_name);
246       csv_put_separator (csv);
247       csv_output_field (csv, s);
248       free (s);
249       putc ('\n', csv->file);
250     }
251 }
252
253 struct output_driver_factory csv_driver_factory = { "csv", csv_create };
254
255 static const struct output_driver_class csv_driver_class =
256   {
257     "csv",
258     csv_destroy,
259     csv_submit,
260     csv_flush,
261   };