Consolidate quoting style in printed strings.
[pspp] / src / output / csv.c
index 625ce82a07505dd99dddff562c5981568af2d311..c648bfa79c30ae0e9330f3f7bae60290e1d96210 100644 (file)
 #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"
@@ -43,14 +45,17 @@ struct csv_driver
 
     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);
 }
 
@@ -62,7 +67,7 @@ opt (struct output_driver *d, struct string_map *options, const char *key,
 }
 
 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;
@@ -70,16 +75,16 @@ csv_create (const char *name, enum output_device_type device_type,
 
   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;
     }
@@ -162,6 +167,8 @@ csv_submit (struct output_driver *driver,
 {
   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);
@@ -225,12 +232,23 @@ csv_submit (struct output_driver *driver,
         }
       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,