#include <errno.h>
#include <stdlib.h>
-#include <data/file-name.h>
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
-#include <libpspp/string-map.h>
-#include <output/text-item.h>
-#include <output/driver-provider.h>
-#include <output/options.h>
-#include <output/table-item.h>
-#include <output/table-provider.h>
+#include "data/file-name.h"
+#include "libpspp/assertion.h"
+#include "libpspp/compiler.h"
+#include "libpspp/message.h"
+#include "libpspp/string-map.h"
+#include "output/text-item.h"
+#include "output/driver-provider.h"
+#include "output/options.h"
+#include "output/message-item.h"
+#include "output/table-item.h"
+#include "output/table-provider.h"
#include "gl/error.h"
#include "gl/xalloc.h"
char *separator; /* Comma or tab. */
char *file_name; /* Output file name. */
+ char *command_name; /* Current command. */
FILE *file; /* Output file. */
int n_items; /* Number of items output so far. */
};
+static const struct output_driver_class csv_driver_class;
+
static struct csv_driver *
csv_driver_cast (struct output_driver *driver)
{
- assert (driver->class == &csv_class);
+ assert (driver->class == &csv_driver_class);
return UP_CAST (driver, struct csv_driver, driver);
}
}
static struct output_driver *
-csv_create (const char *name, enum output_device_type device_type,
+csv_create (const char *file_name, enum settings_output_devices device_type,
struct string_map *o)
{
struct output_driver *d;
csv = xzalloc (sizeof *csv);
d = &csv->driver;
- output_driver_init (&csv->driver, &csv_class, name, device_type);
+ output_driver_init (&csv->driver, &csv_driver_class, file_name, device_type);
csv->separator = parse_string (opt (d, o, "separator", ","));
- csv->file_name = parse_string (opt (d, o, "output-file", "pspp.csv"));
+ csv->file_name = xstrdup (file_name);
csv->file = fn_open (csv->file_name, "w");
csv->n_items = 0;
if (csv->file == NULL)
{
- error (0, errno, _("csv: opening output file \"%s\""), csv->file_name);
+ error (0, errno, _("error opening output file `%s'"), csv->file_name);
output_driver_destroy (d);
return NULL;
}
{
struct csv_driver *csv = csv_driver_cast (driver);
+ output_driver_track_current_command (output_item, &csv->command_name);
+
if (is_table_item (output_item))
{
struct table_item *table_item = to_table_item (output_item);
}
putc ('\n', csv->file);
}
+ else if (is_message_item (output_item))
+ {
+ const struct message_item *message_item = to_message_item (output_item);
+ const struct msg *msg = message_item_get_msg (message_item);
+ char *s = msg_to_string (msg, csv->command_name);
+ csv_put_separator (csv);
+ csv_output_field (csv->file, s);
+ free (s);
+ putc ('\n', csv->file);
+ }
}
-const struct output_driver_class csv_class =
+struct output_driver_factory csv_driver_factory = { "csv", csv_create };
+
+static const struct output_driver_class csv_driver_class =
{
"csv",
- csv_create,
csv_destroy,
csv_submit,
csv_flush,