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