output: Phrase output driver error messages more consistently.
[pspp-builds.git] / src / output / odt.c
index f02ae3defd559fbf54fb3504b78490d07b9182aa..bfc698e79aa11068363f16592ad5ec53d2b05c5f 100644 (file)
@@ -32,6 +32,7 @@
 #include "libpspp/str.h"
 #include "libpspp/version.h"
 #include "output/driver-provider.h"
+#include "output/message-item.h"
 #include "output/options.h"
 #include "output/tab.h"
 #include "output/table-item.h"
@@ -64,12 +65,17 @@ struct odt_driver
 
   /* Number of tables so far. */
   int table_num;
+
+  /* Name of current command. */
+  char *command_name;
 };
 
+static const struct output_driver_class odt_driver_class;
+
 static struct odt_driver *
 odt_driver_cast (struct output_driver *driver)
 {
-  assert (driver->class == &odt_class);
+  assert (driver->class == &odt_driver_class);
   return UP_CAST (driver, struct odt_driver, driver);
 }
 
@@ -85,7 +91,7 @@ create_mimetype (const char *dirname)
 
   if (fp == NULL)
     {
-      error (0, errno, _("failed to create output file %s"),
+      error (0, errno, _("error opening output file \"%s\""),
              ds_cstr (&filename));
       ds_destroy (&filename);
       return false;
@@ -293,7 +299,7 @@ opt (struct output_driver *d, struct string_map *options, const char *key,
 }
 
 static struct output_driver *
-odt_create (const char *name, enum output_device_type device_type,
+odt_create (const char *file_name, enum settings_output_devices device_type,
             struct string_map *o)
 {
   struct output_driver *d;
@@ -301,9 +307,9 @@ odt_create (const char *name, enum output_device_type device_type,
 
   odt = xzalloc (sizeof *odt);
   d = &odt->driver;
-  output_driver_init (d, &odt_class, name, device_type);
+  output_driver_init (d, &odt_driver_class, file_name, device_type);
 
-  odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt"));
+  odt->file_name = xstrdup (file_name);
   odt->debug = parse_boolean (opt (d, o, "debug", "false"));
 
   odt->dirname = xstrdup ("odt-XXXXXX");
@@ -401,6 +407,7 @@ odt_destroy (struct output_driver *driver)
   else
     fprintf (stderr, "Not removing directory %s\n", odt->dirname);
 
+  free (odt->command_name);
   free (odt->dirname);
   free (odt);
 }
@@ -501,31 +508,45 @@ odt_submit_table (struct odt_driver *odt, struct table_item *item)
   xmlTextWriterEndElement (odt->content_wtr); /* table */
 }
 
+static void
+odt_output_text (struct odt_driver *odt, const char *text)
+{
+  xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
+  xmlTextWriterWriteString (odt->content_wtr, _xml(text));
+  xmlTextWriterEndElement (odt->content_wtr);
+}
+
 /* Submit a table to the ODT driver */
 static void
 odt_submit (struct output_driver *driver,
             const struct output_item *output_item)
 {
   struct odt_driver *odt = odt_driver_cast (driver);
+
+  output_driver_track_current_command (output_item, &odt->command_name);
+
   if (is_table_item (output_item))
     odt_submit_table (odt, to_table_item (output_item));
   else if (is_text_item (output_item))
     {
-      const struct text_item *text_item = to_text_item (output_item);
-      const char *text = text_item_get_text (text_item);
-
       /* XXX apply different styles based on text_item's type.  */
-      xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
-      xmlTextWriterWriteString (odt->content_wtr, _xml(text));
-      xmlTextWriterEndElement (odt->content_wtr);
+      odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
+    }
+  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, odt->command_name);
+      odt_output_text (odt, s);
+      free (s);
     }
 }
 
-/* ODT driver class. */
-const struct output_driver_class odt_class =
+struct output_driver_factory odt_driver_factory = { "odt", odt_create };
+
+static const struct output_driver_class odt_driver_class =
 {
   "odf",
-  odt_create,
   odt_destroy,
   odt_submit,
   NULL,