Consolidate quoting style in printed strings.
[pspp] / 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;            /* Comma or tab. */
47     char *file_name;            /* Output file name. */
48     char *command_name;         /* Current command. */
49     FILE *file;                 /* Output file. */
50     int n_items;                /* Number of items output so far. */
51   };
52
53 static const struct output_driver_class csv_driver_class;
54
55 static struct csv_driver *
56 csv_driver_cast (struct output_driver *driver)
57 {
58   assert (driver->class == &csv_driver_class);
59   return UP_CAST (driver, struct csv_driver, driver);
60 }
61
62 static struct driver_option *
63 opt (struct output_driver *d, struct string_map *options, const char *key,
64      const char *default_value)
65 {
66   return driver_option_get (d, options, key, default_value);
67 }
68
69 static struct output_driver *
70 csv_create (const char *file_name, enum settings_output_devices device_type,
71             struct string_map *o)
72 {
73   struct output_driver *d;
74   struct csv_driver *csv;
75
76   csv = xzalloc (sizeof *csv);
77   d = &csv->driver;
78   output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
79
80   csv->separator = parse_string (opt (d, o, "separator", ","));
81   csv->file_name = xstrdup (file_name);
82   csv->file = fn_open (csv->file_name, "w");
83   csv->n_items = 0;
84
85   if (csv->file == NULL)
86     {
87       error (0, errno, _("error opening output file `%s'"), csv->file_name);
88       output_driver_destroy (d);
89       return NULL;
90     }
91
92   return d;
93 }
94
95 static void
96 csv_destroy (struct output_driver *driver)
97 {
98   struct csv_driver *csv = csv_driver_cast (driver);
99
100   if (csv->file != NULL)
101     fn_close (csv->file_name, csv->file);
102
103   free (csv->separator);
104   free (csv->file_name);
105   free (csv);
106 }
107
108 static void
109 csv_flush (struct output_driver *driver)
110 {
111   struct csv_driver *csv = csv_driver_cast (driver);
112   if (csv->file != NULL)
113     fflush (csv->file);
114 }
115
116 static void
117 csv_output_field (FILE *file, const char *field)
118 {
119   while (*field == ' ')
120     field++;
121
122   if (field[strcspn (field, "\"\n\r,\t")])
123     {
124       const char *p;
125
126       putc ('"', file);
127       for (p = field; *p != '\0'; p++)
128         {
129           if (*p == '"')
130             putc ('"', file);
131           putc (*p, file);
132         }
133       putc ('"', file);
134     }
135   else
136     fputs (field, file);
137 }
138
139 static void
140 csv_output_field_format (FILE *file, const char *format, ...)
141   PRINTF_FORMAT (2, 3);
142
143 static void
144 csv_output_field_format (FILE *file, const char *format, ...)
145 {
146   va_list args;
147   char *s;
148
149   va_start (args, format);
150   s = xvasprintf (format, args);
151   va_end (args);
152
153   csv_output_field (file, s);
154   free (s);
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_submit (struct output_driver *driver,
166             const struct output_item *output_item)
167 {
168   struct csv_driver *csv = csv_driver_cast (driver);
169
170   output_driver_track_current_command (output_item, &csv->command_name);
171
172   if (is_table_item (output_item))
173     {
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);
177       int x, y;
178
179       csv_put_separator (csv);
180
181       if (caption != NULL)
182         {
183           csv_output_field_format (csv->file, "Table: %s", caption);
184           putc ('\n', csv->file);
185         }
186
187       for (y = 0; y < table_nr (t); y++)
188         {
189           for (x = 0; x < table_nc (t); x++)
190             {
191               struct table_cell cell;
192
193               table_get_cell (t, x, y, &cell);
194
195               if (x > 0)
196                 fputs (csv->separator, csv->file);
197
198               if (x != cell.d[TABLE_HORZ][0] || y != cell.d[TABLE_VERT][0])
199                 csv_output_field (csv->file, "");
200               else
201                 csv_output_field (csv->file, cell.contents);
202
203               table_cell_free (&cell);
204             }
205           putc ('\n', csv->file);
206         }
207     }
208   else if (is_text_item (output_item))
209     {
210       const struct text_item *text_item = to_text_item (output_item);
211       enum text_item_type type = text_item_get_type (text_item);
212       const char *text = text_item_get_text (text_item);
213
214       if (type == TEXT_ITEM_COMMAND_OPEN || type == TEXT_ITEM_COMMAND_CLOSE
215           || type == TEXT_ITEM_SYNTAX)
216         return;
217
218       csv_put_separator (csv);
219       switch (type)
220         {
221         case TEXT_ITEM_TITLE:
222           csv_output_field_format (csv->file, "Title: %s", text);
223           break;
224
225         case TEXT_ITEM_SUBTITLE:
226           csv_output_field_format (csv->file, "Subtitle: %s", text);
227           break;
228
229         default:
230           csv_output_field (csv->file, text);
231           break;
232         }
233       putc ('\n', csv->file);
234     }
235   else if (is_message_item (output_item))
236     {
237       const struct message_item *message_item = to_message_item (output_item);
238       const struct msg *msg = message_item_get_msg (message_item);
239       char *s = msg_to_string (msg, csv->command_name);
240       csv_put_separator (csv);
241       csv_output_field (csv->file, s);
242       free (s);
243       putc ('\n', csv->file);
244     }
245 }
246
247 struct output_driver_factory csv_driver_factory = { "csv", csv_create };
248
249 static const struct output_driver_class csv_driver_class =
250   {
251     "csv",
252     csv_destroy,
253     csv_submit,
254     csv_flush,
255   };