Add support for reading and writing SPV files.
[pspp] / src / output / odt.c
index 60a5b7fd4b337f257c1d6258960cbffdc5809188..104e57a9332c373502794a9550ddda5bc1b09f4d 100644 (file)
@@ -40,7 +40,6 @@
 #include "output/driver-provider.h"
 #include "output/message-item.h"
 #include "output/options.h"
-#include "output/tab.h"
 #include "output/table-item.h"
 #include "output/table-provider.h"
 #include "output/text-item.h"
@@ -57,6 +56,7 @@ struct odt_driver
   struct output_driver driver;
 
   struct zip_writer *zip;     /* ZIP file writer. */
+  struct file_handle *handle; /* Handle for 'file_name'. */
   char *file_name;            /* Output file name. */
 
   /* content.xml */
@@ -80,26 +80,6 @@ odt_driver_cast (struct output_driver *driver)
   return UP_CAST (driver, struct odt_driver, driver);
 }
 
-/* Create the "mimetype" file needed by ODF */
-static bool
-create_mimetype (struct zip_writer *zip)
-{
-  FILE *fp;
-
-  fp = create_temp_file ();
-  if (fp == NULL)
-    {
-      msg_error (errno, _("error creating temporary file"));
-      return false;
-    }
-
-  fprintf (fp, "application/vnd.oasis.opendocument.text");
-  zip_writer_add (zip, fp, "mimetype");
-  close_temp_file (fp);
-
-  return true;
-}
-
 /* Creates a new temporary file and stores it in *FILE, then creates an XML
    writer for it and stores it in *W. */
 static void
@@ -303,13 +283,11 @@ odt_create (struct file_handle *fh, enum settings_output_devices device_type,
   output_driver_init (d, &odt_driver_class, file_name, device_type);
 
   odt->zip = zip;
+  odt->handle = fh;
   odt->file_name = xstrdup (file_name);
 
-  if (!create_mimetype (zip))
-    {
-      output_driver_destroy (d);
-      return NULL;
-    }
+  zip_writer_add_string (zip, "mimetype",
+                         "application/vnd.oasis.opendocument.text");
 
   /* Create the manifest */
   create_writer (&odt->manifest_file, &odt->manifest_wtr);
@@ -380,6 +358,7 @@ odt_destroy (struct output_driver *driver)
       zip_writer_close (odt->zip);
     }
 
+  fh_unref (odt->handle);
   free (odt->file_name);
   free (odt);
 }
@@ -459,6 +438,26 @@ write_table_item_text (struct odt_driver *odt,
   xmlTextWriterEndElement (odt->content_wtr);
 }
 
+static void
+write_table_item_layers (struct odt_driver *odt,
+                         const struct table_item_layers *layers)
+{
+  if (!layers)
+    return;
+
+  for (size_t i = 0; i < layers->n_layers; i++)
+    {
+      const struct table_item_layer *layer = &layers->layers[i];
+      xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
+      xmlTextWriterWriteFormatAttribute (odt->content_wtr,
+                                         _xml("text:outline-level"), "%d", 2);
+      xmlTextWriterWriteString (odt->content_wtr, _xml (layer->content) );
+      for (size_t i = 0; i < layer->n_footnotes; i++)
+        write_footnote (odt, layer->footnotes[i]);
+      xmlTextWriterEndElement (odt->content_wtr);
+    }
+}
+
 static void
 write_table (struct odt_driver *odt, const struct table_item *item)
 {
@@ -467,7 +466,7 @@ write_table (struct odt_driver *odt, const struct table_item *item)
 
   /* Write a heading for the table */
   write_table_item_text (odt, table_item_get_title (item));
-  write_table_item_text (odt, table_item_get_layers (item));
+  write_table_item_layers (odt, table_item_get_layers (item));
 
   /* Start table */
   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));