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